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
424
A
424A
A. Squats
900
implementation
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.For another exercise, Pasha needs exactly hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit d...
The first line contains integer n (2 ≀ n ≀ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
In the first line, print a single integer β€” the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
Input: 4xxXx | Output: 1XxXx
Beginner
1
414
263
256
4
432
E
432E
E. Square Tiling
2,300
constructive algorithms; greedy
You have an n Γ— m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally: each cell must be painted some color (the colors are marked by uppercase Latin letters); we will assume that two cells ...
The first line contains two integers, n and m (1 ≀ n, m ≀ 100).
Print lexicographically minimum coloring of the table that meets the described conditions. One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if: consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then th...
Input: 1 3 | Output: ABA
Expert
2
554
63
572
4
2,021
C1
2021C1
C1. Adjust The Presentation (Easy Version)
1,300
constructive algorithms; greedy
This is the easy version of the problem. In the two versions, the constraints on \(q\) and the time limit are different. In this version, \(q=0\). You can make hacks only if all the versions of the problem are solved.A team consisting of \(n\) members, numbered from \(1\) to \(n\), is set to present a slide show at a l...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 2 \cdot 10^5\); \(q=0\)) β€” the number of members, the number ...
For each test case, output \(q+1\) lines corresponding to the \(q+1\) states of the array \(b\). Output ""YA"" if the slide show is good, and ""TIDAK"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yA"", ""Ya"", ""ya"", and ""YA"" will be recognized as positive responses.
For the first test case, you do not need to move the members as both slides are presented by member \(1\), who is already at the front of the line.For the second test case, the following is a possible way to move members so that the presentation is good: \([1,2,3]\), do not move member \(1\). \([1,2,3]\), move member \...
Input: 34 2 01 2 3 41 13 6 01 2 31 1 2 3 3 24 6 03 1 4 23 1 1 2 3 4 | Output: YA YA TIDAK
Easy
2
1,750
876
319
20
1,009
D
1009D
D. Relatively Prime Graph
1,700
brute force; constructive algorithms; graphs; greedy; math
Let's call an undirected graph \(G = (V, E)\) relatively prime if and only if for each edge \((v, u) \in E\) \(GCD(v, u) = 1\) (the greatest common divisor of \(v\) and \(u\) is \(1\)). If there is no edge between some pair of vertices \(v\) and \(u\) then the value of \(GCD(v, u)\) doesn't matter. The vertices are num...
The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) β€” the number of vertices and the number of edges.
If there exists no valid graph with the given number of vertices and edges then output ""Impossible"".Otherwise print the answer in the following format:The first line should contain the word ""Possible"".The \(i\)-th of the next \(m\) lines should contain the \(i\)-th edge \((v_i, u_i)\) of the resulting graph (\(1 \l...
Here is the representation of the graph from the first example:
Input: 5 6 | Output: Possible2 53 25 13 44 15 4
Medium
5
654
127
532
10
1,305
H
1305H
H. Kuroni the Private Tutor
3,500
binary search; greedy
As a professional private tutor, Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him.The exam consists of \(n\) questions, and \(m\) students have taken the exam. Each question was worth \(1\) point. Question \(i\) was solved by at least \(l_...
The first line of input contains two integers (\(1 \le n, m \le 10^{5}\)), denoting the number of questions of the exam and the number of students respectively.The next \(n\) lines contain two integers each, with the \(i\)-th line containing \(l_{i}\) and \(r_{i}\) (\(0 \le l_{i} \le r_{i} \le m\)).The next line contai...
Output two integers: the maximum number of students who could have gotten as many points as the student with rank \(1\), and the maximum possible score for rank \(1\) achieving this maximum number of students. If there is no valid arrangement that fits the given data, output \(-1\) \(-1\).
For the first sample, here is one possible arrangement that fits the data:Students \(1\) and \(2\) both solved problems \(1\) and \(2\).Student \(3\) solved problems \(2\) and \(3\).Student \(4\) solved problem \(4\).The total score of all students is \(T = 7\). Note that the scores of the students are \(2\), \(2\), \(...
Input: 5 4 2 4 2 3 1 1 0 1 0 0 1 4 1 7 | Output: 3 2
Master
2
1,001
707
290
13
1,541
B
1541B
B. Pleasant Pairs
1,200
brute force; implementation; math; number theory
You are given an array \(a_1, a_2, \dots, a_n\) consisting of \(n\) distinct integers. Count the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_i \cdot a_j = i + j\).
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. Then \(t\) cases follow.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^5\)) β€” the length of array \(a\).The second line of each test case contains \(n\) space separated integers \(a_1, a_2, \l...
For each test case, output the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_i \cdot a_j = i + j\).
For the first test case, the only pair that satisfies the constraints is \((1, 2)\), as \(a_1 \cdot a_2 = 1 + 2 = 3\)For the second test case, the only pair that satisfies the constraints is \((2, 3)\).For the third test case, the pairs that satisfy the constraints are \((1, 2)\), \((1, 5)\), and \((2, 3)\).
Input: 3 2 3 1 3 6 1 5 5 3 1 5 9 2 | Output: 1 1 3
Easy
4
185
523
119
15
1,455
A
1455A
A. Strange Functions
800
math; number theory
Let's define a function \(f(x)\) (\(x\) is a positive integer) as follows: write all digits of the decimal representation of \(x\) backwards, then get rid of the leading zeroes. For example, \(f(321) = 123\), \(f(120) = 21\), \(f(1000000) = 1\), \(f(111) = 111\).Let's define another function \(g(x) = \dfrac{x}{f(f(x))}...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.Each test case consists of one line containing one integer \(n\) (\(1 \le n < 10^{100}\)). This integer is given without leading zeroes.
For each test case, print one integer β€” the number of different values of the function \(g(x)\), if \(x\) can be any integer from \([1, n]\).
Explanations for the two first test cases of the example: if \(n = 4\), then for every integer \(x\) such that \(1 \le x \le n\), \(\dfrac{x}{f(f(x))} = 1\); if \(n = 37\), then for some integers \(x\) such that \(1 \le x \le n\), \(\dfrac{x}{f(f(x))} = 1\) (for example, if \(x = 23\), \(f(f(x)) = 23\),\(\dfrac{x}{f(f(...
Input: 5 4 37 998244353 1000000007 12345678901337426966631415 | Output: 1 2 9 10 26
Beginner
2
532
227
141
14
392
C
392C
C. Yet Another Number Sequence
0
combinatorics; math; matrices
Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recurrence relation: F1 = 1, F2 = 2, Fi = Fi - 1 + Fi - 2 (i > 2).We'll define a new number sequence Ai(k) by the formula: Ai(k) = Fi Γ— ik (i β‰₯ 1).In this problem, your task is to calculate the following sum: A1(k) + A2(k) + ... + An(k)....
The first line contains two space-separated integers n, k (1 ≀ n ≀ 1017; 1 ≀ k ≀ 40).
Print a single integer β€” the sum of the first n elements of the sequence Ai(k) modulo 1000000007 (109 + 7).
Input: 1 1 | Output: 1
Beginner
3
391
85
107
3
195
A
195A
A. Let's Watch Football
1,000
binary search; brute force; math
Valeric and Valerko missed the last Euro football game, so they decided to watch the game's key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, it will ""hang up"" as the size of data to watch per second will be more than the ...
The first line contains three space-separated integers a, b and c (1 ≀ a, b, c ≀ 1000, a > b). The first number (a) denotes the size of data needed to watch one second of the video. The second number (b) denotes the size of data Valeric and Valerko can download from the Net per second. The third number (c) denotes the ...
Print a single number β€” the minimum integer number of seconds that Valeric and Valerko must wait to watch football without pauses.
In the first sample video's length is 1 second and it is necessary 4 units of data for watching 1 second of video, so guys should download 4 Β· 1 = 4 units of data to watch the whole video. The most optimal way is to wait 3 seconds till 3 units of data will be downloaded and then start watching. While guys will be watch...
Input: 4 1 1 | Output: 3
Beginner
3
1,190
346
130
1
1,784
D
1784D
D. Wooden Spoon
2,400
combinatorics; dp
\(2^n\) people, numbered with distinct integers from \(1\) to \(2^n\), are playing in a single elimination tournament. The bracket of the tournament is a full binary tree of height \(n\) with \(2^n\) leaves.When two players meet each other in a match, a player with the smaller number always wins. The winner of the tour...
The only line contains a single integer \(n\) (\(1 \le n \le 20\)) β€” the size of the tournament.There are \(20\) tests in the problem: in the first test, \(n = 1\); in the second test, \(n = 2\); \(\ldots\); in the \(20\)-th test, \(n = 20\).
Print \(2^n\) integers β€” the number of arrangements in which the ""Wooden Spoon"" is awarded to players \(1, 2, \ldots, 2^n\), modulo \(998\,244\,353\).
In the first example, the ""Wooden Spoon"" is always awarded to player \(2\).In the second example, there are \(8\) arrangements where players \(1\) and \(4\) meet each other in the first match, and in these cases, the ""Wooden Spoon"" is awarded to player \(3\). In the remaining \(16\) arrangements, the ""Wooden Spoon...
Input: 1 | Output: 0 2
Expert
2
1,063
242
152
17
2,087
D
2087D
D. Uppercase or Lowercase?
0
*special; *special; interactive
This is an interactive problem.In a certain database, there are \(n\) handles stored in the form of a numbered list. You are interested in one specific handle \(h\), or more precisely, in its position in this list.You can ask the database for the position \(i\) from the list, and it will return the handle at the \(i\)-...
The first line contains the number \(n\) and the string \(h\) (\(1 \le n \le 500\)) β€” the total number of handles in the list and the handle you are searching for.It is guaranteed that all handles are non-empty strings of no more than \(20\) characters, consisting only of lowercase Latin letters, except the first lette...
Note that spaces in examples are added only for improved readability. When checking solutions, the interactor will not output any empty lines.
Input: 4 adedalic Bleddest Neon adedalic awoo | Output: ? 1 ? 2 ? 3 ? 4 ! 3
Beginner
3
1,799
449
0
20
1,423
M
1423M
M. Milutin's Plums
2,800
interactive
As you all know, the plum harvesting season is on! Little Milutin had his plums planted in an orchard that can be represented as an \(n\) by \(m\) matrix. While he was harvesting, he wrote the heights of all trees in a matrix of dimensions \(n\) by \(m\).At night, when he has spare time, he likes to perform various sta...
This problem is interactive.The first line of input will contain two integers \(n\) and \(m\), representing the number of rows and the number of columns in Milutin's orchard. It is guaranteed that \(1 \le n, m \le 10^6\).The following lines will contain the answers to your queries.
Once you know have found the minimum value \(r\), you should print ! \(r\) to the standard output.
Input: 5 5 13 15 10 9 15 15 17 12 11 17 10 12 7 6 12 17 19 14 13 19 16 18 13 12 18 | Output:
Master
1
1,077
282
98
14
2,109
C1
2109C1
C1. Hacking Numbers (Easy Version)
1,500
bitmasks; constructive algorithms; interactive; math; number theory
This is the easy version of the problem. In this version, you can send at most \(\mathbf{7}\) commands. You can make hacks only if all versions of the problem are solved.This is an interactive problem.Welcome, Duelists! In this interactive challenge, there is an unknown integer \(x\) (\(1 \le x \le 10^9\)). You must ma...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows. The first and only line of each test case contains one integer \(n\) (\(1 \le n \le 10^9\)).
SolutionJuryExplanation\(\texttt{2}\)There are 2 test cases.\(\texttt{100}\)In the first test case, the unknown integer \(x = 9\) and we have to make it equal to \(n = 100\).\(\texttt{add -10}\)\(\texttt{0}\)The answer to ""add -10"" is ""0"". This means that the addition command was not successful as \(x + y = 9 + (-1...
Input: 2 100 0 1 1 1 5 1 1 1 | Output: add -10 add 1 mul 10 ! digit div 2 !
Medium
5
1,371
254
0
21
1,717
F
1717F
F. Madoka and The First Session
2,500
constructive algorithms; flows; graph matchings; graphs; implementation
Oh no, on the first exam Madoka got this hard problem:Given integer \(n\) and \(m\) pairs of integers (\(v_i, u_i\)). Also there is an array \(b_1, b_2, \ldots, b_n\), initially filled with zeros.Then for each index \(i\), where \(1 \leq i \leq m\), perform either \(b_{v_i} := b_{v_i} - 1\) and \(b_{u_i} := b_{u_i} + 1...
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 10000, 1 \leq m \leq 10000\)) β€” the length of the array \(a\) and the number of pair of integers.The second line contains \(n\) integers \(s_1, s_2, \ldots s_n\) (\(0 \le s_i \le 1\)) β€” the elements of the array \(s\).The third line contains \(n\) in...
In the first line print ""YES"" if it is possible to perform operations in the required way, and ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).In case you printed ""YES"", print \(m\) pairs of integers. If for pair \((v...
In the first example, the array \(b\) will change as follows: \([0,0,0,0,0] \rightarrow [-1,0,0,1,0] \rightarrow [-2,0,0,1,1] \rightarrow [-2,0,1,0,1] \rightarrow [-2,0,2,0,0] \rightarrow [-2,0,2,1,-1]\). \(a_i = b_i\) for all indices \(i\) from \(1\) to \(5\).In the second example, it is enough for us that \(b_2 = 1\)...
Input: 5 5 1 1 1 1 1 -2 0 2 1 -1 1 5 1 4 3 5 3 4 4 5 | Output: YES 1 5 1 4 5 3 4 3 5 4
Expert
5
889
843
575
17
1,799
D2
1799D2
D2. Hot Start Up (hard version)
2,100
data structures; dp
This is a hard version of the problem. The constraints of \(t\), \(n\), \(k\) are the only difference between versions.You have a device with two CPUs. You also have \(k\) programs, numbered \(1\) through \(k\), that you can run on the CPUs. The \(i\)-th program (\(1 \le i \le k\)) takes \(cold_i\) seconds to run on so...
Input consists of multiple test cases. The first line contains a single integer \(t\), the number of test cases (\(1 \le t \le 10^5\)).The first line of each test case contains \(n\) and \(k\) (\(1 \le n, k \le 3 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le ...
For each test case, print the minimum time needed to run all programs in the given order.
In the first test case, we can do the following: Run program \(a_1 = 1\) on CPU \(1\). It takes \(cold_1 = 3\) seconds to run. Run program \(a_2 = 2\) on CPU \(2\). It takes \(cold_2 = 2\) seconds to run. Run program \(a_3 = 2\) on CPU \(2\). The last program run on this CPU was also program \(2\), so it takes \(hot_2 ...
Input: 93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5 | Output: 6 11 301 225 8 4999999996 11 6 63
Hard
2
1,069
678
89
17
568
D
568D
D. Sign Posts
2,800
brute force; geometry; math
One Khanate had a lot of roads and very little wood. Riding along the roads was inconvenient, because the roads did not have road signs indicating the direction to important cities.The Han decided that it's time to fix the issue, and ordered to put signs on every road. The Minister of Transport has to do that, but he h...
The input starts with two positive integers n, k (1 ≀ n ≀ 105, 1 ≀ k ≀ 5)Next n lines contain three integers each, Ai, Bi, Ci, the coefficients of the equation that determines the road (|Ai|, |Bi|, |Ci| ≀ 105, Ai2 + Bi2 β‰  0).It is guaranteed that no two roads coincide.
If there is no solution, print ""NO"" in the single line (without the quotes).Otherwise, print in the first line ""YES"" (without the quotes).In the second line print a single number m (m ≀ k) β€” the number of used signs. In the next m lines print the descriptions of their locations.Description of a location of one sign...
Note that you do not have to minimize m, but it shouldn't be more than k.In the first test all three roads intersect at point (0,0).In the second test all three roads form a triangle and there is no way to place one sign so that it would stand on all three roads at once.
Input: 3 11 0 00 -1 07 -93 0 | Output: YES11 2
Master
3
741
269
928
5
301
D
301D
D. Yaroslav and Divisors
2,200
data structures
Yaroslav has an array p = p1, p2, ..., pn (1 ≀ pi ≀ n), consisting of n distinct integers. Also, he has m queries: Query number i is represented as a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). The answer to the query li, ri is the number of pairs of integers q, w (li ≀ q, w ≀ ri) such that pq is the divisor of pw. Help...
The first line contains the integers n and m (1 ≀ n, m ≀ 2Β·105). The second line contains n distinct integers p1, p2, ..., pn (1 ≀ pi ≀ n). The following m lines contain Yaroslav's queries. The i-th line contains integers li, ri (1 ≀ li ≀ ri ≀ n).
Print m integers β€” the answers to Yaroslav's queries in the order they appear in the input.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Input: 1 111 1 | Output: 1
Hard
1
354
247
238
3
837
D
837D
D. Round Subset
2,100
dp; math
Let's call the roundness of the number the number of zeros to which it ends.You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.
The first line contains two integer numbers n and k (1 ≀ n ≀ 200, 1 ≀ k ≀ n).The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≀ ai ≀ 1018).
Print maximal roundness of product of the chosen subset of length k.
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] β€” product 80, roundness 1, [50, 20] β€” product 1000, roundness 3.In the second example subset [15, 16, 25] has product 6000, roundness 3.In the third example all subsets has product with roundness 0.
Input: 3 250 4 20 | Output: 3
Hard
2
243
168
68
8
996
B
996B
B. World Cup
1,300
binary search; math
Allen wants to enter a fan zone that occupies a round square and has \(n\) entrances.There already is a queue of \(a_i\) people in front of the \(i\)-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.Allen uses the following strategy to enter the fan zone: Initially he stan...
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of entrances.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β€” the number of people in queues. These numbers do not include Allen.
Print a single integer β€” the number of entrance that Allen will use.
In the first example the number of people (not including Allen) changes as follows: \([\textbf{2}, 3, 2, 0] \to [1, \textbf{2}, 1, 0] \to [0, 1, \textbf{0}, 0]\). The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.In the second example the number of people...
Input: 42 3 2 0 | Output: 3
Easy
2
708
256
68
9
1,200
E
1200E
E. Compress Words
2,000
brute force; hashing; implementation; string suffix structures; strings
Amugae has a sentence consisting of \(n\) words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges ""sample"" and ""please"" ...
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), the number of the words in Amugae's sentence.The second line contains \(n\) words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ...,...
In the only line output the compressed word after the merging process ends as described in the problem.
Input: 5 I want to order pizza | Output: Iwantorderpizza
Hard
5
556
382
103
12
1,305
B
1305B
B. Kuroni and Simple Strings
1,200
constructive algorithms; greedy; strings; two pointers
Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!W...
The only line of input contains a string \(s\) (\(1 \le |s| \le 1000\)) formed by characters '(' and ')', where \(|s|\) is the length of \(s\).
In the first line, print an integer \(k\) β€” the minimum number of operations you have to apply. Then, print \(2k\) lines describing the operations in the following format:For each operation, print a line containing an integer \(m\) β€” the number of characters in the subsequence you will remove.Then, print a line contain...
In the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices \(2\) and \(3\), which results in the same final string.In the second sample, it is a...
Input: (()(( | Output: 1 2 1 3
Easy
4
1,679
143
662
13
2,041
H
2041H
H. Sheet Music
2,300
combinatorics; dp; math
Image generated by ChatGPT 4o. Alice likes singing. As a singing enthusiast, Alice has listened to countless songs and has tried singing them many times. However, occasionally, some songs make Alice feel bored. After some research, Alice believes that this is because even though the songs she chose are all different, d...
The only line contains two integers \(n, k\). \(1\leq n\leq 10^6\) \(1\leq k \leq 10^9\)
Output the number of different songs modulo \(998244353\).
Input: 3 2 | Output: 7
Expert
3
1,649
88
58
20
162
B
162B
B. Binary notation
1,800
*special
You are given a positive integer n. Output its binary notation.
The only line of input data contains an integer n (1 ≀ n ≀ 106).
Output the binary notation of n (without any leading zeros).
In the first example 5 = 1 * 22 + 0 * 21 + 1 * 20.
Input: 5 | Output: 101
Medium
1
63
64
60
1
606
B
606B
B. Testing Robots
1,600
implementation
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x Γ— y, after that a mine will be i...
The first line of the input contains four integers x, y, x0, y0 (1 ≀ x, y ≀ 500, 1 ≀ x0 ≀ x, 1 ≀ y0 ≀ y) β€” the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right.The second line contains a sequence of commands s, which should be ...
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .
Input: 3 4 2 2UURDRDRL | Output: 1 1 0 1 1 1 1 0 6
Medium
1
1,448
438
189
6
1,163
B2
1163B2
B2. Cat Party (Hard Edition)
1,600
data structures; implementation
This problem is same as the previous one, but has larger constraints.Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.For each of the \(n\) days since the day Shiro moved to th...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the total number of days.The second line contains \(n\) integers \(u_1, u_2, \ldots, u_n\) (\(1 \leq u_i \leq 10^5\)) β€” the colors of the ribbons the cats wear.
Print a single integer \(x\) β€” the largest possible streak of days.
In the first example, we can choose the longest streak of \(13\) days, since upon removing the last day out of the streak, all of the remaining colors \(1\), \(2\), \(3\), and \(4\) will have the same number of occurrences of \(3\). Note that the streak can also be \(10\) days (by removing the \(10\)-th day from this s...
Input: 13 1 1 1 2 2 2 3 3 3 4 4 4 5 | Output: 13
Medium
2
1,170
234
67
11
1,839
D
1839D
D. Ball Sorting
2,100
data structures; dp; sortings
There are \(n\) colorful balls arranged in a row. The balls are painted in \(n\) distinct colors, denoted by numbers from \(1\) to \(n\). The \(i\)-th ball from the left is painted in color \(c_i\). You want to reorder the balls so that the \(i\)-th ball from the left has color \(i\). Additionally, you have \(k \ge 1\)...
The first line contains integer \(t\) (\(1 \le t \le 500\)) β€” the number of test cases. The descriptions of the test cases follow.The first line contains one integer \(n\) (\(1 \le n \le 500\)) β€” the number of balls.The second line contains \(n\) distinct integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le n\)) β€” the c...
For each test case, output \(n\) integers: the \(i\)-th (\(1 \le i \le n\)) of them should be equal to the minimum amount of coins you need to spend in order to reorder balls in the required way for \(k = i\).
In the first test case there are \(n = 6\) balls. The colors of the balls from left to right are \([\, 2, 3, 1, 4, 6, 5 \,]\). Let's suppose \(k = 1\). One of the ways to reorder the balls in the required way for \(3\) coins:\([\, 2, 3, 1, 4, 6, 5 \,]\) \(\xrightarrow{\, 1 \,}\) \([\, 2, 3, 1, 4, \color{red}{0}, 6, 5 \...
Input: 362 3 1 4 6 531 2 3117 3 4 6 8 9 10 2 5 11 1 | Output: 3 2 2 2 2 2 0 0 0 10 5 4 4 4 4 4 4 4 4 4
Hard
3
1,789
432
209
18
2,071
B
2071B
B. Perfecto
1,100
brute force; constructive algorithms; greedy; math
A permutation \(p\) of length \(n\)\(^{\text{βˆ—}}\) is perfect if, for each index \(i\) (\(1 \le i \le n\)), it satisfies the following: The sum of the first \(i\) elements \(p_1 + p_2 + \ldots + p_i\) is not a perfect square\(^{\text{†}}\). You would like things to be perfect. Given a positive integer \(n\), find a per...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first and only line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).It is guaranteed that the sum of \(n\) over all test ...
For each test case: If no solution exists, print a single integer \(-1\). Otherwise, print \(n\) integers \(p_1,p_2,\ldots,p_n\) β€” the perfect permutation you find. If there are multiple solutions, print any of them.
In the first test case, there is only one permutation with length \(n = 1\) that is \(p = [1]\), which is not perfect: \(p_1 = 1 = x^2\) for \(x = 1\). In the second test case, one possible perfect permutation with length \(n = 4\) is \(p = [2, 4, 1, 3]\): \(p_1 = 2 \neq x^2\); \(p_1 + p_2 = 2 + 4 = 6 \neq x^2\); \(p_1...
Input: 3 1 4 5 | Output: -1 2 4 1 3 5 1 4 3 2
Easy
4
871
351
216
20
1,765
I
1765I
I. Infinite Chess
2,800
implementation; shortest paths
The black king lives on a chess board with an infinite number of columns (files) and \(8\) rows (ranks). The columns are numbered with all integer numbers (including negative). The rows are numbered from \(1\) to \(8\).Initially, the black king is located on the starting square \((x_s, y_s)\), and he needs to reach som...
The first line contains two integers \(x_s\) and \(y_s\) (\(1 \le x_s \le 10^8\); \(1 \le y_s \le 8\)) β€” the starting coordinates of the black king.The second line contains two integers \(x_t\) and \(y_t\) (\(1 \le x_t \le 10^8\); \(1 \le y_t \le 8\)) β€” the coordinates of the target square for the black king.The third ...
Print one integer β€” the minimum number of moves needed for the black king to reach the target square while not violating the conditions, or \(-1\) if it is impossible.
The image below demonstrates the solution for the second example. Here, the letters K, R, s, and t represent the white king, the white rook, the starting square, and the target square, respectively. Bold crosses mark the squares which are under attack by the white pieces. Bold dots show the shortest path for the black ...
Input: 1 8 7 8 2 N 4 8 B 4 6 | Output: 10
Master
2
2,227
1,185
167
17
1,399
C
1399C
C. Boats Competition
1,200
brute force; greedy; two pointers
There are \(n\) people who want to participate in a boat competition. The weight of the \(i\)-th participant is \(w_i\). Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.So, if there are \(k\) teams \((a_1, ...
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 50\)) β€” the number of participants. The second line of the test case contains \(n\) integers \(w_1, w_2, \dots,...
For each test case, print one integer \(k\): the maximum number of teams people can compose with the total weight \(s\), if you choose \(s\) optimally.
In the first test case of the example, we can reach the optimal answer for \(s=6\). Then the first boat is used by participants \(1\) and \(5\) and the second boat is used by participants \(2\) and \(4\) (indices are the same as weights).In the second test case of the example, we can reach the optimal answer for \(s=12...
Input: 5 5 1 2 3 4 5 8 6 6 6 6 6 6 8 8 8 1 2 2 1 2 1 1 2 3 1 3 3 6 1 1 3 4 2 2 | Output: 2 3 4 1 2
Easy
3
864
406
151
13
1,263
F
1263F
F. Economic Difficulties
2,400
data structures; dfs and similar; dp; flows; graphs; trees
An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!Each grid (main and reserve) has a head node (its number is \(1\)). Every other node gets electricity from the head node. Each node can be reached fr...
The first line contains an integer \(n\) (\(1 \le n \le 1000\)) β€” the number of devices in the palace.The next line contains an integer \(a\) (\(1 + n \le a \le 1000 + n\)) β€” the amount of nodes in the main grid.Next line contains \(a - 1\) integers \(p_i\) (\(1 \le p_i \le a\)). Each integer \(p_i\) means that the mai...
Print a single integer β€” the maximal amount of wires that can be cut so that each device is powered.
For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red): The second and the third examples can be seen below:
Input: 3 6 4 1 1 4 2 6 5 3 4 1 1 1 3 4 2 | Output: 5
Expert
6
2,158
1,232
100
12
1,183
B
1183B
B. Equalize Prices
900
math
There are \(n\) products in the shop. The price of the \(i\)-th product is \(a_i\). The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product \(i\) in such a way that the difference between the old ...
The first line of the input contains one integer \(q\) (\(1 \le q \le 100\)) β€” the number of queries. Each query is presented by two lines.The first line of the query contains two integers \(n\) and \(k\) (\(1 \le n \le 100, 1 \le k \le 10^8\)) β€” the number of products and the value \(k\). The second line of the query ...
Print \(q\) integers, where the \(i\)-th integer is the answer \(B\) on the \(i\)-th query.If it is impossible to equalize prices of all given products with restriction that for all products the condition \(|a_i - B| \le k\) should be satisfied (where \(a_i\) is the old price of the product and \(B\) is the new equal p...
In the first example query you can choose the price \(B=2\). It is easy to see that the difference between each old price and each new price \(B=2\) is no more than \(1\).In the second example query you can choose the price \(B=6\) and then all the differences between old and new price \(B=6\) will be no more than \(2\...
Input: 4 5 1 1 1 2 3 1 4 2 6 4 8 5 2 2 1 6 3 5 5 2 5 | Output: 2 6 -1 7
Beginner
1
1,186
446
418
11
196
A
196A
A. Lexicographically Maximum Subsequence
1,100
greedy; strings
You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≀ p1 < p2 < ... < pk ≀ |s|) a subsequence of string s = s1s2... s|s|.String x = x1x2... x|x| is lexicographically larger than string y = y1y2......
The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.
Print the lexicographically maximum subsequence of string s.
Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).The first sample: aBaBBAThe second sample: abbCbCCaCbbCBaaBA
Input: ababba | Output: bbba
Easy
2
546
132
60
1
1,062
E
1062E
E. Company
2,300
binary search; data structures; dfs and similar; greedy; trees
The company \(X\) has \(n\) employees numbered from \(1\) through \(n\). Each employee \(u\) has a direct boss \(p_u\) (\(1 \le p_u \le n\)), except for the employee \(1\) who has no boss. It is guaranteed, that values \(p_i\) form a tree. Employee \(u\) is said to be in charge of employee \(v\) if \(u\) is the direct ...
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 100\,000\), \(1 \le q \le 100\,000\)) β€” the number of employees and the number of plans, respectively.The second line contains \(n-1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i \le n\)) meaning \(p_i\) is the direct boss of employee \(i\).It is gu...
Print \(q\) lines, each containing two integers β€” the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case.If there are more than one way to choose that employee, print any of them.
In the example: In the first query, we can choose whether \(4\) or \(5\) or \(6\) and the project manager will be \(3\).In the second query, if we choose any employee other than the employee \(8\), the project manager will be \(1\). If we choose \(8\), the project manager will be \(3\). Since \(lv(3)=1 > lv(1)=0\), cho...
Input: 11 51 1 3 3 3 4 2 7 7 64 64 81 119 118 11 | Output: 4 18 11 011 38 1
Expert
5
1,874
561
269
10
241
F
241F
F. Race
2,300
brute force; implementation
The Old City is a rectangular city represented as an m Γ— n grid of blocks. This city contains many buildings, straight two-way streets and junctions. Each junction and each building is exactly one block. All the streets have width of one block and are either vertical or horizontal. There is a junction on both sides of ...
The first line of input contains three integers m, n and k (3 ≀ m, n ≀ 100, 1 ≀ k ≀ 100000). Next m lines are representing the city's map. Each of them containts n characters, each character is a block: Character ""#"" represents a building. Digits ""1"", ""2"", ..., ""9"" represent a block of an street and this digit ...
In a single line print two integers rf and cf β€” (rf, cf) being the position of the Old Peykan after exactly k minutes.
Input: 3 10 12###########z1a1111b###########2 3 ab 2 8 | Output: 2 8
Expert
2
1,451
1,204
118
2
1,834
C
1834C
C. Game with Reversing
1,200
games; greedy; math; strings
Alice and Bob are playing a game. They have two strings \(S\) and \(T\) of the same length \(n\) consisting of lowercase latin letters. Players take turns alternately, with Alice going first.On her turn, Alice chooses an integer \(i\) from \(1\) to \(n\), one of the strings \(S\) or \(T\), and any lowercase latin lette...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” the length of the strings \(S\) and \(T\).The second line of each tes...
For each test case, output a single number on a separate line β€” the duration of the described game, if both players play optimally.
In the first test case, in her turn, Alice can replace the third symbol of the string \(S\) with x. After that, both strings will become equal to ""abxde"" and the game will end after one move. Since Alice's goal is to finish the game in as few moves as possible, this move will be one of her optimal first moves, and th...
Input: 75abcdeabxde5helloolleo2abcd7aaaaaaaabbbbba1qq6yoyoyooyoyoy8abcdefghhguedfbh | Output: 1 2 3 9 0 2 6
Easy
4
1,230
600
131
18
76
C
76C
C. Mutation
2,700
bitmasks; dp; math
Scientists of planet Olympia are conducting an experiment in mutation of primitive organisms. Genome of organism from this planet can be represented as a string of the first K capital English letters. For each pair of types of genes they assigned ai, j β€” a risk of disease occurence in the organism provided that genes o...
The first line of the input contains three integer numbers N (1 ≀ N ≀ 200 000) β€” length of the genome of base organism, K (1 ≀ K ≀ 22) β€” the maximal index of gene type in the genome and T (1 ≀ T ≀ 2Β·109) β€” maximal allowable risk of disease occurence. The second line contains the genome of the given organism. It is a st...
Output the number of organisms that can be obtained from the base one and which have the total risk of disease occurence not greater than T.
Explanation: one can obtain the following organisms (risks are stated in brackets): BACAC (11), ACAC (10), BAA (5), B (6), AA (4).
Input: 5 3 13BACAC4 1 21 2 32 3 43 4 10 | Output: 5
Master
3
1,558
1,122
140
0
2
B
2B
B. The least round way
2,000
dp; math
There is a square matrix n Γ— n, consisting of non-negative integer numbers. You should find such a way on it that starts in the upper left cell of the matrix; each following cell is to the right or down from the current cell; the way ends in the bottom right cell. Moreover, if we multiply together all the numbers along...
The first line contains an integer number n (2 ≀ n ≀ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
Input: 31 2 34 5 67 8 9 | Output: 0DDRR
Hard
2
440
189
114
0
145
D
145D
D. Lucky Pair
2,900
combinatorics; data structures; implementation
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has an array a of n integers. The numbers in the array are numbered starting from 1. Unfortuna...
The first line contains an integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n space-separated integers ai (1 ≀ ai ≀ 109) β€” array a. It is guaranteed that no more than 1000 elements in the array a are lucky numbers.
On the single line print the only number β€” the answer to the problem.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
The subarray a[l..r] is an array that consists of elements al, al + 1, ..., ar.In the first sample there are 9 possible pairs that satisfy the condition: [1, 1] and [2, 2], [1, 1] and [2, 3], [1, 1] and [2, 4], [1, 1] and [3, 3], [1, 1] and [3, 4], [1, 1] and [4, 4], [1, 2] and [3, 3], [2, 2] and [3, 3], [3, 3] and [4,...
Input: 41 4 2 4 | Output: 9
Master
3
832
241
221
1
1,791
G1
1791G1
G1. Teleporters (Easy Version)
1,100
greedy; sortings
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points \(0, 1, \dots, n\) on the number line. There is a teleporter located on each of the points \(1, 2, \dots, n\). At point \(i\), you can do the following: Move left one unit: it costs \(1\) coin. Move right on...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(1 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^9\)) β€” the length of...
For each test case, output the maximum number of teleporters you can use.
In the first test case, you can move one unit to the right, use the teleporter at index \(1\) and teleport to point \(0\), move two units to the right and use the teleporter at index \(2\). You are left with \(6-1-1-2-1 = 1\) coins you don't have enough coins to use another teleporter. You have used two teleporters, so...
Input: 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000 | Output: 2 2 0 1 2 2 1 1 1 2
Easy
2
615
613
73
17
93
D
93D
D. Flags
2,500
dp; math; matrices
When Igor K. was a freshman, his professor strictly urged him, as well as all other freshmen, to solve programming Olympiads. One day a problem called ""Flags"" from a website called Timmy's Online Judge caught his attention. In the problem one had to find the number of three-colored flags that would satisfy the condit...
The only line contains two integers L and R (1 ≀ L ≀ R ≀ 109). They are the lower and upper borders of the number of stripes on the flag.
Print a single number β€” the number of different flags that would satisfy the condition of the problem and would have from L to R stripes, modulo 1000000007.
In the first test the following flags exist (they are listed in the lexicographical order, the letters B, R, W, Y stand for Black, Red, White and Yellow correspondingly):3 stripes: BWB, BYB, BYR, RWR, RYR, WBW, WBY, WRW, WRY, YBY, YRY (overall 11 flags).4 stripes: BWBW, BWBY, BYBW, BYBY, BYRW, BYRY, RWRW, RWRY, RYBW, R...
Input: 3 4 | Output: 23
Expert
3
1,499
137
156
0
834
A
834A
A. The Useless Toy
900
implementation
Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption.Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spi...
There are two characters in the first string – the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a ...
Output cw, if the direction is clockwise, ccw – if counter-clockwise, and undefined otherwise.
Input: ^ >1 | Output: cw
Beginner
1
1,149
577
94
8
1,958
A
1958A
A. 1-3-5
1,100
*special; dp
In Berland, coins of worth \(1\), \(3\) and \(5\) burles are commonly used (burles are local currency).Eva has to pay exactly \(n\) burles in a shop. She has an infinite amount of coins of all three types. However, she doesn't like to pay using coins worth \(1\) burle β€” she thinks they are the most convenient to use.He...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.Each test case consists of one line, containing one integer \(n\) (\(1 \le n \le 100\)).
For each test case, print one integer β€” the minimum number of \(1\)-burle coins Eva has to use.
In the first test case, Eva should use \(1\) coin worth \(1\) burle, and \(2\) coins worth \(3\) burles.In the second test case, Eva should use \(1\) coin worth \(3\) burles and \(1\) coin worth \(5\) burles.In the third test case, Eva should use \(14\) coins worth \(3\) burles.In the fourth test case, Eva should use \...
Input: 57842211 | Output: 1 0 0 2 0
Easy
2
518
179
95
19
1,187
E
1187E
E. Tree Painting
2,100
dfs and similar; dp; trees
You are given a tree (an undirected connected acyclic graph) consisting of \(n\) vertices. You are playing a game on this tree.Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any blac...
The first line contains an integer \(n\) β€” the number of vertices in the tree (\(2 \le n \le 2 \cdot 10^5\)).Each of the next \(n - 1\) lines describes an edge of the tree. Edge \(i\) is denoted by two integers \(u_i\) and \(v_i\), the indices of vertices it connects (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)).It is gu...
Print one integer β€” the maximum number of points you gain if you will play optimally.
The first example tree is shown in the problem statement.
Input: 9 1 2 2 3 2 5 2 6 1 4 4 9 9 7 9 8 | Output: 36
Hard
3
1,006
362
85
11
1,272
D
1272D
D. Remove One Element
1,500
brute force; dp
You are given an array \(a\) consisting of \(n\) integers.You can remove at most one element from this array. Thus, the final length of the array is \(n-1\) or \(n\).Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.Recall that the contiguous sub...
The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the \(i\)-th element of \(a\).
Print one integer β€” the maximum possible length of the strictly increasing contiguous subarray of the array \(a\) after removing at most one element.
In the first example, you can delete \(a_3=5\). Then the resulting array will be equal to \([1, 2, 3, 4]\) and the length of its largest increasing subarray will be equal to \(4\).
Input: 5 1 2 5 3 4 | Output: 4
Medium
2
510
271
149
12
796
B
796B
B. Find The Bone
1,300
implementation
Zane the wizard is going to perform a magic show shuffling the cups.There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.The problematic bone is initially at the position x = 1. Zane will confuse the audience by sw...
The first line contains three integers n, m, and k (2 ≀ n ≀ 106, 1 ≀ m ≀ n, 1 ≀ k ≀ 3Β·105) β€” the number of cups, the number of holes on the table, and the number of swapping operations, respectively.The second line contains m distinct integers h1, h2, ..., hm (1 ≀ hi ≀ n) β€” the positions along the x-axis where there is...
Print one integer β€” the final position along the x-axis of the bone.
In the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground.
Input: 7 3 43 4 61 22 55 77 1 | Output: 1
Easy
1
1,297
466
68
7
321
E
321E
E. Ciel and Gondolas
2,600
data structures; divide and conquer; dp
Fox Ciel is in the Amusement Park. And now she is in a queue in front of the Ferris wheel. There are n people (or foxes more precisely) in the queue: we use first people to refer one at the head of the queue, and n-th people to refer the last one in the queue.There will be k gondolas, and the way we allocate gondolas l...
The first line contains two integers n and k (1 ≀ n ≀ 4000 and 1 ≀ k ≀ min(n, 800)) β€” the number of people in the queue and the number of gondolas. Each of the following n lines contains n integers β€” matrix u, (0 ≀ uij ≀ 9, uij = uji and uii = 0).Please, use fast input methods (for example, please use BufferedReader in...
Print an integer β€” the minimal possible total unfamiliar value.
In the first example, we can allocate people like this: {1, 2} goes into a gondolas, {3, 4, 5} goes into another gondolas.In the second example, an optimal solution is : {1, 2, 3} | {4, 5, 6} | {7, 8}.
Input: 5 20 0 1 1 10 0 1 1 11 1 0 0 01 1 0 0 01 1 0 0 0 | Output: 0
Expert
3
1,328
347
63
3
1,873
C
1873C
C. Target Practice
800
implementation; math
A \(10 \times 10\) target is made out of five ""rings"" as shown. Each ring has a different point value: the outermost ring β€” 1 point, the next ring β€” 2 points, ..., the center ring β€” 5 points. Vlad fired several arrows at the target. Help him determine how many points he got.
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.Each test case consists of 10 lines, each containing 10 characters. Each character in the grid is either \(\texttt{X}\) (representing an arrow) or \(\texttt{.}\) (re...
For each test case, output a single integer β€” the total number of points of the arrows.
In the first test case, there are three arrows on the outer ring worth 1 point each, two arrows on the ring worth 3 points each, and two arrows on the ring worth 4 points each. The total score is \(3 \times 1 + 2 \times 3 + 2 \times 4 = 17\). In the second test case, there aren't any arrows, so the score is \(0\).
Input: 4X..........................X.......X..........X......................X..X..........................X................................................................................................................................................X.......................................................XXXXXXXXXXXX...
Beginner
2
277
341
87
18
304
B
304B
B. Calendar
1,300
brute force; implementation
Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows: Every year that is exactly divisible by four is a leap year,...
The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 ≀ yyyy ≀ 2038 and yyyy:mm:dd is a legal date).
Print a single integer β€” the answer to the problem.
Input: 1900:01:012038:12:31 | Output: 50768
Easy
2
769
129
51
3
373
A
373A
A. Collecting Beats is Fun
900
implementation
Cucumber boy is fan of Kyubeat, a famous music game.Kyubeat has 16 panels for playing arranged in 4 Γ— 4 table. When a panel lights up, he has to press that panel.Each panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most k panels in a time with his one...
The first line contains a single integer k (1 ≀ k ≀ 5) β€” the number of panels Cucumber boy can press with his one hand.Next 4 lines contain 4 characters each (digits from 1 to 9, or period) β€” table of panels. If a digit i was written on the panel, it means the boy has to press that panel in time i. If period was writte...
Output ""YES"" (without quotes), if he is able to press all the panels in perfect timing. If not, output ""NO"" (without quotes).
In the third sample boy cannot press all panels in perfect timing. He can press all the panels in timing in time 1, but he cannot press the panels in time 2 in timing with his two hands.
Input: 1.135124734685789 | Output: YES
Beginner
1
735
372
129
3
1,200
C
1200C
C. Round Corridor
1,400
math; number theory
Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by \(n\) sectors, and the outer area is equally divided by \(m\) sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer ar...
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 10^{18}\), \(1 \le q \le 10^4\)) β€” the number of sectors in the inner area, the number of sectors in the outer area and the number of questions.Each of the next \(q\) lines contains four integers \(s_x\), \(s_y\), \(e_x\), \(e_y\) (\(1 \le ...
For each question, print ""YES"" if Amugae can move from \((s_x, s_y)\) to \((e_x, e_y)\), and ""NO"" otherwise.You can print each letter in any case (upper or lower).
Example is shown on the picture in the statement.
Input: 4 6 3 1 1 2 3 2 6 1 2 2 6 2 4 | Output: YES NO YES
Easy
2
774
547
167
12
754
C
754C
C. Vladik and chat
2,200
brute force; constructive algorithms; dp; implementation; strings
Recently Vladik discovered a new entertainment β€” coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In...
The first line contains single integer t (1 ≀ t ≀ 10) β€” the number of chats. The t chats follow. Each chat is given in the following format.The first line of each chat description contains single integer n (1 ≀ n ≀ 100) β€” the number of users in the chat.The next line contains n space-separated distinct usernames. Each ...
Print the information about the t chats in the following format:If it is not possible to recover senders, print single line ""Impossible"" for this chat. Otherwise print m messages in the following format:<username>:<text>If there are multiple answers, print any of them.
Input: 12Vladik netman2?: Hello, Vladik!?: Hi | Output: netman: Hello, Vladik!Vladik: Hi
Hard
5
916
1,759
271
7
1,131
C
1131C
C. Birthday
1,200
binary search; greedy; sortings
Cowboy Vlad has a birthday today! There are \(n\) children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standi...
The first line contains a single integer \(n\) (\(2 \leq n \leq 100\)) β€” the number of the children who came to the cowboy Vlad's birthday.The second line contains integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) denoting heights of every child.
Print exactly \(n\) integers β€” heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.If there are multiple possible answers, print any of them.
In the first example, the discomfort of the circle is equal to \(1\), since the corresponding absolute differences are \(1\), \(1\), \(1\) and \(0\). Note, that sequences \([2, 3, 2, 1, 1]\) and \([3, 2, 1, 1, 2]\) form the same circles and differ only by the selection of the starting point.In the second example, the d...
Input: 5 2 1 1 3 2 | Output: 1 2 3 2 1
Easy
3
1,048
258
213
11
797
D
797D
D. Broken BST
2,100
data structures; dfs and similar
Let T be arbitrary binary tree β€” tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β€” it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree T: Set pointe...
First line contains integer number n (1 ≀ n ≀ 105) β€” number of vertices in the tree.Each of the next n lines contains 3 numbers v, l, r (0 ≀ v ≀ 109) β€” value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number - 1 is ...
Print number of times when search algorithm will fail.
In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.
Input: 315 -1 -110 1 35 -1 -1 | Output: 2
Hard
2
1,588
402
54
7
1,882
D
1882D
D. Tree XOR
1,900
bitmasks; dfs and similar; dp; greedy; trees
You are given a tree with \(n\) vertices labeled from \(1\) to \(n\). An integer \(a_{i}\) is written on vertex \(i\) for \(i = 1, 2, \ldots, n\). You want to make all \(a_{i}\) equal by performing some (possibly, zero) spells.Suppose you root the tree at some vertex. On each spell, you can select any vertex \(v\) and ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^{4}\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^{5}\)).The second line of each test case contains \(n\) integers \...
For each test case, print \(m_1, m_2, \ldots, m_n\) on a new line.
In the first test case, to find \(m_1\) we root the tree at vertex \(1\). In the first spell, choose \(v=2\) and \(c=1\). After performing the spell, \(a\) will become \([3, 3, 0, 1]\). The cost of this spell is \(3\). In the second spell, choose \(v=3\) and \(c=3\). After performing the spell, \(a\) will become \([3, ...
Input: 243 2 1 01 22 32 41100 | Output: 8 6 12 10 0
Hard
5
953
678
66
18
1,725
J
1725J
J. Journey
2,500
dp; trees
One day, Pak Chanek who is already bored of being alone at home decided to go traveling. While looking for an appropriate place, he found that Londonesia has an interesting structure.According to the information gathered by Pak Chanek, there are \(N\) cities numbered from \(1\) to \(N\). The cities are connected to eac...
The first line contains a single integer \(N\) (\(1 \le N \le 10^5\)) β€” the number of cities in Londonesia.The \(i\)-th of the next \(N-1\) lines contains three integers \(U_i\), \(V_i\), and \(W_i\) (\(1 \le U_i, V_i \le N\), \(1 \le W_i \le 10^9\)) β€” a two-directional road that connects cities \(U_i\) and \(V_i\) tha...
Output one integer, which represents the minimum time in hours that is needed to visit each city at least once.
In the first example, the journey that has the minimum time is \(2 β†’ 1 \xrightarrow{\text{teleport}} 4 β†’ 3\).In the second example, the journey that has the minimum time is \(3 β†’ 1 β†’ 4 β†’ 1 β†’ 2 \xrightarrow{\text{teleport}} 5\).
Input: 4 1 2 4 2 3 5 3 4 4 | Output: 8
Expert
2
1,115
395
111
17
209
B
209B
B. Pixels
2,100
constructive algorithms; math
Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors x and y (x β‰  y) meet in a violent fight, then the pixel t...
The first line contains three space-separated integers a, b and c (0 ≀ a, b, c ≀ 231; a + b + c > 0) β€” the number of red, green and blue pixels, correspondingly.
Print a single number β€” the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1.
In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels...
Input: 1 1 1 | Output: 1
Hard
2
854
161
181
2
1,703
F
1703F
F. Yet Another Problem About Pairs Satisfying an Inequality
1,300
binary search; data structures; dp; greedy; sortings
You are given an array \(a_1, a_2, \dots a_n\). Count the number of pairs of indices \(1 \leq i, j \leq n\) such that \(a_i < i < a_j < j\).
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β€” the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \leq a_i \leq 10^9\))...
For each test case, output a single integer β€” the number of pairs of indices satisfying the condition in the statement.Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
For the first test cases the pairs are \((i, j)\) = \(\{(2, 4), (2, 8), (3, 8)\}\). The pair \((2, 4)\) is true because \(a_2 = 1\), \(a_4 = 3\) and \(1 < 2 < 3 < 4\). The pair \((2, 8)\) is true because \(a_2 = 1\), \(a_8 = 4\) and \(1 < 2 < 4 < 8\). The pair \((3, 8)\) is true because \(a_3 = 2\), \(a_8 = 4\) and \(2...
Input: 581 1 2 3 8 2 1 421 2100 2 1 6 3 4 1 2 8 321 100000000030 1000000000 2 | Output: 3 0 10 0 1
Easy
5
140
443
305
17
620
D
620D
D. Professor GukiZ and Two Arrays
2,200
binary search; two pointers
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.In one operation professor can swap some element from the array a and some element fro...
The first line contains integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array a.The second line contains n integers ai ( - 109 ≀ ai ≀ 109) β€” the elements of the array a.The third line contains integer m (1 ≀ m ≀ 2000) β€” the number of elements in the array b.The fourth line contains m integers bj ( - 109 ≀ bj ≀...
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.The second line should contain the number of swaps k (0 ≀ k ≀ 2).Each of the next k lines should contain two integers xp, yp (1 ≀ xp ≀ n, 1 ≀ yp ≀ m) β€” the index of the element in the array a and the index of the element...
Input: 55 4 3 2 141 1 1 1 | Output: 121 14 2
Hard
2
807
356
459
6
159
A
159A
A. Friends or Not
1,400
*special; greedy; implementation
Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that ...
The first line of the input contains two integers n and d (1 ≀ n, d ≀ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as ""Ai Bi ti"" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≀ i ≀ n). Ai and Bi are non-empty strings at...
In the first line print integer k β€” the number of pairs of friends. In the next k lines print pairs of friends as ""Ai Bi"" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.
In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
Input: 4 1vasya petya 1petya vasya 2anya ivan 2ivan anya 4 | Output: 1petya vasya
Easy
3
734
607
253
1
958
F2
958F2
F2. Lightsabers (medium)
1,800
binary search; two pointers
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of th...
The first line of the input contains n (1 ≀ n ≀ 2Β·105) and m (1 ≀ m ≀ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with ) – the desired counts of Jedi Knights with lightsaber...
Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1.
Input: 8 33 3 1 2 2 1 1 33 1 1 | Output: 1
Medium
2
1,201
348
228
9
2,037
D
2037D
D. Sharky Surfing
1,300
data structures; greedy; two pointers
Mualani loves surfing on her sharky surfboard!Mualani's surf path can be modeled by a number line. She starts at position \(1\), and the path ends at position \(L\). When she is at position \(x\) with a jump power of \(k\), she can jump to any integer position in the interval \([x, x+k]\). Initially, her jump power is ...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains three integers \(n\), \(m\), and \(L\) (\(1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9\)) β€” the number of hurdles, the number of power-ups, and the position of the end.The followin...
For each test case, output the minimum number of power-ups she must collect to reach position \(L\). If it is not possible, output \(-1\).
In the first test case, she can collect power-ups \(1\), \(2\), \(3\), and \(5\) to clear all hurdles.In the second test case, she cannot jump over the first hurdle.In the fourth test case, by collecting both power-ups, she can jump over the hurdle.
Input: 42 5 507 1430 402 23 13 518 222 324 3 504 615 1820 2634 381 28 210 21 4 1710 141 61 21 216 91 2 105 92 32 2 | Output: 4 -1 1 2
Easy
3
1,159
1,192
138
20
1,129
B
1129B
B. Wrong Answer
2,000
constructive algorithms
Consider the following problem: given an array \(a\) containing \(n\) integers (indexed from \(0\) to \(n-1\)), find \(\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i\). In this problem, \(1 \leq n \leq 2\,000\) and \(|a_i| \leq 10^6\).In an attempt to solve the problem described,...
The first and only line contains one integer \(k\) (\(1 \leq k \leq 10^9\)).
If there is no sought sequence, print ""-1"".Otherwise, in the first line, print one integer \(n\) (\(1 \leq n \leq 2\,000\)), denoting the number of elements in the sequence.Then, in the second line, print \(n\) space-separated integers: \(a_0, a_1, \ldots, a_{n-1}\) (\(|a_i| \leq 10^6\)).
The first sample corresponds to the example given in the problem statement.In the second sample, one answer is \(n = 7\) with \(a = [30, -12, -99, 123, -2, 245, -300]\), in which case find_answer(n, a) returns \(1098\), while the correct answer is \(1710\).
Input: 8 | Output: 4 6 -8 7 -42
Hard
1
1,468
76
291
11
1,974
C
1974C
C. Beautiful Triple Pairs
1,400
combinatorics; data structures
Polycarp was given an array \(a\) of \(n\) integers. He really likes triples of numbers, so for each \(j\) (\(1 \le j \le n - 2\)) he wrote down a triple of elements \([a_j, a_{j + 1}, a_{j + 2}]\).Polycarp considers a pair of triples \(b\) and \(c\) beautiful if they differ in exactly one position, that is, one of the...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i...
For each test case, output a single integer β€” the number of beautiful pairs of triples among the pairs of the form \([a_j, a_{j + 1}, a_{j + 2}]\).Note that the answer may not fit into 32-bit data types.
In the first example, \(a = [3, 2, 2, 2, 3]\), Polycarp will write the following triples: \([3, 2, 2]\); \([2, 2, 2]\); \([2, 2, 3]\). The beautiful pairs are triple \(1\) with triple \(2\) and triple \(2\) with triple \(3\).In the third example, \(a = [1, 2, 3, 2, 2, 3, 4, 2]\), Polycarp will write the following tripl...
Input: 853 2 2 2 351 2 1 2 181 2 3 2 2 3 4 242 1 1 182 1 1 2 1 1 1 172 1 1 1 1 1 162 1 1 1 1 152 1 1 1 1 | Output: 2 0 3 1 8 4 3 2
Easy
2
619
478
203
19
690
C2
690C2
C2. Brain Network (medium)
1,500
dfs and similar; graphs; trees
Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of i...
The first line of the input contains two space-separated integers n and m (1 ≀ n, m ≀ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector ...
Print one number – the brain latency.
Input: 4 31 21 31 4 | Output: 2
Medium
3
983
390
37
6
193
B
193B
B. Xor
2,000
brute force
John Doe has four arrays: a, b, k, and p. Each array consists of n integers. Elements of all arrays are indexed starting from 1. Array p is a permutation of integers 1 to n.John invented a game for his friends and himself. Initially a player is given array a. The player must consecutively execute exactly u operations o...
The first line contains space-separated integers n, u and r (1 ≀ n, u ≀ 30, 0 ≀ r ≀ 100) β€” the number of elements in each array, the number of operations and the number that describes one of the operations. Each of the next four lines contains n space-separated integers β€” arrays a, b, k, p. The first line has array a, ...
On a single line print number s β€” the maximum number of points that a player can win in John's game.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample you should first apply the operation of the first type, then the operation of the second type.
Input: 3 2 17 7 78 8 81 2 31 3 2 | Output: 96
Hard
1
959
630
247
1
1,010
B
1010B
B. Rocket
1,800
binary search; interactive
This is an interactive problem.Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.L...
The first line contains two integers \(m\) and \(n\) (\(1 \le m \le 10^9\), \(1 \le n \le 30\)) β€” the maximum distance to Mars and the number of elements in the sequence \(p\).
In the example, hacking would look like this:5 2 31 0This means that the current distance to Mars is equal to \(3\), Natasha knows that it does not exceed \(5\), and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...Really:on the first query (\(1\)) the correct answer is \(1\), the rocket a...
Input: 5 21-1-110 | Output: 12453
Medium
2
1,829
176
0
10
702
F
702F
F. T-Shirts
2,800
data structures
The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all n types of t-shirts go on sale. The t-shirt of the i-th type has two integer parameters β€” ci and qi, where ci β€” is the price of the i-th type t-shirt, qi β€” is the quality of the i-th type t-shirt. It should be assumed th...
The first line contains the positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of t-shirt types. Each of the following n lines contains two integers ci and qi (1 ≀ ci, qi ≀ 109) β€” the price and the quality of the i-th type t-shirt.The next line contains the positive integer k (1 ≀ k ≀ 2Β·105) β€” the number of the customers....
The first line of the input data should contain the sequence of k integers, where the i-th number should be equal to the number of t-shirts, which the i-th customer will buy.
In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of ...
Input: 37 53 54 3213 14 | Output: 2 3
Master
1
1,260
495
174
7
1,932
E
1932E
E. Final Countdown
1,600
implementation; math; number theory
You are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero. The countdown consists of \(n\) (\(1 \le n \le 4 \cdot 10^5\)) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state fro...
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1\le n\le 4\cdot 10^5\)). The second line contains a string of \(n\) digits, the current state of ...
For each test case, print a single integer without leading zeroes, the number of seconds left before the countdown reaches zero. Note that this number may be huge.
In the first example, there are four changes that take 2 seconds: 40 to 39, 30 to 29, 20 to 19, and 10 to 09, other changes take 1 second each. So the total time is \(2\cdot 4 + 1\cdot(42-4) = 46\).
Input: 52425123452994000527456480697259671309012631002 | Output: 46 13715 108 5 507200774732968121125145546
Medium
3
734
451
163
19
1,822
E
1822E
E. Making Anti-Palindromes
1,600
greedy; math; strings
You are given a string \(s\), consisting of lowercase English letters. In one operation, you are allowed to swap any two characters of the string \(s\).A string \(s\) of length \(n\) is called an anti-palindrome, if \(s[i] \ne s[n - i + 1]\) for every \(i\) (\(1 \le i \le n\)). For example, the strings ""codeforces"", ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows.Each test case consists of two lines. The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the string \(s\).The second line contains the st...
For each test case, output a single integer β€” the minimum number of operations required to make the string \(s\) an anti-palindrome, or \(-1\) if this is not possible.
In the first test case, the string ""codeforces"" is already an anti-palindrome, so the answer is \(0\).In the second test case, it can be shown that the string ""abc"" cannot be transformed into an anti-palindrome by performing the allowed operations, so the answer is \(-1\).In the third test case, it is enough to swa...
Input: 1010codeforces3abc10taarrrataa10dcbdbdcccc4wwww12cabbaccabaac10aadaaaaddc14aacdaaaacadcdc6abccba12dcbcaebacccd | Output: 0 -1 1 1 -1 3 -1 2 2 2
Medium
3
547
448
167
18
154
B
154B
B. Colliders
1,600
math; number theory
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.In 2312 there was a startling discovery: a...
The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 105) β€” the number of colliders and the number of requests, correspondingly.Next m lines contain numbers of requests, one per line, in the form of either ""+ i"" (without the quotes) β€” activate the i-th collider, or ""- i"" (without the quotes) β€” d...
Print m lines β€” the results of executing requests in the above given format. The requests should be processed in the order, in which they are given in the input. Don't forget that the responses to the requests should be printed without quotes.
Note that in the sample the colliders don't turn on after the second and ninth requests. The ninth request could also receive response ""Conflict with 3"".
Input: 10 10+ 6+ 10+ 5- 10- 5- 6+ 10+ 3+ 6+ 3 | Output: SuccessConflict with 6SuccessAlready offSuccessSuccessSuccessSuccessConflict with 10Already on
Medium
2
2,177
360
243
1
2,068
F
2068F
F. Mascot Naming
1,900
brute force; greedy; implementation; strings
When organizing a big event, organizers often handle side tasks outside their expertise. For example, the chief judge of EUC 2025 must find a name for the event's official mascot while satisfying certain constraints: The name must include specific words as subsequences\(^{\texttt{*}}\), such as the event name and locat...
The first line contains an integer \(n\) (\(1\le n\le 200\,000\)) β€” the number of words that shall appear as subsequences.The \(i\)-th of the following \(n\) lines contains the string \(s_i\) (\(1\le |s_i| \le 200\,000\), \(s_i\) consists of lowercase English letters) β€” the \(i\)-th word in the list of words that shall...
Print \(\texttt{YES}\) if there is a valid name for the mascot. Otherwise, print \(\texttt{NO}\).If there is a valid name, on the next line print a valid name. The string you print must have length at most \(1\,000\,000\) and must consist of lowercase English letters. One can prove that if a valid name for the mascot e...
In the first sample, the words that must appear as subsequences are \(\texttt{porto}\) and \(\texttt{euc}\), while the word that must not appear as a subsequence is \(\texttt{prague}\). There are many valid names for the mascot, e.g., \(\texttt{poretuco}\) or \(\texttt{xxxpppeortoucyyyy}\).If \(\texttt{poretuco}\) is c...
Input: 2portoeucprague | Output: YES poretuco
Hard
4
899
608
436
20
63
D
63D
D. Dividing Island
1,900
constructive algorithms
A revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.The island can be conventionally represented as two rect...
The first line contains 5 space-separated integers β€” a, b, c, d and n (1 ≀ a, b, c, d ≀ 50, b β‰  d, 1 ≀ n ≀ 26). The second line contains n space-separated numbers. The i-th of them is equal to number xi (1 ≀ xi ≀ a Γ— b + c Γ— d). It is guaranteed that .
If dividing the island between parties in the required manner is impossible, print ""NO"" (without the quotes). Otherwise, print ""YES"" (also without the quotes) and, starting from the next line, print max(b, d) lines each containing a + c characters. To mark what square should belong to what party, use lowercase Lati...
Input: 3 4 2 2 35 8 3 | Output: YESaaabbaabbbcbb..ccb..
Hard
1
1,041
252
757
0
74
A
74A
A. Room Leader
1,000
implementation
Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems...
The first line contains an integer n, which is the number of contestants in the room (1 ≀ n ≀ 50). The next n lines contain the participants of a given room. The i-th line has the format of ""handlei plusi minusi ai bi ci di ei"" β€” it is the handle of a contestant, the number of successful hacks, the number of unsucces...
Print on the single line the handle of the room leader.
The number of points that each participant from the example earns, are as follows: Petr β€” 3860 tourist β€” 4140 Egor β€” 4030 c00lH4x0R β€” - 350 some_participant β€” 2220 Thus, the leader of the room is tourist.
Input: 5Petr 3 1 490 920 1000 1200 0tourist 2 0 490 950 1100 1400 0Egor 7 0 480 900 950 0 1000c00lH4x0R 0 10 150 0 0 0 0some_participant 2 1 450 720 900 0 0 | Output: tourist
Beginner
1
902
1,109
55
0
1,470
B
1470B
B. Strange Definition
1,900
bitmasks; graphs; hashing; math; number theory
Let us call two integers \(x\) and \(y\) adjacent if \(\frac{lcm(x, y)}{gcd(x, y)}\) is a perfect square. For example, \(3\) and \(12\) are adjacent, but \(6\) and \(9\) are not.Here \(gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\), and \(lcm(x, y)\) denotes the least common multiple ...
The first input line contains a single integer \(t\) (\(1 \le t \le 10^5)\) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β€” the length of the array.The following line contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10^6\)) β€” arra...
For each query output a single integer β€” the beauty of the array at the corresponding moment.
In the first test case, the initial array contains elements \([6, 8, 4, 2]\). Element \(a_4=2\) in this array is adjacent to \(a_4=2\) (since \(\frac{lcm(2, 2)}{gcd(2, 2)}=\frac{2}{2}=1=1^2\)) and \(a_2=8\) (since \(\frac{lcm(8,2)}{gcd(8, 2)}=\frac{8}{2}=4=2^2\)). Hence, \(d_4=2\), and this is the maximal possible valu...
Input: 2 4 6 8 4 2 1 0 6 12 3 20 5 80 1 1 1 | Output: 2 3
Hard
5
880
725
93
14
1,011
A
1011A
A. Stages
900
greedy; implementation; sortings
Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string β€” concatenation of letters, which correspond to the stages.There are \(n\) stages available. The ro...
The first line of input contains two integers β€” \(n\) and \(k\) (\(1 \le k \le n \le 50\)) – the number of available stages and the number of stages to use in the rocket.The second line contains string \(s\), which consists of exactly \(n\) lowercase Latin letters. Each letter defines a new stage, which can be used to ...
Print a single integer β€” the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.
In the first example, the following rockets satisfy the condition: ""adx"" (weight is \(1+4+24=29\)); ""ady"" (weight is \(1+4+25=30\)); ""bdx"" (weight is \(2+4+24=30\)); ""bdy"" (weight is \(2+4+25=31\)).Rocket ""adx"" has the minimal weight, so the answer is \(29\).In the second example, target rocket is ""belo"". I...
Input: 5 3xyabd | Output: 29
Beginner
3
1,138
374
118
10
447
A
447A
A. DZY Loves Hash
800
implementation
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b d...
The first line contains two integers, p and n (2 ≀ p, n ≀ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≀ xi ≀ 109).
Output a single integer β€” the answer to the problem.
Input: 10 5021534153 | Output: 4
Beginner
1
657
140
52
4
1,443
E
1443E
E. Long Permutation
2,400
brute force; math; two pointers
A permutation is a sequence of integers from \(1\) to \(n\) of length \(n\) containing each number exactly once. For example, \([1]\), \([4, 3, 5, 1, 2]\), \([3, 2, 1]\) β€” are permutations, and \([1, 1]\), \([4, 3, 1]\), \([2, 3, 4]\) β€” no.Permutation \(a\) is lexicographically smaller than permutation \(b\) (they have...
The first line contains two integers \(n\) (\(2 \le n \le 2 \cdot 10^5\)) and \(q\) (\(1 \le q \le 2 \cdot 10^5\)), where \(n\) β€” the length of the initial permutation, and \(q\) β€” the number of queries.The next \(q\) lines contain a single query of the \(1\)-st or \(2\)-nd type. The \(1\)-st type query consists of thr...
For each query of the \(1\)-st type, output on a separate line one integer β€” the required sum.
Initially, the permutation has the form \([1, 2, 3, 4]\). Queries processing is as follows: \(2 + 3 + 4 = 9\); \([1, 2, 3, 4] \rightarrow [1, 2, 4, 3] \rightarrow [1, 3, 2, 4] \rightarrow [1, 3, 4, 2]\); \(1 + 3 = 4\); \(4 + 2 = 6\)
Input: 4 4 1 2 4 2 3 1 1 2 1 3 4 | Output: 9 4 6
Expert
3
1,764
550
94
14
309
D
309D
D. Tennis Rackets
2,700
brute force; geometry
Professional sport is more than hard work. It also is the equipment, designed by top engineers. As an example, let's take tennis. Not only should you be in great shape, you also need an excellent racket! In this problem your task is to contribute to the development of tennis and to help to design a revolutionary new co...
The first and the only input line contains two integers n, m .
Print a single number β€” the answer to the problem.
For the following picture n = 8, m = 2. White circles are the holes for ventilation, red circles β€” holes for net stretching. One of the possible obtuse triangles is painted red.
Input: 3 0 | Output: 9
Master
2
1,210
62
50
3
1,207
F
1207F
F. Remainder Problem
2,100
brute force; data structures; implementation
You are given an array \(a\) consisting of \(500000\) integers (numbered from \(1\) to \(500000\)). Initially all elements of \(a\) are zero.You have to process two types of queries to this array: \(1\) \(x\) \(y\) β€” increase \(a_x\) by \(y\); \(2\) \(x\) \(y\) β€” compute \(\sum\limits_{i \in R(x, y)} a_i\), where \(R(x...
The first line contains one integer \(q\) (\(1 \le q \le 500000\)) β€” the number of queries.Then \(q\) lines follow, each describing a query. The \(i\)-th line contains three integers \(t_i\), \(x_i\) and \(y_i\) (\(1 \le t_i \le 2\)). If \(t_i = 1\), then it is a query of the first type, \(1 \le x_i \le 500000\), and \...
For each query of type \(2\) print one integer β€” the answer to it.
Input: 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 | Output: 4 4 0
Hard
3
452
521
66
12
650
C
650C
C. Table Compression
2,200
dfs and similar; dp; dsu; graphs; greedy
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.Petya decided to compress tables. He is given a table a consisting of n rows and m colum...
The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.Each of the following n rows contain m integers ai, j (1 ≀ ai, j ≀ 109) that are the values in the table.
Output the compressed table in form of n lines each containing m integers.If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
In the first sample test, despite the fact a1, 2 β‰  a21, they are not located in the same row or column so they may become equal after the compression.
Input: 2 21 23 4 | Output: 1 22 3
Hard
5
1,006
237
217
6
180
F
180F
F. Mathematical Analysis Rocks!
1,200
constructive algorithms; implementation; math
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≀ i ≀ n) has a best friend p[i] (1 ≀ p[i] ≀ n). In fact, each student is a best frie...
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≀ ai ≀ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≀ bi ≀ n).
Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≀ p[i] ≀ n). It is guaranteed that the solution exists and that it is unique.
Input: 42 1 4 33 4 2 1 | Output: 4 3 1 2
Easy
3
1,584
261
141
1
260
B
260B
B. Ancient Prophesy
1,600
brute force; implementation; strings
A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters ""-"".We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format ""dd-mm-yyyy"". We'll say t...
The first line contains the Prophesy: a non-empty string that only consists of digits and characters ""-"". The length of the Prophesy doesn't exceed 105 characters.
In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.
Input: 777-444---21-12-2013-12-2013-12-2013---444-777 | Output: 13-12-2013
Medium
3
1,278
165
104
2
1,903
F
1903F
F. Babysitting
2,500
2-sat; binary search; data structures; graphs; trees
Theofanis wants to play video games, however he should also take care of his sister. Since Theofanis is a CS major, he found a way to do both. He will install some cameras in his house in order to make sure his sister is okay.His house is an undirected graph with \(n\) nodes and \(m\) edges. His sister likes to play at...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^{5}, 1 \le m \le 2 \cdot 10^{5}\)) β€” the number of nodes and the number of edges. The \(i\)-th of the following \(m\) lines in the tes...
For each test case, print the maximum minimum difference between indices of the chosen nodes over all vertex covers.
In the first test case, we can install cameras at nodes \(1\), \(3\), and \(7\), so the answer is \(2\). In the second test case, we can install only one camera at node \(1\), so the answer is \(3\).
Input: 37 61 21 31 41 62 35 73 31 21 31 12 41 21 22 11 1 | Output: 2 3 2
Expert
5
992
646
116
19
926
E
926E
E. Merge Equal Elements
1,900
constructive algorithms; data structures
You are given a sequence of positive integers a1, a2, ..., an. While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a sin...
The first line contains a single integer n (2 ≀ n ≀ 2Β·105) β€” the number of elements in the sequence.The second line contains the sequence of integers a1, a2, ..., an (1 ≀ ai ≀ 109).
In the first line print a single integer k β€” the number of elements in the sequence after you stop performing the operation. In the second line print k integers β€” the sequence after you stop performing the operation.
The first example is described in the statements.In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third o...
Input: 65 2 1 1 2 2 | Output: 25 4
Hard
2
909
181
216
9
1,267
K
1267K
K. Key Storage
2,100
combinatorics; math
Karl is developing a key storage service. Each user has a positive integer key.Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.Kar...
The first line contains an integer \(t\) (\(1 \le t \le 50\,000\)) β€” the number of commonly used keys to examine. Each of the next \(t\) lines contains one integer \(k_i\) (\(1 \le k_i \le 10^{18}\)) β€” the key itself.
For each of the keys print one integer β€” the number of other keys that have the same fingerprint.
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders \([1, 1, 2]\). So both numbers have the fingerprint multiset \(\{1, 1, 2\}\).
Input: 3 1 11 123456 | Output: 0 1 127
Hard
2
1,496
217
97
12
1,647
A
1647A
A. Madoka and Math Dad
800
implementation; math
Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is \(n\).Madoka is too tired o...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Description of the test cases follows.The only line of each test case contains an integer \(n\) (\(1 \le n \le 1000\)) β€” the required sum of the digits.
For each test case print the maximum number you can obtain.
The only numbers with the sum of digits equal to \(2\) without zeros are \(2\) and \(11\). But the last one has two ones in a row, so it's not valid. That's why the answer is \(2\).The only numbers with the sum of digits equal to \(3\) without zeros are \(111\), \(12\), \(21\), and \(3\). The first one has \(2\) ones i...
Input: 512345 | Output: 1 2 21 121 212
Beginner
2
382
290
59
16
1,250
G
1250G
G. Discarding Game
2,300
dp; greedy; two pointers
Eulampius has created a game with the following rules: there are two players in the game: a human and a computer; the game lasts for no more than \(n\) rounds. Initially both players have \(0\) points. In the \(j\)-th round the human gains \(a_j\) points, and the computer gains \(b_j\) points. The points are gained sim...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10000\)) β€” the number of test cases. Then the test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(2 \le k \le 10^9\)) β€” the maximum possible number of rounds in the game and the nu...
Print the answers for all test cases in the order they appear in the input.If Polycarpus cannot win the game, then simply print one line ""-1"" (without quotes). In this case, you should not output anything else for that test case. Otherwise, the first line of the test case answer should contain one integer \(d\) β€” the...
In the second test case, if the human pushes the ""Reset"" button after the second and the fourth rounds, the game goes as follows: after the first round the human has \(5\) points, the computer β€” \(4\) points; after the second round the human has \(7\) points, the computer β€” \(10\) points; the human pushes the ""Reset...
Input: 3 4 17 1 3 5 7 3 5 7 9 11 17 5 2 8 2 4 6 1 2 7 2 5 4 6 3 3 5 1 7 4 2 5 3 6 17 6 1 2 7 2 5 1 7 4 2 5 3 | Output: 0 2 2 4 -1
Expert
3
1,706
831
782
12
95
D
95D
D. Horse Races
2,500
dp; math
Petya likes horse racing very much. Horses numbered from l to r take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the distance ...
The first line contains two integers t and k (1 ≀ t, k ≀ 1000) β€” the number of segments and the distance between the numbers correspondingly. Next t lines contain pairs of integers li and ri (1 ≀ l ≀ r ≀ 101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space c...
Output t lines. In each line print one integer β€” the answer for the corresponding segment modulo 1000000007 (109 + 7).
In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.In the second sample, only 74 and 77 are in the given segment.
Input: 1 21 100 | Output: 4
Expert
2
940
329
118
0
1,201
C
1201C
C. Maximum Median
1,400
binary search; greedy; math; sortings
You are given an array \(a\) of \(n\) integers, where \(n\) is odd. You can make the following operation with it: Choose one of the elements of the array (for example \(a_i\)) and increase it by \(1\) (that is, replace it with \(a_i + 1\)). You want to make the median of the array the largest possible using at most \(k...
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(n\) is odd, \(1 \le k \le 10^9\)) β€” the number of elements in the array and the largest number of operations you can make.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)).
Print a single integer β€” the maximum possible median after the operations.
In the first example, you can increase the second element twice. Than array will be \([1, 5, 5]\) and it's median is \(5\).In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is \(3\).In the third example, you can make four operations: increase first...
Input: 3 2 1 3 5 | Output: 5
Easy
4
507
299
74
12
1,608
F
1608F
F. MEX counting
3,200
combinatorics; dp; implementation
For an array \(c\) of nonnegative integers, \(MEX(c)\) denotes the smallest nonnegative integer that doesn't appear in it. For example, \(MEX([0, 1, 3]) = 2\), \(MEX([42]) = 0\).You are given integers \(n, k\), and an array \([b_1, b_2, \ldots, b_n]\).Find the number of arrays \([a_1, a_2, \ldots, a_n]\), for which the...
The first line of the input contains two integers \(n, k\) (\(1 \le n \le 2000\), \(0 \le k \le 50\)).The second line of the input contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-k \le b_i \le n+k\)) β€” elements of the array \(b\).
Output a single integer β€” the number of arrays which satisfy the conditions from the statement, modulo \(998\,244\,353\).
Input: 4 0 0 0 0 0 | Output: 256
Master
3
566
236
121
16
1,144
D
1144D
D. Equalize Them All
1,400
constructive algorithms; greedy
You are given an array \(a\) consisting of \(n\) integers. You can perform the following operations arbitrary number of times (possibly, zero): Choose a pair of indices \((i, j)\) such that \(|i-j|=1\) (indices \(i\) and \(j\) are adjacent) and set \(a_i := a_i + |a_i - a_j|\); Choose a pair of indices \((i, j)\) such ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).
In the first line print one integer \(k\) β€” the minimum number of operations required to obtain the array of equal elements.In the next \(k\) lines print operations itself. The \(p\)-th operation should be printed as a triple of integers \((t_p, i_p, j_p)\), where \(t_p\) is either \(1\) or \(2\) (\(1\) means that you ...
Input: 5 2 4 6 6 6 | Output: 2 1 2 3 1 1 2
Easy
2
857
279
765
11
1,954
D
1954D
D. Colored Balls
1,800
combinatorics; dp; math; sortings
There are balls of \(n\) different colors; the number of balls of the \(i\)-th color is \(a_i\).The balls can be combined into groups. Each group should contain at most \(2\) balls, and no more than \(1\) ball of each color.Consider all \(2^n\) sets of colors. For a set of colors, let's denote its value as the minimum ...
The first line contains a single integer \(n\) (\(1 \le n \le 5000\)) β€” the number of colors.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 5000\)) β€” the number of balls of the \(i\)-th color.Additional constraint on input: the total number of balls doesn't exceed \(5000\).
Print a single integer β€” the sum of values of all \(2^n\) sets of colors, taken modulo \(998\,244\,353\).
Consider the first example. There are \(8\) sets of colors: for the empty set, its value is \(0\); for the set \(\{1\}\), its value is \(1\); for the set \(\{2\}\), its value is \(1\); for the set \(\{3\}\), its value is \(2\); for the set \(\{1,2\}\), its value is \(1\); for the set \(\{1,3\}\), its value is \(2\); fo...
Input: 31 1 2 | Output: 11
Medium
4
740
309
105
19
1,417
B
1417B
B. Two Arrays
1,100
greedy; math; sortings
RedDreamer has an array \(a\) consisting of \(n\) non-negative integers, and an unlucky integer \(T\).Let's denote the misfortune of array \(b\) having length \(m\) as \(f(b)\) β€” the number of pairs of integers \((i, j)\) such that \(1 \le i < j \le m\) and \(b_i + b_j = T\). RedDreamer has to paint each element of \(a...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(T\) (\(1 \le n \le 10^5\), \(0 \le T \le 10^9\)) β€” the number of elements in the array and the unlucky integer, respectively. The s...
For each test case print \(n\) integers: \(p_1\), \(p_2\), ..., \(p_n\) (each \(p_i\) is either \(0\) or \(1\)) denoting the colors. If \(p_i\) is \(0\), then \(a_i\) is white and belongs to the array \(c\), otherwise it is black and belongs to the array \(d\).If there are multiple answers that minimize the value of \(...
Input: 2 6 7 1 2 3 4 5 6 3 6 3 3 3 | Output: 1 0 0 1 1 0 1 0 0
Easy
3
1,195
502
353
14
1,615
F
1615F
F. LEGOndary Grandmaster
2,800
combinatorics; dp; math
After getting bored by playing with crayons, you decided to switch to Legos! Today, you're working with a long strip, with height \(1\) and length \(n\), some positions of which are occupied by \(1\) by \(1\) Lego pieces.In one second, you can either remove two adjacent Lego pieces from the strip (if both are present),...
The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases. Then \(t\) cases follow.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 2000\)) β€” the size of the Lego strip.The second line of each test case contains a string \(s\) of length \(n\), consisting o...
For each test case, output a single integer β€” the answer to the problem modulo \(1\,000\,000\,007\) (\(10^9 + 7\)).
For the first test case, \(00\) is the only possible starting state, and \(11\) is the only possible ending state. It takes exactly one operation to change \(00\) to \(11\).For the second test case, some of the possible starting and ending state pairs are: \((000, 011)\) β€” takes \(1\) operation. \((001, 100)\) β€” takes ...
Input: 6 2 00 11 3 ??? ??? 3 ??1 0?0 4 ??0? ??11 5 ????? 0??1? 10 ?01??01?1? ??100?1??? | Output: 1 16 1 14 101 1674
Master
3
1,292
847
115
16
1,998
A
1998A
A. Find K Distinct Points with Fixed Center
800
constructive algorithms; implementation; math
I couldn't think of a good title for this problem, so I decided to learn from LeetCode.β€” Sun Tzu, The Art of WarYou are given three integers \(x_c\), \(y_c\), and \(k\) (\(-100 \leq x_c, y_c \leq 100\), \(1 \leq k \leq 1000\)). You need to find \(k\) distinct points (\(x_1, y_1\)), (\(x_2, y_2\)), \(\ldots\), (\(x_k, y...
The first line contains \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases.Each test case contains three integers \(x_c\), \(y_c\), and \(k\) (\(-100 \leq x_c, y_c \leq 100\), \(1 \leq k \leq 1000\)) β€” the coordinates of the center and the number of distinct points you must output.It is guaranteed that the sum of...
For each test case, output \(k\) lines, the \(i\)-th line containing two space separated integers, \(x_i\) and \(y_i\), (\(-10^9 \leq x_i, y_i \leq 10^9\)) β€” denoting the position of the \(i\)-th point.If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constr...
For the first test case, \(\left( \frac{10}{1}, \frac{10}{1} \right) = (10, 10)\).For the second test case, \(\left( \frac{-1 + 5 - 4}{3}, \frac{-1 -1 + 2}{3} \right) = (0, 0)\).
Input: 410 10 10 0 3-5 -8 84 -5 3 | Output: 10 10 -1 -1 5 -1 -4 2 -6 -7 -5 -7 -4 -7 -4 -8 -4 -9 -5 -9 -6 -9 -6 -8 1000 -1000 -996 995 8 -10
Beginner
3
811
372
326
19
553
D
553D
D. Nudist Beach
2,300
binary search; graphs; greedy
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers.There are n cities, labeled from 1 to n, and m bidirectional roads between them. Currently, there are Life Fibers in every city...
The first line of input contains three integers n, m, k (2 ≀ n ≀ 100 000, 1 ≀ m ≀ 100 000, 1 ≀ k ≀ n - 1).The second line of input contains k integers, representing the cities with fortresses. These cities will all be distinct. The next m lines contain the roads. The i-th of these lines will have 2 integers ai, bi (1 ≀...
The first line should contain an integer r, denoting the size of an optimum set (1 ≀ r ≀ n - k). The second line should contain r integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses.If there are multiple possible answers, prin...
The first example case achieves a strength of 1/2. No other subset is strictly better.The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.
Input: 9 8 43 9 6 81 21 31 41 52 62 72 82 9 | Output: 31 4 5
Expert
3
1,413
460
334
5
1,163
F
1163F
F. Indecisive Taxi Fee
3,000
data structures; graphs; shortest paths
In the city of Capypaland where Kuro and Shiro resides, there are \(n\) towns numbered from \(1\) to \(n\) and there are \(m\) bidirectional roads numbered from \(1\) to \(m\) connecting them. The \(i\)-th road connects towns \(u_i\) and \(v_i\). Since traveling between the towns is quite difficult, the taxi industry i...
The first line contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m, q \le 2 \cdot 10^5\)) β€” the number of towns, the number of roads, and the number of plans that Kuro has drafted respectively.The \(i\)-th of the next \(m\) contains three integers \(u_i\), \(v_i\) and \(w_i\) (\(1 \l...
Print \(q\) integers β€” the lowest fee Shiro must pay to get from town \(1\) to town \(n\) in each of those \(q\) plans.
In the first example, the original overview of Capypaland looks like this, where the number next to each road denotes the original prices of the roads, The overview of the first plan, The lowest fee Shiro must pay in this plan is \(4\), which corresponds to the path \(1 \rightarrow 4\).The overview of the second plan, ...
Input: 4 5 6 1 2 2 2 4 3 1 4 7 1 3 1 3 4 5 3 4 5 1 3 8 1 4 2 1 3 1 | Output: 4 2 5 6 3 1
Master
3
1,568
767
119
11
499
B
499B
B. Lecture
1,000
implementation; strings
You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consist...
The first line contains two integers, n and m (1 ≀ n ≀ 3000, 1 ≀ m ≀ 3000) β€” the number of words in the professor's lecture and the number of words in each of these languages.The following m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the wo...
Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.
Input: 4 3codeforces codesecrofcontest roundletter messagecodeforces contest letter contest | Output: codeforces round letter round
Beginner
2
1,035
796
140
4
396
E
396E
E. On Iteration of One Well-Known Function
0
math
Of course, many of you can calculate Ο†(n) β€” the number of positive integers that are less than or equal to n, that are coprime with n. But what if we need to calculate Ο†(Ο†(...Ο†(n))), where function Ο† is taken k times and n is given in the canonical decomposition into prime factors? You are given n and k, calculate the ...
The first line contains integer m (1 ≀ m ≀ 105) β€” the number of distinct prime divisors in the canonical representaion of n.Each of the next m lines contains a pair of space-separated integers pi, ai (2 ≀ pi ≀ 106; 1 ≀ ai ≀ 1017) β€” another prime divisor of number n and its power in the canonical representation. The sum...
In the first line, print integer w β€” the number of distinct prime divisors of number Ο†(Ο†(...Ο†(n))), where function Ο† is taken k times.Each of the next w lines must contain two space-separated integers qi, bi (bi β‰₯ 1) β€” another prime divisor and its power in the canonical representaion of the result. Numbers qi must go ...
You can read about canonical representation of a positive integer here: http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic.You can read about function Ο†(n) here: http://en.wikipedia.org/wiki/Euler's_totient_function.
Input: 17 11 | Output: 22 13 1
Beginner
1
411
468
353
3
1,850
E
1850E
E. Cardboard for Pictures
1,100
binary search; geometry; implementation; math
Mircea has \(n\) pictures. The \(i\)-th picture is a square with a side length of \(s_i\) centimeters. He mounted each picture on a square piece of cardboard so that each picture has a border of \(w\) centimeters of cardboard on all sides. In total, he used \(c\) square centimeters of cardboard. Given the picture sizes...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The first line of each test case contains two positive integers \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) and \(c\) (\(1 \leq c \leq 10^{18}\)) β€” the number of paintings, and the amount of used square centimeters of cardboar...
For each test case, output a single integer β€” the value of \(w\) (\(w \geq 1\)) which was used to use exactly \(c\) squared centimeters of cardboard.
The first test case is explained in the statement.For the second test case, the chosen \(w\) was \(2\), thus the only cardboard covers an area of \(c = (2 \cdot 2 + 6)^2 = 10^2 = 100\) squared centimeters.For the third test case, the chosen \(w\) was \(4\), which obtains the covered area \(c = (2 \cdot 4 + 2)^2 \times ...
Input: 10 3 50 3 2 1 1 100 6 5 500 2 2 2 2 2 2 365 3 4 2 469077255466389 10000 2023 10 635472106413848880 9181 4243 7777 1859 2017 4397 14 9390 2245 7225 7 176345687772781240 9202 9407 9229 6257 7743 5738 7966 14 865563946464579627 3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384 19 9771620530088714...
Easy
4
556
806
149
18