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
400
D
400D
D. Dima and Bacteria
2,000
dsu; graphs; shortest paths
Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals ci. For convenience, we will assume that all the bacteria are numbered from 1 to n. The bacteria of type ci are nu...
The first line contains three integers n, m, k (1 ≤ n ≤ 105; 0 ≤ m ≤ 105; 1 ≤ k ≤ 500). The next line contains k integers c1, c2, ..., ck (1 ≤ ci ≤ n). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ 105; 0 ≤ xi ≤ 104). It is guaranteed that .
If Dima's type-distribution is correct, print string «Yes», and then k lines: in the i-th line print integers d[i][1], d[i][2], ..., d[i][k] (d[i][i] = 0). If there is no way to move energy from bacteria i to bacteria j appropriate d[i][j] must equal to -1. If the type-distribution isn't correct print «No».
Input: 4 4 21 32 3 03 4 02 4 12 1 2 | Output: Yes0 22 0
Hard
3
1,330
269
308
4
976
A
976A
A. Minimum Binary Number
800
implementation
String can be called correct if it consists of characters ""0"" and ""1"" and there are no redundant leading zeroes. Here are some examples: ""0"", ""10"", ""1001"".You are given a correct string s.You can perform two different operations on this string: swap any pair of adjacent characters (for example, ""101"" ""110"...
The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s.The second line contains the string s consisting of characters ""0"" and ""1"". It is guaranteed that the string s is correct.
Print one string — the minimum correct string that you can obtain from the given one.
In the first example you can obtain the answer by the following sequence of operations: ""1001"" ""1010"" ""1100"" ""100"".In the second example you can't obtain smaller answer no matter what operations you use.
Input: 41001 | Output: 100
Beginner
1
745
206
85
9
2,079
B
2079B
2,600
*special; data structures; dp; greedy
Expert
4
0
0
0
20
1,099
C
1099C
C. Postcard
1,200
constructive algorithms; implementation
Andrey received a postcard from Irina. It contained only the words ""Hello, Andrey!"", and a strange string consisting of lowercase Latin letters, snowflakes and candy canes. Andrey thought that this string is an encrypted message, and decided to decrypt it.Andrey noticed that snowflakes and candy canes always stand af...
The first line contains the string received in the postcard. The string consists only of lowercase Latin letters, as well as the characters «*» and «?», meaning snowflake and candy cone, respectively. These characters can only appear immediately after the letter. The length of the string does not exceed \(200\).The sec...
Print any message of length \(k\) that the given string can encode, or «Impossible» if such a message does not exist.
Input: hw?ap*yn?eww*ye*ar 12 | Output: happynewyear
Easy
2
1,257
415
117
10
958
C3
958C3
C3. Encryption (hard)
2,500
data structures; dp
Heidi is now just one code away from breaking the encryption of the Death Star plans. The screen that should be presenting her with the description of the next code looks almost like the previous one, though who would have thought that the evil Empire engineers would fill this small screen with several million digits! ...
The first line of the input contains three space-separated integers N, k and p (k ≤ N ≤ 500 000, 2 ≤ k ≤ 100, 2 ≤ p ≤ 100) – the number of elements in A, the number of parts A should be split into, and the modulo for computing scores, respectively.The second line contains N space-separated integers that are the element...
Output the number S as described in the problem statement.
In the first example, if the input sequence is split as (3), (4, 7), (2), the total score would be . It is easy to see that this score is the smallest possible.In the second example, one possible way to obtain score 13 is to make the following split: (16, 3), (24), (13), (9, 8), (7, 5, 12, 12).
Input: 4 3 103 4 7 2 | Output: 6
Expert
2
1,007
377
58
9
2,053
I2
2053I2
I2. Affectionate Arrays (Hard Version)
3,500
data structures; dp; graphs; greedy; math; shortest paths; two pointers
Note that this statement is different to the version used in the official round. The statement has been corrected to a solvable version. In the official round, all submissions to this problem have been removed.This is the hard version of the problem. The difference between the versions is that in this version, you need...
Each test contains multiple test cases. The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3\cdot 10^6\)) — the length of the array \(a_1, a_2, \ldo...
For each test case, output a single line containing an integer: the sum of values of valid arrays \(b_1, b_2, \ldots, b_m\), modulo \(998\,244\,353\).
In the first test case, \(a=[1, 2, 3, 4]\). The only possible array \(b\) is \([1, 2, 3, 4]\), and its value is \(1\).In the second test case, \(a=[2, -3, 2, 2]\). The possible arrays \(b\) are \([1, 2, -3, 2, -1, 2]\) and \([2, 1, -3, 2, -1, 2]\). Both arrays have value \(1\).In the third test case, \(a=[1, -2, 2, 1]\...
Input: 541 2 3 442 -3 2 241 -2 2 1102 -7 6 3 -1 4 2 -5 8 -4204 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1 | Output: 1 2 2 20 1472
Master
7
2,646
620
150
20
811
B
811B
B. Vladik and Complicated Book
1,200
implementation; sortings
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sort...
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.Each of the next...
For each mom’s sorting on it’s own line print ""Yes"", if page which is interesting to Vladik hasn't changed, or ""No"" otherwise.
Explanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is ""Yes"". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is ""No"". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is ""Yes"". [5, ...
Input: 5 55 4 3 2 11 5 31 3 12 4 34 4 42 5 3 | Output: YesNoYesYesNo
Easy
2
764
508
130
8
883
A
883A
A. Automatic Door
2,200
implementation
There is an automatic door at the entrance of a factory. The door works in the following way: when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, when one or several people come to the door and it is open, all people immediately ...
The first line contains four integers n, m, a and d (1 ≤ n, a ≤ 109, 1 ≤ m ≤ 105, 1 ≤ d ≤ 1018) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes.The second line contains integer sequence t1, t2, ..., tm (1 ≤ ti...
Print the number of times the door will open.
In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
Input: 1 1 3 47 | Output: 1
Hard
1
1,064
419
45
8
297
A
297A
A. Parity Game
1,700
constructive algorithms
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character ""0"" and ""1"") a and b. Then you try to turn a into b using two types of operations: Write parity(a)...
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters ""0"" and ""1"". Here |x| denotes the length of the string x.
Print ""YES"" (without quotes) if it is possible to turn a into b, and ""NO"" (without quotes) otherwise.
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
Input: 010110110 | Output: YES
Medium
1
640
204
105
2
1,132
F
1132F
F. Clear the String
2,000
dp
You are given a string \(s\) of length \(n\) consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbac...
The first line contains one integer \(n\) (\(1 \le n \le 500\)) — the length of string \(s\).The second line contains the string \(s\) (\(|s| = n\)) consisting of lowercase Latin letters.
Output a single integer — the minimal number of operation to delete string \(s\).
Input: 5 abaca | Output: 3
Hard
1
425
187
81
11
1,438
D
1438D
D. Powerful Ksenia
2,200
bitmasks; constructive algorithms; math
Ksenia has an array \(a\) consisting of \(n\) positive integers \(a_1, a_2, \ldots, a_n\). In one operation she can do the following: choose three distinct indices \(i\), \(j\), \(k\), and then change all of \(a_i, a_j, a_k\) to \(a_i \oplus a_j \oplus a_k\) simultaneously, where \(\oplus\) denotes the bitwise XOR oper...
The first line contains one integer \(n\) (\(3 \leq n \leq 10^5\)) — the length of \(a\).The second line contains \(n\) integers, \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) — elements of \(a\).
Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most \(n\) operations.If it is possible, print an integer \(m\) (\(0 \leq m \leq n\)), which denotes the number of operations you do.In each of the next \(m\) lines, print three distinct integers \(i, j, k\), represen...
In the first example, the array becomes \([4 \oplus 1 \oplus 7, 2, 4 \oplus 1 \oplus 7, 4 \oplus 1 \oplus 7, 2] = [2, 2, 2, 2, 2]\).
Input: 5 4 2 1 7 2 | Output: YES 1 1 3 4
Hard
3
493
203
467
14
1,540
C2
1540C2
C2. Converging Array (Hard Version)
2,900
dp; math
This is the hard version of the problem. The only difference is that in this version \(1 \le q \le 10^5\). You can make hacks only if both versions of the problem are solved.There is a process that takes place on arrays \(a\) and \(b\) of length \(n\) and length \(n-1\) respectively. The process is an infinite sequence...
The first line contains a single integer \(n\) (\(2 \le n \le 100\)).The second line contains \(n\) integers \(c_1, c_2 \ldots, c_n\) (\(0 \le c_i \le 100\)).The third line contains \(n-1\) integers \(b_1, b_2, \ldots, b_{n-1}\) (\(0 \le b_i \le 100\)).The fourth line contains a single integer \(q\) (\(1 \le q \le 10^5...
Output \(q\) integers, where the \(i\)-th integer is the answer to the \(i\)-th query, i. e. the number of good arrays \(a\) where \(F(a, b) \geq x_i\) modulo \(10^9+7\).
The following explanation assumes \(b = [2, 1]\) and \(c=[2, 3, 4]\) (as in the sample).Examples of arrays \(a\) that are not good: \(a = [3, 2, 3]\) is not good because \(a_1 > c_1\); \(a = [0, -1, 3]\) is not good because \(a_2 < 0\). One possible good array \(a\) is \([0, 2, 4]\). We can show that no operation has a...
Input: 3 2 3 4 2 1 5 -1 0 1 -100000 100000 | Output: 56 28 4 60 0
Master
2
1,266
434
170
15
2,052
G
2052G
G. Geometric Balance
2,800
data structures; geometry; implementation
Peter's little brother Ivan likes to play with a turtle. The turtle is a special toy that lives on the plane and can execute three commands: Rotate \(a\) degrees counterclockwise. Draw \(d\) units in the direction it is facing while dispensing ink. No segment of the plane will be covered by ink more than once. Move \(d...
The first line of the input contains a single integer \(n\;(1 \le n \le 50000)\) — the number of commands Ivan has given.The next \(n\) lines contain commands. Each command is one of: ""rotate \(a\)"" (\(45 \le a \le 360\)) where \(a\) is divisible by \(45\); ""draw \(d\)"" (\(1 \le d \le 10^9\)); ""move \(d\)"" (\(1 \...
Output a single number, the answer to the question. The answer always exists.
Input: 1draw 10 | Output: 180
Master
3
1,176
478
77
20
1,804
F
1804F
F. Approximate Diameter
2,700
binary search; divide and conquer; graphs; shortest paths
Jack has a graph of \(n\) vertices and \(m\) edges. All edges are bidirectional and of unit length. The graph is connected, i. e. there exists a path between any two of its vertices. There can be more than one edge connecting the same pair of vertices. The graph can contain self-loops, i. e. edges connecting a node to ...
The first line of the input contains three integers \(n\), \(m\), and \(q\) (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^5\), \(0 \leq q \leq 10^5\)), the number of vertices in the given graph, the number of edges and the number of updates, respectively.Then follow \(m\) lines describing the initial edges of the gra...
Print a sequence of \(q + 1\) positive integers \(a_0, a_1, a_2, \ldots, a_q\). The \(i\)-th of these integers should differ from the diameter of graph \(G_i\) no more than twice.
In the first example, the correct sequence of \(d(G_0), d(G_1), d(G_2), \ldots, d(G_q)\) is \(6, 6, 6, 3, 3, 3, 2, 2, 2\). In the second example, the input contains an extra line that you can omit reading. It is not a part of the test and will be used for verifying your answer. The output of the second example contains...
Input: 9 10 8 1 2 2 3 2 4 3 5 4 5 5 6 5 7 6 8 7 8 8 9 3 4 6 7 2 8 1 9 1 6 4 9 3 9 7 1 | Output: 10 6 5 6 2 4 2 2 1
Master
4
1,383
1,016
179
18
67
E
67E
E. Save the City!
2,500
geometry
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro...
The first line of the test case will consist of the number of vertices n (3 ≤ n ≤ 1000).Next n lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the i-th line are integers xi and yi (0 ≤ xi, yi ≤ 106) separated by a space.The endpoints of the fence AB are the first two points,...
Output consists of a single line containing the number of points where the watchtower can be set up.
Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence AB. Since, AB has 5 integer coordinates, so answer is 5.For case two, fence CD and DE are not completely visible, thus answer is 0.
Input: 54 88 89 44 00 4 | Output: 5
Expert
1
1,861
343
100
0
788
C
788C
C. The Great Mixing
2,300
dfs and similar; graphs; shortest paths
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so...
The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.
In the first sample case, we can achieve concentration using one liter of Coke of types and : .In the second case, we can achieve concentration using two liters of type and one liter of type: .
Input: 400 4100 300 450 500 | Output: 2
Expert
3
912
312
130
7
1,211
H
1211H
H. Road Repair in Treeland
3,100
*special; binary search; dp; trees
There are \(n\) cities and \(n-1\) two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from \(1\) to \(n\). Yes, of course, you recognized an undirected tree in this description.The government plans to repai...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of input cases in the input. Next, the input cases themselves are given. The first line of each test case set contains an integer \(n\) (\(2 \le n \le 3000\)) — the number of cities in Treeland. Next, in the \(n-1\) lines the roads are written...
Print the answers for all \(t\) test cases in the input. Each test case must begin with a line that contains \(r\) — the minimum possible number of roads assigned to the most used company. Next, in the next line print \(n-1\) the number \(c_1, c_2, \dots, c_{n-1}\) (\(1 \le c_i \le 10^6\)), where \(c_i\) indicates the ...
Input: 3 3 1 2 2 3 6 1 2 1 3 1 4 1 5 1 6 7 3 1 1 4 4 6 5 1 2 4 1 7 | Output: 1 10 20 3 1 1 1 2 2 2 11 11 12 13 12 13
Master
4
1,458
579
417
12
730
K
730K
K. Roads Orientation Problem
3,200
graphs
Berland consists of n cities and m bidirectional roads connecting pairs of cities. There is no road connecting a city to itself, and between any pair of cities there is no more than one road. It is possible to reach any city from any other moving along roads.Currently Mr. President is in the city s and his destination ...
Each test in this problem contains one or more test cases to solve. The first line of the input contains positive number T — the number of cases to solve.Each case starts with a line containing four integers n, m, s and t (2 ≤ n ≤ 4·105, 1 ≤ m ≤ 106, 1 ≤ s, t ≤ n, s ≠ t) — the number of cities, the number of roads and ...
For each case print ""Yes"" if the answer exists. In the following m lines print roads in the required directions. You can print roads in arbitrary order. If there are multiple answers, print any of them.Print the only line ""No"" if there is no answer for a case.
Input: 24 4 1 21 22 33 44 13 2 1 33 12 3 | Output: Yes1 23 24 31 4No
Master
1
1,283
843
264
7
2,037
C
2037C
C. Superultra's Favorite Permutation
1,000
constructive algorithms; greedy; math; number theory
Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra!Construct a permutation\(^{\text{∗}}\) \(p\) of length \(n\) such that \(p_i + p_{i+1}\) is composite\(^{\text{†}}\) over all \(1 ...
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, if it's not possible to construct \(p\), output \(-1\) on a new line. Otherwise, output \(n\) integers \(p_1, p_2, \ldots, p_n\) on a new line.
In the first example, it can be shown that all permutation of size \(3\) contain two adjacent elements whose sum is prime. For example, in the permutation \([2,3,1]\) the sum \(2+3=5\) is prime.In the second example, we can verify that the sample output is correct because \(1+8\), \(8+7\), \(7+3\), \(3+6\), \(6+2\), \(...
Input: 238 | Output: -1 1 8 7 3 6 2 4 5
Beginner
4
882
280
163
20
1,140
B
1140B
B. Good String
1,200
implementation; strings
You have a string \(s\) of length \(n\) consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose w...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) – the number of test cases. Each test case is represented by two lines.The first line of \(i\)-th test case contains one integer \(n\) (\(1 \le n \le 100\)) – the length of string \(s\).The second line of \(i\)-th test case contains string \(s\), consistin...
For each test case print one line.For \(i\)-th test case print the minimum number of characters to be deleted from string \(s\) so that it becomes good.
In the first test case we can delete any character in string <>.In the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < \(\rightarrow\) < < \(\rightarrow\) <.
Input: 3 2 <> 3 ><< 1 > | Output: 1 0 0
Easy
2
1,110
349
152
11
1,946
A
1946A
A. Median of an Array
800
greedy; implementation; sortings
You are given an array \(a\) of \(n\) integers.The median of an array \(q_1, q_2, \ldots, q_k\) is the number \(p_{\lceil \frac{k}{2} \rceil}\), where \(p\) is the array \(q\) sorted in non-decreasing order. For example, the median of the array \([9, 5, 1, 2, 6]\) is \(5\), as in the sorted array \([1, 2, 5, 6, 9]\), t...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array \(a\).The second l...
For each test case, output a single integer — the minimum number of operations required to increase the median of the array.
In the first test case, you can apply one operation to the first number and obtain the array \([3, 2, 8]\), the median of this array is \(3\), as it is the number at index \(\lceil \frac{3}{2} \rceil = 2\) in the non-decreasing sorted array \([2, 3, 8]\). The median of the original array \([2, 2, 8]\) is \(2\), as it i...
Input: 832 2 847 3 3 11100000000055 5 5 4 562 1 2 3 1 421 221 145 5 5 5 | Output: 1 2 1 3 2 1 2 3
Beginner
3
822
541
124
19
1,362
C
1362C
C. Johnny and Another Rating Drop
1,400
bitmasks; greedy; math
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants...
The input consists of multiple test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 10\,000\)) — the number of test cases. The following \(t\) lines contain a description of test cases.The first and only line in each test case contains a single integer \(n\) (\(1 \leq n \leq 10^{18})\).
Output \(t\) lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to \(0\), \(1\), ..., \(n - 1\), \(n\).
For \(n = 5\) we calculate unfairness of the following sequence (numbers from \(0\) to \(5\) written in binary with extra leading zeroes, so they all have the same length): \(000\) \(001\) \(010\) \(011\) \(100\) \(101\) The differences are equal to \(1\), \(2\), \(1\), \(3\), \(1\) respectively, so unfairness is equal...
Input: 5 5 7 11 1 2000000000000 | Output: 8 11 19 1 3999999999987
Easy
3
1,231
305
190
13
335
E
335E
E. Counting Skyscrapers
2,800
dp; math; probabilities
A number of skyscrapers have been built in a line. The number of skyscrapers was chosen uniformly at random between 2 and 314! (314 factorial, a very large number). The height of each skyscraper was chosen randomly and independently, with height i having probability 2 - i for all positive integers i. The floors of a sk...
The first line of input will be a name, either string ""Alice"" or ""Bob"". The second line of input contains two integers n and h (2 ≤ n ≤ 30000, 0 ≤ h ≤ 30). If the name is ""Alice"", then n represents the value of Alice's counter when she reaches the rightmost skyscraper, otherwise n represents the value of Bob's co...
Output a single real value giving the expected value of the Alice's counter if you were given Bob's counter, or Bob's counter if you were given Alice's counter. You answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
In the first example, Bob's counter has a 62.5% chance of being 3, a 25% chance of being 4, and a 12.5% chance of being 5.
Input: Alice3 1 | Output: 3.500000000
Master
3
2,448
428
255
3
269
D
269D
D. Maximum Waterfall
2,600
data structures; dp; graphs; sortings
Emuskald was hired to design an artificial waterfall according to the latest trends in landscape architecture. A modern artificial waterfall consists of multiple horizontal panels affixed to a wide flat wall. The water flows down the top of the wall from panel to panel until it reaches the bottom of the wall.The wall h...
The first line of input contains two space-separated integers n and t (1 ≤ n ≤ 105, 2 ≤ t ≤ 109), the number of the panels excluding the top and the bottom panels, and the height of the wall. Each of the n following lines contain three space-separated integers hi, li and ri (0 < hi < t, - 109 ≤ li < ri ≤ 109), the heig...
Output a single integer — the maximum possible amount of water flow in the desired waterfall.
The first test case corresponds to the picture.
Input: 5 64 1 63 2 75 9 113 10 151 13 16 | Output: 4
Expert
4
2,028
429
93
2
1,881
E
1881E
E. Block Sequence
1,500
dp
Given a sequence of integers \(a\) of length \(n\).A sequence is called beautiful if it has the form of a series of blocks, each starting with its length, i.e., first comes the length of the block, and then its elements. For example, the sequences [\(\color{red}{3},\ \color{red}{3},\ \color{red}{4},\ \color{red}{5},\ \...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the sequence \(a\).The second line of each test c...
For each test case, output a single number — the minimum number of deletions required to make the sequence \(a\) beautiful.
In the first test case of the example, the given sequence is already beautiful, as shown in the statement.In the second test case of the example, the sequence can only be made beautiful by removing all elements from it.In the fifth test case of the example, the sequence can be made beautiful by removing the first and l...
Input: 773 3 4 5 2 6 145 6 3 263 4 1 6 7 731 4 351 2 3 4 551 2 3 1 254 5 5 1 5 | Output: 0 4 1 1 2 1 0
Medium
1
779
541
123
18
720
D
720D
D. Slalom
3,100
data structures; dp; sortings
Little girl Masha likes winter sports, today she's planning to take part in slalom skiing.The track is represented as a grid composed of n × m squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1, 1) to the square (n, m). She can move from a square to adjace...
The first line of input data contains three positive integers: n, m and k (3 ≤ n, m ≤ 106, 0 ≤ k ≤ 105) — the size of the track and the number of obstacles.The following k lines contain four positive integers each: x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — coordinates of bottom left, and top right squares of ...
Output one integer — the number of ways to pass the track modulo 109 + 7.
Input: 3 3 0 | Output: 1
Master
3
1,053
463
73
7
1,854
D
1854D
D. Michael and Hotel
3,000
binary search; interactive; trees
Michael and Brian are stuck in a hotel with \(n\) rooms, numbered from \(1\) to \(n\), and need to find each other. But this hotel's doors are all locked and the only way of getting around is by using the teleporters in each room. Room \(i\) has a teleporter that will take you to room \(a_i\) (it might be that \(a_i = ...
The input contains a single integer \(n\) (\(2 \leq n \leq 500\)).
In the sample test, there are \(n=5\) rooms and the (hidden) array describing the behavior of the teleporters is \([1, 2, 1, 3, 2]\). The first query asks whether starting from room number \(a=3\), and using the teleporters \(5\) times, one ends up in one of the two rooms \(S=\{2, 3\}\). This action results in ending u...
Input: 5 0 1 | Output: ? 3 5 2 2 3 ? 2 5 2 2 3 ! 3 1 3 4
Master
3
1,019
66
0
18
637
A
637A
A. Voting for Photos
1,000
*special; constructive algorithms; implementation
After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the win...
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.
Print the identifier of the photo which won the elections.
In the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). Thus, the winner is the photo with identifier 2, as it got: more likes than the photo with id 3; as many likes as the photo with id 1, but the photo ...
Input: 51 3 2 2 1 | Output: 2
Beginner
3
426
262
58
6
1,326
A
1326A
A. Bad Ugly Numbers
1,000
constructive algorithms; number theory
You are given a integer \(n\) (\(n > 0\)). Find any integer \(s\) which satisfies these conditions, or report that there are no such numbers:In the decimal representation of \(s\): \(s > 0\), \(s\) consists of \(n\) digits, no digit in \(s\) equals \(0\), \(s\) is not divisible by any of it's digits.
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 400\)), the number of test cases. The next \(t\) lines each describe a test case.Each test case contains one positive integer \(n\) (\(1 \leq n \leq 10^5\)).It is guaranteed that the sum of \(n\) for ...
For each test case, print an integer \(s\) which satisfies the conditions described above, or ""-1"" (without quotes), if no such number exists. If there are multiple possible solutions for \(s\), print any solution.
In the first test case, there are no possible solutions for \(s\) consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: \(23\), \(27\), \(29\), \(34\), \(37\), \(38\), \(43\), \(46\), \(47\), \(49\), \(53\), \(54\), \(56\), \(57\), \(58\), \(59\)...
Input: 4 1 2 3 4 | Output: -1 57 239 6789
Beginner
2
301
360
216
13
1,559
B
1559B
B. Mocha and Red and Blue
900
dp; greedy
As their story unravels, a timeless tale is told once again...Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to sol...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Each test case consists of two lines.The first line of each test case contains an integer \(n\) (\(1\leq n\leq 100\)) — the length of the squares row.The second line of each test case...
For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.
In the first test case, if the squares are painted ""BRRBRBR"", the imperfectness is \(1\) (since squares \(2\) and \(3\) have the same color), which is the minimum possible imperfectness.
Input: 5 7 ?R???BR 7 ???R??? 1 ? 1 B 10 ?R??RB??B? | Output: BRRBRBR BRBRBRB B B BRRBRBBRBR
Beginner
2
1,034
488
203
15
1,115
G2
1115G2
G2. OR oracle
1,600
*special
Implement a quantum oracle on \(N\) qubits which implements the following function: $$$\(f(\vec{x}) = x_0 \vee x_1 \vee \dotsb \vee x_{N-1}\)\(You have to implement an operation which takes the following inputs: an array of \)N\( (\)1 \le N \le 8\() qubits \)x\( in an arbitrary state (input register), a qubit \)y\( in ...
Medium
1
778
0
0
11
1,477
C
1477C
C. Nezzar and Nice Beatmap
2,200
constructive algorithms; geometry; greedy; math; sortings
Nezzar loves the game osu!.osu! is played on beatmaps, which can be seen as an array consisting of distinct points on a plane. A beatmap is called nice if for any three consecutive points \(A,B,C\) listed in order, the angle between these three points, centered at \(B\), is strictly less than \(90\) degrees. Points \(A...
The first line contains a single integer \(n\) (\(3 \le n \le 5000\)).Then \(n\) lines follow, \(i\)-th of them contains two integers \(x_i\), \(y_i\) (\(-10^9 \le x_i, y_i \le 10^9\)) — coordinates of point \(A_i\).It is guaranteed that all points are distinct.
If there is no solution, print \(-1\).Otherwise, print \(n\) integers, representing a valid permutation \(p\).If there are multiple possible answers, you can print any.
Here is the illustration for the first test: Please note that the angle between \(A_1\), \(A_2\) and \(A_5\), centered at \(A_2\), is treated as \(0\) degrees. However, angle between \(A_1\), \(A_5\) and \(A_2\), centered at \(A_5\), is treated as \(180\) degrees.
Input: 5 0 0 5 0 4 2 2 1 3 0 | Output: 1 2 5 3 4
Hard
5
949
262
168
14
101
D
101D
D. Castle
2,300
dp; greedy; probabilities; sortings; trees
Gerald is positioned in an old castle which consists of n halls connected with n - 1 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Gerald i...
The first line contains the only integer n (2 ≤ n ≤ 105) — the number of halls in the castle. Next n - 1 lines each contain three integers. The i-th line contains numbers ai, bi and ti (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ti ≤ 1000) — the numbers of halls connected with the i-th corridor and the time needed to go along the co...
Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10 - 6.
In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one.In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to the ...
Input: 21 2 1 | Output: 1.0
Expert
5
1,786
451
155
1
1,820
A
1820A
A. Yura's New Name
800
implementation; strings
After holding one team contest, boy Yura got very tired and wanted to change his life and move to Japan. In honor of such a change, Yura changed his name to something nice.Fascinated by this idea he already thought up a name \(s\) consisting only of characters ""_"" and ""^"". But there's a problem — Yura likes smiley ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) —the number of test cases. The description of test cases follows.The first and only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 100\)), consisting of characters ""_"" and ""^"", —...
For each test case, output a single integer — the minimum number of characters you need to add to the name to make it fit for Yura. If you don't need to change anything in the name, print \(0\).
In the first test case, you can get the following name by adding \(5\) characters: ^_^_^_^_^_^_^In the third test case, we can add one character ""^"" to the end of the name, then we get the name:^_^In the fourth test case, we can add one character ""^"" to the end of the name, then we get the name:^^In the fifth test ...
Input: 7^______^___^_^^^_^___^^_^^_^^^^^_^_^^___^^_ | Output: 5 5 1 1 0 3 2
Beginner
2
1,161
340
194
18
1,738
H
1738H
H. Palindrome Addicts
3,300
data structures; strings
Your task is to maintain a queue consisting of lowercase English letters as follows: ""push \(c\)"": insert a letter \(c\) at the back of the queue; ""pop"": delete a letter from the front of the queue. Initially, the queue is empty. After each operation, you are asked to count the number of distinct palindromic substr...
The first line is an integer \(q\) (\(1 \leq q \leq 10^6\)), indicating the number of operations.Then \(q\) lines follow. Each of the following lines contains one of the operations as follows. ""push \(c\)"": insert a letter \(c\) at the back of the queue, where \(c\) is a lowercase English letter; ""pop"": delete a le...
After each operation, print the number of distinct palindromic substrings in the string presented in the queue.
Let \(s_k\) be the string presented in the queue after the \(k\)-th operation, and let \(c_k\) be the number of distinct palindromic substrings of \(s_k\). The following table shows the details of the example. \(k\)\(s_k\)\(c_k\)\(1\)\(a\)\(1\)\(2\)\(\textsf{empty}\)\(0\)\(3\)\(a\)\(1\)\(4\)\(aa\)\(2\)\(5\)\(aab\)\(3\)...
Input: 12 push a pop push a push a push b push b push a push a pop pop pop push b | Output: 1 0 1 2 3 4 5 6 5 4 3 4
Master
2
884
439
111
17
2,061
G
2061G
G. Kevin and Teams
2,900
constructive algorithms; graphs; interactive
This is an interactive problem. Kevin has \(n\) classmates, numbered \(1, 2, \ldots, n\). Any two of them may either be friends or not friends.Kevin wants to select \(2k\) classmates to form \(k\) teams, where each team contains exactly \(2\) people. Each person can belong to at most one team.Let \(u_i\) and \(v_i\) be...
In the first test case:Kevin claims he can form \(1\) team regardless of the friendship relationships among the \(3\) people.Kevin asks about the friendship relationship between people \(1\) and \(2\). The jury responds that they are friends.Kevin answers that he can form a team with people \(1\) and \(2\).In the secon...
Input: 2 3 1 5 1 0 1 0 0 | Output: 1 ? 1 2 ! 1 2 2 ? 1 2 ? 3 4 ? 3 5 ? 1 3 ? 2 4 ! 1 2 3 5
Master
3
1,182
0
0
20
472
F
472F
F. Design Tutorial: Change the Goal
2,700
constructive algorithms; math; matrices
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topco...
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
If there is no solution, output -1.If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj.If there are multiple solutions you ...
Assignment a ^= b denotes assignment a = a ^ b, where operation ""^"" is bitwise XOR of two integers.
Input: 23 56 0 | Output: 21 22 2
Master
3
814
176
481
4
1,844
D
1844D
D. Row Major
1,400
constructive algorithms; greedy; math; number theory; strings
The row-major order of an \(r \times c\) grid of characters \(A\) is the string obtained by concatenating all the rows, i.e. $$$\( A_{11}A_{12} \dots A_{1c}A_{21}A_{22} \dots A_{2c} \dots A_{r1}A_{r2} \dots A_{rc}. \)\(A grid of characters \)A\( is bad if there are some two adjacent cells (cells sharing an edge) with t...
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 a single integer \(n\) (\(1 \le n \le 10^6\)).It is guaranteed that the sum of \(n\) over all test cases does not exce...
For each test case, output a string with the minimum number of distinct characters among all suitable strings of length \(n\).If there are multiple solutions, print any of them.
In the first test case, there are \(3\) ways \(s\) can be the row-major order of a grid, and they are all not bad: tththathatat It can be proven that \(3\) distinct characters is the minimum possible.In the second test case, there are \(2\) ways \(s\) can be the row-major order of a grid, and they are both not bad: iis...
Input: 44216 | Output: that is a tomato
Easy
5
702
332
177
18
1,797
C
1797C
C. Li Hua and Chess
1,600
constructive algorithms; greedy; interactive
This is an interactive problem.Li Ming and Li Hua are playing a game. Li Hua has a chessboard of size \(n\times m\). Denote \((r, c)\) (\(1\le r\le n, 1\le c\le m\)) as the cell on the \(r\)-th row from the top and on the \(c\)-th column from the left. Li Ming put a king on the chessboard and Li Hua needs to guess its ...
In test case 1, the king is at \((2,2)\). It takes \(1\) step to move to \((2,3)\) and \(2\) steps to move to \((2,4)\). Note that the questions may not seem sensible. They are just a sample of questions you may ask.
Input: 2 3 4 1 2 5 3 3 1 2 | Output: ? 2 3 ? 2 4 ! 2 2 ? 2 2 ? 5 2 ? 5 3 ! 5 1
Medium
3
796
0
0
17
1,680
B
1680B
B. Robots
800
implementation
There is a field divided into \(n\) rows and \(m\) columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R).You can send a command to all robots at the same time. The command can be of one of the four types: move up; move right; move down; move left. When you send a command, all robots at...
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases.Each test case starts with a line containing two integers \(n\) and \(m\) (\(1 \le n, m \le 5\)) — the number of rows and the number of columns, respectively. Then \(n\) lines follow; each of them contains a string of \(m\) char...
If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.
Explanations for test cases of the example: in the first test case, it is enough to send a command to move left. in the second test case, if you try to send any command, at least one robot explodes. in the third test case, it is enough to send a command to move left. in the fourth test case, there is already a robot in...
Input: 61 3ERR2 2ERRE2 2ERER1 1R4 3EEEEEEERREER3 3EEEEERREE | Output: YES NO YES YES YES NO
Beginner
1
736
477
144
16
1,656
F
1656F
F. Parametric MST
2,600
binary search; constructive algorithms; graphs; greedy; math; sortings
You are given \(n\) integers \(a_1, a_2, \ldots, a_n\). For any real number \(t\), consider the complete weighted graph on \(n\) vertices \(K_n(t)\) with weight of the edge between vertices \(i\) and \(j\) equal to \(w_{ij}(t) = a_i \cdot a_j + t \cdot (a_i + a_j)\). Let \(f(t)\) be the cost of the minimum spanning tre...
The input consists of multiple test cases. The first line contains a single integer \(T\) (\(1 \leq T \leq 10^4\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the number of vertices of the graph.The secon...
For each test case, print a single line with the maximum value of \(f(t)\) (it can be shown that it is an integer), or INF if \(f(t)\) is not bounded above.
Input: 521 02-1 131 -1 -233 -1 -241 2 3 -4 | Output: INF -1 INF -6 -18
Expert
6
429
490
156
16
23
C
23C
C. Oranges and Apples
2,500
constructive algorithms; sortings
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't...
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with o...
Input: 2210 155 720 1810 0 | Output: YES1 3YES1
Expert
2
183
367
329
0
630
F
630F
F. Selection of Personnel
1,300
combinatorics; math
One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the n...
The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.
Output one integer — the number of different variants of group composition.
Input: 7 | Output: 29
Easy
2
371
118
75
6
1,740
F
1740F
F. Conditional Mix
2,600
combinatorics; dp; math
Pak Chanek is given an array \(a\) of \(n\) integers. For each \(i\) (\(1 \leq i \leq n\)), Pak Chanek will write the one-element set \(\{a_i\}\) on a whiteboard.After that, in one operation, Pak Chanek may do the following: Choose two different sets \(S\) and \(T\) on the whiteboard such that \(S \cap T = \varnothing\...
The first line contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)).
Output the number of distinct multisets \(M\) modulo \(998\,244\,353\).
In the first example, the possible multisets \(M\) are \(\{1,1,1,1,1,1\}\), \(\{1,1,1,1,2\}\), \(\{1,1,1,3\}\), \(\{1,1,2,2\}\), \(\{1,1,4\}\), \(\{1,2,3\}\), and \(\{2,2,2\}\).As an example, let's consider a possible sequence of operations. In the beginning, the sets are \(\{1\}\), \(\{1\}\), \(\{2\}\), \(\{1\}\), \(\...
Input: 6 1 1 2 1 4 3 | Output: 7
Expert
3
1,092
160
71
17
1,921
E
1921E
E. Eat the Chip
1,600
brute force; games; greedy; math
Alice and Bob are playing a game on a checkered board. The board has \(h\) rows, numbered from top to bottom, and \(w\) columns, numbered from left to right. Both players have a chip each. Initially, Alice's chip is located at the cell with coordinates \((x_a, y_a)\) (row \(x_a\), column \(y_a\)), and Bob's chip is loc...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. This is followed by the description of the test cases.Each test case consists of a single line containing six integers \(h\), \(w\), \(x_a\), \(y_a\), \(x_b\), \(y_b\) (\(1 \le x_...
For each test case, output ""Alice"" if Alice wins, ""Bob"" if Bob wins, and ""Draw"" if neither player can secure a victory. You can output each letter in any case (lowercase or uppercase). For example, the strings ""bOb"", ""bob"", ""Bob"", and ""BOB"" will be accepted as Bob's victory.
Input: 126 5 2 2 5 34 1 2 1 4 11 4 1 3 1 15 5 1 4 5 24 4 1 1 4 410 10 1 6 10 810 10 2 6 10 710 10 9 1 8 110 10 8 1 10 210 10 1 1 2 110 10 1 3 4 110 10 3 1 1 1 | Output: Alice Bob Draw Draw Draw Alice Draw Draw Bob Alice Alice Draw
Medium
4
1,625
612
289
19
645
E
645E
E. Intellectual Inquiry
2,200
dp; greedy; strings
After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.Each morning, Bessie travels to school along a sidewalk consisting of m + n t...
The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 000, 1 ≤ k ≤ 26).The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.
Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first k lowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.Please note, that you are not asked to maximize the remainder modulo 109 + 7! T...
In the first sample, the optimal labeling gives 8 different subsequences: """" (the empty string), ""a"", ""c"", ""b"", ""ac"", ""ab"", ""cb"", and ""acb"". In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: """" (the empty string), ""a"", ""b"", ""aa"", ""ab"", ""...
Input: 1 3ac | Output: 8
Hard
3
1,204
208
390
6
527
D
527D
D. Clique Problem
1,800
data structures; dp; greedy; implementation; sortings
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? No...
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Print a single number — the number of vertexes in the maximum clique of the given graph.
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!The picture for the sample test.
Input: 42 33 16 10 2 | Output: 3
Medium
5
921
226
88
5
2,060
E
2060E
E. Graph Composition
1,500
dfs and similar; dsu; graphs; greedy
You are given two simple undirected graphs \(F\) and \(G\) with \(n\) vertices. \(F\) has \(m_1\) edges while \(G\) has \(m_2\) edges. You may perform one of the following two types of operations any number of times: Select two integers \(u\) and \(v\) (\(1 \leq u,v \leq n\)) such that there is an edge between \(u\) an...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of independent test cases.The first line of each test case contains three integers \(n\), \(m_1\), and \(m_2\) (\(1 \leq n \leq 2\cdot 10^5\), \(0 \leq m_1,m_2 \leq 2\cdot 10^5\)) — the number of vertices, the number of edges in \(F\), and t...
For each test case, output a single integer denoting the minimum operations required on a new line.
In the first test case you can perform the following three operations: Add an edge between vertex \(1\) and vertex \(3\). Remove the edge between vertex \(1\) and vertex \(2\). Remove the edge between vertex \(2\) and vertex \(3\). It can be shown that fewer operations cannot be achieved.In the second test case, \(F\) ...
Input: 53 2 11 22 31 32 1 11 21 23 2 03 21 21 0 03 3 11 21 32 31 2 | Output: 3 0 2 0 2
Medium
4
771
903
99
20
2,030
D
2030D
D. QED's Favorite Permutation
1,700
data structures; implementation; sortings
QED is given a permutation\(^{\text{∗}}\) \(p\) of length \(n\). He also has a string \(s\) of length \(n\) containing only characters \(\texttt{L}\) and \(\texttt{R}\). QED only likes permutations that are sorted in non-decreasing order. To sort \(p\), he can select any of the following operations and perform them any...
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(3 \leq n \leq 2 \cdot 10^5\), \(1 \leq q \leq 2 \cdot 10^5\)) – the length of the permutation and the number of queries.The following line contains \(n\) integers \...
For each query, output ""YES"" (without quotes) if it is possible, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
In the first testcase, \(s = \texttt{RRRLL}\) after the first query. QED may sort \(p\) using the following operations: Initially, \(p = [1,4,2,5,3]\). Select \(i = 2\) and swap \(p_2\) with \(p_{3}\). Now, \(p = [1,2,4,5,3]\). Select \(i = 5\) and swap \(p_5\) with \(p_{4}\). Now, \(p = [1,2,4,3,5]\). Select \(i = 4\)...
Input: 35 31 4 2 5 3RLRLL2438 51 5 2 4 8 3 6 7RRLLRRRL435346 21 2 3 4 5 6RLRLRL45 | Output: YES YES NO NO YES NO NO NO YES YES
Medium
3
1,406
875
245
20
597
B
597B
B. Restaurant
1,600
dp; greedy; sortings
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).Restaurant management can accept and reject orders. What is the maximal number of orders the re...
The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).
Print the maximal number of orders that can be accepted.
Input: 27 114 7 | Output: 1
Medium
3
497
157
56
5
1,977
C
1977C
C. Nikita and LCM
1,900
brute force; data structures; dp; greedy; math; number theory; sortings
Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.Suppose Nikita has an array of integers \(a\) of length \(n\). He will call a subsequence\(^\dagger\) of the array special if its least common multiple (LCM) is not contained in \(a\). The L...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2000\)) — 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 2000\)) — the length of the array \(a\).The second ...
For each test case, output a single integer — the length of the longest special subsequence of \(a\).
In the first test case, the LCM of any non-empty subsequence is contained in \(a\), so the answer is \(0\).In the second test case, we can take the subsequence \([3, 2, 10, 1]\), its LCM is equal to \(30\), which is not contained in \(a\).In the third test case, we can take the subsequence \([2, 3, 6, 100\,003]\), its ...
Input: 651 2 4 8 1663 2 10 20 60 172 3 4 6 12 100003 120003692 42 7 3 6 7 7 1 684 99 57 179 10203 2 11 4081211 | Output: 0 4 4 5 8 0
Hard
7
747
536
101
19
313
A
313A
A. Ilya and Bank Account
900
implementation; number theory
Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account ow...
The single line contains integer n (10 ≤ |n| ≤ 109) — the state of Ilya's bank account.
In a single line print an integer — the maximum state of the bank account that Ilya can get.
In the first test sample Ilya doesn't profit from using the present.In the second test sample you can delete digit 1 and get the state of the account equal to 0.
Input: 2230 | Output: 2230
Beginner
2
1,072
87
92
3
1,186
A
1186A
A. Vus the Cossack and a Contest
800
implementation
Vus the Cossack holds a programming competition, in which \(n\) people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly \(m\) pens and \(k\) notebooks.Determine whether the Cossack can reward all participants, giving each of them at least one pen and at least one noteb...
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m, k \leq 100\)) — the number of participants, the number of pens, and the number of notebooks respectively.
Print ""Yes"" if it possible to reward all the participants. Otherwise, print ""No"".You can print each letter in any case (upper or lower).
In the first example, there are \(5\) participants. The Cossack has \(8\) pens and \(6\) notebooks. Therefore, he has enough pens and notebooks.In the second example, there are \(3\) participants. The Cossack has \(9\) pens and \(3\) notebooks. He has more than enough pens but only the minimum needed number of notebook...
Input: 5 8 6 | Output: Yes
Beginner
1
324
184
140
11
1,948
A
1948A
A. Special Characters
800
brute force; constructive algorithms
You are given an integer \(n\).Your task is to build a string of uppercase Latin letters. There must be exactly \(n\) special characters in this string. Let's call a character special if it is equal to exactly one of its neighbors.For example, there are \(6\) special characters in the AAABAACC string (at positions: \(1...
The first line contains a single integer \(t\) (\(1 \le t \le 50\)) — the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)).
For each test case, print the answer as follows: if there is no suitable string, print one line containing the string NO; otherwise, print two lines. The first line should contain the string YES; on the second line print a string of length at most \(200\) — the answer itself (it can be shown that if some answers exist,...
Input: 3612 | Output: YES AAABAACC NO YES MM
Beginner
2
427
180
423
19
181
B
181B
B. Number of Triplets
1,300
binary search; brute force
You are given n points on a plane. All points are different.Find the number of different groups of three points (A, B, C) such that point B is the middle of segment AC. The groups of three points are considered unordered, that is, if point B is the middle of segment AC, then groups (A, B, C) and (C, B, A) are considere...
The first line contains a single integer n (3 ≤ n ≤ 3000) — the number of points. Next n lines contain the points. The i-th line contains coordinates of the i-th point: two space-separated integers xi, yi ( - 1000 ≤ xi, yi ≤ 1000).It is guaranteed that all given points are different.
Print the single number — the answer to the problem.
Input: 31 12 23 3 | Output: 1
Easy
2
331
284
52
1
162
D
162D
D. Remove digits
1,900
*special
You are given a string. Remove all digits from it. When a character is removed from a string, all characters to the right of it are shifted one position to the left.
The only line of input contains a string between 1 and 100 characters long. Each character of the string has ASCII-code between 33 (exclamation mark) and 126 (tilde), inclusive.
Output the given string with all digits removed from it. If the original string had only digits, output an empty string.
Input: VK-Cup-2012! | Output: VK-Cup-!
Hard
1
165
177
120
1
686
A
686A
A. Free Ice Cream
800
constructive algorithms; implementation
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the que...
The first line contains two space-separated integers n and x (1 ≤ n ≤ 1000, 0 ≤ x ≤ 109).Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1 ≤ di ≤ 109). Record ""+ di"" in i-th line means that a carrier with di ice cream packs occupies i-th place from the start of the q...
Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.
Consider the first sample. Initially Kay and Gerda have 7 packs of ice cream. Carrier brings 5 more, so now they have 12 packs. A kid asks for 10 packs and receives them. There are only 2 packs remaining. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed. Carrier bring 40 pa...
Input: 5 7+ 5- 10- 20+ 40- 20 | Output: 22 1
Beginner
2
977
413
141
6
31
E
31E
E. TV Game
2,400
dp
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmo...
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
Input: 21234 | Output: HHMM
Expert
1
776
152
226
0
1,776
F
1776F
F. Train Splitting
1,700
constructive algorithms; graphs; greedy
There are \(n\) big cities in Italy, and there are \(m\) train routes between pairs of cities. Each route connects two different cities bidirectionally. Moreover, using the trains one can reach every city starting from any other city.Right now, all the routes are operated by the government-owned Italian Carriage Passen...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 50\), \(n-1 \le m \le n(n-1)/2\)) — the number of citi...
For each test case, on the first line print an integer \(k\) (\(2 \le k \le m\)) — the number of companies in your plan; on the second line print \(m\) integers \(c_1, \, c_2, \, \dots, \, c_m\) (\(1 \le c_i \le k\)) — in your plan company \(c_i\) operates the \(i\)-th route.If there are multiple valid plans, you may p...
In the first test case, the output is illustrated in the following picture, where different colors correspond to different companies (blue for \(1\), red for \(2\), green for \(3\), and yellow for \(4\)): If we consider, for example, only companies \(2\) and \(3\), we can see that from any city it is possible to reach ...
Input: 25 91 21 31 41 52 32 42 53 43 53 31 23 12 3 | Output: 4 1 2 3 1 4 2 2 4 3 3 2 3 1
Medium
3
1,321
770
337
17
852
F
852F
F. Product transformation
2,200
combinatorics; math; number theory
Consider an array A with N elements, all being the same integer a.Define the product transformation as a simultaneous update Ai = Ai·Ai + 1, that is multiplying each element to the element right to it for , with the last number AN remaining the same. For example, if we start with an array A with a = 2 and N = 4, then a...
The first and only line of input contains four integers N, M, a, Q (7 ≤ Q ≤ 109 + 123, 2 ≤ a ≤ 106 + 123, , is prime), where is the multiplicative order of the integer a modulo Q, see notes for definition.
You should output the array A from left to right.
The multiplicative order of a number a modulo Q , is the smallest natural number x such that ax mod Q = 1. For example, .
Input: 2 2 2 7 | Output: 1 2
Hard
3
573
205
49
8
1,912
H
1912H
H. Hypercatapult Commute
2,400
graphs
A revolutionary new transport system is currently operating in Byteland. This system requires neither roads nor sophisticated mechanisms, only giant catapults. The system works as follows. There are \(n\) cities in Byteland. In every city there is a catapult, right in the city center. People who want to travel are put ...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \leq n \leq 1000\), \(0 \leq m \leq 10^5\)) — the number of cities and the number of passengers. The next \(m\) lines contain pairs of numbers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\), \(a_i \neq b_i\)).
In the first line print the number \(k\) — the minimal number of catapults you need to use.In the next \(k\) lines, print descriptions of each catapult launch, in the order they need to be performed. Each description should consist of two integers \(c_i\), \(d_i\), the index of the city to launch from, and the index of...
Input: 5 61 31 22 34 21 55 1 | Output: 5 5 1 1 2 4 2 2 3 3 5
Expert
1
1,165
281
617
19
1,545
B
1545B
B. AquaMoon and Chess
1,900
combinatorics; math
Cirno gave AquaMoon a chessboard of size \(1 \times n\). Its cells are numbered with integers from \(1\) to \(n\) from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell \(i\) with a pawn, and do either of t...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10\,000\)) — the number of test cases.The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) — the size of the chessboard.The second line contains a string of \(n\) characters, consists of charact...
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo \(998\,244\,353\).
In the first test case the strings ""1100"", ""0110"" and ""0011"" are reachable from the initial state with some sequence of operations.
Input: 6 4 0110 6 011011 5 01010 20 10001111110110111000 20 00110110100110111101 20 11101111011000100010 | Output: 3 6 1 1287 1287 715
Hard
2
906
555
143
15
762
D
762D
D. Maximum path
2,300
dp; greedy; implementation
You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side.Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum ...
The first line contains an integer n (1 ≤ n ≤ 105) — the number of columns in the table.Next three lines contain n integers each — the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≤ aij ≤ 109) of the table.
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
The path for the first example: The path for the second example:
Input: 31 1 11 -1 11 1 1 | Output: 7
Expert
3
329
256
150
7
1,190
E
1190E
E. Tokitsukaze and Explosion
3,100
binary search; greedy
Tokitsukaze and her friends are trying to infiltrate a secret base built by Claris. However, Claris has been aware of that and set a bomb which is going to explode in a minute. Although they try to escape, they have no place to go after they find that the door has been locked.At this very moment, CJB, Father of Tokitsu...
The first line contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 10^5\)), indicating the number of people and the number of barriers respectively.The \(i\)-th line of the next \(n\) lines contains two integers \(X_i\), \(Y_i\) (\(-10^5 \leq X_i, Y_i \leq 10^5\)), indicating the \(i\)-th person's location \(P_i(X_i...
Print a single real number — the maximum distance meeting the requirement. Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max(1, |b|)} \leq 10^{-6...
In the first two examples, CJB must set the barrier crossing \(O(0, 0)\).In the last two examples, CJB can set each barrier crossing some \(P_i\) such that the barrier is perpendicular to the line between \(P_i\) and \(O\).
Input: 3 1 2 0 0 2 -1 0 | Output: 0.0000000000
Master
2
1,368
418
324
11
1,552
G
1552G
G. A Serious Referee
3,000
bitmasks; brute force; dfs and similar; sortings
Andrea has come up with what he believes to be a novel sorting algorithm for arrays of length \(n\). The algorithm works as follows.Initially there is an array of \(n\) integers \(a_1,\, a_2,\, \dots,\, a_n\). Then, \(k\) steps are executed.For each \(1\le i\le k\), during the \(i\)-th step the subsequence of the array...
The first line contains two integers \(n\) and \(k\) (\(1\le n\le 40\), \(0\le k\le 10\)) — the length of the arrays handled by Andrea's algorithm and the number of steps of Andrea's algorithm.Then \(k\) lines follow, each describing the subsequence considered in a step of Andrea's algorithm.The \(i\)-th of these lines...
If Andrea's algorithm is correct print ACCEPTED, otherwise print REJECTED.
Explanation of the first sample: The algorithm consists of \(3\) steps. The first one sorts the subsequence \([a_1, a_2, a_3]\), the second one sorts the subsequence \([a_2, a_3, a_4]\), the third one sorts the subsequence \([a_1,a_2]\). For example, if initially \(a=[6, 5, 6, 3]\), the algorithm transforms the array a...
Input: 4 3 3 1 2 3 3 2 3 4 2 1 2 | Output: ACCEPTED
Master
4
963
590
74
15
263
C
263C
C. Circle of Numbers
2,000
brute force; dfs and similar; implementation
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c ar...
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.It is guaranteed that no pair of integers, connected by a arc, occurs in t...
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number ""-1"" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print an...
Input: 51 22 33 44 55 11 32 43 54 15 2 | Output: 1 2 3 4 5
Hard
3
917
428
536
2
1,360
F
1360F
F. Spy-string
1,700
bitmasks; brute force; constructive algorithms; dp; hashing; strings
You are given \(n\) strings \(a_1, a_2, \ldots, a_n\): all of them have the same length \(m\). The strings consist of lowercase English letters.Find any string \(s\) of length \(m\) such that each of the given \(n\) strings differs from \(s\) in at most one position. Formally, for each given string \(a_i\), there is no...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Then \(t\) test cases follow.Each test case starts with a line containing two positive integers \(n\) (\(1 \le n \le 10\)) and \(m\) (\(1 \le m \le 10\)) — the number of strings and their length.Then follow \(n\) strings \(a_i\),...
Print \(t\) answers to the test cases. Each answer (if it exists) is a string of length \(m\) consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print ""-1"" (""minus one"", without quotes).
The first test case was explained in the statement.In the second test case, the answer does not exist.
Input: 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c | Output: abab -1 aaa ab z
Medium
6
713
407
259
13
61
C
61C
C. Capture Valerian
2,000
math
It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran.Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is capture...
The first line contains two integers a and b (2 ≤ a, b ≤ 25). Only b may be replaced by an R which indicates Roman numbering system.The next line contains a single non-negative integer c in base a which may contain leading zeros but its length doesn't exceed 103. It is guaranteed that if we have Roman numerals included...
Write a single line that contains integer c in base b. You must omit leading zeros.
You can find more information about roman numerals here: http://en.wikipedia.org/wiki/Roman_numerals
Input: 10 21 | Output: 1
Hard
1
2,194
447
83
0
1,579
F
1579F
F. Array Stabilization (AND version)
1,700
brute force; graphs; math; number theory; shortest paths
You are given an array \(a[0 \ldots n - 1] = [a_0, a_1, \ldots, a_{n - 1}]\) of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.In one step, the array \(a\) is replaced by another array of length \(n\) according to the following rules: First, a ...
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases.The next \(2t\) lines contain descriptions of the test cases. The first line of each test case description contains two integers: \(n\) (\(1 \le n \le 10^6\)) — array size and \(d\) (\(1 \le d \le n\)) — cyclic shift offset. Th...
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to \(1\) in the array after the end of the process, print -1.
In the third sample test case the array will change as follows: At the beginning \(a = [1, 1, 0, 1, 0]\), and \(a^{\rightarrow 2} = [1, 0, 1, 1, 0]\). Their element-by-element ""AND"" is equal to $$$\([1 \,\&\, 1, 1 \,\&\, 0, 0 \,\&\, 1, 1 \,\&\, 1, 0 \,\&\, 0] = [1, 0, 0, 1, 0]\)\( Now \)a = [1, 0, 0, 1, 0]\(, then \)...
Input: 5 2 1 0 1 3 2 0 1 0 5 2 1 1 0 1 0 4 2 0 1 0 1 1 1 0 | Output: 1 1 3 -1 0
Medium
5
1,399
531
319
15
921
06
92106
06. Labyrinth-6
3,200
See the problem statement here: http://codeforces.com/contest/921/problem/01.
Master
0
77
0
0
9
847
I
847I
I. Noise Level
1,900
dfs and similar; implementation; math
The Berland's capital has the form of a rectangle with sizes n × m quarters. All quarters are divided into three types: regular (labeled with the character '.') — such quarters do not produce the noise but are not obstacles to the propagation of the noise; sources of noise (labeled with an uppercase Latin letter from '...
The first line contains four integers n, m, q and p (1 ≤ n, m ≤ 250, 1 ≤ q, p ≤ 106) — the sizes of Berland's capital, the number of noise units that a quarter 'A' produces, and the allowable noise level.Each of the following n lines contains m characters — the description of the capital quarters, in the format that wa...
Print the number of quarters, in which the noise level exceeds the allowed level p.
The illustration to the first example is in the main part of the statement.
Input: 3 3 100 140...A*..B. | Output: 3
Hard
3
1,957
435
83
8
2,028
F
2028F
F. Alice's Adventures in Addition
2,700
bitmasks; brute force; dp; implementation
Note that the memory limit is unusual.The Cheshire Cat has a riddle for Alice: given \(n\) integers \(a_1, a_2, \ldots, a_n\) and a target \(m\), is there a way to insert \(+\) and \(\times\) into the circles of the expression $$$\(a_1 \circ a_2 \circ \cdots \circ a_n = m\)\( to make it true? We follow the usual order ...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n, m\) (\(1\le n\le 2\cdot 10^5\); \(1\le m\le 10^4\)) — the number of int...
For each test case, output ""YES"" without quotes if it is possible to get the target by inserting \(+\) or \(\times\) and ""NO"" otherwise.You can output each letter in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
Possible solutions for the first four test cases are shown below. $$$\(\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\ 2 \times 1 + 1 + 1 + 2 &= 6 \\ 2 + 1 + 1 + 1 + 2 &= 7 \\ \end{align*}\)\( It is impossible to get a result of \)8$$$ in the fifth test case.
Input: 65 42 1 1 1 25 52 1 1 1 25 62 1 1 1 25 72 1 1 1 25 82 1 1 1 25 62 0 2 2 3 | Output: YES YES YES YES NO YES
Master
4
487
565
285
20
1,512
C
1512C
C. A-B Palindrome
1,200
constructive algorithms; implementation; strings
You are given a string \(s\) consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string \(s\) by '0' or '1' so that the string becomes a palindrome and has exactly \(a\) characters '0' and exactly \(b\) characters '1'. Note that each of the characters '?' is replaced i...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains two integers \(a\) and \(b\) (\(0 \le a, b \le 2 \cdot 10^5\), \(a + b \ge 1\)).The second line of each test case contains the string \(s\) of length \(a+b\), consisting of the c...
For each test case, output: ""-1"", if you can't replace all the characters '?' in the string \(s\) by '0' or '1' so that the string becomes a palindrome and that it contains exactly \(a\) characters '0' and exactly \(b\) characters '1'; the string that is obtained as a result of the replacement, otherwise. If there ar...
Input: 9 4 4 01?????0 3 3 ?????? 1 0 ? 2 2 0101 2 2 01?0 0 1 0 0 3 1?1 2 2 ?00? 4 3 ??010?0 | Output: 01011010 -1 0 -1 0110 -1 111 1001 0101010
Easy
3
869
462
386
15
1,554
C
1554C
C. Mikasa
1,800
binary search; bitmasks; greedy; implementation
You are given two integers \(n\) and \(m\). Find the \(\operatorname{MEX}\) of the sequence \(n \oplus 0, n \oplus 1, \ldots, n \oplus m\). Here, \(\oplus\) is the bitwise XOR operator.\(\operatorname{MEX}\) of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequen...
The first line contains a single integer \(t\) (\(1 \le t \le 30\,000\)) — the number of test cases.The first and only line of each test case contains two integers \(n\) and \(m\) (\(0 \le n, m \le 10^9\)).
For each test case, print a single integer — the answer to the problem.
In the first test case, the sequence is \(3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5\), or \(3, 2, 1, 0, 7, 6\). The smallest non-negative integer which isn't present in the sequence i. e. the \(\operatorname{MEX}\) of the sequence is \(4\).In the second test case, the sequence is \(4 \oplus...
Input: 5 3 5 4 6 3 2 69 696 123456 654321 | Output: 4 3 0 640 530866
Medium
4
417
206
71
15
756
D
756D
D. Bacterial Melee
2,400
brute force; combinatorics; dp; string suffix structures
Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters ""a"", ..., ""z"".The testtube is divided into n consecutive ...
The first line contains an integer n — the number of regions in the testtube (1 ≤ n ≤ 5 000).The second line contains n small Latin letters that describe the initial population of the testtube.
Print one number — the answer to the problem modulo 109 + 7.
In the first sample the population can never change since all bacteria are of the same type.In the second sample three configurations are possible: ""ab"" (no attacks), ""aa"" (the first colony conquers the second colony), and ""bb"" (the second colony conquers the first colony).To get the answer for the third sample, ...
Input: 3aaa | Output: 1
Expert
4
1,622
193
60
7
1,822
F
1822F
F. Gardening Friends
1,700
brute force; dfs and similar; dp; graphs; trees
Two friends, Alisa and Yuki, planted a tree with \(n\) vertices in their garden. A tree is an undirected graph without cycles, loops, or multiple edges. Each edge in this tree has a length of \(k\). Initially, vertex \(1\) is the root of the tree.Alisa and Yuki are growing the tree not just for fun, they want to sell i...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains integers \(n\), \(k\), \(c\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le k, c \le 10^9\)) — the number of vertices in the tree, the len...
For each test case, output a single integer — the maximum profit that Yuki and Alisa can get.
Input: 43 2 32 13 15 4 12 14 25 43 46 5 34 16 12 65 13 210 6 41 31 99 77 66 49 22 88 55 10 | Output: 2 12 17 32
Medium
5
1,268
614
93
18
76
E
76E
E. Points
1,700
implementation; math
You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.
The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.
The only line of output should contain the required sum of squares of distances between all pairs of points.
Input: 41 1-1 -11 -1-1 1 | Output: 32
Medium
2
127
251
108
0
1,363
B
1363B
B. Subsequence Hate
1,400
implementation; strings
Shubham has a binary string \(s\). A binary string is a string containing only characters ""0"" and ""1"".He can perform the following operation on the string any amount of times: Select an index of the string, and flip the character at that index. This means, if the character was ""0"", it becomes ""1"", and vice vers...
The first line of the input contains a single integer \(t\) \((1\le t \le 100)\) — the number of test cases.Each of the next \(t\) lines contains a binary string \(s\) \((1 \le |s| \le 1000)\).
For every string, output the minimum number of operations required to make it good.
In test cases \(1\), \(2\), \(5\), \(6\) no operations are required since they are already good strings.For the \(3\)rd test case: ""001"" can be achieved by flipping the first character — and is one of the possible ways to get a good string.For the \(4\)th test case: ""000"" can be achieved by flipping the second char...
Input: 7 001 100 101 010 0 1 001100 | Output: 0 0 1 1 0 0 2
Easy
2
910
193
83
13
7
B
7B
B. Memory Manager
1,600
implementation
There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations: al...
The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc ...
Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return inte...
Input: 6 10alloc 5alloc 3erase 1alloc 6defragmentalloc 6 | Output: 12NULL3
Medium
1
2,269
477
386
0
2,129
C2
2129C2
C2. Interactive RBS (Medium Version)
2,000
binary search; bitmasks; constructive algorithms; interactive
This is an interactive problem.This is the medium version of the problem. The only difference is the limit on the number of queries. You can make hacks only if all versions of the problem are solved.There is a hidden bracket sequence \(s\) of length \(n\), where \(s\) only contains \(\texttt{'('}\) and \(\texttt{')'}\)...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). The description of the test cases follows.
In the first test case, the hidden bracket sequence is \(s=\texttt{"")((""}\).For the query ""? 4 1 2 3 3"", the jury returns \(0\) because \(f(s_{1}s_{2}s_{3}s_{3}) = f(\texttt{"")(((""}) = 0\).For the query ""? 2 2 1"", the jury returns \(1\) because \(f(s_{2}s_{1}) = f(\texttt{""()""}) = 1\).For the query ""? 2 3 1"...
Input: 2 3 0 1 1 2 3 | Output: ? 4 1 2 3 3 ? 2 2 1 ? 2 3 1 ! )(( ? 4 1 2 1 2 ! ()
Hard
4
1,480
159
0
21
1,184
D1
1184D1
D1. Parallel Universes (Easy)
1,600
implementation
The Third Doctor Who once correctly said that travel between parallel universes is ""like travelling sideways"". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250.Heidi recently got her hands on a multiverse observation to...
The first line contains four integers \(n\), \(k\), \(m\) and \(t\) (\(2 \le k \le n \le m \le 250\), \(1 \le t \le 1000\)).Each of the following \(t\) lines is in one of the following formats: ""\(1\) \(i\)"" — meaning that a universe is inserted at the position \(i\) (\(1 \le i \le l + 1\)), where \(l\) denotes the c...
Output \(t\) lines. Each line should contain \(l\), the current length of the multiverse and \(k\), the current position of the Doctor.It is guaranteed that the sequence of the steps will be valid, i.e. the multiverse will have length at most \(m\) and when the link breaking is performed, there will be at least one uni...
The multiverse initially consisted of 5 universes, with the Doctor being in the second.First, link 1 was broken, leaving the multiverse with 4 universes, and the Doctor in the first.Then, a universe was added to the leftmost end of the multiverse, increasing the multiverse length to 5, and the Doctor was then in the se...
Input: 5 2 10 4 0 1 1 1 0 4 1 2 | Output: 4 1 5 2 4 2 5 3
Medium
1
1,338
494
344
11
709
A
709A
A. Juicer
900
implementation
Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly gr...
The first line of the input contains three integers n, b and d (1 ≤ n ≤ 100 000, 1 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.The second line contains n integers a1, a2, ......
Print one integer — the number of times Kolya will have to empty the waste section.
In the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
Input: 2 7 105 6 | Output: 1
Beginner
1
684
437
83
7
1,569
F
1569F
F. Palindromic Hamiltonian Path
3,000
brute force; dfs and similar; dp; graphs; hashing
You are given a simple undirected graph with \(n\) vertices, \(n\) is even. You are going to write a letter on each vertex. Each letter should be one of the first \(k\) letters of the Latin alphabet.A path in the graph is called Hamiltonian if it visits each vertex exactly once. A string is called palindromic if it rea...
The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 12\); \(n\) is even; \(0 \le m \le \frac{n \cdot (n-1)}{2}\); \(1 \le k \le 12\)) — the number of vertices in the graph, the number of edges in the graph and the number of first letters of the Latin alphabet that can be used.Each of the next \...
Print a single integer — number of good strings.
Input: 4 3 3 1 2 2 3 3 4 | Output: 9
Master
5
878
488
48
15
1,898
F
1898F
F. Vova Escapes the Matrix
2,600
brute force; dfs and similar; divide and conquer; shortest paths
Following a world tour, Vova got himself trapped inside an \(n \times m\) matrix. Rows of this matrix are numbered by integers from \(1\) to \(n\) from top to bottom, and the columns are numbered by integers from \(1\) to \(m\) from left to right. The cell \((i, j)\) is the cell on the intersection of row \(i\) and col...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \leq n,m \leq 1000\)) — the dimensions of the matrix.The next \(n\) lines contain ...
For each test case, output a single integer — the maximum number of cells Misha may block.
In the first test case, the matrix is of the \(3\)-rd type. Misha can create obstacles in all empty cells except the cells \((1, 3)\), \((2, 3)\), \((2, 4)\). There are \(9\) such cells, and adding such obstacles does not change the type of the matrix.In the second test case, the matrix is of the \(3\)-rd type. Blockin...
Input: 84 4#..#..V.....#..#3 6#.#####....#####V#3 3####V####6 5#.####...####.##V..##.#####..#7 5######.V.##.#.##.#.##.#.##...##.#.#3 7#.....#.#####.#...V.#5 8####.####..V#..##...#..##...##.####.####5 5#...###.####V####.###...# | Output: 9 0 0 3 4 10 12 5
Expert
4
1,414
1,044
90
18
685
E
685E
E. Travelling Through the Snow Queen's Kingdom
2,800
bitmasks; brute force; divide and conquer; graphs
Gerda is travelling to the palace of the Snow Queen.The road network consists of n intersections and m bidirectional roads. Roads are numbered from 1 to m. Snow Queen put a powerful spell on the roads to change the weather conditions there. Now, if Gerda steps on the road i at the moment of time less or equal to i, she...
The first line of the input contains integers n, m and q (2 ≤ n ≤ 1000, 1 ≤ m, q ≤ 200 000) — the number of intersections in the road network of Snow Queen's Kingdom, the number of roads and the number of queries you have to answer.The i-th of the following m lines contains the description of the road number i. The des...
For each query print ""Yes"" (without quotes) if Gerda can be at the Snow Queen palace on time (not later than ri) or ""No"" (without quotes) otherwise.
Input: 5 4 61 22 33 43 51 3 1 41 3 2 41 4 4 51 4 4 12 3 1 42 2 2 3 | Output: YesYesYesNoNoYes
Master
4
839
993
152
6
989
B
989B
B. A Tide of Riverscape
1,200
constructive algorithms; strings
Walking along a riverside, Mino silently takes a note of something.""Time,"" Mino thinks aloud.""What?""""Time and tide wait for no man,"" explains Mino. ""My name, taken from the river, always reminds me of this.""""And what are you recording?""""You see it, tide. Everything has its own period, and I think I've figure...
The first line contains two space-separated integers \(n\) and \(p\) (\(1 \leq p \leq n \leq 2000\)) — the length of the given string and the supposed period, respectively.The second line contains a string \(s\) of \(n\) characters — Mino's records. \(s\) only contains characters '0', '1' and '.', and contains at least...
Output one line — if it's possible that \(p\) is not a period of the resulting string, output any one of such strings; otherwise output ""No"" (without quotes, you can print letters in any case (upper or lower)).
In the first example, \(7\) is not a period of the resulting string because the \(1\)-st and \(8\)-th characters of it are different.In the second example, \(6\) is not a period of the resulting string because the \(4\)-th and \(10\)-th characters of it are different.In the third example, \(9\) is always a period becau...
Input: 10 71.0.1.0.1. | Output: 1000100010
Easy
2
1,084
339
212
9
645
C
645C
C. Enduring Exodus
1,600
binary search; two pointers
In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.Fa...
The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John.The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the ...
Print the minimum possible distance between Farmer John's room and his farthest cow.
In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.In the second sample, Farmer John can book room 1 for himself and room 3 for...
Input: 7 20100100 | Output: 2
Medium
2
677
515
84
6
538
A
538A
A. Cutting Banner
1,400
brute force; implementation
A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforcesω that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case ...
The single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES.
Print 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).
Input: CODEWAITFORITFORCES | Output: YES
Easy
2
1,254
245
101
5
2,121
D
2121D
D. 1709
1,300
implementation; sortings
You are given two arrays of integers \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\). It is guaranteed that each integer from \(1\) to \(2 \cdot n\) appears in exactly one of the arrays.You need to perform a certain number of operations (possibly zero) so that both of the following conditions are satisfied: For...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 40\)) — the length of the arrays \(a\) and \(b\).The s...
For each test case, output the sequence of operations. In the first line for each test case, output the number of operations \(k\). Note that \(0 \leq k \leq 1709\). In the following \(k\) lines for each test case, output the operations themselves: If you want to swap the values \(a_i\) and \(a_{i + 1}\), output two in...
In the first test case, \(a_1 < b_1\), so no operations need to be applied. In the second test case, \(a_1 > b_1\). After applying the operation, these values will be swapped. In the third test case, after applying the operation, \(a = [1, 3]\) and \(b = [2, 4]\). In the fourth test case, after applying the operation, ...
Input: 611212121 34 221 43 236 5 43 2 135 3 42 6 1 | Output: 0 1 3 1 1 2 1 1 3 2 9 3 1 3 2 3 3 1 1 2 1 2 2 1 2 1 1 2 1 6 2 2 1 1 1 2 2 1 3 1 3 2
Easy
2
945
657
689
21
1,272
A
1272A
A. Three Friends
900
brute force; greedy; math; sortings
Three friends are going to meet each other. Initially, the first friend stays at the position \(x = a\), the second friend stays at the position \(x = b\) and the third friend stays at the position \(x = c\) on the coordinate axis \(Ox\).In one minute each friend independently from other friends can change the position...
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) — the number of test cases.The next \(q\) lines describe test cases. The \(i\)-th test case is given as three integers \(a, b\) and \(c\) (\(1 \le a, b, c \le 10^9\)) — initial positions of the first, second and third friend correspondingly. ...
For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after o...
Input: 8 3 3 4 10 20 30 5 5 5 2 4 3 1 1000000000 1000000000 1 1000000000 999999999 3 2 5 3 2 6 | Output: 0 36 0 0 1999999994 1999999994 2 4
Beginner
4
1,075
358
330
12
384
B
384B
B. Multitasking
1,500
greedy; implementation; sortings; two pointers
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i ...
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element ...
On the first line of the output print an integer p, the size of the array (p can be at most ). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices.If there are multiple correct answers, you can print any.
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
Input: 2 5 01 3 2 5 41 4 3 2 5 | Output: 32 42 34 5
Medium
4
640
356
275
3
1,105
B
1105B
B. Zuhair and Strings
1,100
brute force; implementation; strings
Given a string \(s\) of length \(n\) and integer \(k\) (\(1 \le k \le n\)). The string \(s\) has a level \(x\), if \(x\) is largest non-negative integer, such that it's possible to find in \(s\): \(x\) non-intersecting (non-overlapping) substrings of length \(k\), all characters of these \(x\) substrings are the same (...
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) — the length of the string and the value of \(k\).The second line contains the string \(s\) of length \(n\) consisting only of lowercase Latin letters.
Print a single integer \(x\) — the level of the string.
In the first example, we can select \(2\) non-intersecting substrings consisting of letter 'a': ""(aa)ac(aa)bb"", so the level is \(2\).In the second example, we can select either substring ""a"" or ""b"" to get the answer \(1\).
Input: 8 2 aaacaabb | Output: 2
Easy
3
1,139
241
55
11
1,040
B
1040B
B. Shashlik Cooking
1,300
dp; greedy; math
Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out \(n\) skewers parallel to each other, and enumerated them with consecutive integers from \(1\) to \(n\) in order from ...
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 1000\), \(0 \leq k \leq 1000\)) — the number of skewers and the number of skewers from each side that are turned in one step.
The first line should contain integer \(l\) — the minimum number of actions needed by Miroslav to turn over all \(n\) skewers. After than print \(l\) integers from \(1\) to \(n\) denoting the number of the skewer that is to be turned over at the corresponding step.
In the first example the first operation turns over skewers \(1\), \(2\) and \(3\), the second operation turns over skewers \(4\), \(5\), \(6\) and \(7\).In the second example it is also correct to turn over skewers \(2\) and \(5\), but turning skewers \(2\) and \(4\), or \(1\) and \(5\) are incorrect solutions because...
Input: 7 2 | Output: 21 6
Easy
3
1,326
195
265
10
336
C
336C
C. Vasily the Bear and Sequence
1,800
brute force; greedy; implementation; number theory
Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 ...
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).
In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers,...
Input: 51 2 3 4 5 | Output: 24 5
Medium
4
1,003
148
456
3
599
A
599A
A. Patrick and Shopping
800
implementation
Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length ...
The first line of the input contains three integers d1, d2, d3 (1 ≤ d1, d2, d3 ≤ 108) — the lengths of the paths. d1 is the length of the path connecting Patrick's house and the first shop; d2 is the length of the path connecting Patrick's house and the second shop; d3 is the length of the path connecting both shops.
Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house.
The first sample is shown on the picture in the problem statement. One of the optimal routes is: house first shop second shop house.In the second sample one of the optimal routes is: house first shop house second shop house.
Input: 10 20 30 | Output: 60
Beginner
1
767
318
111
5
1,663
E
1663E
E. Are You Safe?
0
*special; implementation
Seven boys and seven girls from Athens were brought before King Minos. The evil king laughed and said ""I hope you enjoy your stay in Crete"".With a loud clang, the doors behind them shut, and darkness reigned. The terrified children huddled together. In the distance they heard a bellowing, unholy roar.Bravely, they ad...
\(n\) (\(1 \le n \le 100\)) — the dimensions of the labyrinth. Each of the next \(n\) lines contains a string of lowercase English letters of length \(n\).
Input: 10 atzxcodera mzhappyayc mineodteri trxesonaac mtollexcxc hjikeoutlc eascripsni rgvymnjcxm onzazwswwg geothermal | Output: YES
Beginner
2
350
155
0
16
1,765
E
1765E
E. Exchange
1,000
brute force; math
Monocarp is playing a MMORPG. There are two commonly used types of currency in this MMORPG — gold coins and silver coins. Monocarp wants to buy a new weapon for his character, and that weapon costs \(n\) silver coins. Unfortunately, right now, Monocarp has no coins at all.Monocarp can earn gold coins by completing ques...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of one line containing three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 10^7\); \(1 \le a, b \le 50\)).
For each test case, print one integer — the minimum possible number of quests Monocarp has to complete.
In the first test case of the example, Monocarp should complete \(4\) quests, and then sell \(4\) gold coins for \(100\) silver coins.In the second test case, Monocarp should complete \(400000\) quests, and then sell \(400000\) gold coins for \(10\) million silver coins.In the third test case, Monocarp should complete ...
Input: 4100 25 309999997 25 5052 50 4849 50 1 | Output: 4 400000 1 1
Beginner
2
1,068
223
103
17
1,406
D
1406D
D. Three Sequences
2,200
constructive algorithms; data structures; greedy; math
You are given a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\).You have to construct two sequences of integers \(b\) and \(c\) with length \(n\) that satisfy: for every \(i\) (\(1\leq i\leq n\)) \(b_i+c_i=a_i\) \(b\) is non-decreasing, which means that for every \(1<i\leq n\), \(b_i\geq b_{i-1}\) must hold \(c\) ...
The first line contains an integer \(n\) (\(1\leq n\leq 10^5\)).The secound line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\leq i\leq n\), \(-10^9\leq a_i\leq 10^9\)).The third line contains an integer \(q\) (\(1\leq q\leq 10^5\)).Each of the next \(q\) lines contains three integers \(l,r,x\) (\(1\leq l\leq r\...
Print \(q+1\) lines.On the \(i\)-th (\(1 \leq i \leq q+1\)) line, print the answer to the problem for the sequence after \(i-1\) changes.
In the first test: The initial sequence \(a = (2, -1, 7, 3)\). Two sequences \(b=(-3,-3,5,5),c=(5,2,2,-2)\) is a possible choice. After the first change \(a = (2, -4, 4, 0)\). Two sequences \(b=(-3,-3,5,5),c=(5,-1,-1,-5)\) is a possible choice. After the second change \(a = (2, -4, 6, 2)\). Two sequences \(b=(-4,-4,6,6...
Input: 4 2 -1 7 3 2 2 4 -3 3 4 2 | Output: 5 5 6
Hard
4
811
377
137
14
2,106
D
2106D
D. Flower Boy
1,500
binary search; dp; greedy; two pointers
Flower boy has a garden of \(n\) flowers that can be represented as an integer sequence \(a_1, a_2, \dots, a_n\), where \(a_i\) is the beauty of the \(i\)-th flower from the left.Igor wants to collect exactly \(m\) flowers. To do so, he will walk the garden from left to right and choose whether to collect the flower at...
The first line of the input contains a single integer \(t\) \((1 \le t \le 10^4)\) — the number of test cases.The first line of each test case contains exactly two integers \(n\) and \(m\) \((1 \le m \le n \le 2 \cdot 10^5)\) — the number of flowers in the garden and the number of flowers Igor wants to collect, respect...
For each test case, on a separate line, output the minimum integer \(k\) Igor must pick when he performs the aforementioned operation to ensure that he can collect \(m\) flowers. If he can collect \(m\) flowers without using the operation, output \(0\). If it is impossible to collect \(m\) flowers despite using the ope...
In the first test case, suppose Igor grows a flower of beauty \(6\) and places it between the third and the fourth flowers. Then, the garden will look like the following: \([3, 5, 2, 6, 3, 3, 5, 8, 1, 2]\). Then, he can select the second, fourth, sixth, seventh, and eighth flowers with beauties \([5, 6, 3, 5, 8]\). In ...
Input: 79 53 5 2 3 3 5 8 1 24 6 2 4 66 31 2 6 8 2 15 4 35 34 3 5 4 37 4 56 38 4 2 1 2 56 1 45 51 2 3 4 55 4 3 2 16 31 2 3 4 5 69 8 75 57 7 6 7 77 7 7 7 7 | Output: 6 3 7 0 -1 -1 7
Medium
4
1,142
801
342
21
583
A
583A
A. Asphalting Roads
1,000
implementation
City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.Sand roads have long been recognized out of date, so the decision was made to asphalt ...
The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city. Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetab...
In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.
In the sample the brigade acts like that: On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road; On the second day the brigade of the workers comes to the intersecti...
Input: 21 11 22 12 2 | Output: 1 4
Beginner
1
849
470
143
5