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
919
C
919C
C. Seat Arrangements
1,300
brute force; implementation
Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.The classroom contains \(n\) rows of seat...
The first line contains three positive integers \(n,m,k\) (\(1 \leq n, m, k \leq 2\,000\)), where \(n,m\) represent the sizes of the classroom and \(k\) is the number of consecutive seats you need to find.Each of the next \(n\) lines contains \(m\) characters '.' or '*'. They form a matrix representing the classroom, '...
A single number, denoting the number of ways to find \(k\) empty seats in the same row or column.
In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement. \((1,3)\), \((2,3)\) \((2,2)\), \((2,3)\) \((2,1)\), \((2,2)\)
Input: 2 3 2**.... | Output: 3
Easy
2
781
379
97
9
1,690
E
1690E
E. Price Maximization
1,500
binary search; greedy; math; two pointers
A batch of \(n\) goods (\(n\) β€” an even number) is brought to the store, \(i\)-th of which has weight \(a_i\). Before selling the goods, they must be packed into packages. After packing, the following will be done: There will be \(\frac{n}{2}\) packages, each package contains exactly two goods; The weight of the packag...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β€”the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) (\(2 \le n \le 2\cdot10^5\)) and \(k\) (\(1 \le k \le 1000\)). The number \(n\) β€” is even.The second l...
For each test case, print on a separate line a single number β€” the maximum possible total cost of all the packages.
The first test case is analyzed in the statement.In the second test case, you can get a total value equal to \(4\) if you put the first and second goods in the first package and the third and fourth goods in the second package.In the third test case, the cost of each item is \(0\), so the total cost will also be \(0\).
Input: 66 33 2 7 1 4 84 32 1 5 64 120 0 0 02 11 16 102 0 0 5 9 46 55 3 8 6 3 2 | Output: 8 4 0 2 1 5
Medium
4
1,640
518
115
16
1,316
E
1316E
E. Team Building
2,300
bitmasks; dp; greedy; sortings
Alice, the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of \(p\) players playing in \(p\) different positions. She also recognizes the importance of audience support, so she wants to select \(k\) people as part of the audience.There are \(n\) people in Byteland...
The first line contains \(3\) integers \(n,p,k\) (\(2 \leq n \leq 10^{5}, 1 \leq p \leq 7, 1 \le k, p+k \le n\)).The second line contains \(n\) integers \(a_{1},a_{2},\ldots,a_{n}\). (\(1 \leq a_{i} \leq 10^{9}\)).The \(i\)-th of the next \(n\) lines contains \(p\) integers \(s_{i, 1}, s_{i, 2}, \dots, s_{i, p}\). (\(1...
Print a single integer \({res}\) β€” the maximum possible strength of the club.
In the first sample, we can select person \(1\) to play in the \(1\)-st position and persons \(2\) and \(3\) as audience members. Then the total strength of the club will be equal to \(a_{2}+a_{3}+s_{1,1}\).
Input: 4 1 2 1 16 10 3 18 19 13 15 | Output: 44
Expert
4
1,173
348
77
13
1,332
F
1332F
F. Independent Set
2,500
dfs and similar; dp; trees
Eric is the teacher of graph theory class. Today, Eric teaches independent set and edge-induced subgraph.Given a graph \(G=(V,E)\), an independent set is a subset of vertices \(V' \subset V\) such that for every pair \(u,v \in V'\), \((u,v) \not \in E\) (i.e. no edge in \(E\) connects two vertices from \(V'\)).An edge-...
The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)), representing the number of vertices of the graph \(G\).Each of the following \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\), \(u \not= v\)), describing edges of the given tree.It is guaranteed that the given edges f...
Output one integer, representing the desired value modulo \(998,244,353\).
For the second example, all independent sets are listed below.
Input: 2 2 1 | Output: 3
Expert
3
1,204
331
74
13
1,941
C
1941C
C. Rudolf and the Ugly String
900
dp; greedy; strings
Rudolf has a string \(s\) of length \(n\). Rudolf considers the string \(s\) to be ugly if it contains the substring\(^\dagger\) ""pie"" or the substring ""map"", otherwise the string \(s\) will be considered beautiful.For example, ""ppiee"", ""mmap"", ""dfpiefghmap"" are ugly strings, while ""mathp"", ""ppiiee"" are b...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β€” the length of the string \(s\).The next line of each test case contains the string \(s\) o...
For each test case, output a single integer β€” the minimum number of characters that need to be deleted to make the string \(s\) beautiful. If the string is initially beautiful, then output \(0\).
In the first test case, for example, you can delete the \(4\)th and \(9\)th characters to make the string beautiful.In the second test case, the string is already beautiful.
Input: 69mmapnapie9azabazapi8mappppie18mapmapmapmapmapmap1p11pppiepieeee | Output: 2 0 2 6 0 2
Beginner
3
802
451
195
19
1,409
F
1409F
F. Subsequences of Length Two
2,100
dp; strings
You are given two strings \(s\) and \(t\) consisting of lowercase Latin letters. The length of \(t\) is \(2\) (i.e. this string consists only of two characters).In one move, you can choose any character of \(s\) and replace it with any lowercase Latin letter. More formally, you choose some \(i\) and replace \(s_i\) (th...
The first line of the input contains two integers \(n\) and \(k\) (\(2 \le n \le 200\); \(0 \le k \le n\)) β€” the length of \(s\) and the maximum number of moves you can make. The second line of the input contains the string \(s\) consisting of \(n\) lowercase Latin letters. The third line of the input contains the stri...
Print one integer β€” the maximum possible number of occurrences of \(t\) in \(s\) as a subsequence if you replace no more than \(k\) characters in \(s\) optimally.
In the first example, you can obtain the string ""abab"" replacing \(s_1\) with 'a' and \(s_4\) with 'b'. Then the answer is \(3\).In the second example, you can obtain the string ""ssddsdd"" and get the answer \(10\).In the fourth example, you can obtain the string ""aaacaaa"" and get the answer \(15\).
Input: 4 2 bbaa ab | Output: 3
Hard
2
699
371
162
14
469
A
469A
A. I Wanna Be the Guy
800
greedy; implementation
There is a game called ""I Wanna Be the Guy"", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass ...
The first line contains a single integer n (1 ≀ n ≀ 100). The next line contains an integer p (0 ≀ p ≀ n) at first, then follows p distinct integers a1, a2, ..., ap (1 ≀ ai ≀ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's a...
If they can pass all the levels, print ""I become the guy."". If it's impossible, print ""Oh, my keyboard!"" (without the quotes).
In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.In the second sample, no one can pass level 4.
Input: 43 1 2 32 2 4 | Output: I become the guy.
Beginner
2
442
364
130
4
1,322
C
1322C
C. Instant Noodles
2,300
graphs; hashing; math; number theory
Wu got hungry after an intense training session, and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.You are given a bipartite graph with positive integers in all vertices of the right half. For a subset \(S\) of vertices of the left ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 500\,000\)) β€” the number of test cases in the given test set. Test case descriptions follow.The first line of each case description contains two integers \(n\) and \(m\) (\(1~\leq~n,~m~\leq~500\,000\)) β€” the number of vertices in either half of the graph, ...
For each test case print a single integer β€” the required greatest common divisor.
The greatest common divisor of a set of integers is the largest integer \(g\) such that all elements of the set are divisible by \(g\).In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and \(f(S)\) for any non-empty subset is \(2\), thus the greatest common diviso...
Input: 3 2 4 1 1 1 1 1 2 2 1 2 2 3 4 1 1 1 1 1 1 2 2 2 2 3 4 7 36 31 96 29 1 2 1 3 1 4 2 2 2 4 3 1 4 3 | Output: 2 1 12
Expert
4
688
1,030
81
13
1,185
D
1185D
D. Extra Element
1,700
implementation; math
A sequence \(a_1, a_2, \dots, a_k\) is called an arithmetic progression if for each \(i\) from \(1\) to \(k\) elements satisfy the condition \(a_i = a_1 + c \cdot (i - 1)\) for some fixed \(c\).For example, these five sequences are arithmetic progressions: \([5, 7, 9, 11]\), \([101]\), \([101, 100, 99]\), \([13, 97]\) ...
The first line of the input contains one integer \(n\) (\(2 \le n \le 2\cdot10^5\)) β€” length of the sequence \(b\). The second line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(-10^9 \le b_i \le 10^9\)) β€” elements of the sequence \(b\).
Print such index \(j\) (\(1 \le j \le n\)), so that if you delete the \(j\)-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.
Note to the first example. If you delete the \(4\)-th element, you can get the arithmetic progression \([2, 4, 6, 8]\).Note to the second example. The original sequence is already arithmetic progression, so you can delete \(1\)-st or last element and you will get an arithmetical progression again.
Input: 5 2 6 8 7 4 | Output: 4
Medium
2
778
243
301
11
1,252
F
1252F
F. Regular Forestation
2,400
hashing; trees
A forestation is an act of planting a bunch of trees to grow a forest, usually to replace a forest that had been cut down. Strangely enough, graph theorists have another idea on how to make a forest, i.e. by cutting down a tree!A tree is a graph of \(N\) nodes connected by \(N - 1\) edges. Let \(u\) be a node in a tree...
Input begins with a line containting an integer: \(N\) (\(3 \le N \le 4000\)) representing the number of nodes in the given tree. The next \(N - 1\) lines each contains two integers: \(a_i\) \(b_i\) (\(1 \le a_i < b_i \le N\)) representing an edge \((a_i,b_i)\) in the given tree. It is guaranteed that any two nodes in ...
Output in a line an integer representing the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point, or output -1 if there is no such good cutting point.
Explanation for the sample input/output #1This is the example from the problem description.
Input: 13 1 5 1 7 2 4 2 8 2 11 3 12 4 7 4 12 6 11 7 13 9 10 9 12 | Output: 3
Expert
2
2,362
386
199
12
1,139
A
1139A
A. Even Substrings
800
implementation; strings
You are given a string \(s=s_1s_2\dots s_n\) of length \(n\), which only contains digits \(1\), \(2\), ..., \(9\).A substring \(s[l \dots r]\) of \(s\) is a string \(s_l s_{l + 1} s_{l + 2} \ldots s_r\). A substring \(s[l \dots r]\) of \(s\) is called even if the number represented by it is even. Find the number of eve...
The first line contains an integer \(n\) (\(1 \le n \le 65000\)) β€” the length of the string \(s\).The second line contains a string \(s\) of length \(n\). The string \(s\) consists only of digits \(1\), \(2\), ..., \(9\).
Print the number of even substrings of \(s\).
In the first example, the \([l, r]\) pairs corresponding to even substrings are: \(s[1 \dots 2]\) \(s[2 \dots 2]\) \(s[1 \dots 4]\) \(s[2 \dots 4]\) \(s[3 \dots 4]\) \(s[4 \dots 4]\) In the second example, all \(10\) substrings of \(s\) are even substrings. Note, that while substrings \(s[1 \dots 1]\) and \(s[2 \dots 2...
Input: 4 1234 | Output: 6
Beginner
2
477
221
45
11
15
E
15E
E. Triangles
2,600
combinatorics; dp
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels,...
The input data contain the only even integer n (2 ≀ n ≀ 106).
Output the only number β€” the amount of Peter's routes modulo 1000000009.
Input: 2 | Output: 10
Expert
2
1,400
61
72
0
57
B
57B
B. Martian Architecture
1,600
implementation
Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece β€” ""A Road to the Sun"". The building stands on cubical stones of the same size. The foundation divides the entire ""road"" into cells, into which the cubical s...
The first line contains three space-separated integers: n, m, k (1 ≀ n, m ≀ 105, 1 ≀ k ≀ min(n, 100)) which is the number of cells, the number of ""Roads to the Sun"" and the number of cells in the query correspondingly. Each of the following m roads contain three space-separated integers: ai, bi, ci (1 ≀ ai ≀ bi ≀ n, ...
You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Input: 5 2 11 5 12 4 13 | Output: 5
Medium
1
1,680
590
248
0
1,045
G
1045G
G. AI robots
2,200
data structures
In the last mission, MDCS has successfully shipped \(N\) AI robots to Mars. Before they start exploring, system initialization is required so they are arranged in a line. Every robot can be described with three numbers: position (\(x_i\)), radius of sight (\(r_i\)) and IQ (\(q_i\)).Since they are intelligent robots, so...
The first line contains two integers, numbers \(N (1 \leq N \leq 10^5) \) and \(K (0 \leq K \leq 20)\).Next \(N\) lines contain three numbers each \(x_i, r_i, q_i (0 \leq x_i,r_i,q_i \leq 10^9)\) β€” position, radius of sight and IQ of every robot respectively.
Output contains only one number β€” solution to the problem.
The first robot can see the second, but not vice versa. The first robot can't even see the third. The second and the third robot can see each other and their IQs don't differ more than 2 so only one conversation will happen.
Input: 3 23 6 17 3 1010 5 8 | Output: 1
Hard
1
779
259
58
10
111
C
111C
C. Petya and Spiders
2,100
bitmasks; dp; dsu
Little Petya loves training spiders. Petya has a board n Γ— m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to som...
The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 40, nΒ·m ≀ 40) β€” the board sizes.
In the first line print the maximum number of cells without spiders.
In the first sample the only possible answer is:sIn the second sample one of the possible solutions is: rdlruls denotes command ""stay idle"", l, r, d, u denote commands ""crawl left"", ""crawl right"", ""crawl down"", ""crawl up"", correspondingly.
Input: 1 1 | Output: 0
Hard
3
758
105
68
1
1,200
B
1200B
B. Block Adventure
1,200
dp; greedy
Gildong is playing a video game called Block Adventure. In Block Adventure, there are \(n\) columns of blocks in a row, and the columns are numbered from \(1\) to \(n\). All blocks have equal heights. The height of the \(i\)-th column is represented as \(h_i\), which is the number of blocks stacked in the \(i\)-th colu...
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 100\), \(0 \le m \le 10^6\), \(0 \le k \le 10^6\)) β€” the number ...
For each test case, print ""YES"" if it is possible to win the game. Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
In the first case, Gildong can take one block from the \(1\)-st column, move to the \(2\)-nd column, put the block on the \(2\)-nd column, then move to the \(3\)-rd column.In the second case, Gildong has to put the block in his bag on the \(1\)-st column to get to the \(2\)-nd column. But it is impossible to get to the...
Input: 53 0 14 3 53 1 21 4 74 10 010 20 10 202 5 50 111 9 999 | Output: YESNOYESNOYES
Easy
2
1,504
627
148
12
582
D
582D
D. Number of Binominal Coefficients
3,300
dp; math; number theory
For a given prime integer p and integers Ξ±, A calculate the number of pairs of integers (n, k), such that 0 ≀ k ≀ n ≀ A and is divisible by pΞ±. As the answer can be rather large, print the remainder of the answer moduly 109 + 7.Let us remind you that is the number of ways k objects can be chosen from the set of n objec...
The first line contains two integers, p and Ξ± (1 ≀ p, Ξ± ≀ 109, p is prime). The second line contains the decimal record of integer A (0 ≀ A < 101000) without leading zeroes.
In the single line print the answer to the problem.
In the first sample three binominal coefficients divisible by 4 are , and .
Input: 2 27 | Output: 3
Master
3
323
173
51
5
1,928
E
1928E
E. Modular Sequence
2,300
brute force; constructive algorithms; dp; graphs; greedy; math; number theory
You are given two integers \(x\) and \(y\). A sequence \(a\) of length \(n\) is called modular if \(a_1=x\), and for all \(1 < i \le n\) the value of \(a_{i}\) is either \(a_{i-1} + y\) or \(a_{i-1} \bmod y\). Here \(x \bmod y\) denotes the remainder from dividing \(x\) by \(y\).Determine if there exists a modular sequ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)). The description of the test cases follows.The first and only line of each test case contains four integers \(n\), \(x\), \(y\), and \(s\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le x \le 2 \cdot 10...
For each test case, if the desired sequence exists, output ""Yes"" on the first line (without quotes). Then, on the second line, output \(n\) integers \(a_1, a_2, \ldots, a_n\) separated by a space β€” the elements of the sequence \(a\). If there are multiple suitable sequences, output any of them.If the sequence does no...
In the first example, the sequence \([8, 11, 2, 5, 2]\) satisfies the conditions. Thus, \(a_1 = 8 = x\), \(a_2 = 11 = a_1 + 3\), \(a_3 = 2 = a_2 \bmod 3\), \(a_4 = 5 = a_3 + 3\), \(a_5 = 2 = a_4 \bmod 3\).In the second example, the first element of the sequence should be equal to \(5\), so the sequence \([2, 2, 2]\) is...
Input: 35 8 3 283 5 3 69 1 5 79 | Output: YES 8 11 2 2 5 NO NO
Expert
7
427
644
527
19
1,370
F1
1370F1
F1. The Hidden Pair (Easy Version)
2,400
binary search; dfs and similar; graphs; interactive; shortest paths; trees
Note that the only difference between the easy and hard version is the constraint on the number of queries. You can make hacks only if all versions of the problem are solved.This is an interactive problem.You are given a tree consisting of \(n\) nodes numbered with integers from \(1\) to \(n\). Ayush and Ashish chose t...
The first line contains a single integer \(t\) \((1 \leq t \leq 10)\) β€” the number of test cases. Please note, how the interaction process is organized.The first line of each test case consists of a single integer \(n\) \((2 \le n \le 1000)\) β€” the number of nodes in the tree.The next \(n - 1\) lines consist of two int...
The tree from the first test is shown below, and the hidden nodes are \(1\) and \(3\).
Input: 1 3 1 2 1 3 1 1 2 3 3 1 3 1 Correct | Output: ? 1 1 ? 1 2 ? 1 3 ? 2 2 3 ! 1 3
Expert
6
1,256
395
0
13
2,052
K
2052K
2,800
Master
0
0
0
0
20
1,884
A
1884A
A. Simple Design
800
brute force; greedy; math
A positive integer is called \(k\)-beautiful, if the digit sum of the decimal representation of this number is divisible by \(k^{\dagger}\). For example, \(9272\) is \(5\)-beautiful, since the digit sum of \(9272\) is \(9 + 2 + 7 + 2 = 20\).You are given two integers \(x\) and \(k\). Please find the smallest integer \(...
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 two integers \(x\) and \(k\) (\(1 \le x \le 10^9\), \(1 \le k \le 10\)).
For each test case, output the smallest integer \(y \ge x\) which is \(k\)-beautiful.
In the first test case, numbers from \(1\) to \(4\) consist of a single digit, thus the digit sum is equal to the number itself. None of the integers from \(1\) to \(4\) are divisible by \(5\).In the fourth test case, the digit sum of \(777\) is \(7 + 7 + 7 = 21\) which is already divisible by \(3\).
Input: 61 510 837 9777 31235 101 10 | Output: 5 17 45 777 1243 19
Beginner
3
470
274
85
18
370
D
370D
D. Broken Monitor
2,100
brute force; constructive algorithms; greedy; implementation
Innocentius has a problem β€” his computer monitor has broken. Now some of the pixels are ""dead"", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus.Innocentius is touch-typing a program that pain...
The first line contains the resolution of the monitor as a pair of integers n, m (1 ≀ n, m ≀ 2000). The next n lines contain exactly m characters each β€” the state of the monitor pixels at the moment of the game. Character ""."" (period, ASCII code 46) corresponds to the black pixel, and character ""w"" (lowercase Engli...
Print the monitor screen. Represent the sought frame by characters ""+"" (the ""plus"" character). The pixels that has become white during the game mustn't be changed. Print them as ""w"". If there are multiple possible ways to position the frame of the minimum size, print any of them.If the required frame doesn't exis...
In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.
Input: 4 8..w..w....................w..w.. | Output: ..w++w....+..+....+..+....w++w..
Hard
4
1,448
430
369
3
1,661
B
1661B
B. Getting Zero
1,300
bitmasks; brute force; dfs and similar; dp; graphs; greedy; shortest paths
Suppose you have an integer \(v\). In one operation, you can: either set \(v = (v + 1) \bmod 32768\) or set \(v = (2 \cdot v) \bmod 32768\). You are given \(n\) integers \(a_1, a_2, \dots, a_n\). What is the minimum number of operations you need to make each \(a_i\) equal to \(0\)?
The first line contains the single integer \(n\) (\(1 \le n \le 32768\)) β€” the number of integers.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < 32768\)).
Print \(n\) integers. The \(i\)-th integer should be equal to the minimum number of operations required to make \(a_i\) equal to \(0\).
Let's consider each \(a_i\): \(a_1 = 19\). You can, firstly, increase it by one to get \(20\) and then multiply it by two \(13\) times. You'll get \(0\) in \(1 + 13 = 14\) steps. \(a_2 = 32764\). You can increase it by one \(4\) times: \(32764 \rightarrow 32765 \rightarrow 32766 \rightarrow 32767 \rightarrow 0\). \(a_3...
Input: 4 19 32764 10240 49 | Output: 14 4 4 15
Easy
7
282
187
135
16
235
C
235C
C. Cyclical Quest
2,700
data structures; string suffix structures; strings
Some days ago, WJMZBMR learned how to answer the query ""how many times does a string x occur in a string s"" quickly by preprocessing the string s. But now he wants to make it harder.So he wants to ask ""how many consecutive substrings of s are cyclical isomorphic to a given string x"". You are given string s and n st...
The first line contains a non-empty string s. The length of string s is not greater than 106 characters.The second line contains an integer n (1 ≀ n ≀ 105) β€” the number of queries. Then n lines follow: the i-th line contains the string xi β€” the string for the i-th query. The total length of xi is less than or equal to ...
For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.
Input: baabaabaaa5ababaaaabaaaaba | Output: 75735
Master
3
825
402
194
2
266
B
266B
B. Queue at the School
800
constructive algorithms; graph matchings; implementation; shortest paths
During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each...
The first line contains two integers n and t (1 ≀ n, t ≀ 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find. The next line contains string s, which represents the schoolchildren's initial arrangement. If the i-th position in t...
Print string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal ""B"", otherwise it must equal ""G"".
Input: 5 1BGGBG | Output: GBGGB
Beginner
4
894
437
193
2
1,712
B
1712B
B. Woeful Permutation
800
constructive algorithms; greedy; number theory
I wonder, does the falling rain Forever yearn for it's disdain?Effluvium of the MindYou are given a positive integer \(n\).Find any permutation \(p\) of length \(n\) such that the sum \(\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)\) is as large as possible. Here \(\operat...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1\,000\)). Description of the test cases follows.The only line for each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does not excee...
For each test case print \(n\) integers \(p_1\), \(p_2\), \(\ldots\), \(p_n\) β€” the permutation with the maximum possible value of \(\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)\).If there are multiple answers, print any of them.
For \(n = 1\), there is only one permutation, so the answer is \([1]\).For \(n = 2\), there are two permutations: \([1, 2]\) β€” the sum is \(\operatorname{lcm}(1,1) + \operatorname{lcm}(2, 2) = 1 + 2 = 3\). \([2, 1]\) β€” the sum is \(\operatorname{lcm}(1,2) + \operatorname{lcm}(2, 1) = 2 + 2 = 4\).
Input: 212 | Output: 1 2 1
Beginner
3
715
331
277
17
1,575
L
1575L
L. Longest Array Deconstruction
2,100
data structures; divide and conquer; dp; sortings
Mr. Chanek gives you a sequence \(a\) indexed from \(1\) to \(n\). Define \(f(a)\) as the number of indices where \(a_i = i\). You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the \(3\)-rd element from the sequence \([4, 2, 3, ...
The first line contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the initial length of the sequence.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2 \cdot 10^5\)) β€” the initial sequence \(a\).
Output an integer denoting the largest \(f(a)\) that can be obtained by doing zero or more operations.
In the first example, \(f(A) = 3\) by doing the following operations.\([2,1,\textbf{4},2,5,3,7] \rightarrow [\textbf{2},1,2,5,3,7] \rightarrow [1,2,5,3,\textbf{7}] \rightarrow [1,2,\textbf{5},3] \rightarrow [1,2,3]\)In the second example, \(f(A) = 2\) and no additional operation is needed.
Input: 7 2 1 4 2 5 3 7 | Output: 3
Hard
4
513
242
102
15
722
B
722B
B. Verse Pattern
1,200
implementation; strings
You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: '...
The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of lines in the text.The second line contains integers p1, ..., pn (0 ≀ pi ≀ 100) β€” the verse pattern.Next n lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-emp...
If the given text matches the given verse pattern, then print ""YES"" (without quotes) in the only line of the output. Otherwise, print ""NO"" (without quotes).
In the first sample, one can split words into syllables in the following way: in-telco-dech al-len-geSince the word ""ch"" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one.
Input: 32 2 3intelcodech allenge | Output: YES
Easy
2
1,043
464
160
7
86
D
86D
D. Powerful array
2,200
data structures; implementation; math; two pointers
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≀ l ≀ r ≀ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products KsΒ·KsΒ·s for every positive integer s. T...
First line contains two integers n and t (1 ≀ n, t ≀ 200000) β€” the array length and the number of queries correspondingly.Second line contains n positive integers ai (1 ≀ ai ≀ 106) β€” the elements of the array.Next t lines contain two positive integers l, r (1 ≀ l ≀ r ≀ n) each β€” the indices of the left and the right en...
Output t lines, the i-th line of the output should contain single positive integer β€” the power of the i-th query subarray.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored): Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32Β·1 + 22Β·2 + 12Β·3 = 20.
Input: 3 21 2 11 21 3 | Output: 36
Hard
4
491
353
260
0
1,300
A
1300A
A. Non-zero
800
implementation; math
Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \dots, a_n\)]. In one step they can add \(1\) to any element of the array. Formally, in one step they can choose any integer index \(i\) (\(1 \le i \le n\)) and do \(a_i := a_i + 1\).If either the sum or the product of all elements in the array is...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β€” the size of the array.The second line of each test case contains \(n\) integ...
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
In the first test case, the sum is \(0\). If we add \(1\) to the first element, the array will be \([3,-1,-1]\), the sum will be equal to \(1\) and the product will be equal to \(3\).In the second test case, both product and sum are \(0\). If we add \(1\) to the second and the third element, the array will be \([-1,1,1...
Input: 4 3 2 -1 -1 4 -1 0 0 1 2 -1 2 3 0 -2 1 | Output: 1 2 0 2
Beginner
2
695
401
142
13
304
A
304A
A. Pythagorean Theorem II
1,200
brute force; math
In mathematics, the Pythagorean theorem β€” is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states: In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squ...
The only line contains one integer n (1 ≀ n ≀ 104) as we mentioned above.
Print a single integer β€” the answer to the problem.
Input: 5 | Output: 1
Easy
2
782
73
51
3
1,075
A
1075A
A. The King's Race
800
implementation; math
On a chessboard with a width of \(n\) and a height of \(n\), rows are numbered from bottom to top from \(1\) to \(n\), columns are numbered from left to right from \(1\) to \(n\). Therefore, for each cell of the chessboard, you can assign the coordinates \((r,c)\), where \(r\) is the number of the row, and \(c\) is the...
The first line contains a single integer \(n\) (\(2 \le n \le 10^{18}\)) β€” the length of the side of the chess field.The second line contains two integers \(x\) and \(y\) (\(1 \le x,y \le n\)) β€” coordinates of the cell, where the coin fell.
In a single line print the answer ""White"" (without quotes), if the white king will win, or ""Black"" (without quotes), if the black king will win.You can print each letter in any case (upper or lower).
An example of the race from the first sample where both the white king and the black king move optimally: The white king moves from the cell \((1,1)\) into the cell \((2,2)\). The black king moves form the cell \((4,4)\) into the cell \((3,3)\). The white king moves from the cell \((2,2)\) into the cell \((2,3)\). This...
Input: 42 3 | Output: White
Beginner
2
1,617
240
203
10
940
D
940D
D. Alena And The Heater
1,600
binary search; implementation
""We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme.""""Little Alena got an array as a birthday present...""The array b of length n is obtained from the array a of length n and two integers l and r (l ≀ r) using the following procedure:b1 = b2 = b3 =...
The first line of input contains a single integer n (5 ≀ n ≀ 105) β€” the length of a and b'.The second line of input contains n space separated integers a1, ..., an ( - 109 ≀ ai ≀ 109) β€” the elements of a.The third line of input contains a string of n characters, consisting of 0 and 1 β€” the elements of b'. Note that the...
Output two integers l and r ( - 109 ≀ l ≀ r ≀ 109), conforming to the requirements described above.If there are multiple solutions, output any of them.It's guaranteed that the answer exists.
In the first test case any pair of l and r pair is valid, if 6 ≀ l ≀ r ≀ 109, in that case b5 = 1, because a1, ..., a5 < l.
Input: 51 2 3 4 500001 | Output: 6 15
Medium
2
758
350
190
9
1,096
A
1096A
A. Find Divisible
800
greedy; implementation; math
You are given a range of positive integers from \(l\) to \(r\).Find such a pair of integers \((x, y)\) that \(l \le x, y \le r\), \(x \ne y\) and \(x\) divides \(y\).If there are multiple answers, print any of them.You are also asked to answer \(T\) independent queries.
The first line contains a single integer \(T\) (\(1 \le T \le 1000\)) β€” the number of queries.Each of the next \(T\) lines contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 998244353\)) β€” inclusive borders of the range.It is guaranteed that testset only includes queries, which have at least one suitable pair.
Print \(T\) lines, each line should contain the answer β€” two integers \(x\) and \(y\) such that \(l \le x, y \le r\), \(x \ne y\) and \(x\) divides \(y\). The answer in the \(i\)-th line should correspond to the \(i\)-th query from the input.If there are multiple answers, print any of them.
Input: 3 1 10 3 14 1 10 | Output: 1 7 3 9 5 10
Beginner
3
270
319
291
10
1,430
G
1430G
G. Yet Another DAG Problem
2,600
bitmasks; dfs and similar; dp; flows; graphs; math
You are given a directed acyclic graph (a directed graph that does not contain cycles) of \(n\) vertices and \(m\) arcs. The \(i\)-th arc leads from the vertex \(x_i\) to the vertex \(y_i\) and has the weight \(w_i\).Your task is to select an integer \(a_v\) for each vertex \(v\), and then write a number \(b_i\) on eac...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 18\); \(0 \le m \le \dfrac{n(n - 1)}{2}\)).Then \(m\) lines follow, the \(i\)-th of them contains three integers \(x_i\), \(y_i\) and \(w_i\) (\(1 \le x_i, y_i \le n\), \(1 \le w_i \le 10^5\), \(x_i \ne y_i\)) β€” the description of the \(i\)-th arc.It i...
Print \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_v \le 10^9\)), which must be written on the vertices so that all \(b_i\) are positive, and the value of the expression \(\sum \limits_{i = 1}^{m} w_i b_i\) is the lowest possible. If there are several answers, print any of them. It can be shown that the ans...
Input: 3 2 2 1 4 1 3 2 | Output: 1 2 0
Expert
6
639
452
428
14
486
D
486D
D. Valid Sets
2,100
dfs and similar; dp; math; trees
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it.We call a set S of tree nodes valid if following conditions are satisfied: S is non-empty. S is connected. In other words, ...
The first line contains two space-separated integers d (0 ≀ d ≀ 2000) and n (1 ≀ n ≀ 2000).The second line contains n space-separated positive integers a1, a2, ..., an(1 ≀ ai ≀ 2000).Then the next n - 1 line each contain pair of integers u and v (1 ≀ u, v ≀ n) denoting that there is an edge between u and v. It is guara...
Print the number of valid sets modulo 1000000007.
In the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4} and {1, 3, 4}. Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies the third condition, but conflicts with the second condition.
Input: 1 42 1 3 21 21 33 4 | Output: 8
Hard
4
576
355
49
4
2,095
D
2095D
D. Where Am I?
0
*special; geometry
Output two real numbers \(-90 \leq a \leq 90\) and \(-180 \leq b \leq 180\), with up to 6 digits after the decimal point.
Input: ​ | Output: ​
Beginner
2
0
0
121
20
106
B
106B
B. Choosing Laptop
1,000
brute force; implementation
Vasya is choosing a laptop. The shop has n laptops to all tastes.Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties.If all three properties of a laptop are strictly less than those properties of s...
The first line contains number n (1 ≀ n ≀ 100).Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, speed, ram, hdd and cost are integers 1000 ≀ speed ≀ 4200 is the processor's speed in megahertz 256 ≀ ram ≀ 4096 the RAM volume in megabytes 1 ≀ hdd ≀ 500 is the HDD in gigabytes 100 ≀ cost ≀ 1000...
Print a single number β€” the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data.
In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop.
Input: 52100 512 150 2002000 2048 240 3502300 1024 200 3202500 2048 80 3002000 512 180 150 | Output: 4
Beginner
2
657
375
181
1
1,531
A
1531A
A. Π—ΠΈΠ½Π³Π΅Ρ€ | color
0
*special; implementation
НСмногиС Π·Π½Π°ΡŽΡ‚, Ρ‡Ρ‚ΠΎ сотрудники Π’ΠšΠΎΠ½Ρ‚Π°ΠΊΡ‚Π΅ ΠΌΠΎΠ³ΡƒΡ‚ ΠΌΠ΅Π½ΡΡ‚ΡŒ Ρ†Π²Π΅Ρ‚ подсвСтки Π² ΠΊΡƒΠΏΠΎΠ»Π΅ Π·Π½Π°ΠΌΠ΅Π½ΠΈΡ‚ΠΎΠ³ΠΎ Π”ΠΎΠΌΠ° Π—ΠΈΠ½Π³Π΅Ρ€Π°, Π³Π΄Π΅ располоТСна ΡˆΡ‚Π°Π±-ΠΊΠ²Π°Ρ€Ρ‚ΠΈΡ€Π° Π’ΠšΠΎΠ½Ρ‚Π°ΠΊΡ‚Π΅. Для этого Π½ΡƒΠΆΠ½ΠΎ всСго лишь ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ сообщСниС с Ρ†Π²Π΅Ρ‚ΠΎΠΌ Π² ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ Ρ‡Π°Ρ‚ Β«Π—ΠΈΠ½Π³Π΅Ρ€ | colorΒ», Π° Π±ΠΎΡ‚ Π΅Π³ΠΎ распознаСт ΠΈ смСнит подсвСтку. ΠŸΡ€ΠΈ этом Π½Π° врСмя городских мСроприятий см...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π·Π°Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число \(n\) (\(1 \le n \le 100\)) β€” число сообщСний, ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Ρ… Π±ΠΎΡ‚ΠΎΠΌ.Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… \(n\) строках Π·Π°Π΄Π°Π½Ρ‹ сообщСния, ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Π΅ Π±ΠΎΡ‚ΠΎΠΌ, Π² хронологичСском порядкС, ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΡΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΡŽ Π² строкС. КаТдоС сообщСниС β€” строка ΠΈΠ· ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅Π³ΠΎ Π½Π°Π±ΠΎΡ€Π°: lock, unlock, red, orange, yellow, green, blue, i...
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ†Π²Π΅Ρ‚ ΠΊΡƒΠΏΠΎΠ»Π° послС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ сообщСний Π±ΠΎΡ‚ΠΎΠΌ.
Input: 7 red violet unlock red orange lock indigo | Output: orange
Beginner
2
1,032
334
53
15
2,094
C
2094C
C. Brr Brrr Patapim
900
math
Brr Brrr Patapim is trying to learn of TiramisΓΉ's secret passcode, which is a permutation\(^{\text{βˆ—}}\) of \(2\cdot n\) elements. To help Patapim guess, TiramisΓΉ gave him an \(n\times n\) grid \(G\), in which \(G_{i,j}\) (or the element in the \(i\)-th row and \(j\)-th column of the grid) contains \(p_{i+j}\), or the ...
The first line contains an integer \(t\) β€” the number of test cases (\(1 \leq t \leq 200\)).The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 800\)).Each of the following \(n\) lines contains \(n\) integers, giving the grid \(G\). The first of these lines contains \(G_{1,1}, G_{1,2},\ldots,G_{...
For each test case, please output \(2n\) numbers on a new line: \(p_1,p_2,\ldots,p_{2n}\).
Input: 331 6 26 2 42 4 31122 33 4 | Output: 5 1 6 2 4 3 2 1 1 2 3 4
Beginner
1
780
571
90
20
459
B
459B
B. Pashmak and Flowers
1,300
combinatorics; implementation; sortings
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference i...
The first line of the input contains n (2 ≀ n ≀ 2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≀ bi ≀ 109).
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers.
Input: 21 2 | Output: 1 1
Easy
3
672
141
136
4
1,956
E1
1956E1
E1. Nene vs. Monsters (Easy Version)
2,500
brute force; implementation; math
This is the easy version of the problem. The only difference between the versions is the constraints on \(a_i\). You can make hacks only if both versions of the problem are solved.Nene is fighting with \(n\) monsters, located in a circle. These monsters are numbered from \(1\) to \(n\), and the \(i\)-th (\(1 \le i \le ...
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 test cases follows.The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of monsters.The second line contains \(n\) integers \(a_1, a_2, \ldots, ...
For each test case, in the first line output an integer \(m\) β€” the number of monsters with non-zero energy level after \(10^{100}\) uses of the spell; in the second line of output \(m\) integers \(i_1,i_2,\ldots,i_m\) (\(1 \le i_1 < i_2 < \ldots < i_m \le n\)) β€” the indices of these monsters in the increasing order. I...
In the first test case, the following actions happen during the first \(3\) uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 5-2)=3\); the...
Input: 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0 | Output: 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
Expert
3
1,272
491
386
19
2,002
C
2002C
C. Black Circles
1,200
brute force; geometry; greedy; math
There are \(n\) circles on a two-dimensional plane. The \(i\)-th circle is centered at \((x_i,y_i)\). Initially, all circles have a radius of \(0\).The circles' radii increase at a rate of \(1\) unit per second.You are currently at \((x_s,y_s)\); your goal is to reach \((x_t,y_t)\) without touching the circumference of...
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 a single integer \(n\) (\(1\le n\le10^5\)) β€” the number of circles.The next \(n\) lines each contain two integers \(x_i\...
For each test case, output \(\texttt{YES}\) if it is possible to reach the goal without touching the circle boundaries, and output \(\texttt{NO}\) otherwise.You can output \(\texttt{Yes}\) and \(\texttt{No}\) in any case (for example, strings \(\texttt{yEs}\), \(\texttt{yes}\), \(\texttt{Yes}\), and \(\texttt{YES}\) wi...
In the first test case, a feasible way of movement is as follows.
Input: 732 52 1410 134 9 9 7310 116 912 1214 13 4 815 712 6 11 1321000000000 22 10000000001 1 2 21999999998 1000000000999999999 999999999 1 111000000000 11 1000000000 1 110989237121 2397081206669655 527238537522705783 380636165532545346 320061691207818728 199485303884520552 315781807992311437 802563521205138355 3248186...
Easy
4
518
698
361
20
1,647
B
1647B
B. Madoka and the Elegant Gift
1,200
brute force; constructive algorithms; graphs; implementation
Madoka's father just reached \(1\) million subscribers on Mathub! So the website decided to send him a personalized award β€” The Mathhub's Bit Button! The Bit Button is a rectangular table with \(n\) rows and \(m\) columns with \(0\) or \(1\) in each cell. After exploring the table Madoka found out that: A subrectangle ...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 200\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains two positive integers \(n, m\) (\(1 \le n, m \le 100\)).The next \(n\) lines contain strings of length ...
For each test case print ""YES"" if its table is elegant or print ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).
In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect. In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
Input: 53 31000110113 31101111101 5011114 5111110101001000010003 2110011 | Output: YES NO YES NO YES
Easy
4
965
515
213
16
69
B
69B
B. Bets
1,200
greedy; implementation
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname ""Boss"". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor.To begin with friends...
The first line contains two integers n and m (1 ≀ n, m ≀ 100). Then follow m lines, each containing 4 integers li, ri, ti, ci (1 ≀ li ≀ ri ≀ n, 1 ≀ ti, ci ≀ 1000).
Print a single integer, the maximal profit in roubles that the friends can get. In each of n sections it is not allowed to place bets on more than one sportsman.
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles.In the second ...
Input: 4 41 4 20 51 3 21 103 3 4 303 4 4 20 | Output: 60
Easy
2
1,778
163
161
0
1,599
E
1599E
E. Two Arrays
3,200
data structures; matrices
You are given two integer arrays of length \(N\), \(A1\) and \(A2\). You are also given \(Q\) queries of 4 types: 1 k l r x: set \(Ak_i:=min(Ak_i, x)\) for each \(l \leq i \leq r\).2 k l r x: set \(Ak_i:=max(Ak_i, x)\) for each \(l \leq i \leq r\).3 k l r x: set \(Ak_i:=Ak_i+x\) for each \(l \leq i \leq r\).4 l r: find...
The first line contains two integers \(N\) and \(Q\). (\(1 \leq N, Q \leq 5 \times 10^4\)) The second line contains \(N\) integers, array \(A1_1, A1_2, \dots A1_N\). (\(0 \leq A1_i \leq 10^6\))The third line contains \(N\) integers, array \(A2_1, A2_2, \dots A2_N\). (\(0 \leq A2_i \leq 10^6\))The next \(Q\) lines descr...
Print the answer to each query of the fourth type, in separate lines.
In the first example: The answer for the first query is \(F(1 + 2) + F(0 + 1) + F(2 + 0) = F(3) + F(1) + F(2) = 2 + 1 + 1 = 4\). After the second query, the array \(A2\) changes to \([2, 4, 0]\). After the third query, the array \(A1\) changes to \([0, 0, 0]\). The answer for the fourth query is \(F(0 + 2) + F(0 + 4) +...
Input: 3 4 1 0 2 2 1 0 4 1 3 3 2 2 2 3 1 1 1 3 0 4 1 3 | Output: 4 4
Master
2
604
690
69
15
813
F
813F
F. Bipartite Checking
2,500
data structures; dsu; graphs
You are given an undirected graph consisting of n vertices. Initially there are no edges in the graph. Also you are given q queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of the g...
The first line contains two integers n and q (2 ≀ n, q ≀ 100000).Then q lines follow. ith line contains two numbers xi and yi (1 ≀ xi < yi ≀ n). These numbers describe ith query: if there is an edge between vertices xi and yi, then remove it, otherwise add it.
Print q lines. ith line must contain YES if the graph is bipartite after ith query, and NO otherwise.
Input: 3 52 31 31 21 21 2 | Output: YESYESNOYESNO
Expert
3
409
260
101
8
152
B
152B
B. Steps
1,300
binary search; implementation
One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: ""How did he do that?"" The answer is simple.Vasya noticed that the yard is a rectangu...
The first input line contains two integers n and m (1 ≀ n, m ≀ 109) β€” the yard's sizes. The second line contains integers xc and yc β€” the initial square's coordinates (1 ≀ xc ≀ n, 1 ≀ yc ≀ m).The third line contains an integer k (1 ≀ k ≀ 104) β€” the number of vectors. Then follow k lines, each of them contains two integ...
Print the single number β€” the number of steps Vasya had made.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, ...
Input: 4 51 131 11 10 -2 | Output: 4
Easy
2
1,461
374
213
1
1,830
A
1830A
A. Copil Copac Draws Trees
1,400
dfs and similar; dp; graphs; trees
Copil Copac is given a list of \(n-1\) edges describing a tree of \(n\) vertices. He decides to draw it using the following algorithm: Step \(0\): Draws the first vertex (vertex \(1\)). Go to step \(1\). Step \(1\): For every edge in the input, in order: if the edge connects an already drawn vertex \(u\) to an undrawn ...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of vertices of the tree....
For each test case, output the number of readings Copil Copac needs to draw the tree.
In the first test case:After the first reading, the tree will look like this: After the second reading: Therefore, Copil Copac needs \(2\) readings to draw the tree.
Input: 264 51 31 23 41 675 62 42 71 31 24 5 | Output: 2 3
Easy
4
679
662
85
18
1,742
A
1742A
A. Sum
800
implementation
You are given three integers \(a\), \(b\), and \(c\). Determine if one of them is the sum of the other two.
The first line contains a single integer \(t\) (\(1 \leq t \leq 9261\)) β€” the number of test cases.The description of each test case consists of three integers \(a\), \(b\), \(c\) (\(0 \leq a, b, c \leq 20\)).
For each test case, output ""YES"" if one of the numbers is the sum of the other two, 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).
In the first test case, \(1 + 3 = 4\).In the second test case, none of the numbers is the sum of the other two.In the third test case, \(9 + 11 = 20\).
Input: 71 4 32 5 89 11 200 0 020 20 204 12 315 7 8 | Output: YES NO YES YES NO NO YES
Beginner
1
107
209
250
17
1,360
A
1360A
A. Minimal Square
800
greedy; math
Find the minimum area of a square land on which you can place two identical rectangular \(a \times b\) houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths \(a\) and \(b\) (\(1 \le a, b \le 100\)) β€” positive integer...
The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) β€”the number of test cases in the input. Then \(t\) test cases follow.Each test case is a line containing two integers \(a\), \(b\) (\(1 \le a, b \le 100\)) β€” side lengths of the rectangles.
Print \(t\) answers to the test cases. Each answer must be a single integer β€” minimal area of square land, that contains two rectangles with dimensions \(a \times b\).
Below are the answers for the first two test cases:
Input: 8 3 2 4 2 1 1 3 1 4 7 1 3 7 4 100 100 | Output: 16 16 4 9 64 9 64 40000
Beginner
2
905
256
167
13
753
B
753B
B. Interactive Bulls and Cows (Easy)
1,600
brute force; constructive algorithms; implementation
This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For exa...
To read answers to the queries, the program must use the standard input.The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string s and the string xi printed by your program. If the system...
The program must use the standard output to print queries.Your program must output requests β€” 4-digit strings x1, x2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.Your program is allowed to do at most 5...
The secret string s in the example is ""0123"".
Input: 0 12 01 10 42 14 0 | Output: 800001793159321001120123
Medium
3
2,583
610
330
7
208
B
208B
B. Solitaire
1,900
dfs and similar; dp
A boy named Vasya wants to play an old Russian solitaire called ""Accordion"". In this solitaire, the player must observe the following rules: A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; Before each move the table has several piles of cards lying in a lin...
The first input line contains a single integer n (1 ≀ n ≀ 52) β€” the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the s...
On a single line print the answer to the problem: string ""YES"" (without the quotes) if completing the solitaire is possible, string ""NO"" (without the quotes) otherwise.
In the first sample you can act like that: put the 4-th pile on the 1-st one; put the 3-rd pile on the 2-nd one; put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
Input: 42S 2S 2C 2C | Output: YES
Hard
2
1,150
734
172
2
1,583
E
1583E
E. Moment of Bloom
2,200
constructive algorithms; dfs and similar; graph matchings; graphs; greedy; trees
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang.Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of \(n\) nodes with \(m\) edges. You also have \(q\) queries. Each quer...
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 3 \cdot 10^5\), \(n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2}, 3 \cdot 10^5\right)}\)).Each of the next \(m\) lines contains two integers \(x\) and \(y\) (\(1 \leq x, y \leq n\), \(x\neq y\)) indicating an undirected edge between node \(x\) and \(y\)...
If it is possible to force all edge weights to be even, print ""YES"" on the first line, followed by \(2q\) lines indicating the choice of path for each query in the same order the queries are given. For each query, the first line should contain a single integer \(x\): the number of nodes in the chosen path. The next l...
Here is what the queries look like for the first test case (red corresponds to the 1st query, blue 2nd query, and green 3rd query): Notice that every edge in the graph is part of either \(0\) or \(2\) colored query edges.The graph in the second test case looks like this: There does not exist an assignment of paths that...
Input: 6 7 2 1 2 3 3 5 1 4 6 1 5 6 4 5 3 1 4 5 1 4 5 | Output: YES 2 1 4 4 5 3 2 1 5 4 1 2 3 5
Hard
6
1,068
680
723
15
414
E
414E
E. Mashmokh's Designed Problem
3,200
data structures
After a lot of trying, Mashmokh designed a problem and it's your job to solve it.You have a tree T with n vertices. Each vertex has a unique index from 1 to n. The root of T has index 1. For each vertex of this tree v, you are given a list of its children in a specific order. You must perform three types of query on th...
The first line of input contains two space-separated integers n, m (2 ≀ n ≀ 105; 1 ≀ m ≀ 105), the number of vertices of T and number of queries to perform.The i-th of the following n lines contains an integer li (0 ≀ li ≀ n), number of i-th vertex's children. Then li space-separated integers follow, the j-th of them i...
For each query of the first or third type output one line containing the result of the query.
Input: 4 91 21 31 401 1 42 4 21 3 43 13 22 3 21 1 23 13 2 | Output: 3224134
Master
1
1,158
913
93
4
859
D
859D
D. Third Month Insanity
2,100
dp; probabilities; trees
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2N teams participating in the tournament, numbered from 1 to 2N. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2N - ...
Input will begin with a line containing N (2 ≀ N ≀ 6).2N lines follow, each with 2N integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the...
Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10 - 9.Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if .
In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
Input: 20 40 100 10060 0 40 400 60 0 450 60 55 0 | Output: 1.75
Hard
3
1,498
354
252
8
1,462
A
1462A
A. Favorite Sequence
800
implementation; two pointers
Polycarp has a favorite sequence \(a[1 \dots n]\) consisting of \(n\) integers. He wrote it out on the whiteboard as follows: he wrote the number \(a_1\) to the left side (at the beginning of the whiteboard); he wrote the number \(a_2\) to the right side (at the end of the whiteboard); then as far to the left as possib...
The first line contains a single positive integer \(t\) (\(1 \le t \le 300\)) β€” the number of test cases in the test. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 300\)) β€” the length of the sequence written on the whiteboard.The next line contains \(n\) integers...
Output \(t\) answers to the test cases. Each answer β€” is a sequence \(a\) that Polycarp wrote out on the whiteboard.
In the first test case, the sequence \(a\) matches the sequence from the statement. The whiteboard states after each step look like this:\([3] \Rightarrow [3, 1] \Rightarrow [3, 4, 1] \Rightarrow [3, 4, 1, 1] \Rightarrow [3, 4, 5, 1, 1] \Rightarrow [3, 4, 5, 9, 1, 1] \Rightarrow [3, 4, 5, 2, 9, 1, 1]\).
Input: 6 7 3 4 5 2 9 1 1 4 9 2 7 1 11 8 4 3 1 2 7 8 7 9 4 2 1 42 2 11 7 8 1 1 1 1 1 1 1 1 | Output: 3 1 4 1 5 9 2 9 1 2 7 8 2 4 4 3 9 1 7 2 8 7 42 11 7 1 1 1 1 1 1 1 1
Beginner
2
890
412
116
14
2,005
B2
2005B2
B2. The Strict Teacher (Hard Version)
1,200
binary search; greedy; math; sortings
This is the hard version of the problem. The only differences between the two versions are the constraints on \(m\) and \(q\). In this version, \(m, q \le 10^5\). You can make hacks only if both versions of the problem are solved.Narek and Tsovak were busy preparing this round, so they have not managed to do their home...
In the first line of the input, you are given a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of each test case follows.In the first line of each test case, you are given three integers \(n\), \(m\), and \(q\) (\(3 \le n \le 10^9\), \(1 \le m, q \le 10^5\)) β€” the number of cell...
For each test case, output \(q\) lines, the \(i\)-th of them containing the answer of the \(i\)-th query.
In the only query of the first example, the student can run to cell \(1\). It will take the teacher five moves to reach from cell \(6\) to cell \(1\), so the answer is \(5\).In the second query of the second example, the student can just stay at cell \(3\). The teacher, initially located in cell \(4\), can reach cell \...
Input: 28 1 16310 3 31 4 82 3 10 | Output: 5 1 1 2
Easy
4
1,658
1,011
105
20
1,183
C
1183C
C. Computer Game
1,400
binary search; math
Vova is playing a computer game. There are in total \(n\) turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is \(k\).During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly gre...
The first line of the input contains one integer \(q\) (\(1 \le q \le 10^5\)) β€” the number of queries. Each query is presented by a single line.The only line of the query contains four integers \(k, n, a\) and \(b\) (\(1 \le k, n \le 10^9, 1 \le b < a \le 10^9\)) β€” the initial charge of Vova's laptop battery, the numbe...
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
In the first example query Vova can just play \(4\) turns and spend \(12\) units of charge and then one turn play and charge and spend \(2\) more units. So the remaining charge of the battery will be \(1\).In the second example query Vova cannot complete the game because even if he will play and charge the battery duri...
Input: 6 15 5 3 2 15 5 4 3 15 5 2 1 15 5 5 1 16 7 5 2 20 5 7 3 | Output: 4 -1 5 2 0 1
Easy
2
1,454
387
157
11
2,073
I
2073I
3,100
Master
0
0
0
0
20
1,108
B
1108B
B. Divisors of Two Integers
1,100
brute force; greedy; math; number theory
Recently you have received two positive integer numbers \(x\) and \(y\). You forgot them, but you remembered a shuffled list containing all divisors of \(x\) (including \(1\) and \(x\)) and all divisors of \(y\) (including \(1\) and \(y\)). If \(d\) is a divisor of both numbers \(x\) and \(y\) at the same time, there a...
The first line contains one integer \(n\) (\(2 \le n \le 128\)) β€” the number of divisors of \(x\) and \(y\).The second line of the input contains \(n\) integers \(d_1, d_2, \dots, d_n\) (\(1 \le d_i \le 10^4\)), where \(d_i\) is either divisor of \(x\) or divisor of \(y\). If a number is divisor of both numbers \(x\) a...
Print two positive integer numbers \(x\) and \(y\) β€” such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
Input: 10 10 2 8 1 2 4 1 20 4 5 | Output: 20 8
Easy
4
869
382
191
11
270
B
270B
B. Multithreading
1,400
data structures; greedy; implementation
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the ""recent actions"" list. He likes to read thread conversations where each thread consists of multiple messages.Recent actions shows a list of n different threads ordered by the time of the latest message in the thread....
The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≀ ai ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
Output a single integer β€” the number of threads that surely contain a new message.
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.In the second test case, there may be no new messages at all, since the thread order hasn't changed.In the third t...
Input: 55 2 1 3 4 | Output: 2
Easy
3
1,179
287
82
2
1,876
G
1876G
G. Clubstep
3,500
binary search; brute force; data structures; greedy; trees
There is an extremely hard video game that is one of Chaneka's favourite video games. One of the hardest levels in the game is called Clubstep. Clubstep consists of \(n\) parts, numbered from \(1\) to \(n\). Chaneka has practised the level a good amount, so currently, her familiarity value with each part \(i\) is \(a_i...
The first line contains a single integer \(n\) (\(1 \leq n \leq 3\cdot10^5\)) β€” the number of parts in Clubstep.The second line contains \(n\) integers \(a_1,a_2,a_3,\ldots,a_n\) (\(1\leq a_i\leq10^9\)) β€” Chaneka's familiarity value with each part.The third line contains a single integer \(q\) (\(1\leq q\leq3\cdot10^5\...
Output \(q\) lines with an integer in each line. The integer in the \(j\)-th line represents the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part \(p\) (\(l_j \leq p \leq r_j\)) is at least \(x_j\).
For the \(1\)-st question, one possible strategy is to do the following: Do \(1\) attempt that dies on part \(1\). This takes \(1\) second. The familiarity values become \([3, 3, 2, 1, 2]\). Do \(1\) attempt that dies on part \(4\). This takes \(4\) seconds. The familiarity values become \([4, 4, 3, 3, 2]\). Do \(2\) a...
Input: 5 1 3 2 1 2 3 1 5 5 2 4 5 3 3 1 | Output: 15 11 0
Master
5
1,582
536
245
18
1,210
A
1210A
A. Anadi and Domino
1,700
brute force; graphs
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every \(a\) and \(b\) such that \(1 \leq a \leq b \leq 6\), there is exactly one domino with \(a\) dots on one half and \(b\) dots on the other half. The set contains exactly \(21\) dominoes. Here is an exact illustration of ...
The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 7\), \(0 \leq m \leq \frac{n\cdot(n-1)}{2}\)) β€” the number of vertices and the number of edges in the graph.The next \(m\) lines contain two integers each. Integers in the \(i\)-th line are \(a_i\) and \(b_i\) (\(1 \leq a, b \leq n\), \(a \neq b\)) a...
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
Here is an illustration of Anadi's graph from the first sample test: And here is one of the ways to place a domino on each of its edges: Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex \(1\) have three dots.
Input: 4 4 1 2 2 3 3 4 4 1 | Output: 4
Medium
2
1,073
564
112
12
1,868
C
1868C
C. Travel Plan
2,400
combinatorics; dp; implementation; math; trees
During the summer vacation after Zhongkao examination, Tom and Daniel are planning to go traveling.There are \(n\) cities in their country, numbered from \(1\) to \(n\). And the traffic system in the country is very special. For each city \(i\) (\(1 \le i \le n\)), there is a road between city \(i\) and \(2i\), if \(2i...
The first line of input contains a single integer \(t\) (\(1\le t\le 200\)) β€” the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1\leq n\leq 10^{18}\), \(1\leq m\leq 10^5\)) β€” the number of the cities and the maximum value of a city.I...
For each test case output one integer β€” the sum of scores of all possible travel plans, modulo \(998\,244\,353\).
In the first test case, there is only one possible travel plan: Path \(1\rightarrow 1\): \(s_{1,1}=a_1=1\).Path \(1\rightarrow 2\): \(s_{1,2}=\max(1,1)=1\).Path \(1\rightarrow 3\): \(s_{1,3}=\max(1,1)=1\).Path \(2\rightarrow 2\): \(s_{2,2}=a_2=1\).Path \(2\rightarrow 1\rightarrow 3\): \(s_{2,3}=\max(1,1,1)=1\).Path \(3...
Input: 53 12 210 943 20154 147 | Output: 6 19 583217643 68816635 714002110
Expert
5
997
403
113
18
2,072
E
2072E
E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?
1,500
binary search; brute force; constructive algorithms; dp; geometry; greedy; math
Akito decided to study a new powerful spell. Since it possesses immeasurable strength, it certainly requires a lot of space and careful preparation. For this, Akito went out into the field. Let's represent the field as a Cartesian coordinate system.For the spell, Akito needs to place \(0 \le n \le 500\) staffs at disti...
The first line of input contains a single number \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.In the only line of each test case, there is a single number \(k\) (\(0 \le k \le 10^5\)) β€” the number of pairs of staffs for which the equality \(\rho(i, j) = d(i, j)\) must hold.
For each test case, the first line of output should print the number \(n\) (\(0 \le n \le 500\)) β€” the number of placed staffs.In the following \(n\) lines, pairs of integers \(x_i, y_i\) \((-10^9 \le x_i, y_i \le 10^9)\) should be printed β€” the coordinates of the \(i\)-th staff. The points in which staffs stand must b...
Input: 3025 | Output: 6 69 52 4 20 789 9308706 1337 1337 -1234 -5678 23456178 707 10 -236 -346262358 273568 6435267 2365437 31441367 246574 -45642372 -236 56 4743623 -192892 10408080 -8173135 -237415357 31441367 -78125638 278 56 143231 5 1 1 2 1 1 5 3 5 1 10
Medium
7
660
285
331
20
763
D
763D
D. Timofey and a flat tree
2,900
data structures; graphs; hashing; shortest paths; trees
Little Timofey has a big tree β€” an undirected connected graph with n vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex.Timofey assumes that ...
First line contains single integer n (1 ≀ n ≀ 105) β€” number of vertices in the tree.Each of the next n - 1 lines contains two integers ui and vi (1 ≀ ui, vi ≀ 105, ui β‰  vi), denoting the vertices the i-th edge connects.It is guaranteed that the given graph is a tree.
Print single integer β€” the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them.
In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic.In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic.In the third example, if w...
Input: 31 22 3 | Output: 1
Master
5
1,021
267
131
7
254
B
254B
B. Jury Size
1,500
brute force; implementation
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems...
The first line contains integer n β€” the number of Olympiads in 2013 (1 ≀ n ≀ 100). Each of the following n lines contains four integers mi, di, pi and ti β€” the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≀ mi ≀ 1...
Print a single number β€” the minimum jury size.
Input: 25 23 1 23 13 2 3 | Output: 2
Medium
2
1,420
712
46
2
1,159
A
1159A
A. A pile of stones
800
implementation; math
Vasya has a pile, that consists of some number of stones. \(n\) times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.You are given \(n\) operations which Vasya has made. Find the minimal possible number of stones that...
The first line contains one positive integer \(n\) β€” the number of operations, that have been made by Vasya (\(1 \leq n \leq 100\)).The next line contains the string \(s\), consisting of \(n\) symbols, equal to ""-"" (without quotes) or ""+"" (without quotes). If Vasya took the stone on \(i\)-th operation, \(s_i\) is e...
Print one integer β€” the minimal possible number of stones that can be in the pile after these \(n\) operations.
In the first test, if Vasya had \(3\) stones in the pile at the beginning, after making operations the number of stones will be equal to \(0\). It is impossible to have less number of piles, so the answer is \(0\). Please notice, that the number of stones at the beginning can't be less, than \(3\), because in this case...
Input: 3 --- | Output: 0
Beginner
2
370
405
111
11
1,967
F
1967F
F. Next and Prev
3,200
brute force; data structures; implementation
Let \(p_1, \ldots, p_n\) be a permutation of \([1, \ldots, n]\).Let the \(q\)-subsequence of \(p\) be a permutation of \([1, q]\), whose elements are in the same relative order as in \(p_1, \ldots, p_n\). That is, we extract all elements not exceeding \(q\) together from \(p\) in the original order, and they make the \...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3\cdot 10^5\)) β€” the length of the permutation.The second line of each test cas...
For each test case, for each query, print a single line with an integer: the answer to the query.
The \(1\)-subsequence is \([1]\), and \(pre=[-10^{100}]\), \(nxt=[10^{100}]\). \(ans(1)=\min(10^{100}-(-10^{100}),1)=1\).The \(5\)-subsequence is \([1,4,3,2,5]\), and \(pre=[-10^{100},-10^{100},2,3,-10^{100}]\), \(nxt=[2,5,5,5,10^{100}]\). \(ans(1)=5,ans(2)=10,ans(3)=14\).
Input: 176 1 4 3 2 5 71 101 31 23 1 2 31 32 2 6 | Output: 1 9 8 5 10 14 16 14 30
Master
3
1,001
881
97
19
19
E
19E
E. Fairy
2,900
dfs and similar; divide and conquer; dsu
Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper n points and joined some of the...
The first input line contains two integer numbers: n β€” amount of the drawn points and m β€” amount of the drawn segments (1 ≀ n ≀ 104, 0 ≀ m ≀ 104). The following m lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers v, u (1 ≀ v ≀ n, 1 ≀ u ≀ n) β€” indexes...
In the first line output number k β€” amount of the segments in the answer. In the second line output k space-separated numbers β€” indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order.
Input: 4 41 21 32 43 4 | Output: 41 2 3 4
Master
3
827
403
261
0
382
B
382B
B. Number Busters
2,000
binary search; math
Arthur and Alexander are number busters. Today they've got a competition. Arthur took a group of four integers a, b, w, x (0 ≀ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. ...
The first line contains integers a, b, w, x, c (1 ≀ a ≀ 2Β·109, 1 ≀ w ≀ 1000, 0 ≀ b < w, 0 < x < w, 1 ≀ c ≀ 2Β·109).
Print a single integer β€” the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.
Input: 4 2 3 1 6 | Output: 2
Hard
2
793
114
178
3
150
D
150D
D. Mission Impassable
2,600
dp; strings
Market stalls now have the long-awaited game The Colder Scrools V: Nvodsk. The game turned out to be difficult as hell and most students can't complete the last quest (""We don't go to Nvodsk...""). That threatened winter exams. The rector already started to wonder whether he should postpone the winter exams till April...
The first line contains an integer l (1 ≀ l ≀ 150) β€” the length of the string.The second line contains exactly l integers ak ( - 1 ≀ ak ≀ 105) β€” the points a player gains for deleting.The third line contains exactly l lowercase Latin letters β€” the original string from which a player can delete palindromes. The line con...
Print a single number β€” the maximum number of points one can gain if he plays on the given string.
In the first sample we cannot delete any substring, so the best result is 0. In the second sample we are allowed to delete only those palindromes whose length equals 1, thus, if we delete the whole string, we get 7 points. In the third sample the optimal strategy is: first we delete character c, then string aa, then bb...
Input: 7-1 -1 -1 -1 -1 -1 -1abacaba | Output: 0
Expert
2
1,615
404
98
1
1,000
A
1000A
A. Codehorses T-shirts
1,200
greedy; implementation
Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either ""M"" or from \(0\) to \(3\) ""X"" followed by ""S"" or ""L"". For example, sizes ""M"", ""XXS"", ""L"", ""XXXL"" are valid and ""XM"", ""Z...
The first line contains one integer \(n\) (\(1 \le n \le 100\)) β€” the number of T-shirts.The \(i\)-th of the next \(n\) lines contains \(a_i\) β€” the size of the \(i\)-th T-shirt of the list for the previous year.The \(i\)-th of the next \(n\) lines contains \(b_i\) β€” the size of the \(i\)-th T-shirt of the list for the...
Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.
In the first example Ksenia can replace ""M"" with ""S"" and ""S"" in one of the occurrences of ""XS"" with ""L"".In the second example Ksenia should replace ""L"" in ""XXXL"" with ""S"".In the third example lists are equal.
Input: 3XSXSMXLSXS | Output: 2
Easy
2
1,185
471
153
10
354
B
354B
B. Game with Strings
2,400
bitmasks; dp; games
Given an n Γ— n table T consisting of lowercase English letters. We'll consider some string s good if the table contains a correct path corresponding to the given string. In other words, good strings are all strings we can obtain by moving from the left upper cell of the table only to the right and down. Here's the form...
The first line contains a single number n (1 ≀ n ≀ 20).Next n lines contain n lowercase English letters each β€” table T.
In a single line print string ""FIRST"", if the first player wins, ""SECOND"", if the second player wins and ""DRAW"", if the game ends with a draw.
Consider the first sample:Good strings are strings: a, ab, ac, abd, acd.The first player moves first and adds letter a to the string, as there is only one good string of length 1. Then the second player can add b or c and the game will end with strings abd or acd, correspondingly. In the first case it will be a draw (t...
Input: 2abcd | Output: DRAW
Expert
3
1,873
119
148
3
354
C
354C
C. Vasya and Beautiful Arrays
2,100
brute force; dp; number theory
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the ...
The first line contains two integers n and k (1 ≀ n ≀ 3Β·105; 1 ≀ k ≀ 106). The second line contains n integers ai (1 ≀ ai ≀ 106) β€” array a.
In the single line print a single number β€” the maximum possible beauty of the resulting array.
In the first sample we can obtain the array:3 6 9 12 12 15In the second sample we can obtain the next array:7 21 49 14 77
Input: 6 13 6 10 12 13 16 | Output: 3
Hard
3
693
139
94
3
1,949
E
1949E
E. Damage per Second
2,900
brute force; math
You just created a new character in your favourite role-playing game and now have to decide how to skill him. The two skill attributes to be chosen are: damage per hit and hits per second. Damage per hit is the amount of damage you deal with a single hit, while hits per second is the number of hits you can make in one ...
The first line contains two integers \(n\) and \(k\) (\(1\leq n\leq200\,000\), \(2\leq k\leq200\,000\)) β€” the number of enemies and the number of skill points.The second line contains \(n\) integers \(h_i\) (\(1\leq h_i\leq10^{13}\)) β€” the health of the \(i\)th enemy.
Print two positive integers \(x\) and \(y\) (\(1\le x, y\) and \(x+y\le k\)) β€” the number of skill points you want to invest in damage per hit and hits per second. If there are multiple optimal solutions, print any of them.
In the first sample, there is only one monster and you have \(7\) skill points to distribute. If you make \(3\) damage per hit, you will need \(5\) hits to kill it. If you do \(4\) hits per second, you will need \(1.25\) seconds to beat the monster. There is no way to beat the monster faster than this.In the second sam...
Input: 1 714 | Output: 3 4
Master
2
913
268
223
19
245
A
245A
A. System Administrator
800
implementation
Polycarpus is a system administrator. There are two servers under his strict guidance β€” a and b. To stay informed about the servers' performance, Polycarpus executes commands ""ping a"" and ""ping b"". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a progra...
The first line contains a single integer n (2 ≀ n ≀ 1000) β€” the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers β€” the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≀ ti ≀ 2; xi, yi β‰₯ 0; xi + yi = 10). If ti =...
In the first line print string ""LIVE"" (without the quotes) if server a is ""alive"", otherwise print ""DEAD"" (without the quotes).In the second line print the state of server b in the similar format.
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this ...
Input: 21 5 52 6 4 | Output: LIVELIVE
Beginner
1
927
661
202
2
1,481
C
1481C
C. Fence Painting
1,600
brute force; constructive algorithms; greedy
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence β€” so plain and boring, that you'd like to repaint it. You have a fence consisting of \(n\) planks, where the \(i\)-th plank has the color \(a_i\). You want to repaint the fence in such a way that...
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 two integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) β€” the number of planks in the fence and the number of painters.The second line of each test case cont...
For each test case, output ""NO"" if it is impossible to achieve the coloring \(b\).Otherwise, print ""YES"" and \(m\) integers \(x_1, x_2, \dots, x_m\), where \(x_j\) is the index of plank the \(j\)-th painter should paint.You may print every letter in any case you want (so, for example, the strings ""yEs"", ""yes"", ...
Input: 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 | Output: YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO
Medium
3
760
818
379
14
1,700
D
1700D
D. River Locks
1,900
binary search; dp; greedy; math
Recently in Divanovo, a huge river locks system was built. There are now \(n\) locks, the \(i\)-th of them has the volume of \(v_i\) liters, so that it can contain any amount of water between \(0\) and \(v_i\) liters. Each lock has a pipe attached to it. When the pipe is open, \(1\) liter of water enters the lock every...
The first lines contains one integer \(n\) (\(1 \le n \le 200\,000\)) β€” the number of locks. The second lines contains \(n\) integers \(v_1, v_2, \dots, v_n\) (\(1 \le v_i \le 10^9\))) β€” volumes of the locks. The third line contains one integer \(q\) (\(1 \le q \le 200\,000\)) β€” the number of queries. Each of the next ...
Print \(q\) integers. The \(j\)-th of them should be equal to the minimum number of pipes to turn on so that after \(t_j\) seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print \(-1\).
There are \(6\) queries in the first example test. In the queries \(1, 3, 4\) the answer is \(-1\). We need to wait \(4\) seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks \(1\), \(3\), and \(4\). After \(4\) seconds the locks \(1\) and \(4\) are full. In the fo...
Input: 5 4 1 5 4 1 6 1 6 2 3 4 5 | Output: -1 3 -1 -1 4 3
Hard
4
1,384
460
233
17
876
B
876B
B. Divisiblity of Differences
1,300
implementation; math; number theory
You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in mult...
First line contains three integers n, k and m (2 ≀ k ≀ n ≀ 100 000, 1 ≀ m ≀ 100 000) β€” number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.Second line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109) β€” the numbers in the multiset.
If it is not possible to select k numbers in the desired way, output Β«NoΒ» (without the quotes).Otherwise, in the first line of output print Β«YesΒ» (without the quotes). In the second line print k integers b1, b2, ..., bk β€” the selected numbers. If there are multiple possible solutions, print any of them.
Input: 3 2 31 8 4 | Output: Yes1 4
Easy
3
418
311
304
8
1,045
F
1045F
F. Shady Lady
3,400
geometry; math
Ani and Borna are playing a short game on a two-variable polynomial. It's a special kind of a polynomial: the monomials are fixed, but all of its coefficients are fill-in-the-blanks dashes, e.g. $$$\( \_ xy + \_ x^4 y^7 + \_ x^8 y^3 + \ldots \)\(Borna will fill in the blanks with positive integers. He wants the polynom...
The first line contains a positive integer \(N\) \((2 \leq N \leq 200\, 000)\), denoting the number of the terms in the starting special polynomial.Each of the following \(N\) lines contains a description of a monomial: the \(k\)-th line contains two **space**-separated integers \(a_k\) and \(b_k\) \((0 \leq a_k, b_k \...
If Borna can always choose the coefficients such that the resulting polynomial is bounded from below, regardless of what monomial Ani steals, output ""Borna"". Else, output ""Ani"". You shouldn't output the quotation marks.
In the first sample, the initial polynomial is \(\_xy+ \_x^2 + \_y^2\). If Ani steals the \(\_y^2\) term, Borna is left with \(\_xy+\_x^2\). Whatever positive integers are written on the blanks, \(y \rightarrow -\infty\) and \(x := 1\) makes the whole expression go to negative infinity.In the second sample, the initial...
Input: 31 12 00 2 | Output: Ani
Master
2
822
488
223
10
220
D
220D
D. Little Elephant and Triangle
2,500
geometry; math
The Little Elephant is playing with the Cartesian coordinates' system. Most of all he likes playing with integer points. The Little Elephant defines an integer point as a pair of integers (x; y), such that 0 ≀ x ≀ w and 0 ≀ y ≀ h. Thus, the Little Elephant knows only (w + 1)Β·(h + 1) distinct integer points.The Little E...
A single line contains two integers w and h (1 ≀ w, h ≀ 4000).
In a single output line print an integer β€” the remainder of dividing the answer to the problem by 1000000007 (109 + 7).
Input: 2 1 | Output: 36
Expert
2
804
62
119
2
1,883
C
1883C
C. Raspberries
1,000
dp; math
You are given an array of integers \(a_1, a_2, \ldots, a_n\) and a number \(k\) (\(2 \leq k \leq 5\)). In one operation, you can do the following: Choose an index \(1 \leq i \leq n\), Set \(a_i = a_i + 1\).Find the minimum number of operations needed to make the product of all the numbers in the array \(a_1 \cdot a_2 \...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 10^5\), \(2 \leq k \leq 5\)) β€” the size of...
For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by \(k\).
In the first test case, we need to choose the index \(i = 2\) twice. After that, the array will be \(a = [7, 5]\). The product of all the numbers in the array is \(35\).In the fourth test case, the product of the numbers in the array is \(120\), which is already divisible by \(5\), so no operations are needed.In the ei...
Input: 152 57 33 37 4 15 29 7 7 3 95 55 4 1 2 37 49 5 1 5 9 5 13 46 3 63 46 1 53 41 5 94 41 4 1 13 43 5 34 58 9 9 32 51 62 510 104 51 6 1 12 57 7 | Output: 2 2 1 0 2 0 1 2 0 1 1 4 0 4 3
Beginner
2
363
559
138
18
1,792
D
1792D
D. Fixed Prefix Permutations
1,700
binary search; bitmasks; data structures; hashing; math; sortings
You are given \(n\) permutations \(a_1, a_2, \dots, a_n\), each of length \(m\). Recall that a permutation of length \(m\) is a sequence of \(m\) distinct integers from \(1\) to \(m\).Let the beauty of a permutation \(p_1, p_2, \dots, p_m\) be the largest \(k\) such that \(p_1 = 1, p_2 = 2, \dots, p_k = k\). If \(p_1 \...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n \le 5 \cdot 10^4\); \(1 \le m \le 10\)) β€” the number of permutations and the length of each permutation.The \(i\)-th of the next \(n\) lines co...
For each testcase, print \(n\) integers. The \(i\)-th value should be equal to the largest beauty of a permutation \(a_i \cdot a_j\) over all \(j\) (\(1 \le j \le n\)).
Input: 33 42 4 1 31 2 4 32 1 3 42 21 22 18 103 4 9 6 10 2 7 8 1 53 9 1 8 5 7 4 10 2 63 10 1 7 5 9 6 4 2 81 2 3 4 8 6 10 7 9 51 2 3 4 10 6 8 5 7 99 6 1 2 10 4 7 8 3 57 9 3 2 5 6 4 8 1 109 4 3 7 5 6 1 10 8 2 | Output: 1 4 4 2 2 10 8 1 6 8 10 1 7
Medium
6
604
463
168
17
1,796
E
1796E
E. Colored Subgraphs
2,500
dfs and similar; dp; games; greedy; trees
Monocarp has a tree, consisting of \(n\) vertices.He is going to select some vertex \(r\) and perform the following operations on each vertex \(v\) from \(1\) to \(n\): set \(d_v\) equal to the distance from \(v\) to \(r\) (the number of edges on the shortest path); color \(v\) some color. A nice coloring satisfies two...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of each testcase contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the number of vertices in the tree.Each of the next \(n-1\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le n\)) ...
For each testcase print a single integer β€” the maximum possible cost of the tree.
Input: 441 22 33 451 21 31 41 531 33 273 22 57 53 11 61 4 | Output: 4 1 3 3
Expert
5
793
446
81
17
300
C
300C
C. Beautiful Numbers
1,800
brute force; combinatorics
Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.For example, let's say that Vitaly's favourite digits are 1 ...
The first line contains three integers: a, b, n (1 ≀ a < b ≀ 9, 1 ≀ n ≀ 106).
Print a single integer β€” the answer to the problem modulo 1000000007 (109 + 7).
Input: 1 3 3 | Output: 1
Medium
2
722
77
79
3
1,070
F
1070F
F. Debate
1,500
greedy
Elections in Berland are coming. There are only two candidates β€” Alice and Bob.The main Berland TV channel plans to show political debates. There are \(n\) people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views:...
The first line contains integer \(n\) (\(1 \le n \le 4\cdot10^5\)) β€” the number of people who want to take part in the debate as a spectator.These people are described on the next \(n\) lines. Each line describes a single person and contains the string \(s_i\) and integer \(a_i\) separated by space (\(1 \le a_i \le 500...
Print a single integer β€” maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.
In the first example \(4\) spectators can be invited to maximize total influence: \(1\), \(2\), \(3\) and \(6\). Their political views are: ""11"", ""10"", ""01"" and ""00"". So in total \(2\) out of \(4\) spectators support Alice and \(2\) out of \(4\) spectators support Bob. The total influence is \(6+4+3+9=22\).In t...
Input: 611 610 401 300 300 700 9 | Output: 22
Medium
1
1,212
475
200
10
382
E
382E
E. Ksenia and Combinatorics
2,600
combinatorics; dp
Ksenia has her winter exams. Today she is learning combinatorics. Here's one of the problems she needs to learn to solve.How many distinct trees are there consisting of n vertices, each with the following properties: the tree is marked, that is, the vertices of the tree are numbered from 1 to n; each vertex of the tree...
The first line contains two integers n, k (1 ≀ n, k ≀ 50).
Print a single integer β€” the answer to the problem modulo 1000000007 (109 + 7).
If you aren't familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory).
Input: 1 1 | Output: 0
Expert
2
817
58
79
3
2,123
A
2123A
A. Blackboard Game
800
math
Initially, the integers from \(0\) to \(n-1\) are written on a blackboard.In one round, Alice chooses an integer \(a\) on the blackboard and erases it; then Bob chooses an integer \(b\) on the blackboard such that \(a+b \equiv 3 \pmod 4\)\(^{\text{βˆ—}}\) and erases it. Rounds take place in succession until a player is u...
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases.The only line of each test case contains an integer \(n\) (\(1\leq n \leq 100\)) β€” the number of integers written on the blackboard.
For each test case, output on a single line ""Alice"" if Alice wins with optimal play, and ""Bob"" if Bob wins with optimal play.You can output the answer in any case (upper or lower). For example, the strings ""aLiCe"", ""alice"", ""ALICE"", and ""alICE"" will be recognized as ""Alice"".
In the first sample, suppose Alice chooses \(0\), then Bob cannot choose any number so Alice wins immediately.In the second sample, suppose Alice chooses \(0\), then Bob can choose \(3\). Then suppose Alice chooses \(2\), then Bob can choose \(1\). Then Alice has no numbers remaining, so Bob wins.
Input: 52457100 | Output: Alice Bob Alice Alice Bob
Beginner
1
534
224
289
21
1,423
J
1423J
J. Bubble Cup hypothesis
2,400
bitmasks; constructive algorithms; dp; math
The Bubble Cup hypothesis stood unsolved for \(130\) years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:Given a number \(m\), how many polynomials \(P\) with coefficients in set \({\{...
The first line contains a single integer \(t\) \((1 \leq t \leq 5\cdot 10^5)\) - number of test cases.On next line there are \(t\) numbers, \(m_i\) \((1 \leq m_i \leq 10^{18})\) - meaning that in case \(i\) you should solve for number \(m_i\).
For each test case \(i\), print the answer on separate lines: number of polynomials \(P\) as described in statement such that \(P(2)=m_i\), modulo \(10^9 + 7\).
In first case, for \(m=2\), polynomials that satisfy the constraint are \(x\) and \(2\).In second case, for \(m=4\), polynomials that satisfy the constraint are \(x^2\), \(x + 2\), \(2x\) and \(4\).
Input: 2 2 4 | Output: 2 4
Expert
4
405
243
160
14
1,607
A
1607A
A. Linear Keyboard
800
implementation; strings
You are given a keyboard that consists of \(26\) keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word \(s\) on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its let...
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The next \(2t\) lines contain descriptions of the test cases.The first line of a description contains a keyboard β€” a string of length \(26\), which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' a...
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word \(s\) on the given keyboard.
Input: 5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba | Output: 13 0 68 0 74
Beginner
2
1,115
517
185
16
1,252
G
1252G
G. Performance Review
2,100
data structures
Randall is a software engineer at a company with \(N\) employees. Every year, the company re-evaluates its employees. At the end of every year, the company replaces its several worst-performing employees and replaces with the same number of new employees, so that the company keeps having \(N\) employees. Each person ha...
Input begins with a line containing three integers: \(N\) \(M\) \(Q\) (\(2 \le N \le 100\,000\); \(1 \le M, Q \le 100\,000\)) representing the number of employees, the number of years to be considered, and the number of scenarios, respectively. The next line contains \(N\) integers: \(A_i\) (\(0 \le A_i \le 10^9\)) rep...
For each scenario in the same order as input, output in a line an integer \(0\) if Randall will not be in the company after \(M\) years, or \(1\) if Randall will still be in the company after \(M\) years.
Explanation for the sample input/output #1Randall performance is represented by \(50\). For the first scenario, the value of \((B_1)_3\) is updated to \(300\), causes the following: Initially, the performance of the employees is \([50, 40, 30, 20, 10]\). At the end of the first year, \(4\) worst-performing employees ar...
Input: 5 3 3 50 40 30 20 10 4 1 2 3 100 1 4 2 6 7 1 3 300 2 1 400 2 1 5 | Output: 1 0 1
Hard
1
1,370
988
204
12
463
B
463B
B. Caisa and Pylons
1,100
brute force; implementation; math
Caisa solved the problem with the sugar and now he is on the way back to home. Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, ...
The first line contains integer n (1 ≀ n ≀ 105). The next line contains n integers h1, h2, ..., hn (1 ≀ hi ≀ 105) representing the heights of the pylons.
Print a single number representing the minimum number of dollars paid by Caisa.
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
Input: 53 4 3 2 4 | Output: 4
Easy
3
1,009
153
79
4
478
B
478B
B. Random Teams
1,300
combinatorics; constructive algorithms; greedy; math
n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have...
The only line of input contains two integers n and m, separated by a single space (1 ≀ m ≀ n ≀ 109) β€” the number of participants and the number of teams respectively.
The only line of the output should contain two integers kmin and kmax β€” the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.
In the first sample all the participants get into one team, so there will be exactly ten pairs of friends.In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.In t...
Input: 5 1 | Output: 10 10
Easy
4
358
166
185
4
1,264
C
1264C
C. Beautiful Mirrors with queries
2,400
data structures; probabilities
Creatnx has \(n\) mirrors, numbered from \(1\) to \(n\). Every day, Creatnx asks exactly one mirror ""Am I beautiful?"". The \(i\)-th mirror will tell Creatnx that he is beautiful with probability \(\frac{p_i}{100}\) for all \(1 \le i \le n\).Some mirrors are called checkpoints. Initially, only the \(1\)st mirror is a ...
The first line contains two integers \(n\), \(q\) (\(2 \leq n, q \le 2 \cdot 10^5\)) β€” the number of mirrors and queries.The second line contains \(n\) integers: \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq 100\)).Each of \(q\) following lines contains a single integer \(u\) (\(2 \leq u \leq n\)) β€” next query.
Print \(q\) numbers – the answers after each query by modulo \(998244353\).
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy...
Input: 2 2 50 50 2 2 | Output: 4 6
Expert
2
1,690
311
75
12
665
B
665B
B. Shopping
1,400
brute force
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect"" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order.Due to the sp...
The first line contains three integers n, m and k (1 ≀ n, k ≀ 100, 1 ≀ m ≀ k) β€” the number of users, the number of items each user wants to buy and the total number of items at the market.The next line contains k distinct integers pl (1 ≀ pl ≀ k) denoting the initial positions of the items in the store. The items are n...
Print the only integer t β€” the total time needed for Ayush to process all the orders.
Customer 1 wants the items 1 and 5.pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].Time taken for the first customer is 3 + 5 = 8.Customer 2 wants the items 3 and 1.pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].pos(1) = 3, so the new positions are: [1,...
Input: 2 2 53 4 1 2 51 53 1 | Output: 14
Easy
1
941
457
85
6
64
D
64D
D. Presents
1,800
*special; greedy
You are given the prices of three presents. Also there are three sisters. It is known that the most valuable present is for the eldest sister. The second (by price) is for the second sister. And the less valuable present is for the youngest sister. If two (or three) presents have the same price, corresponding sisters m...
The only line contains three integer numbers a1, a2, a3 (1 ≀ a1, a2, a3 ≀ 100) β€” the prices of the presents.
Print three numbers i1, i2, i3 (1 ≀ i1, i2, i3 ≀ 3), all of them should be distinct. The first number stands for the seniority of the sister which will get the first present (1 stands for the eldest, 3 for the youngest). The second and third numbers mean the seniority of the sisters which get the second and the third p...
In the second sample another possible answer is ""2 3 1"".
Input: 11 13 1 | Output: 2 1 3
Medium
2
347
108
389
0
1,468
J
1468J
J. Road Reform
1,800
dsu; graphs; greedy
There are \(n\) cities and \(m\) bidirectional roads in Berland. The \(i\)-th road connects the cities \(x_i\) and \(y_i\), and has the speed limit \(s_i\). The road network allows everyone to get from any city to any other city. The Berland Transport Ministry is planning a road reform.First of all, maintaining all \(m...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2})\); \(1 \le k \le 10^9\)) β€” the number of cities, the number of...
For each test case, print one integer β€” the minimum number of changes the Ministry has to perform so that the maximum speed limit among the remaining \((n - 1)\) roads is exactly \(k\).
The explanation for the example test:The first test case is described in the problem statement.In the second test case, the road network initially looks like that: The Ministry can demolish the roads \(1\)–\(2\), \(3\)–\(2\) and \(3\)–\(4\), and then increase the speed limit on the road \(1\)–\(4\) three times.In the t...
Input: 44 5 74 1 31 2 52 3 82 4 13 4 44 6 51 2 11 3 11 4 22 4 14 3 13 2 13 2 101 2 81 3 105 5 151 2 173 1 152 3 101 4 142 5 8 | Output: 1 3 0 0
Medium
3
1,451
1,005
185
14
847
C
847C
C. Sum of Nestings
1,800
constructive algorithms
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence ""(()())"" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': ""((1+1)+(1+1))"". Also the ...
The first line contains two integers n and k (1 ≀ n ≀ 3Β·105, 0 ≀ k ≀ 1018) β€” the number of opening brackets and needed total nesting.
Print the required regular bracket sequence consisting of round brackets.If there is no solution print ""Impossible"" (without quotes).
The first example is examined in the statement.In the second example the answer is ""(((())))"". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6.In the third it is impossibl...
Input: 3 1 | Output: ()(())
Medium
1
1,066
133
135
8