contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,029
D
1029D
D. Concatenated Multiples
1,900
implementation; math
You are given an array \(a\), consisting of \(n\) positive integers.Let's call a concatenation of numbers \(x\) and \(y\) the number that is obtained by writing down numbers \(x\) and \(y\) one right after another without changing the order. For example, a concatenation of numbers \(12\) and \(3456\) is a number \(1234...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(2 \le k \le 10^9\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).
Print a single integer β€” the number of ordered pairs of positions \((i, j)\) (\(i \neq j\)) in array \(a\) such that the concatenation of \(a_i\) and \(a_j\) is divisible by \(k\).
In the first example pairs \((1, 2)\), \((1, 3)\), \((2, 3)\), \((3, 1)\), \((3, 4)\), \((4, 2)\), \((4, 3)\) suffice. They produce numbers \(451\), \(4510\), \(110\), \(1045\), \(1012\), \(121\), \(1210\), respectively, each of them is divisible by \(11\).In the second example all \(n(n - 1)\) pairs suffice.In the thi...
Input: 6 1145 1 10 12 11 7 | Output: 7
Hard
2
486
196
180
10
133
A
133A
A. HQ9+
900
implementation
HQ9+ is a joke programming language which has only four one-character instructions: ""H"" prints ""Hello, World!"", ""Q"" prints the source code of the program itself, ""9"" prints the lyrics of ""99 Bottles of Beer"" song, ""+"" increments the value stored in the internal accumulator.Instructions ""H"" and ""Q"" are c...
The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.
Output ""YES"", if executing the program will produce any output, and ""NO"" otherwise.
In the first case the program contains only one instruction β€” ""H"", which prints ""Hello, World!"".In the second case none of the program characters are language instructions.
Input: Hi! | Output: YES
Beginner
1
545
239
87
1
580
B
580B
B. Kefa and Company
1,500
binary search; sortings; two pointers
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to ...
The first line of the input contains two space-separated integers, n and d (1 ≀ n ≀ 105, ) β€” the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of th...
Print the maximum total friendship factir that can be reached.
In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.In the second sample test we can take all the friends.
Input: 4 575 50 100150 2075 1 | Output: 100
Medium
3
626
430
62
5
1,404
D
1404D
D. Game of Pairs
2,800
constructive algorithms; dfs and similar; interactive; math; number theory
This is an interactive problem.Consider a fixed positive integer \(n\). Two players, First and Second play a game as follows: First considers the \(2n\) numbers \(1, 2, \dots, 2n\), and partitions them as he wants into \(n\) disjoint pairs. Then, Second chooses exactly one element from each of the pairs that First crea...
In the first sample, \(n = 2\), and you decide to play as Second. The judge chooses the pairs \((1, 2)\) and \((3, 4)\), and you reply with the numbers \(1\) and \(3\). This is a valid choice since it contains exactly one number from each pair, and the sum \(1 + 3 = 4\) is divisible by \(4\).In the second sample, \(n =...
Input: 2 1 1 2 2 0 | Output: Second 1 3
Master
5
648
0
0
14
1,256
C
1256C
C. Platforms Jumping
1,700
greedy
There is a river of width \(n\). The left bank of the river is cell \(0\) and the right bank is cell \(n + 1\) (more formally, the river can be represented as a sequence of \(n + 2\) cells numbered from \(0\) to \(n + 1\)). There are also \(m\) wooden platforms on a river, the \(i\)-th platform has length \(c_i\) (so t...
The first line of the input contains three integers \(n\), \(m\) and \(d\) (\(1 \le n, m, d \le 1000, m \le n\)) β€” the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.The second line of the input contains \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_i \le n, \sum...
If it is impossible to reach \(n+1\) from \(0\), print NO in the first line. Otherwise, print YES in the first line and the array \(a\) of length \(n\) in the second line β€” the sequence of river cells (excluding cell \(0\) and cell \(n + 1\)).If the cell \(i\) does not belong to any platform, \(a_i\) should be \(0\). O...
Consider the first example: the answer is \([0, 1, 0, 2, 2, 0, 3]\). The sequence of jumps you perform is \(0 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 7 \rightarrow 8\).Consider the second example: it does not matter how to place the platform because you always can jump from \(0\) to \(11\).Consider the th...
Input: 7 3 2 1 2 1 | Output: YES 0 1 0 2 2 0 3
Medium
1
1,515
405
1,145
12
1,579
B
1579B
B. Shifting Sort
1,100
implementation; sortings
The new generation external memory contains an array of integers \(a[1 \ldots n] = [a_1, a_2, \ldots, a_n]\).This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back in...
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The next \(2t\) lines contain the descriptions of the test cases. The first line of each test case description contains an integer \(n\) (\(2 \leq n \leq 50\)) β€” the length of the array. The second line consists of space-separa...
Print \(t\) answers to all input test cases. The first line of the answer of each test case should contain an integer \(k\) (\(0 \le k \le n\)) β€” the number of actions to sort the array. The next \(k\) lines should contain descriptions of the actions formatted as ""\(l\) \(r\) \(d\)"" (without quotes) where \(l\) and \...
Explanation of the fourth data set in the example: The segment \(a[2 \ldots 4]\) is selected and is shifted to the left by \(2\): \([2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3]\) The segment \(a[1 \ldots 5]\) is then selected and is shifted to the left by \(3\): \([\color{blue}{2, 4, 5, 1...
Input: 4 2 2 1 3 1 2 1 4 2 4 1 3 5 2 5 1 4 3 | Output: 1 1 2 1 1 1 3 2 3 2 4 1 2 3 1 1 3 2 4 2 4 2 1 5 3 1 2 1 1 3 1
Easy
2
1,540
449
972
15
10
C
10C
C. Digital Root
2,000
number theory
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equation AB = C is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root d(x) of the number x ...
The first line contains the only number N (1 ≀ N ≀ 106).
Output one number β€” the amount of required A, B and C from the range [1, N].
For the first sample the required triples are (3, 4, 3) and (4, 3, 3).
Input: 4 | Output: 2
Hard
1
1,016
56
76
0
3
D
3D
D. Least Cost Bracket Sequence
2,600
greedy
This is yet another problem on regular bracket sequences.A bracket sequence is called regular, if by inserting ""+"" and ""1"" into it we get a correct mathematical expression. For example, sequences ""(())()"", ""()"" and ""(()(()))"" are regular, while "")("", ""(()"" and ""(()))("" are not. You have a pattern of a b...
The first line contains a non-empty pattern of even length, consisting of characters ""("", "")"" and ""?"". Its length doesn't exceed 5Β·104. Then there follow m lines, where m is the number of characters ""?"" in the pattern. Each line contains two integer numbers ai and bi (1 ≀ ai, bi ≀ 106), where ai is the cost of ...
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.Print -1, if there is no answer. If the answer is not unique, print any of them.
Input: (??)1 22 8 | Output: 4()()
Expert
1
636
408
194
0
983
B
983B
B. XOR-pyramid
1,800
dp
For an array \(b\) of length \(m\) we define the function \(f\) as \( f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{cases} \) where \(\oplus\) is bitwise exclusive OR.For example, \(f(1,2,4,8)=f(1\oplus2,2\oplus4,4\oplu...
The first line contains a single integer \(n\) (\(1 \le n \le 5000\)) β€” the length of \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2^{30}-1\)) β€” the elements of the array.The third line contains a single integer \(q\) (\(1 \le q \le 100\,000\)) β€” the number of queries.Each of ...
Print \(q\) lines β€” the answers for the queries.
In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.In second sample, optimal segment for first query are \([3,6]\), for second query β€” \([2,5]\), for third β€” \([3,4]\), for fourth β€” \([1,2]\).
Input: 38 4 122 31 2 | Output: 512
Medium
1
608
425
48
9
628
F
628F
F. Bear and Fair Set
2,500
flows; graphs
Limak is a grizzly bear. He is big and dreadful. You were chilling in the forest when you suddenly met him. It's very unfortunate for you. He will eat all your cookies unless you can demonstrate your mathematical skills. To test you, Limak is going to give you a puzzle to solve.It's a well-known fact that Limak, as eve...
The first line contains three integers n, b and q (5 ≀ n ≀ b ≀ 104, 1 ≀ q ≀ 104, n divisible by 5) β€” the size of the set, the upper limit for numbers in the set and the number of hints.The next q lines describe the hints. The i-th of them contains two integers upToi and quantityi (1 ≀ upToi ≀ b, 0 ≀ quantityi ≀ n).
Print ''fair"" if there exists at least one set that has all the required properties and matches all the given hints. Otherwise, print ''unfair"".
In the first example there is only one set satisfying all conditions: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.In the second example also there is only one set satisfying all conditions: {6, 7, 8, 9, 10, 11, 12, 13, 14, 15}.Easy to see that there is no set satisfying all conditions from the third example. So Limak lied to you :...
Input: 10 20 110 10 | Output: fair
Expert
2
1,468
316
146
6
741
E
741E
E. Arpa’s abnormal DNA and Mehrdad’s deep interest
3,400
data structures; string suffix structures
All of us know that girls in Arpa’s land are... ok, you’ve got the idea :DAnyone knows that Arpa isn't a normal man, he is ... well, sorry, I can't explain it more. Mehrdad is interested about the reason, so he asked Sipa, one of the best biology scientists in Arpa's land, for help. Sipa has a DNA editor. Sipa put Arpa...
The first line contains strings S, T and integer q (1 ≀ |S|, |T|, q ≀ 105) β€” Arpa's DNA, the DNA of a normal man, and the number of Mehrdad's questions. The strings S and T consist only of small English letters.Next q lines describe the Mehrdad's questions. Each of these lines contain five integers l, r, k, x, y (0 ≀ l...
Print q integers. The j-th of them should be the number i of the most interesting option among those that satisfy the conditions of the j-th question. If there is no option i satisfying the conditions in some question, print -1.
Explanation of first sample case:In the first question Sipa has two options: dabc (i = 0) and abdc (i = 2). The latter (abcd) is better than abdc, so answer is 2.In the last question there is no i such that 0 ≀ i ≀ 1 and .
Input: abc d 40 3 2 0 00 3 1 0 01 2 1 0 00 1 3 2 2 | Output: 2 3 2 -1
Master
2
1,252
356
228
7
1,815
F
1815F
F. OH NO1 (-2-3-4)
3,500
constructive algorithms; graphs; math
You are given an undirected graph with \(n\) vertices and \(3m\) edges. The graph may contain multi-edges, but does not contain self-loops. The graph satisfies the following property: the given edges can be divided into \(m\) groups of \(3\), such that each group is a triangle.A triangle is defined as three edges \((a,...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 10^6\), \(1 \le m \le 4 \cdot 10^5\)) β€” denoting the graph have \(n\) vertices and \(3m\) edges....
For each test case, output \(m\) lines of \(3\) integers each.The \(i\)-th line should contains three integers \(e_{ab},e_{bc},e_{ca}\) (\(1 \leq e_{ab}, e_{bc} , e_{ca} \leq 4\)), denoting the choice of value \(x\) for edges \((a_i, b_i)\), \((b_i,c_i)\) and \((c_i, a_i)\) respectively.
In the first test case, the initial weights are \([0,0,0,0]\). We have added values as follows: Added \(2\) to vertices \(1\) and \(2\) Added \(1\) to vertices \(2\) and \(3\) Added \(3\) to vertices \(3\) and \(1\) The final weights are \([5,3,4,0]\). The output is valid because \(a_1 \neq a_2\), \(a_1 \neq a_3\), \(a...
Input: 44 10 0 0 01 2 35 20 0 0 0 01 2 31 4 54 43 4 5 61 2 31 2 41 3 42 3 45 40 1000000 412 412 4121 2 31 4 52 4 53 4 5 | Output: 2 1 3 2 3 3 4 3 3 3 1 2 2 2 3 2 3 4 3 1 1 2 3 4 1 2 4 4 4 3 4 1 1
Master
3
1,091
923
288
18
1,545
A
1545A
A. AquaMoon and Strange Sort
1,500
sortings
AquaMoon has \(n\) friends. They stand in a row from left to right, and the \(i\)-th friend from the left wears a T-shirt with a number \(a_i\) written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operat...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 50\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the number of Aquamoon's friends.The second line contains \(n\) integers \(a_1, a_2, \do...
For each test case, if there exists a possible sequence of operations, print ""YES"" (without quotes); otherwise, print ""NO"" (without quotes).You can print each letter in any case (upper or lower).
The possible list of operations in the first test case: Swap \(a_1\) and \(a_2\). The resulting sequence is \(3, 4, 2, 5\). The directions are: left, left, right, right. Swap \(a_2\) and \(a_3\). The resulting sequence is \(3, 2, 4, 5\). The directions are: left, left, right, right. Swap \(a_1\) and \(a_2\). The result...
Input: 3 4 4 3 2 5 4 3 3 2 2 5 1 2 3 5 4 | Output: YES YES NO
Medium
1
763
479
199
15
1,619
H
1619H
H. Permutation and Queries
2,400
brute force; data structures; divide and conquer; two pointers
You are given a permutation \(p\) of \(n\) elements. A permutation of \(n\) elements is an array of length \(n\) containing each integer from \(1\) to \(n\) exactly once. For example, \([1, 2, 3]\) and \([4, 3, 5, 1, 2]\) are permutations, but \([1, 2, 4]\) and \([4, 3, 2, 1, 2]\) are not permutations. You should perfo...
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)).The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\).Each of the next \(q\) lines contains three integers. The first integer is \(t\) (\(1 \le t \le 2\)) β€” type of query. If \(t = 1\), then the next two integers are \(x\) and \(y...
For every second-type query, print one integer in a new line β€” answer to this query.
In the first example \(p = \{5, 3, 4, 2, 1\}\). The first query is to print \(p_3\). The answer is \(4\).The second query is to print \(p_{p_1}\). The answer is \(1\).The third query is to swap \(p_1\) and \(p_3\). Now \(p = \{4, 3, 5, 2, 1\}\).The fourth query is to print \(p_{p_1}\). The answer is \(2\).
Input: 5 4 5 3 4 2 1 2 3 1 2 1 2 1 1 3 2 1 2 | Output: 4 1 2
Expert
4
512
545
84
16
1,909
C
1909C
C. Heavy Intervals
1,400
constructive algorithms; data structures; dsu; greedy; math; sortings
Shiki - Pure Rubyβ €You have \(n\) intervals \([l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]\), such that \(l_i < r_i\) for each \(i\), and all the endpoints of the intervals are distinct.The \(i\)-th interval has weight \(c_i\) per unit length. Therefore, the weight of the \(i\)-th interval is \(c_i \cdot (r_i - l_i)\).You ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of intervals.The second line of each test case contains \(...
For each test case, output a single integer: the minimum possible sum of weights of the intervals after your operations.
In the first test case, you can make \(l = [8, 3]\); \(r = [23, 12]\); \(c = [100, 100]\). In that case, there are two intervals: interval \([8, 23]\) with weight \(100\) per unit length, and \(100 \cdot (23-8) = 1500\) in total; interval \([3, 12]\) with weight \(100\) per unit length, and \(100 \cdot (12-3) = 900\) i...
Input: 228 312 23100 100420 1 2 530 4 3 102 3 2 3 | Output: 2400 42
Easy
6
880
939
120
19
1,297
H
1297H
H. Paint the String
0
*special; dp; strings
You are given a string \(s\) of lowercase Latin letters. It is required to paint each letter of the string in one of two colors (red or blue) so that if you write all the red letters from left to right and write all the blue letters from left to right, then the lexicographically maximum of the two written strings is le...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases in the test. Next, the test cases are given, one per line.Each test case is a non-empty string \(s\) of length between \(2\) to \(100\) characters, inclusive, which consists of lowercase Latin letters.
Print \(t\) lines, the \(i\)-th of them should contain the answer to the \(i\)-th test case of the input. Print a string of length \(n\), where \(n\) is the length of the given string \(s\): the \(j\)-th character of the string should be either 'R'or 'B' depending on the color of the \(j\)-th character in the answer (p...
Input: 5 kotlin codeforces abacaba ffccgc yz | Output: RRRRBB RRRRRRRBBB RRRRBBB RBBBBR RR
Beginner
3
789
291
401
12
734
A
734A
A. Anton and Danik
800
implementation; strings
Anton likes to play chess, and so does his friend Danik.Once they have played n games in a row. For each game it's known who was the winner β€” Anton or Danik. None of the games ended with a tie.Now Anton wonders, who won more games, he or Danik? Help him determine this.
The first line of the input contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of games played.The second line contains a string s, consisting of n uppercase English letters 'A' and 'D' β€” the outcome of each of the games. The i-th character of the string is equal to 'A' if the Anton won the i-th game and 'D' if...
If Anton won more games than Danik, print ""Anton"" (without quotes) in the only line of the output.If Danik won more games than Anton, print ""Danik"" (without quotes) in the only line of the output.If Anton and Danik won the same number of games, print ""Friendship"" (without quotes).
In the first sample, Anton won 6 games, while Danik β€” only 1. Hence, the answer is ""Anton"".In the second sample, Anton won 3 games and Danik won 4 games, so the answer is ""Danik"".In the third sample, both Anton and Danik won 3 games and the answer is ""Friendship"".
Input: 6ADAAAA | Output: Anton
Beginner
2
269
345
287
7
811
E
811E
E. Vladik and Entertaining Flags
2,600
data structures; dsu; graphs
In his spare time Vladik estimates beauty of the flags.Every flag could be represented as the matrix n Γ— m which consists of positive integers.Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there e...
First line contains three space-separated integers n, m, q (1 ≀ n ≀ 10, 1 ≀ m, q ≀ 105) β€” dimensions of flag matrix and number of segments respectively.Each of next n lines contains m space-separated integers β€” description of flag matrix. All elements of flag matrix is positive integers not exceeding 106.Each of next q...
For each segment print the result on the corresponding line.
Partitioning on components for every segment from first test case:
Input: 4 5 41 1 1 1 11 2 2 3 31 1 1 2 54 4 5 5 51 52 51 24 5 | Output: 6734
Expert
3
804
441
60
8
1,666
I
1666I
I. Interactive Treasure Hunt
2,200
brute force; constructive algorithms; geometry; interactive; math
This is an interactive problem.There is a grid of \(n\times m\) cells. Two treasure chests are buried in two different cells of the grid. Your task is to find both of them. You can make two types of operations: DIG \(r\) \(c\): try to find the treasure in the cell \((r, c)\). The interactor will tell you if you found t...
Input: 1 2 3 1 1 3 0 1 | Output: SCAN 1 2 DIG 1 2 SCAN 2 2 DIG 1 1 DIG 1 3
Hard
5
865
0
0
16
2,114
B
2114B
B. Not Quite a Palindromic String
900
greedy; math
Vlad found a binary string\(^{\text{βˆ—}}\) \(s\) of even length \(n\). He considers a pair of indices (\(i, n - i + 1\)), where \(1 \le i < n - i + 1\), to be good if \(s_i = s_{n - i + 1}\) holds true.For example, in the string '010001' there is only \(1\) good pair, since \(s_1 \ne s_6\), \(s_2 \ne s_5\), and \(s_3=s_...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(0 \le k \le \frac{n}{2}\), \(n\) is even) β€” the length of the string and the required number of good pairs.The second line of...
For each test case, output ""YES"" if there is a way to rearrange the characters of the string so that there are exactly \(k\) good pairs, otherwise output ""NO"".You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positi...
Input: 66 20000002 1014 1101110 2110101100110 111010110012 111 | Output: NO NO YES NO YES YES
Beginner
2
771
477
330
21
311
C
311C
C. Fetch the Treasure
2,500
brute force; data structures; graphs; shortest paths
Rainbow built h cells in a row that are numbered from 1 to h from left to right. There are n cells with treasure. We call each of these n cells ""Treasure Cell"". The i-th ""Treasure Cell"" is the ai-th cell and the value of treasure in it is ci dollars.Then, Freda went in the first cell. For now, she can go just k cel...
The first line of the input contains four integers: h (1 ≀ h ≀ 1018), n, m (1 ≀ n, m ≀ 105) and k (1 ≀ k ≀ 104).Each of the next n lines contains two integers: ai (1 ≀ ai ≀ h), ci (1 ≀ ci ≀ 109). That means the i-th ""Treasure Cell"" is the ai-th cell and cost of the treasure in that cell is ci dollars. All the ai are ...
For each operation of type 3, output an integer indicates the value (in dollars) of the most valuable treasure among the ""Treasure Cells"" Freda can reach. If there is no such treasure, output 0.
In the sample, there are 10 cells and 3 ""Treasure Cells"". The first ""Treasure Cell"" is cell 5, having 50 dollars tresure in it. The second ""Treasure Cell"" is cell 7, having 60 dollars tresure in it. The third ""Treasure Cell"" is cell 8, having 100 dollars tresure in it.At first, Freda can only reach cell 1, 3, 5...
Input: 10 3 5 25 507 608 1002 2 531 333 | Output: 5510050
Expert
4
1,518
896
196
3
1,661
D
1661D
D. Progressions Covering
1,900
data structures; greedy
You are given two arrays: an array \(a\) consisting of \(n\) zeros and an array \(b\) consisting of \(n\) integers.You can apply the following operation to the array \(a\) an arbitrary number of times: choose some subsegment of \(a\) of length \(k\) and add the arithmetic progression \(1, 2, \ldots, k\) to this subsegm...
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 3 \cdot 10^5\)) β€” the number of elements in both arrays and the length of the subsegment, respectively.The second line of the input contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10^{12}\)), where \(b_i\) is the \...
Print one integer β€” the minimum possible number of operations required to satisfy the condition \(a_i \ge b_i\) for each \(i\) from \(1\) to \(n\).
Consider the first example. In this test, we don't really have any choice, so we need to add at least five progressions to make the first element equals \(5\). The array \(a\) becomes \([5, 10, 15]\).Consider the second example. In this test, let's add one progression on the segment \([1; 3]\) and two progressions on t...
Input: 3 3 5 4 6 | Output: 5
Hard
2
1,104
355
147
16
75
E
75E
E. Ship's Shortest Path
2,400
geometry; shortest paths
You have got a new job, and it's very interesting, you are a ship captain. Your first task is to move your ship from one point to another point, and for sure you want to move it at the minimum cost.And it's well known that the shortest distance between any 2 points is the length of the line segment between these 2 poin...
The first line contains 4 integers, xStart, yStart, xEnd and yEnd ( - 100 ≀ xStart, yStart, xEnd, yEnd ≀ 100). The second line contains an integer n, which is the number of points in the polygon (3 ≀ n ≀ 30), followed by a line containing n pairs of integers x and y, which are the coordinates of the points ( - 100 ≀ x,...
Print one line which contains the minimum possible cost. The absolute or relative error in the answer should not exceed 10 - 6.
Input: 1 7 6 744 2 4 12 3 12 3 2 | Output: 6.000000000
Expert
2
1,639
367
127
0
82
A
82A
A. Double Cola
1,100
implementation; math
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a ""Double Cola"" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can,...
The input data consist of a single integer n (1 ≀ n ≀ 109).It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Print the single line β€” the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: ""Sheldon"", ""Leonard"", ""Penny"", ""Rajesh"", ""Howard"" (without the quotes). In that order precisely the friends are in the queue initially.
Input: 1 | Output: Sheldon
Easy
2
782
193
319
0
56
E
56E
E. Domino Principle
2,200
binary search; data structures; sortings
Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has ...
The first line contains integer n (1 ≀ n ≀ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≀ xi ≀ 108, 2 ≀ hi ≀ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Print n space-separated numbers zi β€” the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).
Input: 416 520 510 1018 2 | Output: 3 1 4 1
Hard
3
697
263
150
0
245
G
245G
G. Suggested Friends
2,200
brute force; graphs
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion. Let's say that all friendship relationships in a social network are given as m username pairs ai, ...
The first line contains a single integer m (1 ≀ m ≀ 5000) β€” the number of pairs of friends in the social network. Next m lines contain pairs of names of the users who are friends with each other. The i-th line contains two space-separated names ai and bi (ai β‰  bi). The users' names are non-empty and consist of at most ...
In the first line print a single integer n β€” the number of network users. In next n lines print the number of suggested friends for each user. In the i-th line print the name of the user ci and the number of his suggested friends di after a space. You can print information about the users in any order.
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
Input: 5Mike GeraldKate MikeKate TankGerald TankGerald David | Output: 5Mike 1Gerald 1Kate 1Tank 1David 2
Hard
2
911
716
303
2
245
F
245F
F. Log Stream Analysis
2,000
binary search; brute force; implementation; strings
You've got a list of program warning logs. Each record of a log stream is a string in this format: ""2012-MM-DD HH:MM:SS:MESSAGE"" (without the quotes). String ""MESSAGE"" consists of spaces, uppercase and lowercase English letters and characters ""!"", ""."", "","", ""?"". String ""2012-MM-DD"" determines a correct da...
The first line of the input contains two space-separated integers n and m (1 ≀ n, m ≀ 10000).The second and the remaining lines of the input represent the log stream. The second line of the input contains the first record of the log stream, the third line contains the second record and so on. Each record of the log str...
If there is no sought moment of time, print -1. Otherwise print a string in the format ""2012-MM-DD HH:MM:SS"" (without the quotes) β€” the first moment of time when the number of warnings for the last n seconds got no less than m.
Input: 60 32012-03-16 16:15:25: Disk size is2012-03-16 16:15:25: Network failute2012-03-16 16:16:29: Cant write varlog2012-03-16 16:16:42: Unable to start process2012-03-16 16:16:43: Disk size is too small2012-03-16 16:16:53: Timeout detected | Output: 2012-03-16 16:16:43
Hard
4
693
858
229
2
1,874
G
1874G
G. Jellyfish and Inscryption
3,500
dp
Jellyfish loves playing a game called ""Inscryption"" which is played on a directed acyclic graph with \(n\) vertices and \(m\) edges. All edges \(a \to b\) satisfy \(a < b\).You need to move from vertex \(1\) to vertex \(n\) along the directed edges, and then fight with the final boss.You will collect cards and props ...
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 200\), \(n - 1\leq m \leq \min(\frac{n(n-1)}2, 2000)\)) β€” the number of the vertices and the number of the edges.In the following \(n\) lines, the \(i\)-th \((1 \leq i \leq n)\) line describes the special event that will trigger on vertex \(i\): 0 β€” ...
Output a single integer β€” the maximum sum of the power of all your cards and props.
In the first example, we will play the game in the following order: move from vertex \(1\) to vertex \(2\), get a card with \(2\) HP, and \(10\) damage. move from vertex \(2\) to vertex \(4\), choose the card we get from vertex \(2\) and increase its HP by \(8\). move from vertex \(4\) to vertex \(6\), choose the card ...
Input: 6 8 0 1 2 10 1 6 6 2 8 3 10 0 1 2 1 3 2 4 2 5 3 4 3 5 4 6 5 6 | Output: 100000000000
Master
1
1,214
1,170
83
18
1,797
B
1797B
B. Li Hua and Pattern
1,100
constructive algorithms; greedy
Li Hua has a pattern of size \(n\times n\), each cell is either blue or red. He can perform exactly \(k\) operations. In each operation, he chooses a cell and changes its color from red to blue or from blue to red. Each cell can be chosen as many times as he wants. Is it possible to make the pattern, that matches its r...
The first line contains the single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.The first line of each test case contains two integers \(n,k\) (\(1\le n\le 10^3,0\le k \le 10^9\)) β€” the size of the pattern and the number of operations.Each of next \(n\) lines contains \(n\) integers \(a_{i,j}\) (\(a_{...
For each set of input, print ""YES"" if it's possible to make the pattern, that matches its rotation by \(180^{\circ}\) after applying exactly \(k\) of operations, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recogni...
In test case 1, you can't perform any operation. The pattern after rotation is on the right. In test case 2, you can perform operations on \((2,1),(3,2),(3,4)\). The pattern after operations is in the middle and the pattern after rotation is on the right.
Input: 34 01 1 1 10 0 0 11 0 1 01 1 1 14 31 0 1 11 0 0 00 1 0 11 1 0 15 40 0 0 0 00 1 1 1 10 1 0 0 01 1 1 1 10 0 0 0 0 | Output: NO YES YES
Easy
2
398
483
346
17
353
B
353B
B. Two Heaps
1,900
combinatorics; constructive algorithms; greedy; implementation; math; sortings
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from...
The first line contains integer n (1 ≀ n ≀ 100). The second line contains 2Β·n space-separated integers ai (10 ≀ ai ≀ 99), denoting the numbers on the cubes.
In the first line print a single number β€” the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 ≀ bi ≀ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.If there are multiple optimal ways to split the cubes into the heaps, p...
In the first test case Valera can put the first cube in the first heap, and second cube β€” in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal...
Input: 110 99 | Output: 12 1
Hard
6
916
156
337
3
1,780
F
1780F
F. Three Chairs
2,300
bitmasks; brute force; combinatorics; data structures; dp; number theory; sortings
One day Kira found \(n\) friends from Morioh and decided to gather them around a table to have a peaceful conversation. The height of friend \(i\) is equal to \(a_i\). It so happened that the height of each of the friends is unique.Unfortunately, there were only \(3\) chairs in Kira's house, and obviously, it will not ...
The first line contains the number \(n\) (\(3 \le n \le 3\cdot10^5\)) β€” the number of Kira's friends.The next line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 3\cdot10^5\)) β€” heights of Kira's friends.
In a single line output the number of ways to invite three friends.
In the first example, only one way is suitable: invite friends \(1\), \(2\), and \(3\). Here \(1 < 2 < 3\), and the numbers \(1\) and \(3\) are coprime.
Input: 3 1 2 3 | Output: 1
Expert
7
1,156
233
67
17
1,938
M
1938M
3,500
Master
0
0
0
0
19
929
A
929A
A. ΠŸΡ€ΠΎΠΊΠ°Ρ‚ вСлосипСдов
1,400
*special; greedy; implementation
Как извСстно, Π² Ρ‚Π΅ΠΏΠ»ΡƒΡŽ ΠΏΠΎΠ³ΠΎΠ΄Ρƒ ΠΌΠ½ΠΎΠ³ΠΈΠ΅ ΠΆΠΈΡ‚Π΅Π»ΠΈ ΠΊΡ€ΡƒΠΏΠ½Ρ‹Ρ… Π³ΠΎΡ€ΠΎΠ΄ΠΎΠ² ΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ΡΡ сСрвисами городского Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚Π°. Π’ΠΎΡ‚ ΠΈ Аркадий сСгодня Π±ΡƒΠ΄Π΅Ρ‚ Π΄ΠΎΠ±ΠΈΡ€Π°Ρ‚ΡŒΡΡ ΠΎΡ‚ ΡˆΠΊΠΎΠ»Ρ‹ Π΄ΠΎ Π΄ΠΎΠΌΠ°, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ городскиС вСлосипСды.Π¨ΠΊΠΎΠ»Π° ΠΈ Π΄ΠΎΠΌ находятся Π½Π° ΠΎΠ΄Π½ΠΎΠΉ прямой ΡƒΠ»ΠΈΡ†Π΅, ΠΊΡ€ΠΎΠΌΠ΅ Ρ‚ΠΎΠ³ΠΎ, Π½Π° Ρ‚ΠΎΠΉ ΠΆΠ΅ ΡƒΠ»ΠΈΡ†Π΅ Π΅ΡΡ‚ΡŒ n Ρ‚ΠΎΡ‡Π΅ΠΊ, Π³Π΄Π΅ ΠΌΠΎΠΆΠ½ΠΎ Π²Π·ΡΡ‚ΡŒ вСлосипСд Π² ΠΏΡ€ΠΎΠΊΠ°Ρ‚ ΠΈΠ»ΠΈ с...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа n ΠΈ k (2 ≀ n ≀ 1 000, 1 ≀ k ≀ 100 000) β€” количСство Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚ΠΎΠ² ΠΈ максимальноС расстояниС, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Аркадий ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΡ€ΠΎΠ΅Ρ…Π°Ρ‚ΡŒ Π½Π° ΠΎΠ΄Π½ΠΎΠΌ вСлосипСдС.Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΉ строкС слСдуСт ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ Ρ†Π΅Π»Ρ‹Ρ… чисСл x1, x2, ..., xn (0 ≀ x1 < x2 < ... < xn ≀ 100 000) β€” ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Ρ‚ΠΎΡ‡Π΅ΠΊ, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹...
Если Аркадий Π½Π΅ смоТСт Π΄ΠΎΠ±Ρ€Π°Ρ‚ΡŒΡΡ ΠΎΡ‚ ΡˆΠΊΠΎΠ»Ρ‹ Π΄ΠΎ Π΄ΠΎΠΌΠ° Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π° вСлосипСдах, Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ -1. Π’ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС, Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ минимальноС количСство вСлосипСдов, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΡ€ΠΊΠ°Π΄ΠΈΡŽ Π½ΡƒΠΆΠ½ΠΎ Π²Π·ΡΡ‚ΡŒ Π² Ρ‚ΠΎΡ‡ΠΊΠ°Ρ… ΠΏΡ€ΠΎΠΊΠ°Ρ‚Π°.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ Аркадий Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π·ΡΡ‚ΡŒ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ вСлосипСд Π² ΠΏΠ΅Ρ€Π²ΠΎΠΌ Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚Π΅ ΠΈ Π΄ΠΎΠ΅Ρ…Π°Ρ‚ΡŒ Π½Π° Π½Ρ‘ΠΌ Π΄ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚Π°. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚Π΅ ΠΎΠ½ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Π·ΡΡ‚ΡŒ Π½ΠΎΠ²Ρ‹ΠΉ вСлосипСд, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΎΠ½ смоТСт Π΄ΠΎΠ±Ρ€Π°Ρ‚ΡŒΡΡ Π΄ΠΎ Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠ³ΠΎ Π²Π΅Π»ΠΎΠΏΡ€ΠΎΠΊΠ°Ρ‚Π°, рядом с ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ ΠΈ находится Π΅Π³ΠΎ Π΄ΠΎΠΌ. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ ΠΡ€ΠΊΠ°Π΄ΠΈΡŽ Π½ΡƒΠΆΠ½ΠΎ всСго Π΄Π²Π° вСлосипСда, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΠ±...
Input: 4 43 6 8 10 | Output: 2
Easy
3
1,527
417
196
9
1,141
F2
1141F2
F2. Same Sum Blocks (Hard)
1,900
data structures; greedy
This problem is given in two editions, which differ exclusively in the constraints on the number \(n\).You are given an array of integers \(a[1], a[2], \dots, a[n].\) A block is a sequence of contiguous (consecutive) elements \(a[l], a[l+1], \dots, a[r]\) (\(1 \le l \le r \le n\)). Thus, a block is defined by a pair of...
The first line contains integer \(n\) (\(1 \le n \le 1500\)) β€” the length of the given array. The second line contains the sequence of elements \(a[1], a[2], \dots, a[n]\) (\(-10^5 \le a_i \le 10^5\)).
In the first line print the integer \(k\) (\(1 \le k \le n\)). The following \(k\) lines should contain blocks, one per line. In each line print a pair of indices \(l_i, r_i\) (\(1 \le l_i \le r_i \le n\)) β€” the bounds of the \(i\)-th block. You can print blocks in any order. If there are multiple answers, print any of...
Input: 7 4 1 2 2 1 5 3 | Output: 3 7 7 2 3 4 5
Hard
2
1,106
201
326
11
1,895
B
1895B
B. Points and Minimum Distance
800
greedy; math; sortings
You are given a sequence of integers \(a\) of length \(2n\). You have to split these \(2n\) integers into \(n\) pairs; each pair will represent the coordinates of a point on a plane. Each number from the sequence \(a\) should become the \(x\) or \(y\) coordinate of exactly one point. Note that some points can be equal....
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 100\)) β€” the number of points to be formed.The next line contains \(2n\) integers \(a_1, a_2, \dots, a_{2n}\) (\(0 \le a_i \le 1\,000\)) β€” the des...
For each testcase, print the minimum possible length of path \(s\) in the first line.In the \(i\)-th of the following \(n\) lines, print two integers \(x_i\) and \(y_i\) β€” the coordinates of the point that needs to be visited at the \(i\)-th position.If there are multiple answers, print any of them.
In the first testcase, for instance, you can form points \((10, 1)\) and \((15, 5)\) and start the path \(s\) from the first point and end it at the second point. Then the length of the path will be \(|10 - 15| + |1 - 5| = 5 + 4 = 9\).In the second testcase, you can form points \((20, 20)\), \((10, 30)\), and \((10, 30...
Input: 2215 1 10 5310 30 20 20 30 10 | Output: 9 10 1 15 5 20 20 20 10 30 10 30
Beginner
3
818
351
300
18
1,499
C
1499C
C. Minimum Grid Path
1,500
brute force; data structures; greedy; math
Let's say you are standing on the \(XY\)-plane at point \((0, 0)\) and you want to reach point \((n, n)\).You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your \(x\) coordinate, or up, i. e. vertically and in the direction that increase your \(y\) coordinate. In o...
The first line contains the single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains the single integer \(n\) (\(2 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le 10^9\)) β€” the costs of each seg...
For each test case, print the minimum possible cost of the path from \((0, 0)\) to \((n, n)\) consisting of at most \(n\) alternating segments.
In the first test case, to reach \((2, 2)\) you need to make at least one turn, so your path will consist of exactly \(2\) segments: one horizontal of length \(2\) and one vertical of length \(2\). The cost of the path will be equal to \(2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202\).In the second test case, one of the o...
Input: 3 2 13 88 3 2 3 1 5 4 3 2 1 4 | Output: 202 13 19
Medium
4
1,487
393
143
14
2,026
C
2026C
C. Action Figures
1,500
binary search; brute force; constructive algorithms; data structures; greedy; implementation
There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains \(n\) figures, the \(i\)-th figure costs \(i\) coins and is available for purchase from day \(i\) to day \(n\).For each of the \(n\) days, Monocarp knows whether he can visit the shop...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β€” the number of figures in the set (and the number of days); the second line contains a string \(s\) (\(|s| = n\), ea...
For each test case, print one integer β€” the minimum amount of money Monocarp has to spend.
In the first test case, Monocarp buys the \(1\)-st figure on the \(1\)-st day and spends \(1\) coin.In the second test case, Monocarp can buy the \(1\)-st and the \(3\)-rd figure on the \(3\)-rd day, the \(2\)-nd and the \(4\)-th figure on the \(4\)-th day, and the \(5\)-th and the \(6\)-th figure on the \(6\)-th day. ...
Input: 411610110171110001511111 | Output: 1 8 18 6
Medium
6
920
657
90
20
5
B
5B
B. Center Alignment
1,200
implementation; strings
Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor Β«TextpadΒ» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!
The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000.
Format the given text, aligning it center. Frame the whole text with characters Β«*Β» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and brin...
Input: This isCodeforcesBetaRound5 | Output: ************* This is ** **Codeforces** Beta ** Round ** 5 *************
Easy
2
293
300
476
0
1,009
B
1009B
B. Minimum Ternary String
1,400
greedy; implementation
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace ""01"" with ""10"" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace ""12"" with ""21"" or vice versa).F...
The first line of the input contains the string \(s\) consisting only of characters '0', '1' and '2', its length is between \(1\) and \(10^5\) (inclusive).
Print a single string β€” the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
Input: 100210 | Output: 001120
Easy
2
1,123
155
165
10
87
B
87B
B. Vasya and Types
1,800
implementation; strings
Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below...
The first line contains an integer n (1 ≀ n ≀ 100) β€” the number of operators. Then follow n lines with operators. Each operator is of one of two types: either ""typedef A B"", or ""typeof A"". In the first case the B type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands.All th...
For every typeof operator print on the single line the answer to that operator β€” the type that the given operator returned.
Let's look at the second sample.After the first two queries typedef the b type is equivalent to void*, and c β€” to void**.The next query typedef redefines b β€” it is now equal to &b = &void* = void. At that, the c type doesn't change.After that the c type is defined as &&b* = &&void* = &void = errtype. It doesn't influen...
Input: 5typedef void* ptvtypeof ptvtypedef &&ptv nodetypeof nodetypeof &ptv | Output: void*errtypevoid
Medium
2
2,472
590
123
0
140
C
140C
C. New Year Snowmen
1,800
binary search; data structures; greedy
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any thr...
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of snowballs. The next line contains n integers β€” the balls' radii r1, r2, ..., rn (1 ≀ ri ≀ 109). The balls' radii can coincide.
Print on the first line a single number k β€” the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers β€” the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the...
Input: 71 2 3 4 5 6 7 | Output: 23 2 16 5 4
Medium
3
580
190
393
1
1,801
F
1801F
F. Another n-dimensional chocolate bar
2,700
dp; math; meet-in-the-middle; number theory
Mom bought the boy Vasya a \(n\)-dimensional chocolate bar, which is a \(n\)-dimensional cube with the length of each side equal to \(1\). The chocolate is planned to be divided into slices. According to the \(i\)th dimension, it can be divided by hyperplanes into \(a_i\) equal parts. Thus, the chocolate is divided in ...
The first line contains two integers \(n\) and \(k\) \((1 \le n \le 100\), \(1 \le k \le 10^7)\) β€” the dimension of the chocolate bar, and how many parts it needs to be divided into.The second line contains \(n\) integers \(a_1,\ a_2,\ \dots,\ a_n\) \((1 \le a_i \le 10^7)\) β€” the number of pieces on which the chocolate...
Print one number β€” the maximum possible volume of the smallest of the obtained pieces, multiplied by \(k\), with an absolute or relative error of no more than \(10^{-9}\).If it is impossible to cut a chocolate bar into at least \(k\) pieces under the given restrictions, output \(0\).
In the first example, a one – dimensional chocolate bar can be divided as follows: Then the answer will be \(\frac{2}{5} \cdot 2 = 0.8\)In the second example, the chocolate bar can be cut as follows:Then the answer will be \(\frac{2}{5} \cdot \frac{3}{10} \cdot 6 = 0.72\)In the third example, the chocolate bar can be c...
Input: 1 2 5 | Output: 0.8
Master
4
1,868
360
284
18
290
A
290A
A. Mysterious strings
1,400
*special; implementation
The input contains a single integer a (1 ≀ a ≀ 40).
Output a single string.
Input: 2 | Output: Adams
Easy
2
0
51
23
2
1,899
C
1899C
C. Yarik and Array
1,100
dp; greedy; two pointers
A subarray is a continuous part of array.Yarik recently found an array \(a\) of \(n\) elements and became very interested in finding the maximum sum of a non empty subarray. However, Yarik doesn't like consecutive integers with the same parity, so the subarray he chooses must have alternating parities for adjacent elem...
The first line contains an integer \(t\) \((1 \le t \le 10^4)\) β€” number of test cases. Each test case is described as follows.The first line of each test case contains an integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) β€” length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\...
For each test case, output a single integer β€” the answer to the problem.
Input: 751 2 3 4 549 9 8 86-1 4 -1 0 5 -44-1 2 4 -31-10003101 -99 10120-10 5 -8 10 6 -10 7 9 -2 -6 7 2 -4 6 -1 7 -6 -7 4 1 | Output: 15 17 8 4 -1000 101 10
Easy
3
508
466
72
18
935
A
935A
A. Fafa and his Company
800
brute force; implementation
Fafa owns a company that works on huge projects. There are n employees in Fafa's company. Whenever the company has a new project to start working on, Fafa has to divide the tasks of this project among all the employees.Fafa finds doing this every time is very tiring for him. So, he decided to choose the best l employee...
The input consists of a single line containing a positive integer n (2 ≀ n ≀ 105) β€” the number of employees in Fafa's company.
Print a single integer representing the answer to the problem.
In the second sample Fafa has 3 ways: choose only 1 employee as a team leader with 9 employees under his responsibility. choose 2 employees as team leaders with 4 employees under the responsibility of each of them. choose 5 employees as team leaders with 1 employee under the responsibility of each of them.
Input: 2 | Output: 1
Beginner
2
1,014
126
62
9
1,575
E
1575E
E. Eye-Pleasing City Park Tour
2,600
data structures; trees
There is a city park represented as a tree with \(n\) attractions as its vertices and \(n - 1\) rails as its edges. The \(i\)-th attraction has happiness value \(a_i\).Each rail has a color. It is either black if \(t_i = 0\), or white if \(t_i = 1\). Black trains only operate on a black rail track, and white trains onl...
The first line contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq k \leq n-1\)) β€” the number of attractions in the city park and the number of tickets you have.The second line contains \(n\) integers \(a_1, a_2,\ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) β€” the happiness value of each attractio...
Output an integer denoting the total happiness value for all valid tours \((u, v)\) (\(1 \leq u \leq v \leq n\)), modulo \(10^9 + 7\).
Input: 5 0 1 3 2 6 4 1 2 1 1 4 0 3 2 1 2 5 0 | Output: 45
Expert
2
1,197
564
134
15
244
A
244A
A. Dividing Orange
900
implementation
One day Ms Swan bought an orange in a shop. The orange consisted of nΒ·k segments, numbered with integers from 1 to nΒ·k. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote t...
The first line contains two integers n, k (1 ≀ n, k ≀ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≀ ai ≀ nΒ·k), where ai is the number of the orange segment that the i-th child would like to get.It is guaranteed that all numbers ai are distinct.
Print exactly nΒ·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.You can print a child's segment indexes in any order. ...
Input: 2 24 1 | Output: 2 4 1 3
Beginner
1
803
276
425
2
60
D
60D
D. Savior
2,500
brute force; dsu; math
Misha decided to help Pasha and Akim be friends again. He had a cunning plan β€” to destroy all the laughy mushrooms. He knows that the laughy mushrooms can easily burst when they laugh. Mushrooms grow on the lawns. There are a[t] mushrooms on the t-th lawn.Misha knows that the lawns where the mushrooms grow have a uniqu...
The first line contains one integer n (1 ≀ n ≀ 106) which is the number of lawns. The next line contains n integers ai which are the number of mushrooms on the i-lawn (1 ≀ ai ≀ 107). All the numbers are different.
Print a single number β€” the minimal number of lawns on which Misha should laugh for all the mushrooms to burst.
Input: 12 | Output: 1
Expert
3
738
213
111
0
2,039
F1
2039F1
F1. Shohag Loves Counting (Easy Version)
2,800
combinatorics; dp; math; number theory
This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on \(t\), \(m\), and the sum of \(m\). You can only make hacks if both versions of the problem are solved.For an integer array \(a\) of length \(n\), define \(f(k)\) as the greatest common divisor ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains an integer \(m\) (\(1 \le m \le 10^5\)).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(10^5\).
For each test case, output an integer β€” the number of valid arrays modulo \(998\,244\,353\).
In the first test case, the valid arrays are \([1]\), \([1, 2]\), \([2]\), and \([2, 1]\).In the second test case, there are a total of \(29\) valid arrays. In particular, the array \([2, 1, 4]\) with length \(n = 3\) is valid because all elements are from \(1\) to \(m = 5\) and \(f(1)\), \(f(2)\) and \(f(n = 3)\) all ...
Input: 3259 | Output: 4 29 165
Master
4
1,128
272
92
20
547
A
547A
A. Mike and Frog
2,200
brute force; greedy; implementation; math
Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become and height of Abol w...
The first line of input contains integer m (2 ≀ m ≀ 106).The second line of input contains integers h1 and a1 (0 ≀ h1, a1 < m).The third line of input contains integers x1 and y1 (0 ≀ x1, y1 < m).The fourth line of input contains integers h2 and a2 (0 ≀ h2, a2 < m).The fifth line of input contains integers x2 and y2 (0...
Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.
In the first sample, heights sequences are following:Xaniar: Abol:
Input: 54 21 10 12 3 | Output: 3
Hard
4
643
377
116
5
164
E
164E
E. Polycarpus and Tasks
3,100
Polycarpus has many tasks. Each task is characterized by three integers li, ri and ti. Three integers (li, ri, ti) mean that to perform task i, one needs to choose an integer si (li ≀ si; si + ti - 1 ≀ ri), then the task will be carried out continuously for ti units of time, starting at time si and up to time si + ti -...
The first line of the input contains a single integer n (1 ≀ n ≀ 105) β€” the number of tasks in set A. Then n lines describe the tasks. The i-th line contains three space-separated integers li, ri, ti (1 ≀ li ≀ ri ≀ 109, 1 ≀ ti ≀ ri - li + 1) β€” the description of the i-th task.It is guaranteed that for any tasks j, k (c...
For each task i print a single integer β€” the result of processing task i on the i-th iteration of the cycle (step 3) in function f(A). In the i-th line print: 0 β€” if you managed to add task i on step 4. -1 β€” if you didn't manage to add or replace task i (step 7). resi (1 ≀ resi ≀ n) β€” if you managed to replace the task...
Input: 51 8 52 9 33 10 38 11 411 12 2 | Output: 0 0 1 0 -1
Master
0
2,117
386
423
1
374
D
374D
D. Inna and Sequence
2,000
binary search; data structures; dp; trees
Dima's spent much time thinking what present to give to Inna and gave her an empty sequence w. Now they want to fill sequence w with numbers zero and one. For that, they decided to play an amusing game. Before the game begins, Dima chooses m integers a1, a2, ..., am (1 ≀ a1 < a2 < ... < am). Then Inna and Dima start pl...
The first line of the input contains two integers n and m (1 ≀ n, m ≀ 106) showing how many events took place and how many numbers Dima chose.The next line contains m distinct integers ai (1 ≀ ai ≀ 106) sorted in the increasing order. Next n lines describe the events in the chronological order. Each line contains a sin...
In a single line print a sequence of numbers 0 and 1 β€” the elements of the sequence after all events happen. Print the elements of the sequence in the order from the beginning to the end of the sequence.If after all events the sequence ends up empty, print ""Poor stack!"".
Input: 10 31 3 6-11100-101-11 | Output: 011
Hard
4
1,141
536
273
3
1,360
B
1360B
B. Honest Coach
800
greedy; sortings
There are \(n\) athletes in front of you. Athletes are numbered from \(1\) to \(n\) from left to right. You know the strength of each athlete β€” the athlete number \(i\) has the strength \(s_i\).You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in o...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases in the input. Then \(t\) test cases follow.Each test case consists of two lines. The first line contains positive integer \(n\) (\(2 \le n \le 50\)) β€” number of athletes. The second line contains \(n\) positive integers \(s_1, s_...
For each test case print one integer β€” the minimum value of \(|\max(A) - \min(B)|\) with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
The first test case was explained in the statement. In the second test case, one of the optimal splits is \(A=[2, 1]\), \(B=[3, 2, 4, 3]\), so the answer is \(|2-2|=0\).
Input: 5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200 | Output: 1 0 2 999 50
Beginner
2
1,128
470
209
13
1,619
G
1619G
G. Unusual Minesweeper
2,000
binary search; dfs and similar; dsu; greedy; sortings
Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules.There are mines on the field, for each the coordinates of its location are known (\(x_i, y_i\)). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.An empty line is written in front of each test suite.Next comes a line that contains integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le k \le 10^9\)) β€” the number of mines and the distance th...
Print \(t\) lines, each of the lines must contain the answer to the corresponding set of input data β€” the minimum number of seconds it takes to explode all the mines.
Picture from examples First example: \(0\) second: we explode a mine at the cell \((2, 2)\), it does not detonate any other mine since \(k=0\). \(1\) second: we explode the mine at the cell \((0, 1)\), and the mine at the cell \((0, 0)\) explodes itself. \(2\) second: we explode the mine at the cell \((1, 1)\), and the...
Input: 3 5 0 0 0 1 0 1 4 1 0 2 1 1 3 2 2 9 5 2 0 0 1 0 1 4 1 0 2 1 1 3 2 2 9 6 1 1 -1 3 0 -1 9 0 1 7 -1 0 1 -1 1 9 -1 -1 7 | Output: 2 1 0
Hard
5
928
745
166
16
366
D
366D
D. Dima and Trap Graph
2,000
binary search; data structures; dfs and similar; dsu; shortest paths; two pointers
Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal...Dima constructed a trap graph. He shouted: ""Hey Seryozha, have a look at my cool graph!"" to get his roommate inter...
The first line of the input contains two integers n and m (2 ≀ n ≀ 103, 0 ≀ m ≀ 3Β·103). Then follow m lines describing the edges. Each line contains four integers ak, bk, lk and rk (1 ≀ ak, bk ≀ n, 1 ≀ lk ≀ rk ≀ 106). The numbers mean that in the trap graph the k-th edge connects nodes ak and bk, this edge corresponds ...
In a single line of the output print an integer β€” the maximum loyalty among all paths from the first node to the n-th one. If such paths do not exist or the maximum loyalty equals 0, print in a single line ""Nice work, Dima!"" without the quotes.
Explanation of the first example.Overall, we have 2 ways to get from node 1 to node 4: first you must go along the edge 1-2 with range [1-10], then along one of the two edges 2-4. One of them contains range [3-5], that is, we can pass through with numbers 3, 4, 5. So the loyalty of such path is 3.If we go along edge 2-...
Input: 4 41 2 1 102 4 3 51 3 1 52 4 2 7 | Output: 6
Hard
6
1,097
419
246
3
412
D
412D
D. Giving Awards
2,000
dfs and similar
The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else.Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one fo...
The first line contains space-separated integers n and m β€” the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≀ ai, bi ≀ n; ai β‰  bi), these integers indicate that the person number ai owes money to a person a number bi. Assume th...
Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on.If there are multiple correct orders, you are allowed to print ...
Input: 2 11 2 | Output: 2 1
Hard
1
1,162
540
332
4
571
D
571D
D. Campus
3,100
binary search; data structures; dsu; trees
Oscolcovo city has a campus consisting of n student dormitories, n universities and n military offices. Initially, the i-th dormitory belongs to the i-th university and is assigned to the i-th military office.Life goes on and the campus is continuously going through some changes. The changes can be of four types: Unive...
The first line contains two integers, n and m (1 ≀ n, m ≀ 5Β·105) β€” the number of dormitories and the number of queries, respectively.Next m lines contain the queries, each of them is given in one of the following formats: Β«U aj bjΒ» β€” merging universities; Β«M cj djΒ» β€” merging military offices; Β«A xjΒ» β€” students of unive...
In the i-th line print the answer to the i-th query asking the number of people in the dormitory.
Consider the first sample test: In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student. After the third query university 1 owns dormitories 1 and 2. The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1....
Input: 2 7A 1Q 1U 1 2A 1Z 1Q 1Q 2 | Output: 102
Master
4
1,428
656
97
5
1,649
A
1649A
A. Game
800
implementation
You are playing a very popular computer game. The next level consists of \(n\) consecutive locations, numbered from \(1\) to \(n\), each of them containing either land or water. It is known that the first and last locations contain land, and for completing the level you have to move from the first location to the last....
There are several test cases in the input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. This is followed by the test cases description.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 100\)) β€” the number of locations.The second line ...
For each test case print a single integer β€” the answer to the problem.
In the first test case, it is enough to make one free jump from the first location to the second one, which is also the last one, so the answer is \(0\).In the second test case, the only way to move from the first location to the last one is to jump between them, which will cost \(4\) coins.In the third test case, you ...
Input: 321 151 0 1 0 141 0 1 1 | Output: 0 4 2
Beginner
1
829
625
70
16
1,874
C
1874C
C. Jellyfish and EVA
2,300
dp; graphs; greedy; math; probabilities
Monsters have invaded the town again! Asuka invites her good friend, Jellyfish, to drive EVA with her.There are \(n\) cities in the town. All the monsters are in city \(n\). Jellyfish and Asuka are currently in city \(1\) and need to move to city \(n\) to defeat the monsters.There are \(m\) roads. The \(i\)-th road all...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2000\)). The description of the test cases follows.The first line of each test case contains two integers, \(n\) and \(m\) (\(2 \leq n \leq 5000\), \(0 \leq m \leq \min(\frac{n(n-1)}{2}, 2 \cdot 10^5)\)) β€” th...
Output the maximum probability of the mission being successful if Jellyfish chooses the roads optimally.Your answer will be accepted if the absolute or relative error does not exceed \(10^{-9}\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(\frac{|a-b|}{\max...
In the first test case, Jellyfish will choose \(v_1=3\), and Asuka will choose \(v_2=2\) with a possibility of \(0.5\) and \(v_2=3\) with a possibility of \(0.5\). The mission is considered a success with a possibility of \(0.5\).
Input: 33 21 21 37 81 21 31 41 52 63 64 66 710 201 21 31 41 51 62 62 72 82 93 43 73 83 104 64 84 106 107 87 97 10 | Output: 0.500000000000 0.625000000000 0.491666666667
Expert
5
1,496
781
344
18
2,014
D
2014D
D. Robert Hood and Mrs Hood
1,400
brute force; data structures; greedy; sortings
Impress thy brother, yet fret not thy mother.Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor.All days are numbered from \(1\) to \(n\). Visitors stay for \(d\) continuous days, all of those \(d\) days must be between day \(1\) and \(n\) inclusive.Robin has a total of \(k...
The first line of the input contains a single integer \(t\) (\(1\leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case consists of three integers \(n\), \(d\), \(k\) (\(1 \le n \le 10^5, 1 \le d, k \le n\)) β€” the number of total days, duration of the visits, and the number of jobs. Then follow ...
For each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day \(1\) and \(n\) inclusive.
In the first test case, the only job fills all \(2\) days, both should visit on day \(1\).In the second test case, day \(2\) overlaps with \(2\) jobs and day \(1\) overlaps with only \(1\).In the third test case, Robert visits for days \([1,2]\), Mrs. Hood visits for days \([4,5]\).
Input: 62 1 11 24 1 21 22 47 2 31 21 36 75 1 21 23 59 2 12 89 2 47 94 81 32 3 | Output: 1 1 2 1 1 4 1 1 1 1 3 4
Easy
4
806
552
167
20
207
B1
207B1
B1. Military Trainings
1,600
The Smart Beaver from ABBYY started cooperating with the Ministry of Defence. Now they train soldiers to move armoured columns. The training involves testing a new type of tanks that can transmit information. To test the new type of tanks, the training has a special exercise, its essence is as follows.Initially, the co...
The first line contains integer n β€” the number of tanks in the column. Each of the next n lines contains one integer ai (1 ≀ ai ≀ 250000, 1 ≀ i ≀ n) β€” the message receiving radii of the tanks in the order from tank 1 to tank n (let us remind you that initially the tanks are located in the column in ascending order of t...
Print a single integer β€” the minimum possible total time of transmitting the messages.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample the original order of tanks is 1, 2, 3. The first tank sends a message to the second one, then the second tank sends it to the third one β€” it takes two seconds. The third tank moves to the beginning of the column and the order of tanks now is 3, 1, 2. The third tank sends a message to the first one,...
Input: 3211 | Output: 5
Medium
0
2,856
661
233
2
1,887
F
1887F
F. Minimum Segments
3,400
constructive algorithms
You had a sequence \(a_1, a_2, \ldots, a_n\) consisting of integers from \(1\) to \(n\), not necessarily distinct. For some unknown reason, you decided to calculate the following characteristic of the sequence: Let \(r_i\) (\(1 \le i \le n\)) be the smallest \(j \ge i\) such that on the subsegment \(a_i, a_{i+1}, \ldot...
Each test consist of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the lost sequence \(a\).The se...
For each test case, output the following: If there is no sequence \(a\) with the characteristic \(r\), print ""No"". Otherwise, print ""Yes"" on the first line, and on the second line, print any sequence of integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) that matches the characteristic \(r\). You can output ""...
In the first test case, the sequence \(a = [1, 2, 1]\) is suitable. The integers \(1\) and \(2\) appear on the subsegments \([1, 2]\) and \([2, 3]\).In the second test case, it can be proved that there is no suitable sequence \(a\).
Input: 532 3 452 3 5 4 61131 3 483 6 6 6 8 9 9 9 | Output: Yes 1 2 1 No Yes 1 No Yes 1 3 5 3 5 1 1 3
Master
1
907
563
455
18
146
B
146B
B. Lucky Mask
1,300
brute force; implementation
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya calls a mask of a positive integer n the number that is obtained after successive writing of a...
The only line contains two integers a and b (1 ≀ a, b ≀ 105). It is guaranteed that number b is lucky.
In the only line print a single number β€” the number c that is sought by Petya.
Input: 1 7 | Output: 7
Easy
2
687
102
78
1
1,801
E
1801E
E. Gasoline prices
3,000
data structures; divide and conquer; dsu; hashing; trees
Berland β€” is a huge country consisting of \(n\) cities. The road network of Berland can be represented as a root tree, that is, there is only \(n - 1\) road in the country, and you can get from any city to any other exactly one way, if you do not visit any city twice. For the convenience of representing the country, fo...
The first line contains a single integer \(n\) (\(1 \le n \le 200\,000\)) β€” the number of cities in Berland.The second line contains \((n - 1)\) numbers \(p_2,\ p_3,\ p_4,\ \ldots,\ p_n\) (\(1 \le p_i \le n\)), where \(p_i\) denotes the number of the next city on the way from city \(i\) to city \(1\).In each of the fol...
In \(m\) lines, print one number each. The number in the \(i\) line should be equal to the number of ways to set gasoline prices in all cities so that the king's children born in the years up to and including \(i\) do not reveal violations in inspections. Output the numbers modulo \(10^9 + 7\).
Consider the first example.After the birth of the first two sons, the prices in the cities of \(1\) and \(2\) should be equal. In total, there are 2 ways to choose the same gasoline price for the cities of \(1\) and \(2\) so that it falls within the acceptable price range for these cities. So, there are only ways to se...
Input: 5 1 1 2 2 2 4 1 3 1 3 2 4 4 4 4 1 1 2 2 1 2 2 1 3 4 4 3 3 4 3 5 | Output: 18 18 4 0
Master
5
2,978
1,027
295
18
1,323
A
1323A
A. Even Subset Sum Problem
800
brute force; dp; greedy; implementation
You are given an array \(a\) consisting of \(n\) positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by \(2\)) or determine that there is no such subset.Both the given array and required subset may contain equal values.
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)), number of test cases to solve. Descriptions of \(t\) test cases follow.A description of each test case consists of two lines. The first line contains a single integer \(n\) (\(1 \leq n \leq 100\)), length of array \(a\).The second line contains \(n...
For each test case output \(-1\) if there is no such subset of elements. Otherwise output positive integer \(k\), number of elements in the required subset. Then output \(k\) distinct integers (\(1 \leq p_i \leq n\)), indexes of the chosen elements. If there are multiple solutions output any of them.
There are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is \(4\) and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In...
Input: 3 3 1 4 3 1 15 2 3 5 | Output: 1 2 -1 2 1 2
Beginner
4
270
464
301
13
2,004
G
2004G
G. Substring Compression
3,200
data structures; dp; matrices
Let's define the operation of compressing a string \(t\), consisting of at least \(2\) digits from \(1\) to \(9\), as follows: split it into an even number of non-empty substrings β€” let these substrings be \(t_1, t_2, \dots, t_m\) (so, \(t = t_1 + t_2 + \dots + t_m\), where \(+\) is the concatenation operation); write ...
The first line contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\)).The second line contains the string \(s\) (\(|s| = n\)), consisting only of digits from \(1\) to \(9\).
Output \(n - k + 1\) integers β€” \(f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})\).
Input: 4 45999 | Output: 14
Master
3
874
193
89
20
630
I
630I
I. Parking Lot
1,700
combinatorics; math
To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes.The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars i...
The only line of the input contains one integer n (3 ≀ n ≀ 30) β€” the amount of successive cars of the same make.
Output one integer β€” the number of ways to fill the parking lot by cars of four makes using the described way.
Let's denote car makes in the following way: A β€” Aston Martin, B β€” Bentley, M β€” Mercedes-Maybach, Z β€” Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMMOriginally...
Input: 3 | Output: 24
Medium
2
749
112
110
6
2,021
E2
2021E2
E2. Digital Village (Hard Version)
2,500
data structures; dp; dsu; graphs; math; trees
This is the hard version of the problem. In the three versions, the constraints on \(n\) and \(m\) are different. You can make hacks only if all the versions of the problem are solved.Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph wi...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2000\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(p\) (\(2 \le n \le 5000\); \(n-1 \le m \le 5000\); \(1 \le p \le n\)) β€” the number of ...
For each test case, output \(n\) integers: the minimum total latency that can be achieved for all the houses requiring internet for each \(k = 1,2,\ldots,n\).
In the first test case for \(k=3\), a possible optimal solution is to install servers at vertices \(2\), \(6\) and \(8\) and obtain the following latency: \(\text{latency}(2) = 0\) \(\text{latency}(5) = \max(3, 5) = 5\) \(\text{latency}(6) = 0\) \(\text{latency}(8) = 0\) \(\text{latency}(9) = \max(2, 4) = 4\) So the to...
Input: 29 8 52 5 6 8 91 2 11 3 23 4 104 5 34 6 51 7 107 8 47 9 23 3 23 11 2 12 3 31 3 2 | Output: 34 19 9 4 0 0 0 0 0 2 0 0
Expert
6
962
992
158
20
91
A
91A
A. Newspaper Headline
1,500
greedy; strings
A newspaper is published in Walrusland. Its heading is s1, it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a new...
The input data contain two lines. The first line contain the heading s1, the second line contains the word s2. The lines only consist of lowercase Latin letters (1 ≀ |s1| ≀ 104, 1 ≀ |s2| ≀ 106).
If it is impossible to get the word s2 in the above-described manner, print ""-1"" (without the quotes). Otherwise, print the least number of newspaper headings s1, which Fangy will need to receive the word s2.
Input: abcxyz | Output: -1
Medium
2
828
194
210
0
76
B
76B
B. Mice
2,100
greedy; two pointers
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and ther...
The first line of the input contains four integer numbers N (1 ≀ N ≀ 105), M (0 ≀ M ≀ 105), Y0 (0 ≀ Y0 ≀ 107), Y1 (0 ≀ Y1 ≀ 107, Y0 β‰  Y1). The second line contains a strictly increasing sequence of N numbers β€” x coordinates of mice. Third line contains a strictly increasing sequence of M numbers β€” x coordinates of chee...
The only line of output should contain one number β€” the minimal number of mice which will remain without cheese.
All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten.
Input: 3 2 0 20 1 32 5 | Output: 1
Hard
2
1,072
393
112
0
123
C
123C
C. Brackets
2,300
combinatorics; dp; greedy
A two dimensional array is called a bracket array if each grid contains one of the two possible brackets β€” ""("" or "")"". A path through the two dimensional array cells is called monotonous if any two consecutive cells in the path are side-adjacent and each cell of the path is located below or to the right from the pr...
The first line contains integers n, m and k β€” the sizes of the array and the number of the sought correct bracket array (1 ≀ n, m ≀ 100, 1 ≀ k ≀ 1018). Then an array of priorities is given, n lines each containing m numbers, number pi, j shows the priority of character j in line i (1 ≀ pi, j ≀ nm, all pi, j are differe...
Print the k-th two dimensional correct bracket array.
In the first sample exists only one correct two-dimensional bracket array.In the second and in the third samples two arrays exist.A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters Β«+Β» and Β«1Β» into this sequence. For example, sequences Β«(())()Β», Β«()Β» a...
Input: 1 2 11 2 | Output: ()
Expert
3
1,282
476
53
1
119
D
119D
D. String Transformation
2,500
hashing; strings
Let s be a string whose length equals n. Its characters are numbered from 0 to n - 1, i and j are integers, 0 ≀ i < j < n. Let's define function f as follows:f(s, i, j) = s[i + 1... j - 1] + r(s[j... n - 1]) + r(s[0... i]).Here s[p... q] is a substring of string s, that starts in position p and ends in position q (incl...
The first two input lines are non-empty strings a and b correspondingly. Each string's length does not exceed 106 characters. The strings can contain any characters with ASCII codes from 32 to 126 inclusive.
Print two integers i, j β€” the answer to the problem. If no solution exists, print ""-1 -1"" (without the quotes).
Input: Die Polizei untersucht eine Straftat im IT-Bereich.untersucht eine Straftat.hciereB-TI mi ieziloP eiD | Output: 11 36
Expert
2
735
207
113
1
51
D
51D
D. Geometrical problem
2,200
implementation
Polycarp loves geometric progressions β€” he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression.In this task we shall define geometric progressions as finite sequences of numbers a1, a2, ..., a...
The first line contains an integer n (1 ≀ n ≀ 105) β€” the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104.
Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2.
Input: 43 6 12 24 | Output: 0
Hard
1
711
274
226
0
1,721
A
1721A
A. Image
800
greedy; implementation
You have an image file of size \(2 \times 2\), consisting of \(4\) pixels. Each pixel can have one of \(26\) different colors, denoted by lowercase Latin letters.You want to recolor some of the pixels of the image so that all \(4\) pixels have the same color. In one move, you can choose no more than two pixels of the s...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of two lines. Each of these lines contains two lowercase letters of Latin alphabet without any separators, denoting a row of pixels in the image.
For each test case, print one integer β€” the minimum number of moves you have to make so that all \(4\) pixels of the image have the same color.
Let's analyze the test cases of the example.In the first test case, you can paint the bottom left pixel and the top right pixel (which share the same color) into the color r, so all pixels have this color.In the second test case, two moves are enough: paint both top pixels, which have the same color c, into the color b...
Input: 5rbbrccwbaaaaabcdyyxx | Output: 1 2 0 3 1
Beginner
2
521
261
143
17
918
A
918A
A. Eleven
800
brute force; implementation
Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters. Her friend suggested that her name should only consist of uppercase and lowercase letters 'O'. More precisely, they suggested that the i-th...
The first and only line of input contains an integer n (1 ≀ n ≀ 1000).
Print Eleven's new name on the first and only line of output.
Input: 8 | Output: OOOoOooO
Beginner
2
694
70
61
9
460
A
460A
A. Vasya and Socks
900
brute force; implementation; math
Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, ...) mom buys a pair of socks to Vasya. She does it late in the eve...
The single line contains two integers n and m (1 ≀ n ≀ 100; 2 ≀ m ≀ 100), separated by a space.
Print a single integer β€” the answer to the problem.
In the first sample Vasya spends the first two days wearing the socks that he had initially. Then on day three he puts on the socks that were bought on day two.In the second sample Vasya spends the first nine days wearing the socks that he had initially. Then he spends three days wearing the socks that were bought on t...
Input: 2 2 | Output: 3
Beginner
3
456
95
51
4
551
A
551A
A. GukiZ and Contest
800
brute force; implementation; sortings
Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the r...
The first line contains integer n (1 ≀ n ≀ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≀ ai ≀ 2000) where ai is the rating of i-th student (1 ≀ i ≀ n).
In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.
In the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating.In the second sample, first student is the only one on the contest.In the third sample, students 2 and 5 share the first position w...
Input: 31 3 3 | Output: 3 1 1
Beginner
3
961
195
135
5
995
C
995C
C. Leaving the Bar
2,300
brute force; data structures; geometry; greedy; math; sortings
For a vector \(\vec{v} = (x, y)\), define \(|v| = \sqrt{x^2 + y^2}\).Allen had a bit too much to drink at the bar, which is at the origin. There are \(n\) vectors \(\vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}\). Allen will make \(n\) moves. As Allen's sense of direction is impaired, during the \(i\)-th move he will either ...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of moves.Each of the following lines contains two space-separated integers \(x_i\) and \(y_i\), meaning that \(\vec{v_i} = (x_i, y_i)\). We have that \(|v_i| \le 10^6\) for all \(i\).
Output a single line containing \(n\) integers \(c_1, c_2, \cdots, c_n\), each of which is either \(1\) or \(-1\). Your solution is correct if the value of \(p = \sum_{i = 1}^n c_i \vec{v_i}\), satisfies \(|p| \le 1.5 \cdot 10^6\).It can be shown that a solution always exists under the given constraints.
Input: 3999999 00 999999999999 0 | Output: 1 1 -1
Expert
6
768
266
305
9
408
A
408A
A. Line to Cashier
900
implementation
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki peo...
The first line contains integer n (1 ≀ n ≀ 100) β€” the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≀ ki ≀ 100), where ki is the number of people in the queue to the i-th cashier.The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ...,...
Print a single integer β€” the minimum number of seconds Vasya needs to get to the cashier.
In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100Β·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1Β·5 + 2Β·5 + 2Β·5 + 3Β·5 + 4Β·15 = 100 seconds. He will need 1Β·5 + 9Β·5 + 1Β·5 + 3Β·15 = 100 seconds for the third one and 7Β·5 + 8Β·5 + 2Β·15 = 105 seconds for the fou...
Input: 111 | Output: 20
Beginner
1
821
422
89
4
1,740
H
1740H
H. MEX Tree Manipulation
3,300
data structures; trees
Given a rooted tree, define the value of vertex \(u\) in the tree recursively as the MEX\(^\dagger\) of the values of its children. Note that it is only the children, not all of its descendants. In particular, the value of a leaf is \(0\).Pak Chanek has a rooted tree that initially only contains a single vertex with in...
The first line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β€” the number of operations.Each of the next \(q\) lines contains a single integer \(x_i\) (\(1 \leq x_i \leq i\)) β€” the description of the \(i\)-th query.
For each query, print a line containing an integer representing the sum of the new values of all vertices in the tree after adding the vertex.
In the first example, the tree after the \(6\)-th query will look like this. Vertex \(7\) is a leaf, so its value is \(0\). Vertex \(6\) is a leaf, so its value is \(0\). Vertex \(5\) only has a child with value \(0\), so its value is \(1\). Vertex \(4\) is a leaf, so its value is \(0\). Vertex \(3\) only has a child w...
Input: 7 1 1 3 2 5 2 1 | Output: 1 1 3 2 4 4 7
Master
2
908
232
142
17
883
C
883C
C. Downloading B++
2,300
binary search; implementation
Only T milliseconds left before the start of well-known online programming contest Codehorses Round 2017.Polycarp needs to download B++ compiler to take part in the contest. The size of the file is f bytes.Polycarp's internet tariff allows to download data at the rate of one byte per t0 milliseconds. This tariff is alr...
The first line contains three integer numbers f, T and t0 (1 ≀ f, T, t0 ≀ 107) β€” size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff.The second line contains a description of the first additional p...
Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in T milliseconds. If there is no solution, print the only integer -1.
In the first example Polycarp has to buy the first additional package 5 times and do not buy the second additional package. He downloads 120 bytes (of total 26Β·5 = 130 bytes) in 120Β·8 = 960 milliseconds (960 ≀ 964). He spends 8Β·5 = 40 burles on it.In the second example Polycarp has enough time to download 10 bytes. It ...
Input: 120 964 2026 8 813 10 4 | Output: 40
Expert
2
1,536
1,097
169
8
1,090
I
1090I
2,000
Hard
0
0
0
0
10
2,013
C
2013C
C. Password Cracking
1,400
constructive algorithms; interactive; strings
Dimash learned that Mansur wrote something very unpleasant about him to a friend, so he decided to find out his password at all costs and discover what exactly he wrote.Believing in the strength of his password, Mansur stated that his password β€” is a binary string of length \(n\). He is also ready to answer Dimash's qu...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.
In the first example, the string \(010\) is given. Therefore, the answers to the queries are as follows:""? 00"" \(00\) is not a substring of \(010\), so the answer is \(0\).""? 000"" \(000\) is not a substring, so the answer is \(0\).""? 010"" \(010\) is a substring, so the answer is \(1\).In the second example, the s...
Input: 4 3 0 0 1 4 4 2 | Output: ? 00 ? 000 ? 010 ! 010 ! 1100 ! 0110 ! 10
Easy
3
610
160
0
20
1,919
E
1919E
E. Counting Prefixes
2,600
combinatorics; constructive algorithms; dp; implementation; math
There is a hidden array \(a\) of size \(n\) consisting of only \(1\) and \(-1\). Let \(p\) be the prefix sums of array \(a\). More formally, \(p\) is an array of length \(n\) defined as \(p_i = a_1 + a_2 + \ldots + a_i\). Afterwards, array \(p\) is sorted in non-decreasing order. For example, if \(a = [1, -1, -1, 1, 1]...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)) β€” the size of the hidden array \(a\).The second li...
For each test case, output the answer modulo \(998\,244\,353\).
In the first two test cases, the only possible arrays \(a\) for \(n = 1\) are \(a = [1]\) and \(a = [-1]\). Their respective sorted prefix sum arrays \(p\) are \(p = [1]\) and \(p = [-1]\). Hence, there is no array \(a\) that can result in the sorted prefix sum array \(p = [0]\) and there is exactly \(1\) array \(a\) t...
Input: 510113-1 1 25-1 0 0 1 15-4 -3 -3 -2 -1 | Output: 0 1 0 3 1
Expert
5
733
614
63
19
250
C
250C
C. Movie Critics
1,600
greedy
A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre β€” an integer from 1 to k.On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programm...
The first line of the input contains two integers n and k (2 ≀ k ≀ n ≀ 105), where n is the number of movies and k is the number of genres.The second line of the input contains a sequence of n positive integers a1, a2, ..., an (1 ≀ ai ≀ k), where ai is the genre of the i-th movie. It is guaranteed that each number from...
Print a single number β€” the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.
In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses...
Input: 10 31 1 2 3 2 3 3 1 1 3 | Output: 3
Medium
1
1,330
366
156
2
1,867
E2
1867E2
E2. Salyg1n and Array (hard version)
2,200
constructive algorithms; interactive
This is the hard version of the problem. The only difference between the versions is the limit on the number of queries. In this version, you can make no more than 57 queries. You can make hacks only if both versions of the problem are solved.This is an interactive problem!salyg1n has given you a positive integer \(k\)...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) – the number of test cases.
In the first test case, the jury has chosen the array \(a\) \(=\) \([4, 2, 5, 1]\)In the second test case, the jury has chosen the array \(a\) \(=\) \([5, 7, 1, 3, 3, 7]\)
Input: 2 4 2 6 4 6 6 4 | Output: ? 1 ? 3 ! 2 ? 1 ! 4
Hard
2
1,037
99
0
18
1,526
E
1526E
E. Oolimry and Suffix Array
2,400
combinatorics; constructive algorithms; math
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array. More formally, given a suffix array of length \(n\) and having an alphabet size \(k\), count the number of strings that produce such a suffix array. Let \(s\) be a string of length \(n\). Then the \(i\)-th suffix o...
The first line contain 2 integers \(n\) and \(k\) (\(1 \leq n \leq 200000,1 \leq k \leq 200000\)) β€” the length of the suffix array and the alphabet size respectively.The second line contains \(n\) integers \(s_0, s_1, s_2, \ldots, s_{n-1}\) (\(0 \leq s_i \leq n-1\)) where \(s_i\) is the \(i\)-th element of the suffix a...
Print how many strings produce such a suffix array. Since the number can be very large, print the answer modulo \(998244353\).
In the first test case, ""abb"" is the only possible solution. In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal. In the fourth test case, one possible string is ""ddbacef"".Please remember to print your answers modulo \(998244353\).
Input: 3 2 0 2 1 | Output: 1
Expert
3
953
476
126
15
867
A
867A
A. Between the Offices
800
implementation
As you may know, MemSQL has American offices in both San Francisco and Seattle. Being a manager in the company, you travel a lot between the two cities, always by plane.You prefer flying from Seattle to San Francisco than in the other direction, because it's warmer in San Francisco. You are so busy that you don't remem...
The first line of input contains single integer n (2 ≀ n ≀ 100) β€” the number of days.The second line contains a string of length n consisting of only capital 'S' and 'F' letters. If the i-th letter is 'S', then you were in Seattle office on that day. Otherwise you were in San Francisco. The days are given in chronologi...
Print ""YES"" if you flew more times from Seattle to San Francisco, and ""NO"" otherwise.You can print each letter in any case (upper or lower).
In the first example you were initially at San Francisco, then flew to Seattle, were there for two days and returned to San Francisco. You made one flight in each direction, so the answer is ""NO"".In the second example you just flew from Seattle to San Francisco, so the answer is ""YES"".In the third example you staye...
Input: 4FSSF | Output: NO
Beginner
1
685
375
144
8
540
E
540E
E. Infinite Inversions
2,100
binary search; data structures; implementation; sortings; trees
There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting seq...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of swap operations applied to the sequence.Each of the next n lines contains two integers ai and bi (1 ≀ ai, bi ≀ 109, ai β‰  bi) β€” the arguments of the swap operation.
Print a single integer β€” the number of inversions in the resulting sequence.
In the first sample the sequence is being modified as follows: . It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).
Input: 24 21 4 | Output: 4
Hard
5
394
236
76
5
1,361
E
1361E
E. James and the Chase
3,000
dfs and similar; graphs; probabilities; trees
James Bond has a new plan for catching his enemy. There are some cities and directed roads between them, such that it is possible to travel between any two cities using these roads. When the enemy appears in some city, Bond knows her next destination but has no idea which path she will choose to move there. The city \(...
The first line contains one integer \(t\) (\(1 \leq t \leq 2\,000\)) β€” the number of test cases. Each test case is described as follows:The first line contains two integers \(n\) and \(m\) (\(1 \leq n \le 10^5\), \(0 \leq m \le 2 \cdot 10^5\)) β€” the number of cities and roads between them. Each of the following \(m\) l...
If strictly less than \(20\%\) of all cities are interesting, print \(-1\). Otherwise, let \(k\) be the number of interesting cities. Print \(k\) distinct integers in increasing order β€” the indices of interesting cities.
In all drawings, if a city is colored green, then it is interesting; otherwise, it is colored red.In the first sample, each city is interesting. In the second sample, no city is interesting. In the third sample, cities \(1\), \(2\), \(3\) and \(5\) are interesting. In the last sample, only the city \(1\) is interesting...
Input: 4 3 3 1 2 2 3 3 1 3 6 1 2 2 1 2 3 3 2 1 3 3 1 7 10 1 2 2 3 3 1 1 4 4 5 5 1 4 6 6 7 7 4 6 1 6 8 1 2 2 3 3 4 4 5 5 6 6 1 6 2 5 1 | Output: 1 2 3 -1 1 2 3 5 -1
Master
4
1,088
661
220
13
436
C
436C
C. Dungeons and Candies
1,800
dsu; graphs; greedy; trees
During the loading of the game ""Dungeons and Candies"" you are required to get descriptions of k levels from the server. Each description is a map of an n Γ— m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as ""."" on the map, but if a c...
The first line contains four integers n, m, k, w (1 ≀ n, m ≀ 10; 1 ≀ k, w ≀ 1000). Then follows the description of k levels. Each level is described by n lines, each line contains m characters. Each character is either a letter of the English alphabet or a dot ("".""). Please note that the case of the letters matters.
In the first line print the required minimum number of transferred bytes.Then print k pairs of integers x1, y1, x2, y2, ..., xk, yk, describing the way to transfer levels. Pair xi, yi means that level xi needs to be transferred by way yi. If yi equals 0, that means that the level must be transferred using the first way...
Input: 2 3 3 2A.A...A.a..CX.Y... | Output: 141 02 13 1
Medium
4
1,336
319
686
4
2,119
A
2119A
A. Add or XOR
800
bitmasks; greedy; math
r-906 & IA AI - Psychologic Disco You are given two non-negative integers \(a, b\). You can apply two types of operations on \(a\) any number of times and in any order: \(a \gets a + 1\). The cost of this operation is \(x\); \(a \gets a \oplus 1\), where \(\oplus\) denotes the bitwise XOR operation. The cost of this op...
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 four integers \(a, b, x, y\) (\(1 \le a, b \le 100, 1 \le x, y \le 10^7\)) β€” the two integers given to you and the re...
For each test case, output an integer β€” the minimum cost to make \(a = b\), or \(-1\) if it is impossible.
In the first test case, the optimal strategy is to apply \(a \gets a + 1\) three times. The total cost is \(1+1+1=3\).In the second test case, the optimal strategy is to apply \(a \gets a + 1\), \(a \gets a \oplus 1\), \(a \gets a + 1\), \(a \gets a \oplus 1\) in order. The total cost is \(2+1+2+1=6\).In the fifth test...
Input: 71 4 1 21 5 2 13 2 2 11 3 2 12 1 1 23 1 1 21 100 10000000 10000000 | Output: 3 6 1 3 -1 -1 990000000
Beginner
3
439
362
106
21
1,430
F
1430F
F. Realistic Gameplay
2,600
dp; greedy
Recently you've discovered a new shooter. They say it has realistic game mechanics.Your character has a gun with magazine size equal to \(k\) and should exterminate \(n\) waves of monsters. The \(i\)-th wave consists of \(a_i\) monsters and happens from the \(l_i\)-th moment of time up to the \(r_i\)-th moments of time...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2000\); \(1 \le k \le 10^9\)) β€” the number of waves and magazine size.The next \(n\) lines contain descriptions of waves. The \(i\)-th line contains three integers \(l_i\), \(r_i\) and \(a_i\) (\(1 \le l_i \le r_i \le 10^9\); \(1 \le a_i \le 10^9\)) β€” ...
If there is no way to clear all waves, print \(-1\). Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves.
In the first example: At the moment \(2\), the first wave occurs and \(6\) monsters spawn. You kill \(3\) monsters and start reloading. At the moment \(3\), the second wave occurs and \(3\) more monsters spawn. You kill remaining \(3\) monsters from the first wave and start reloading. At the moment \(4\), you kill rema...
Input: 2 3 2 3 6 3 4 3 | Output: 9
Expert
2
1,514
527
169
14
1,807
A
1807A
A. Plus or Minus
800
implementation
You are given three integers \(a\), \(b\), and \(c\) such that exactly one of these two equations is true: \(a+b=c\) \(a-b=c\) Output + if the first equation is true, and - otherwise.
The first line contains a single integer \(t\) (\(1 \leq t \leq 162\)) β€” the number of test cases.The description of each test case consists of three integers \(a\), \(b\), \(c\) (\(1 \leq a, b \leq 9\), \(-8 \leq c \leq 18\)). The additional constraint on the input: it will be generated so that exactly one of the two ...
For each test case, output either + or - on a new line, representing the correct equation.
In the first test case, \(1+2=3\).In the second test case, \(3-2=1\).In the third test case, \(2-9=-7\). Note that \(c\) can be negative.
Input: 111 2 33 2 12 9 -73 4 71 1 21 1 03 3 69 9 189 9 01 9 -81 9 10 | Output: + - - + + - + + - - +
Beginner
1
183
343
90
18
1,533
H
1533H
H. Submatrices
0
*special; bitmasks; data structures; dp
You are given matrix \(s\) that contains \(n\) rows and \(m\) columns. Each element of a matrix is one of the \(5\) first Latin letters (in upper case).For each \(k\) (\(1 \le k \le 5\)) calculate the number of submatrices that contain exactly \(k\) different letters. Recall that a submatrix of matrix \(s\) is a matrix...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 800\)).Then \(n\) lines follow, each containing the string \(s_i\) (\(|s_i| = m\)) β€” \(i\)-th row of the matrix.
For each \(k\) (\(1 \le k \le 5\)) print one integer β€” the number of submatrices that contain exactly \(k\) different letters.
Input: 2 3 ABB ABA | Output: 9 9 0 0 0
Beginner
4
653
183
126
15
1,313
D
1313D
D. Happy New Year
2,500
bitmasks; dp; implementation
Being Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were \(m\) children lined up in front of him. Let's number them from \(1\) to \(m\). Grandfather Frost knows \(n\) spells. The \(i\)-th spell gives a candy to every child whose place...
The first line contains three integers of \(n\), \(m\), and \(k\) (\(1 \leq n \leq 100\,000, 1 \leq m \leq 10^9, 1 \leq k \leq 8\)) β€” the number of spells, the number of children and the upper limit on the number of candy a child can get if all spells are used, respectively.This is followed by \(n\) lines, each contain...
Print a single integer β€” the maximum number of children that Santa can make happy.
In the first example, Santa should apply the first and third spell. In this case all children will be happy except the third.
Input: 3 5 31 32 43 5 | Output: 4
Expert
3
1,016
422
82
13
838
D
838D
D. Airplane Arrangements
2,700
math; number theory
There is an airplane which has n rows from front to back. There will be m people boarding this airplane.This airplane has an entrance at the very front and very back of the plane.Each person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane o...
The first line of input will contain two integers n, m (1 ≀ m ≀ n ≀ 1 000 000), the number of seats, and the number of passengers, respectively.
Print a single number, the number of ways, modulo 109 + 7.
Here, we will denote a passenger by which seat they were assigned, and which side they came from (either ""F"" or ""B"" for front or back, respectively).For example, one valid way is 3B, 3B, 3B (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F.One invalid ...
Input: 3 3 | Output: 128
Master
2
1,076
144
58
8
630
H
630H
H. Benches
1,400
combinatorics; math
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obvi...
The only line of the input contains one integer n (5 ≀ n ≀ 100) β€” the number of east to west paths and north to south paths.
Output one integer β€” the number of ways to place the benches.
Input: 5 | Output: 120
Easy
2
544
124
61
6
39
B
39B
B. Company Income Growth
1,300
greedy
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 β€” to a2 billion, ..., and in the current (2000 + n)-th...
The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers ai ( - 100 ≀ ai ≀ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.
Output k β€” the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.
Input: 10-2 1 1 3 2 3 4 -10 -2 5 | Output: 52002 2005 2006 2007 2010
Easy
1
1,309
240
240
0
1,490
B
1490B
B. Balanced Remainders
1,000
brute force; constructive algorithms; math
You are given a number \(n\) (divisible by \(3\)) and an array \(a[1 \dots n]\). In one move, you can increase any of the array elements by one. Formally, you choose the index \(i\) (\(1 \le i \le n\)) and replace \(a_i\) with \(a_i + 1\). You can choose the same index \(i\) multiple times for different moves.Let's den...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(3 \le n \le 3 \cdot 10^4\)) β€” the length of the array \(a\). It is guaranteed that the number \(n\) is divisible by \(3\).The next line contains \(n\) integers \(...
For each test case, output one integer β€” the minimum number of moves that must be made for the \(a\) array to make it have balanced remainders.
The first test case is explained in the statements.In the second test case, you need to make one move for \(i=2\).The third test case you need to make three moves: the first move: \(i=9\); the second move: \(i=9\); the third move: \(i=2\). In the fourth test case, the values \(c_0\), \(c_1\) and \(c_2\) initially equal...
Input: 4 6 0 2 5 5 4 8 6 2 0 2 1 0 0 9 7 1 3 4 2 10 3 9 6 6 0 1 2 3 4 5 | Output: 3 1 3 0
Beginner
3
1,355
458
143
14