contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,349
F2
1349F2
F2. Slime and Sequences (Hard Version)
3,500
dp; fft; math
Note that the only differences between easy and hard versions are the constraints on \(n\) and the time limit. You can make hacks only if all versions are solved.Slime is interested in sequences. He defined good positive integer sequences \(p\) of length \(n\) as follows: For each \(k>1\) that presents in \(p\), there ...
The first line contains one integer \(n\ (1\le n\le 100\,000)\).
Print \(n\) integers, the \(i\)-th of them should be equal to \(\left(\sum_{p\in s_n} f_p(i)\right)\ \textrm{mod}\ 998\,244\,353\).
In the first example, \(s=\{[1,1],[1,2]\}\).In the second example, \(s=\{[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,2],[1,2,3]\}\).In the third example, \(s=\{[1]\}\).
Input: 2 | Output: 3 1
Master
3
791
64
131
13
1,333
C
1333C
C. Eugene and an array
1,700
binary search; data structures; implementation; two pointers
Eugene likes working with arrays. And today he needs your help in solving one challenging task.An array \(c\) is a subarray of an array \(b\) if \(c\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.Let's cal...
The first line of the input contains a single integer \(n\) (\(1 \le n \le 2 \times 10^5\)) — the length of array \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)) — the elements of \(a\).
Output a single integer — the number of good subarrays of \(a\).
In the first sample, the following subarrays are good: \([1]\), \([1, 2]\), \([2]\), \([2, -3]\), \([-3]\). However, the subarray \([1, 2, -3]\) isn't good, as its subarray \([1, 2, -3]\) has sum of elements equal to \(0\).In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, th...
Input: 3 1 2 -3 | Output: 5
Medium
4
799
251
64
13
1,345
B
1345B
B. Card Constructions
1,100
binary search; brute force; dp; math
A card pyramid of height \(1\) is constructed by resting two cards against each other. For \(h>1\), a card pyramid of height \(h\) is constructed by placing a card pyramid of height \(h-1\) onto a base. A base consists of \(h\) pyramids of height \(1\), and \(h-1\) cards on top. For example, card pyramids of heights \(...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 1000\)) — the number of test cases. Next \(t\) lines contain descriptions of test cases.Each test case contains a single integer \(n\) (\(1\le n\le 10^9\)) — the number of cards.It is guaranteed that the sum of \(n\) ...
For each test case output a single integer — the number of pyramids you will have constructed in the end.
In the first test, you construct a pyramid of height \(1\) with \(2\) cards. There is \(1\) card remaining, which is not enough to build a pyramid.In the second test, you build two pyramids, each of height \(2\), with no cards remaining.In the third test, you build one pyramid of height \(3\), with no cards remaining.I...
Input: 5 3 14 15 24 1 | Output: 1 2 1 3 0
Easy
4
659
365
105
13
449
B
449B
B. Jzzhu and Cities
2,000
graphs; greedy; shortest paths
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One ...
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).It is guaranteed that there is at least one way...
Output a single integer representing the maximum number of the train routes which can be closed.
Input: 5 5 31 2 12 3 21 3 33 4 41 5 53 54 55 5 | Output: 2
Hard
3
733
487
96
4
1,275
F
1275F
F. Шардирование постов
0
*special; binary search; interactive
Это интерактивная задача.Когда данных становится слишком много и они не помещаются на один сервер, их приходится шардировать. Рассмотрим систему хранения постов пользователей, которая расположена на \(S\) серверах, нумеруемых с единицы. Каждый раз когда пользователь пишет пост, ему выдается уникальный идентификатор в п...
В примере на первом сервере хранятся посты с \(id\) 1, 3 и 10. А на втором 5 и 7. Необходимо найти третье по возрастанию число, это 5.
Input: 1 2 3 2 3 3 5 10 | Output: ? 1 2 ? 2 1 ? 1 3 ! 5
Beginner
3
1,002
0
0
12
366
A
366A
A. Dima and Guards
1,100
implementation
Nothing has changed since the last round. Dima and Inna still love each other and want to be together. They've made a deal with Seryozha and now they need to make a deal with the dorm guards...There are four guardposts in Dima's dorm. Each post contains two guards (in Russia they are usually elderly women). You can bri...
The first line of the input contains integer n (1 ≤ n ≤ 105) — the money Dima wants to spend. Then follow four lines describing the guardposts. Each line contains four integers a, b, c, d (1 ≤ a, b, c, d ≤ 105) — the minimum price of the chocolate and the minimum price of the juice for the first guard and the minimum p...
In a single line of the output print three space-separated integers: the number of the guardpost, the cost of the first present and the cost of the second present. If there is no guardpost Dima can sneak Inna through at such conditions, print -1 in a single line. The guardposts are numbered from 1 to 4 according to the...
Explanation of the first example.The only way to spend 10 rubles to buy the gifts that won't be less than the minimum prices is to buy two 5 ruble chocolates to both guards from the first guardpost.Explanation of the second example.Dima needs 12 rubles for the first guardpost, 14 for the second one, 16 for the fourth o...
Input: 105 6 5 66 6 7 75 8 6 69 9 9 9 | Output: 1 5 5
Easy
1
1,194
415
405
3
1,845
A
1845A
A. Forbidden Integer
800
constructive algorithms; implementation; math; number theory
You are given an integer \(n\), which you want to obtain. You have an unlimited supply of every integer from \(1\) to \(k\), except integer \(x\) (there are no integer \(x\) at all).You are allowed to take an arbitrary amount of each of these integers (possibly, zero). Can you make the sum of taken integers equal to \(...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of testcases.The only line of each testcase contains three integers \(n, k\) and \(x\) (\(1 \le x \le k \le n \le 100\)).
For each test case, in the first line, print ""YES"" or ""NO"" — whether you can take an arbitrary amount of each integer from \(1\) to \(k\), except integer \(x\), so that their sum is equal to \(n\).If you can, the second line should contain a single integer \(m\) — the total amount of taken integers. The third line ...
Another possible answer for the first testcase is \([3, 3, 3, 1]\). Note that you don't have to minimize the amount of taken integers. There also exist other answers.In the second testcase, you only have an unlimited supply of integer \(2\). There is no way to get sum \(5\) using only them.In the fifth testcase, there ...
Input: 510 3 25 2 14 2 17 7 36 1 1 | Output: YES 6 3 1 1 1 1 3 NO YES 2 2 2 YES 1 7 NO
Beginner
4
373
203
478
18
1,701
A
1701A
A. Grass Field
800
implementation
There is a field of size \(2 \times 2\). Each cell of this field can either contain grass or be empty. The value \(a_{i, j}\) is \(1\) if the cell \((i, j)\) contains grass, or \(0\) otherwise.In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choos...
The first line of the input contains one integer \(t\) (\(1 \le t \le 16\)) — the number of test cases. Then \(t\) test cases follow.The test case consists of two lines, each of these lines contains two integers. The \(j\)-th integer in the \(i\)-th row is \(a_{i, j}\). If \(a_{i, j} = 0\) then the cell \((i, j)\) is e...
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all \(a_{i, j}\) zeros) in the corresponding test case.
Input: 30 00 01 00 11 11 1 | Output: 0 1 2
Beginner
1
757
385
197
17
1,949
C
1949C
C. Annual Ants' Gathering
1,900
dfs and similar; dp; greedy; trees
Deep within a forest lies an ancient tree, home to \(n\) ants living in \(n\) tiny houses, indexed from \(1\) to \(n\), connected by the branches of the tree. Once a year, all the ants need to gather to watch the EUC. For this, all ants move along the \(n-1\) branches of the tree they live on to meet at the home of one...
The first line contains one integer \(n\) (\(1\leq n\leq 200\,000\)) — the number of ant homes.Each of the following \(n-1\) lines contains two integers \(u\) and \(v\) (\(1\leq u, v\leq n\)) — there is a branch directly connecting the house \(u\) and house \(v\). It is guaranteed that every ant can reach the house of ...
Print \(\texttt{YES}\) if it is possible to gather all the ants in a single house. Otherwise, print \(\texttt{NO}\).
In the first sample, you can gather all the ants at house \(3\) as follows: You tell to the ant at house \(4\) to move to house \(6\). You tell to the ant at house \(2\) to move to house \(3\). You tell to the two ants at house \(6\) to move to house \(3\) (which already contains two ants). You tell to the ant at house...
Input: 75 13 24 63 67 11 3 | Output: YES
Hard
4
918
377
116
19
1,667
A
1667A
A. Make it Increasing
1,300
brute force; greedy; math
You are given an array \(a\) consisting of \(n\) positive integers, and an array \(b\), with length \(n\). Initially \(b_i=0\) for each \(1 \leq i \leq n\).In one move you can choose an integer \(i\) (\(1 \leq i \leq n\)), and add \(a_i\) to \(b_i\) or subtract \(a_i\) from \(b_i\). What is the minimum number of moves ...
The first line contains a single integer \(n\) (\(2 \leq n \leq 5000\)).The second line contains \(n\) integers, \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \leq a_i \leq 10^9\)) — the elements of the array \(a\).
Print a single integer, the minimum number of moves to make \(b\) increasing.
Example \(1\): you can subtract \(a_1\) from \(b_1\), and add \(a_3\), \(a_4\), and \(a_5\) to \(b_3\), \(b_4\), and \(b_5\) respectively. The final array will be [\(-1\), \(0\), \(3\), \(4\), \(5\)] after \(4\) moves.Example \(2\): you can reach [\(-3\), \(-2\), \(-1\), \(0\), \(1\), \(2\), \(3\)] in \(10\) moves.
Input: 5 1 2 3 4 5 | Output: 4
Easy
3
426
205
77
16
316
C1
316C1
C1. Tidying Up
2,200
flows
Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dre...
The first line contains two space-separated integers n and m. They correspond to the dressing room size. Next n lines contain m space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. It is guaranteed that: n·m is even. All numbers, corresponding to the numbers of ...
Print exactly one integer — the minimum number of the sneakers that need to change their location.
The second sample.
Input: 2 31 1 22 3 3 | Output: 2
Hard
1
1,621
583
98
3
162
E
162E
E. HQ9+
1,800
*special
HQ9+ is a joke programming language which has only four one-character instructions: ""H"" prints ""Hello, World!"", ""Q"" prints the whole source code of the program itself (at each call), ""9"" prints the lyrics of ""99 Bottles of Beer"" song, ""+"" increments the value stored in the internal accumulator.Instructions ...
The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.
Output ""YES"", if executing the program will produce any output, and ""NO"" otherwise (quotes for clarity only).
In the first case the program contains only one instruction — ""H"", which prints ""Hello, World!"".In the second case none of the program characters are language instructions.
Input: Hello! | Output: YES
Medium
1
566
239
113
1
135
B
135B
B. Rectangle and Square
1,600
brute force; geometry; math
Little Petya very much likes rectangles and especially squares. Recently he has received 8 points on the plane as a gift from his mother. The points are pairwise distinct. Petya decided to split them into two sets each containing 4 points so that the points from the first set lay at the vertexes of some square and the ...
You are given 8 pairs of integers, a pair per line — the coordinates of the points Petya has. The absolute value of all coordinates does not exceed 104. It is guaranteed that no two points coincide.
Print in the first output line ""YES"" (without the quotes), if the desired partition exists. In the second line output 4 space-separated numbers — point indexes from the input, which lie at the vertexes of the square. The points are numbered starting from 1. The numbers can be printed in any order. In the third line p...
Pay attention to the third example: the figures do not necessarily have to be parallel to the coordinate axes.
Input: 0 010 1110 00 111 12 22 11 2 | Output: YES5 6 7 81 2 3 4
Medium
3
806
198
597
1
331
A2
331A2
A2. Oh Sweet Beaverette
1,500
data structures; sortings
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming wa...
The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints...
In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right....
Input: 51 2 3 1 2 | Output: 8 11
Medium
2
1,133
444
440
3
1,349
C
1349C
C. Orac and Game of Life
2,000
dfs and similar; graphs; implementation; shortest paths
Please notice the unusual memory limit of this problem.Orac likes games. Recently he came up with the new game, ""Game of Life"".You should play this game on a black and white grid with \(n\) rows and \(m\) columns. Each cell is either black or white.For each iteration of the game (the initial iteration is \(0\)), the ...
The first line contains three integers \(n,m,t\ (1\le n,m\le 1000, 1\le t\le 100\,000)\), representing the number of rows, columns, and the number of Orac queries.Each of the following \(n\) lines contains a binary string of length \(m\), the \(j\)-th character in \(i\)-th line represents the initial color of cell \((i...
Print \(t\) lines, in \(i\)-th line you should print the answer to the \(i\)-th query by Orac. If the color of this cell is black, you should print '1'; otherwise, you should write '0'.
For the first example, the picture above shows the initial situation and the color of cells at the iteration \(1\), \(2\), and \(3\). We can see that the color of \((1,1)\) at the iteration \(1\) is black, the color of \((2,2)\) at the iteration \(2\) is black, and the color of \((3,3)\) at the iteration \(3\) is also ...
Input: 3 3 3 000 111 000 1 1 1 2 2 2 3 3 3 | Output: 1 1 1
Hard
4
861
517
185
13
190
D
190D
D. Non-Secret Cypher
1,900
two pointers
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the informati...
The first line contains two space-separated integers n, k (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.
Print the single number — the number of such subarrays of array a, that they have at least k equal integers.Please do not use the %lld specifier to read or write 64-bit integers in С++. In is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1...
Input: 4 21 2 1 2 | Output: 3
Hard
1
1,171
291
254
1
294
B
294B
B. Shaass and Bookshelf
1,700
dp; greedy
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights. Shaass puts the books on the bo...
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Input: 51 121 32 152 52 1 | Output: 5
Medium
2
745
217
107
2
2,066
D2
2066D2
D2. Club of Young Aircraft Builders (hard version)
2,900
combinatorics; dp; math
This is the hard version of the problem. The difference between the versions is that in this version, not necessary \(a_i = 0\). You can hack only if you solved all versions of this problem. There is a building with \(n\) floors, numbered from \(1\) to \(n\) from bottom to top. There is exactly one person living on eac...
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, c, m\) (\(1 \le n \le 100\), \(1 \le c \le 100\), \(c \le m \le n \cdot c\)) — the number of flo...
For each test case, output the number of ways to fill in the gaps with numbers from \(1\) to \(n\), so that the chronology of the airplane launches could be credible, modulo \(10^9 + 7\).
In the first test example, all six possible ways to fill in the gaps are as follows: \([1, 1, 3, 3]\) \([1, 2, 3, 3]\) \([1, 3, 2, 3]\) \([2, 1, 3, 3]\) \([2, 2, 3, 3]\) \([3, 1, 2, 3]\)Note that the array \([2, 3, 1, 3]\) is not a valid way to fill in the gaps, as the third airplane could not have been launched by the...
Input: 83 2 40 0 0 05 5 70 0 0 0 0 0 06 1 32 0 02 3 50 0 1 0 23 3 43 3 3 02 1 20 12 1 20 25 3 120 0 1 0 2 4 0 0 0 5 0 5 | Output: 6 190 3 2 0 0 1 14
Master
3
1,534
745
187
20
1,903
D1
1903D1
D1. Maximum And Queries (easy version)
1,700
binary search; bitmasks; brute force; greedy
This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\) and \(q\), the memory and time limits. You can make hacks only if all versions of the problem are solved.Theofanis really likes to play with the bits of numbers. He has an array \(a\) of size \(n\) and an in...
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\) and \(n \cdot q \le 10^5\)) — the size of the array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)) — the elements of the array.Next \(q\) lines describe the queries. The \(...
For each query, print one integer — the maximum bitwise AND that array \(a\) can have after at most \(k_i\) operations.
In the first test case, in the first query, we add \(1\) in the first and last elements of the array. Thus, the array becomes \([2,3,7,6]\) with bitwise AND equal to \(2\).In the second test case, in the first query, we add \(1\) in the first element, \(5\) in the second, and \(3\) in the third and now all the elements...
Input: 4 2 1 3 7 5 2 10 | Output: 2 6
Medium
4
889
455
119
19
1,267
F
1267F
2,600
graphs
Expert
1
0
0
0
12
1,428
E
1428E
E. Carrots for Rabbits
2,200
binary search; data structures; greedy; math; sortings
There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought \(n\) carrots with lengths \(a_1, a_2, a_3, \ldots, a_n\). However, rabbits are very fertile and multiply very quickly. Zookeeper now has \(k\) rabbits and does not have enough carrots to feed all of them. To solve this problem, Zookeeper decided t...
The first line contains two integers \(n\) and \(k\) \((1 \leq n \leq k \leq 10^5)\): the initial number of carrots and the number of rabbits.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((1 \leq a_i \leq 10^6)\): lengths of carrots.It is guaranteed that the sum of \(a_i\) is at least \(k\).
Output one integer: the minimum sum of time taken for rabbits to eat carrots.
For the first test, the optimal sizes of carrots are \(\{1,1,1,2,2,2\}\). The time taken is \(1^2+1^2+1^2+2^2+2^2+2^2=15\)For the second test, the optimal sizes of carrots are \(\{4,5,5,5\}\). The time taken is \(4^2+5^2+5^2+5^2=91\).
Input: 3 6 5 3 1 | Output: 15
Hard
5
654
312
77
14
965
E
965E
E. Short Code
2,200
data structures; dp; greedy; strings; trees
Arkady's code contains \(n\) variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code.He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide wi...
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of variables.The next \(n\) lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than \(10^5\). The variable names are distinct.
Print a single integer — the minimum possible total length of new variable names.
In the first example one of the best options is to shorten the names in the given order as ""cod"", ""co"", ""c"".In the second example we can shorten the last name to ""aac"" and the first name to ""a"" without changing the other names.
Input: 3codeforcescodehorsescode | Output: 6
Hard
5
668
319
81
9
549
C
549C
C. The Game Of Parity
2,200
games
There are n cities in Westeros. The i-th city is inhabited by ai people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly k cities left.The pr...
The first line contains two positive space-separated integers, n and k (1 ≤ k ≤ n ≤ 2·105) — the initial number of cities in Westeros and the number of cities at which the game ends. The second line contains n space-separated positive integers ai (1 ≤ ai ≤ 106), which represent the population of each city in Westeros.
Print string ""Daenerys"" (without the quotes), if Daenerys wins and ""Stannis"" (without the quotes), if Stannis wins.
In the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins.In the second sample, if Stannis burns a city with two people, Daenerys burn...
Input: 3 11 2 1 | Output: Stannis
Hard
1
831
319
119
5
1,706
D2
1706D2
D2. Chopping Carrots (Hard Version)
2,400
brute force; constructive algorithms; data structures; dp; greedy; math; number theory; two pointers
This is the hard version of the problem. The only difference between the versions is the constraints on \(n\), \(k\), \(a_i\), and the sum of \(n\) over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers \(a_1, a_2, \ldots,...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5\)).It is gua...
For each test case, print a single integer — the minimum possible cost of an array \(p\) satisfying the condition above.
In the first test case, the optimal array is \(p = [1, 1, 1, 2, 2]\). The resulting array of values of \(\lfloor \frac{a_i}{p_i} \rfloor\) is \([4, 5, 6, 4, 5]\). The cost of \(p\) is \(\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = ...
Input: 75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3 | Output: 2 0 13 1 4 7 0
Expert
8
821
395
120
17
725
D
725D
D. Contest Balloons
1,800
data structures; greedy
One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there ...
The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams.The i-th of n following lines contains two integers ti and wi (0 ≤ ti ≤ wi ≤ 1018) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.
Print one integer denoting the best place Limak can get.
In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has...
Input: 820 100032 3740 100045 5016 1616 1614 10002 1000 | Output: 3
Medium
2
1,294
290
56
7
1,076
F
1076F
F. Summer Practice Report
2,500
dp; greedy
Vova has taken his summer practice this year and now he should write a report on how it went.Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly \(n\) pages and the \(i\)-th page will include \(x_i\) tables and \(y_i\) formulas....
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le k \le 10^6\)).The second line contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \le x_i \le 10^6\)) — the number of tables on the \(i\)-th page.The third line contains \(n\) integers \(y_1, y_2, \dots, y_n\) (\(1 \le y_i \le...
Print ""YES"" if Vova can rearrange tables and formulas on each page in such a way that there is no more than \(k\) tables in a row and no more than \(k\) formulas in a row.Otherwise print ""NO"".
In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page \(1\): ""TTFTTFT"" page \(2\): ""TFTTFTT"" That way all blocks of tables have length \(2\).In the second example there is no way to fit everything in such a way that there are no more than \(2\) tab...
Input: 2 2 5 5 2 2 | Output: YES
Expert
2
1,182
375
196
10
916
E
916E
E. Jamie and Tree
2,400
data structures; trees
To your surprise, Jamie is the final boss! Ehehehe.Jamie has given you a tree with n vertices, numbered from 1 to n. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.Jamie also gives you three types of queries on the tree:1 v — Change the tree's root to vertex with number...
The first line of input contains two space-separated integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of vertices in the tree and the number of queries to process respectively.The second line contains n space-separated integers a1, a2, ..., an ( - 108 ≤ ai ≤ 108) — initial values of the vertices.Next n - 1 line...
For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.
The following picture shows how the tree varies after the queries in the first sample.
Input: 6 71 4 2 8 5 71 23 14 34 53 63 12 4 6 33 41 62 2 4 -51 43 3 | Output: 27195
Expert
2
775
849
139
9
724
F
724F
F. Uniformly Branched Trees
2,700
combinatorics; dp; trees
A tree is a connected graph without cycles.Two trees, consisting of n vertices each, are called isomorphic if there exists a permutation p: {1, ..., n} → {1, ..., n} such that the edge (u, v) is present in the first tree if and only if the edge (pu, pv) is present in the second tree.Vertex of the tree is called interna...
The single line of the input contains three integers n, d and mod (1 ≤ n ≤ 1000, 2 ≤ d ≤ 10, 108 ≤ mod ≤ 109) — the number of vertices in the tree, the degree of internal vertices and the prime modulo.
Print the number of trees over the modulo mod.
Input: 5 2 433416647 | Output: 1
Master
3
554
201
46
7
1,945
H
1945H
H. GCD is Greater
2,600
brute force; data structures; math; number theory
In the evenings during the hike, Kirill and Anton decided to take out an array of integers \(a\) of length \(n\) from their backpack and play a game with it. The rules are as follows: Kirill chooses from \(2\) to \((n-2)\) numbers and encircles them in red. Anton encircles all the remaining numbers in blue. Kirill calc...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 20\,000\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(x\) (\(4\le n \le 4\cdot 10^5\), \(0 \le x \le 4\cdot 10^5\)) ...
For each test case, output ""YES"" on the first line if the condition can be met, on the second line, output the number of chosen numbers by Kirill and the numbers themselves in any order separated by a space, and on the third line, output the size of the second set and the numbers in it.Otherwise, output ""NO"".You ca...
Input: 84 14 3 1 84 14 5 8 45 01 1 1 1 15 231 63 127 63 314 11 3 3 38 34 3 4 1 2 2 5 34 21 4 3 68 4831 61 37 15 53 26 61 12 | Output: YES 2 4 8 2 3 1 YES 2 4 4 2 5 8 NO YES 2 63 63 3 31 127 31 YES 2 3 3 2 1 3 YES 2 4 4 6 3 1 2 2 5 3 YES 2 3 6 2 1 4 YES 2 61 61 6 31 37 15 53 26 12
Expert
4
708
677
481
19
2,082
B
2082B
B. Floor or Ceil
1,600
brute force; greedy
Ecrade has an integer \(x\). There are two kinds of operations. Replace \(x\) with \(\left\lfloor \dfrac{x}{2}\right\rfloor\), where \(\left\lfloor \dfrac{x}{2}\right\rfloor\) is the greatest integer \(\le \dfrac{x}{2}\). Replace \(x\) with \(\left\lceil \dfrac{x}{2}\right\rceil\), where \(\left\lceil \dfrac{x}{2}\righ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The only line of each test case contains three integers \(x\), \(n\), and \(m\) (\(0 \le x, n, m \le 10^9\)).
For each test case, print two integers in one line, representing the minimum and the maximum possible value of \(x\) after \(n + m\) operations.
For simplicity, we call the first operation \(\text{OPER 1}\) and the second operation \(\text{OPER 2}\).In the first test case: If we perform \(12 \xrightarrow{\text{OPER 2}} 6 \xrightarrow{\text{OPER 2}} 3 \xrightarrow{\text{OPER 1}} 1\), we can obtain the minimum possible value \(1\). If we perform \(12 \xrightarrow...
Input: 512 1 212 1 112 0 012 1000000000 1000000000706636307 0 3 | Output: 1 2 3 3 12 12 0 0 88329539 88329539
Medium
2
621
271
144
20
2,033
D
2033D
D. Kousuke's Assignment
1,300
data structures; dp; dsu; greedy; math
After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array \(a\) of \(n\) integers and asked him to calculate the number of non-overlapping segments of the array \(a\), such that each segment is considered beautiful.A segment...
The first line of input contains the number \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Each test case consists of \(2\) lines. The first line contains one integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array. The second line contains \(n\) integers \(a_i\) (\(-10^5 \le a_i \le 10^5\)) — the elem...
For each test case, output a single integer: the maximum number of non-overlapping beautiful segments.
Input: 352 1 -3 2 1712 -4 4 43 -3 -5 860 -4 0 3 0 1 | Output: 1 2 3
Easy
5
508
438
102
20
2,005
B1
2005B1
B1. The Strict Teacher (Easy Version)
1,000
greedy; math; sortings
This is the easy version of the problem. The only differences between the two versions are the constraints on \(m\) and \(q\). In this version, \(m=2\) and \(q=1\). You can make hacks only if both versions of the problem are solved.Narek and Tsovak were busy preparing this round, so they have not managed to do their ho...
In the first line of the input, you are given a single integer \(t\) (\(1 \le t \le 10^5\)) — the number of test cases. The description of each test case follows.In the first line of each test case, you are given three integers \(n\), \(m\), and \(q\) (\(3 \le n \le 10^9\), \(m=2\), \(q=1\)) — the number of cells on th...
For each test case, output \(q\) lines, the \(i\)-th of them containing the answer of the \(i\)-th query.
In the first example, the student can just stay at cell \(2\). The teacher, initially located in cell \(1\), can reach cell \(2\) in one move. Therefore, the answer is \(1\).In the second example, the student should just stay at cell \(1\). The teacher, initially located in cell \(3\), can reach cell \(1\) in two moves...
Input: 310 2 11 428 2 13 618 2 13 68 | Output: 1 2 2
Beginner
3
1,726
800
105
20
1,217
B
1217B
B. Zmei Gorynich
1,600
greedy; math
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads! Initially Zmei Gorynich has \(x\) heads. You can deal \(n\) types of blows. If you deal a blow of the \(i\)-th type, you decrease the number of Gorynich's heads by \(min(d_i, curX)\), there \(cur...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) – the number of queries.The first line of each query contains two integers \(n\) and \(x\) (\(1 \le n \le 100\), \(1 \le x \le 10^9\)) — the number of possible types of blows and the number of heads Zmei initially has, respectively.The following \(n\) line...
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich. If Zmei Gorynuch cannot be defeated print \(-1\).
In the first query you can deal the first blow (after that the number of heads changes to \(10 - 6 + 3 = 7\)), and then deal the second blow.In the second query you just deal the first blow three times, and Zmei is defeated. In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fight...
Input: 3 3 10 6 3 8 2 1 4 4 10 4 1 3 2 2 6 1 100 2 15 10 11 14 100 | Output: 2 3 -1
Medium
2
936
522
140
12
1,196
E
1196E
E. Connected Component on a Chessboard
1,800
constructive algorithms; implementation
You are given two integers \(b\) and \(w\). You have a chessboard of size \(10^9 \times 10^9\) with the top left cell at \((1; 1)\), the cell \((1; 1)\) is painted white.Your task is to find a connected component on this chessboard that contains exactly \(b\) black cells and exactly \(w\) white cells. Two cells are cal...
The first line of the input contains one integer \(q\) (\(1 \le q \le 10^5\)) — the number of queries. Then \(q\) queries follow.The only line of the query contains two integers \(b\) and \(w\) (\(1 \le b, w \le 10^5\)) — the number of black cells required and the number of white cells required.It is guaranteed that th...
For each query, print the answer to it.If it is impossible to find the required component, print ""NO"" on the first line.Otherwise, print ""YES"" on the first line. In the next \(b + w\) lines print coordinates of cells of your component in any order. There should be exactly \(b\) black cells and \(w\) white cells in ...
Input: 3 1 1 1 4 2 5 | Output: YES 2 2 1 2 YES 2 3 1 3 3 3 2 2 2 4 YES 2 3 2 4 2 5 1 3 1 5 3 3 3 5
Medium
2
1,027
418
491
11
852
G
852G
G. Bathroom terminal
1,700
implementation
Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message ""Play Me"". He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is co...
The first line of input contains two integers N and M (1 ≤ N ≤ 100 000, 1 ≤ M ≤ 5000), representing the number of words and patterns respectively.The next N lines represent each word, and after those N lines, following M lines represent each pattern. Each word and each pattern has a maximum length L (1 ≤ L ≤ 50). Each ...
Output contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.
If we switch '?' with 'b', 'e' and with empty character, we get 'abc', 'aec' and 'ac' respectively.
Input: 3 1abcaecaca?c | Output: 3
Medium
1
1,181
455
133
8
637
D
637D
D. Running with Obstacles
1,600
*special; data structures; dp; greedy
A sportsman starts from point xstart = 0 and runs to point with coordinate xfinish = m (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than s meters (in this case for these s meters his path should have no obstacles), and after that he can jump over a lengt...
The first line of the input containsd four integers n, m, s and d (1 ≤ n ≤ 200 000, 2 ≤ m ≤ 109, 1 ≤ s, d ≤ 109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.The second line contains a sequ...
If the runner cannot reach the finishing point, print in the first line of the output ""IMPOSSIBLE"" (without the quotes).If the athlete can get from start to finish, print any way to do this in the following format: print a line of form ""RUN X>"" (where ""X"" should be a positive integer), if the athlete should run f...
Input: 3 10 1 33 4 7 | Output: RUN 2JUMP 3RUN 1JUMP 2RUN 2
Medium
4
861
594
780
6
706
D
706D
D. Vasiliy's Multiset
1,800
binary search; bitmasks; data structures; trees
Author has gone out of the stories about Vasiliy, so here is just a formal task description.You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries: ""+ x"" — add integer x to multiset A. ""- x"" — erase one occurrence of integer x from multiset A. It's guaranteed...
The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.Note, ...
For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.
After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.The answer for the sixth query is integer — maximum among integers , , , and .
Input: 10+ 8+ 9+ 11+ 6+ 1? 3- 8? 3? 8? 11 | Output: 11101413
Medium
4
625
375
150
7
1,978
B
1978B
B. New Bakery
800
binary search; greedy; math; ternary search
Bob decided to open a bakery. On the opening day, he baked \(n\) buns that he can sell. The usual price of a bun is \(a\) coins, but to attract customers, Bob organized the following promotion: Bob chooses some integer \(k\) (\(0 \le k \le \min(n, b)\)). Bob sells the first \(k\) buns at a modified price. In this case,...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The only line of each test case contains three integers \(n\), \(a\), and \(b\) (\(1 \le n, a, b \le 10^9\)) — the number of buns, the us...
For each test case, output a single integer — the maximum profit that Bob can obtain.
In the first test case, it is optimal for Bob to choose \(k = 1\). Then he will sell one bun for \(5\) coins, and three buns at the usual price for \(4\) coins each. Then the profit will be \(5 + 4 + 4 + 4 = 17\) coins.In the second test case, it is optimal for Bob to choose \(k = 5\). Then he will sell all the buns at...
Input: 74 4 55 5 910 10 55 5 111000000000 1000000000 10000000001000000000 1000000000 11000 1 1000 | Output: 17 35 100 45 1000000000000000000 1000000000000000000 500500
Beginner
4
640
402
85
19
341
E
341E
E. Candies Game
3,000
constructive algorithms; greedy
Iahub is playing an uncommon game. Initially, he has n boxes, numbered 1, 2, 3, ..., n. Each box has some number of candies in it, described by a sequence a1, a2, ..., an. The number ak represents the number of candies in box k. The goal of the game is to move all candies into exactly two boxes. The rest of n - 2 boxes...
The first line of the input contains integer n (3 ≤ n ≤ 1000). The next line contains n non-negative integers: a1, a2, ..., an — sequence elements. It is guaranteed that sum of all numbers in sequence a is up to 106.
In case there exists no solution, output -1. Otherwise, in the first line output integer c (0 ≤ c ≤ 106), representing number of moves in your solution. Each of the next c lines should contain two integers i and j (1 ≤ i, j ≤ n, i ≠ j): integers i, j in the kth line mean that at the k-th move you will move candies from...
For the first sample, after the first move the boxes will contain 3, 12 and 3 candies. After the second move, the boxes will contain 6, 12 and 0 candies. Now all candies are in exactly 2 boxes.For the second sample, you can observe that the given configuration is not valid, as all candies are in a single box and they s...
Input: 33 6 9 | Output: 22 31 3
Master
2
895
216
350
3
178
C2
178C2
C2. Smart Beaver and Resolving Collisions
1,900
The Smart Beaver from ABBYY has a lot of hobbies. One of them is constructing efficient hash tables. One of the most serious problems in hash tables is resolving collisions. The Beaver is interested in this problem very much and he decided to explore it in detail.We assume that the hash table consists of h cells number...
The first line of input contains three integers h, m and n (1 ≤ m < h), separated by spaces, where h is the size of the hash table, m is the number that is used to resolve collisions, n is the number of operations.The following n lines contains the descriptions of the operations. Their execution order corresponds to th...
Print a single number — the total number of dummy calls to the hash table.Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams and the %I64d specifier.
Input: 10 2 7+ 11 0+ 22 2+ 33 6+ 44 0+ 55 0- 22+ 66 0 | Output: 7
Hard
0
2,301
1,520
218
1
2,050
E
2050E
E. Three Strings
1,500
dp; implementation; strings
You are given three strings: \(a\), \(b\), and \(c\), consisting of lowercase Latin letters. The string \(c\) was obtained in the following way: At each step, either string \(a\) or string \(b\) was randomly chosen, and the first character of the chosen string was removed from it and appended to the end of string \(c\)...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^3\)) — the number of test cases.The first line of each test case contains one string of lowercase Latin letters \(a\) (\(1 \leq |a| \leq 10^3\)) — the first string, where \(|a|\) denotes the length of string \(a\).The second line of each test...
For each test case, output a single integer — the minimum number of characters that could have been changed in string \(c\).
Input: 7abcbabcdacbdabbaaabbxxxyyyxyxyxyabcddecfcodeshorsecodeforceseggannieegaegaeg | Output: 1 0 2 0 3 2 3
Medium
3
1,014
780
124
20
1,154
A
1154A
A. Restoring Three Numbers
800
math
Polycarp has guessed three positive integers \(a\), \(b\) and \(c\). He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: \(a+b\), \(a+c\), \...
The only line of the input contains four positive integers \(x_1, x_2, x_3, x_4\) (\(2 \le x_i \le 10^9\)) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number \(x_1, x_2, x_3, x_4\).
Print such positive integers \(a\), \(b\) and \(c\) that four numbers written on a board are values \(a+b\), \(a+c\), \(b+c\) and \(a+b+c\) written in some order. Print \(a\), \(b\) and \(c\) in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.
Input: 3 6 5 4 | Output: 2 1 3
Beginner
1
570
237
295
11
1,051
B
1051B
B. Relatively Prime Pairs
1,000
greedy; math; number theory
You are given a set of all integers from \(l\) to \(r\) inclusive, \(l < r\), \((r - l + 1) \le 3 \cdot 10^5\) and \((r - l)\) is always odd.You want to split these numbers into exactly \(\frac{r - l + 1}{2}\) pairs in such a way that for each pair \((i, j)\) the greatest common divisor of \(i\) and \(j\) is equal to \...
The only line contains two integers \(l\) and \(r\) (\(1 \le l < r \le 10^{18}\), \(r - l + 1 \le 3 \cdot 10^5\), \((r - l)\) is odd).
If any solution exists, print ""YES"" in the first line. Each of the next \(\frac{r - l + 1}{2}\) lines should contain some pair of integers. GCD of numbers in each pair should be equal to \(1\). All \((r - l + 1)\) numbers should be pairwise distinct and should have values from \(l\) to \(r\) inclusive.If there are mu...
Input: 1 8 | Output: YES2 74 13 86 5
Beginner
3
492
134
398
10
1,350
A
1350A
A. Orac and Factors
900
math
Orac is studying number theory, and he is interested in the properties of divisors.For two positive integers \(a\) and \(b\), \(a\) is a divisor of \(b\) if and only if there exists an integer \(c\), such that \(a\cdot c=b\).For \(n \ge 2\), we will denote as \(f(n)\) the smallest positive divisor of \(n\), except \(1\...
The first line of the input is a single integer \(t\ (1\le t\le 100)\): the number of times that Orac will ask you.Each of the next \(t\) lines contains two positive integers \(n,k\ (2\le n\le 10^6, 1\le k\le 10^9)\), corresponding to a query by Orac.It is guaranteed that the total sum of \(n\) is at most \(10^6\).
Print \(t\) lines, the \(i\)-th of them should contain the final value of \(n\) in the \(i\)-th query by Orac.
In the first query, \(n=5\) and \(k=1\). The divisors of \(5\) are \(1\) and \(5\), the smallest one except \(1\) is \(5\). Therefore, the only operation adds \(f(5)=5\) to \(5\), and the result is \(10\).In the second query, \(n=8\) and \(k=2\). The divisors of \(8\) are \(1,2,4,8\), where the smallest one except \(1\...
Input: 3 5 1 8 2 3 4 | Output: 10 12 12
Beginner
1
1,200
316
110
13
1,891
D
1891D
D. Suspicious logarithms
1,900
binary search; brute force; math
Let \(f\)(\(x\)) be the floor of the binary logarithm of \(x\). In other words, \(f\)(\(x\)) is largest non-negative integer \(y\), such that \(2^y\) does not exceed \(x\).Let \(g\)(\(x\)) be the floor of the logarithm of \(x\) with base \(f\)(\(x\)). In other words, \(g\)(\(x\)) is the largest non-negative integer \(z...
The first line contains a single integer \(q\) — the number of queries (\(1 \leq q \leq 10^5\)).The next \(q\) lines each contain two integers \(l_i\) and \(r_i\) — the bounds of the \(i\)-th query (\(4 \leq l_i \leq r_i \leq 10^{18}\)).
For each query, output the answer to the query modulo \(10^9 + 7\).
The table below contains the values of the functions \(f\)(\(x\)) and \(g\)(\(x\)) for all \(x\) such that \(1 \leq x \leq 8\). \(x\)\(1\)\(2\)\(3\)\(4\)\(5\)\(6\)\(7\)\(8\)\(f\)\(0\)\(1\)\(1\)\(2\)\(2\)\(2\)\(2\)\(3\)\(g\)\(-\)\(-\)\(-\)\(2\)\(2\)\(2\)\(2\)\(1\)
Input: 12 4 6 4 7 4 8 4 100000 179 1000000000000000000 57 179 4 201018959 7 201018960 729 50624 728 50624 728 50625 729 50625 | Output: 6 8 9 348641 41949982 246 1 0 149688 149690 149694 149692
Hard
3
645
237
67
18
723
F
723F
F. st-Spanning Tree
2,300
dsu; graphs; greedy; implementation
You are given an undirected connected graph consisting of n vertices and m edges. There are no loops and no multiple edges in the graph.You are also given two distinct vertices s and t, and two values ds and dt. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that ...
The first line of the input contains two integers n and m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ min(400 000, n·(n - 1) / 2)) — the number of vertices and the number of edges in the graph. The next m lines contain the descriptions of the graph's edges. Each of the lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the ends...
If the answer doesn't exist print ""No"" (without quotes) in the only line of the output. Otherwise, in the first line print ""Yes"" (without quotes). In the each of the next (n - 1) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exac...
Input: 3 31 22 33 11 2 1 1 | Output: Yes3 21 3
Expert
4
765
537
464
7
2,120
F
2120F
F. Superb Graphs
2,600
2-sat; graphs
As we all know, Aryan is a funny guy. He decides to create fun graphs. For a graph \(G\), he defines fun graph \(G'\) of \(G\) as follows: Every vertex \(v'\) of \(G'\) maps to a non-empty independent set\(^{\text{∗}}\) or clique\(^{\text{†}}\) in \(G\). The sets of vertices of \(G\) that the vertices of \(G'\) map to ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1\leq n\leq 300, 1\leq k\leq 10\)).Then, there are \(k\) graphs described. The first ...
For each testcase, print ""Yes"" if there exists \(k\) graphs satisfying the conditions; otherwise, ""No"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
For the first test case, the following are the graphs of \(G_1, H_1\) and \(G_2, H_2\) such that \(G_1\) is superb graph of \(H_1\) and \(G_2\) is superb graph of \(H_2\). In each graph, vertex \(2\) of \(G_i\) corresonds to independent set \(\{2\_1, 2\_2\}\) of corresponding \(H_i\) and remaining vertices \(v\in\{1,3,...
Input: 35 233 45 35 163 53 41 41 22 34 24 3033 11 41 244 24 31 22 33 2033 13 21 2 | Output: Yes Yes No
Expert
2
2,258
761
268
21
1,850
B
1850B
B. Ten Words of Wisdom
800
implementation; sortings
In the game show ""Ten Words of Wisdom"", there are \(n\) participants numbered from \(1\) to \(n\), each of whom submits one response. The \(i\)-th response is \(a_i\) words long and has quality \(b_i\). No two responses have the same quality, and at least one response has length at most \(10\).The winner of the show ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 50\)) — the number of responses.Then \(n\) lines follow, the \(i\)-th of which contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i ...
For each test case, output a single line containing one integer \(x\) (\(1 \leq x \leq n\)) — the winner of the show, according to the rules given in the statement.It can be shown that, according to the constraints in the statement, exactly one winner exists for each test case.
In the first test case, the responses provided are as follows: Response 1: \(7\) words, quality \(2\) Response 2: \(12\) words, quality \(5\) Response 3: \(9\) words, quality \(3\) Response 4: \(9\) words, quality \(4\) Response 5: \(10\) words, quality \(1\) We can see that the responses with indices \(1\), \(3\), \(4...
Input: 357 212 59 39 410 131 23 45 611 43 | Output: 4 3 1
Beginner
2
455
559
278
18
116
B
116B
B. Little Pigs and Wolves
1,100
greedy; implementation
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size n × m. Each cell in this grid was either empty, containing one little pig, or containing one wolf.A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afraid of wo...
The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow n lines containing m characters each — that is the grid description. ""."" means that this cell is empty. ""P"" means that this cell contains a little pig. ""W"" m...
Print a single number — the maximal number of little pigs that may be eaten by the wolves.
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
Input: 2 3PPWW.P | Output: 2
Easy
2
890
437
90
1
894
E
894E
E. Ralph and Mushrooms
2,100
dp; graphs
Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic sp...
The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.Each of the following m lines contains three integers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y w...
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.
Input: 2 21 2 42 1 41 | Output: 16
Hard
2
1,161
536
97
8
1,029
A
1029A
A. Many Equal Substrings
1,300
implementation; strings
You are given a string \(t\) consisting of \(n\) lowercase Latin letters and an integer number \(k\).Let's define a substring of some string \(s\) with indices from \(l\) to \(r\) as \(s[l \dots r]\).Your task is to construct such string \(s\) of minimum possible length that there are exactly \(k\) positions \(i\) such...
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n, k \le 50\)) — the length of the string \(t\) and the number of substrings.The second line of the input contains the string \(t\) consisting of exactly \(n\) lowercase Latin letters.
Print such string \(s\) of minimum possible length that there are exactly \(k\) substrings of \(s\) equal to \(t\).It is guaranteed that the answer is always unique.
Input: 3 4aba | Output: ababababa
Easy
2
557
259
165
10
1,736
A
1736A
A. Make A Equal to B
800
brute force; greedy; sortings
You are given two arrays \(a\) and \(b\) of \(n\) elements, each element is either \(0\) or \(1\).You can make operations of \(2\) kinds. Pick an index \(i\) and change \(a_i\) to \(1-a_i\). Rearrange the array \(a\) however you want. Find the minimum number of operations required to make \(a\) equal to \(b\).
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 400\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) — the length of the arrays \(a\) and \(b\).The second ...
For each test case, print the minimum number of operations required to make \(a\) equal to \(b\).
In the first case, we need only one operation: change \(a_1\) to \(1-a_i\). Now \(a = [0, 0]\) which is equal to \(b\).In the second case, the optimal way is to rearrange \(a\) to get the array \([0, 1, 11\). Now \(a = [0, 0, 1]\) which is equal to \(b\).In the second case, one of optimal ways would be to first change ...
Input: 531 0 10 0 141 1 0 00 1 1 121 11 141 0 0 10 1 1 0101 | Output: 1 2 0 1 1
Beginner
3
311
618
97
17
833
B
833B
B. The Bakery
2,200
binary search; data structures; divide and conquer; dp; two pointers
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.Soon the expenses started to overcome the income, so Slastyona decided to study the sweets market. She learned it's profitable to pack cake...
The first line contains two integers n and k (1 ≤ n ≤ 35000, 1 ≤ k ≤ min(n, 50)) – the number of cakes and the number of boxes, respectively.The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the types of cakes in the order the oven bakes them.
Print the only integer – the maximum total value of all boxes with cakes.
In the first example Slastyona has only one box. She has to put all cakes in it, so that there are two types of cakes in the box, so the value is equal to 2.In the second example it is profitable to put the first two cakes in the first box, and all the rest in the second. There are two distinct types in the first box, ...
Input: 4 11 2 2 1 | Output: 2
Hard
5
1,037
260
73
8
1,584
D
1584D
D. Guess the Permutation
2,000
binary search; combinatorics; interactive; math
This is an interactive problem.Jury initially had a sequence \(a\) of length \(n\), such that \(a_i = i\).The jury chose three integers \(i\), \(j\), \(k\), such that \(1 \leq i < j < k \leq n\), \(j - i > 1\). After that, Jury reversed subsegments \([i, j - 1]\) and \([j, k]\) of the sequence \(a\).Reversing a subsegm...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. Description of the test cases follows.The single line of each test case contains a single integer \(n\) (\(4 \leq n \leq 10^9\)). After reading it you should start an interaction...
In the first test case, \(i = 1\), \(j = 3\), \(k = 5\), so the sequence \(a\) is \([2, 1, 5, 4, 3]\).In the second test case, \(i = 2\), \(j = 4\), \(k = 5\), so the sequence \(a\) is \([1, 3, 2, 5, 4]\).
Input: 2 5 4 3 3 5 2 2 1 | Output: ? 1 5 ? 2 5 ? 3 5 ! 1 3 5 ? 1 5 ? 2 5 ? 3 5 ! 2 4 5
Hard
4
1,089
498
0
15
66
D
66D
D. Petya and His Friends
1,700
constructive algorithms; math; number theory
Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n.Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is eve...
The first line contains an integer n (2 ≤ n ≤ 50).
If there is no answer, print ""-1"" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them.Do ...
Input: 3 | Output: 995511115
Medium
3
863
50
646
0
1,068
A
1068A
A. Birthday
1,400
math
Ivan is collecting coins. There are only \(N\) different collectible coins, Ivan has \(K\) of them. He will be celebrating his birthday soon, so all his \(M\) freinds decided to gift him coins. They all agreed to three terms: Everyone must gift as many coins as others. All coins given to Ivan must be different. Not les...
The only line of input contains 4 integers \(N\), \(M\), \(K\), \(L\) (\(1 \le K \le N \le 10^{18}\); \(1 \le M, \,\, L \le 10^{18}\)) — quantity of different coins, number of Ivan's friends, size of Ivan's collection and quantity of coins, that must be new in Ivan's collection.
Print one number — minimal number of coins one friend can gift to satisfy all the conditions. If it is impossible to satisfy all three conditions print ""-1"" (without quotes).
In the first test, one coin from each friend is enough, as he will be presented with 15 different coins and 13 of them will definitely be new.In the second test, Ivan has 11 friends, but there are only 10 different coins. So all friends can't present him different coins.
Input: 20 15 2 3 | Output: 1
Easy
1
713
279
176
10
1,352
A
1352A
A. Sum of Round Numbers
800
implementation; math
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from \(1\) to \(9\) (inclusive) are round.For example, the following numbers ar...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases in the input. Then \(t\) test cases follow.Each test case is a line containing an integer \(n\) (\(1 \le n \le 10^4\)).
Print \(t\) answers to the test cases. Each answer must begin with an integer \(k\) — the minimum number of summands. Next, \(k\) terms must follow, each of which is a round number, and their sum is \(n\). The terms can be printed in any order. If there are several answers, print any of them.
Input: 5 5009 7 9876 10000 10 | Output: 2 5000 9 1 7 4 800 70 6 9000 1 10000 1 10
Beginner
2
739
210
293
13
6
B
6B
B. President's Office
1,100
implementation
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all ...
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President's desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and...
Print the only number — the amount of President's deputies.
Input: 3 4 RG.B..RR.TTT. | Output: 2
Easy
1
856
439
59
0
66
E
66E
E. Petya and Post
2,000
data structures; dp
Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then ...
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ...
Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.
Input: 41 7 2 38 1 1 3 | Output: 22 4
Hard
2
1,934
477
252
0
913
C
913C
C. Party Lemonade
1,600
bitmasks; dp; greedy
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci rou...
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.In the third example it'...
Input: 4 1220 30 70 90 | Output: 150
Medium
3
486
286
119
9
1,917
C
1917C
C. Watering an Array
1,600
brute force; greedy; implementation; math
You have an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\). On the \(i\)-th of the next \(d\) days you are going to do exactly one of the following two actions: Add \(1\) to each of the first \(b_i\) elements of the array \(a\) (i.e., set \(a_j := a_j + 1\) for each \(1 \le j \le b_i\)). Count the elements...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) — the number of test cases.The first line of each test case contains three integers \(n\), \(k\) and \(d\) (\(1 \le n \le 2000\), \(1 \le k \le 10^5\), \(k \le d \le 10^9\)) — the length of the array \(a\), the length of the sequence \(v\) and the nu...
For each test case, output one integer: the maximum score you can achieve at the end of the \(d\)-th day.
In the first test case, the sequence \(b\) is equal to \([1, 3, 2, 3, 1, 3, 2, 3, \ldots]\) and one of the optimal solutions for this case is as follows: Perform the operation of the second type on the \(1\)-st day: your score increases by \(3\) and array \(a\) becomes equal to \([0, 0, 0]\). Perform the operation of t...
Input: 53 4 41 2 31 3 2 36 2 36 1 2 4 1 56 65 1 10 5 0 5 051 1 1113 4 61 2 31 3 2 3 | Output: 4 3 0 1 5
Medium
4
1,119
770
105
19
1,469
C
1469C
C. Building a Fence
1,600
dp; greedy; implementation; two pointers
You want to build a fence that will consist of \(n\) equal sections. All sections have a width equal to \(1\) and height equal to \(k\). You will place all sections in one line side by side.Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the \(i\)-th se...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(2 \le k \le 10^8\)) — the number of sections in the fence and the height of each section.The second line of each test c...
For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
In the first test case, one of the possible fences is shown in the picture.In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since \(k = 3\), \(h_1 = 0\), and \(h_2 = 2\) the first rule is also fulfilled.In the third test case, according to the...
Input: 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 | Output: YES YES NO
Medium
4
831
548
221
14
234
A
234A
A. Lefthanders and Righthanders
1,200
implementation
One fine October day a mathematics teacher Vasily Petrov went to a class and saw there n pupils who sat at the desks, two people at each desk. Vasily quickly realized that number n is even. Like all true mathematicians, Vasily has all students numbered from 1 to n.But Vasily Petrov did not like the way the children wer...
The first input line contains a single even integer n (4 ≤ n ≤ 100) — the number of students in the class. The second line contains exactly n capital English letters ""L"" and ""R"". If the i-th letter at the second line equals ""L"", then the student number i is a lefthander, otherwise he is a righthander.
Print integer pairs, one pair per line. In the i-th line print the numbers of students that will sit at the i-th desk. The first number in the pair stands for the student who is sitting to the left, and the second number stands for the student who is sitting to the right. Separate the numbers in the pairs by spaces. If...
Input: 6LLRLLL | Output: 1 42 56 3
Easy
1
1,112
308
369
2
520
E
520E
E. Pluses everywhere
2,200
combinatorics; dp; math; number theory
Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses (""+"") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, n...
The first line contains two integers, n and k (0 ≤ k < n ≤ 105).The second line contains a string consisting of n digits.
Print the answer to the problem modulo 109 + 7.
In the first sample the result equals (1 + 08) + (10 + 8) = 27.In the second sample the result equals 1 + 0 + 8 = 9.
Input: 3 1108 | Output: 27
Hard
4
1,116
121
47
5
1,249
B1
1249B1
B1. Books Exchange (easy version)
1,000
dsu; math
The only difference between easy and hard versions is constraints.There are \(n\) kids, each of them is reading a unique book. At the end of any day, the \(i\)-th kid will give his book to the \(p_i\)-th kid (in case of \(i = p_i\) the kid will give his book to himself). It is guaranteed that all values of \(p_i\) are ...
The first line of the input contains one integer \(q\) (\(1 \le q \le 200\)) — the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 200\)) — the number of kids in the query. The second line of the query contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\...
For each query, print the answer on it: \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) is the number of the day the book of the \(i\)-th child is returned back to him for the first time in this query.
Input: 6 5 1 2 3 4 5 3 2 3 1 6 4 6 2 1 5 3 1 1 4 3 4 1 2 5 5 1 2 4 3 | Output: 1 1 1 1 1 3 3 3 2 3 3 2 1 3 1 2 2 2 2 4 4 4 1 4
Beginner
2
1,537
465
208
12
162
G
162G
G. Non-decimal sum
2,000
*special
You are given an array of integers written in base radix. Calculate their sum and output it written in the same base.
The first line of the input contains an integer n (1 ≤ n ≤ 10) — the size of the array. The second line contains an integer radix (2 ≤ radix ≤ 36) — the base of the numeral system used. Next n lines contain the elements of the array, one per line. Each element is a non-negative integer written in radix-based notation, ...
Output the sum of array elements in radix-based notation. Use the same format as in the input.
Input: 316F020B004 | Output: 2FF
Hard
1
117
480
94
1
288
A
288A
A. Polo the Penguin and Strings
1,300
greedy
Little penguin Polo adores strings. But most of all he adores strings of length n.One day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct. No two neighbouring letters of a ...
A single line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26) — the string's length and the number of distinct letters.
In a single line print the required string. If there isn't such string, print ""-1"" (without the quotes).
Input: 7 4 | Output: ababacd
Easy
1
905
136
106
2
360
D
360D
D. Levko and Sets
2,600
number theory
Levko loves all sorts of sets very much.Levko has two arrays of integers a1, a2, ... , an and b1, b2, ... , bm and a prime number p. Today he generates n sets. Let's describe the generation process for the i-th set: First it has a single number 1. Let's take any element c from this set. For all j (1 ≤ j ≤ m) if number ...
The first line contains three integers n, m and p (1 ≤ n ≤ 104, 1 ≤ m ≤ 105, 2 ≤ p ≤ 109), p is prime. The second line contains space-separated integers a1, a2, ... , an (1 ≤ ai < p). The third line contains space-separated integers b1, b2, ... , bm (1 ≤ bi ≤ 109).
The single number — the size of the union of the sets.
Input: 1 1 725 | Output: 3
Expert
1
583
265
54
3
268
E
268E
E. Playlist
2,100
math; probabilities; sortings
Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs.Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure f...
The first line contains a single integer n (1 ≤ n ≤ 50000). The i-th of the following n lines contains two integers, separated by a single space — li and pi (15 ≤ li ≤ 1000, 0 ≤ pi ≤ 100) — the length of the i-th song in seconds and the probability that Manao will like the song, in percents.
In a single line print a single real number — the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10 - 9.
Consider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist.Consider the second test case. The song which is 360 s...
Input: 3150 20150 50100 50 | Output: 537.500000000
Hard
3
1,717
292
207
2
1,007
C
1007C
C. Guess two numbers
3,000
binary search; interactive
This is an interactive problem.Vasya and Vitya play a game. Vasya thought of two integers \(a\) and \(b\) from \(1\) to \(n\) and Vitya tries to guess them. Each round he tells Vasya two numbers \(x\) and \(y\) from \(1\) to \(n\). If both \(x=a\) and \(y=b\) then Vitya wins. Else Vasya must say one of the three phrase...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^{18}\)) — the upper limit of the numbers.
Let's analyze the sample test. The chosen numbers are \(2\) and \(4\). The interactor was given two instructions.For the query \((4, 3)\), it can return \(2\) or \(3\). Out of the two instructions the second one is chosen, so the interactor returns \(a^{23}_2=3\).For the query \((3, 4)\), it can return only \(3\).For t...
Input: 533210 | Output: 4 33 43 31 52 4
Master
2
756
108
0
10
2,063
F1
2063F1
F1. Counting Is Not Fun (Easy Version)
2,400
combinatorics; data structures; dfs and similar; dp; dsu; graphs; hashing; implementation; math; trees
This is the easy version of the problem. The difference between the versions is that in this version, the limits on \(t\) and \(n\) are smaller. You can hack only if you solved all versions of this problem. Now Little John is rich, and so he finally buys a house big enough to fit himself and his favorite bracket sequen...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. The first line of each test case contains one integer \(n\) (\(2 \le n \le 5000\)) — the number of good pairs.Then, each of the \(n\) following lines contains...
For each test case, output \(n+1\) integers on a separate line: The first integer is the answer before all clues, modulo \(998\,244\,353\). For all \(i \ge 1\), the \(i+1\)-th integer is the answer after the \(i\)-th clue, modulo \(998\,244\,353\).
The first test case of the example is explained in the problem description.The third test case of the example is explained as follows. It can be shown that there are \(132\) balanced bracket sequences with \(6\) good pairs. The answers after each clue are given as follows: You are given the good pair \((2,3)\). There a...
Input: 332 51 63 441 67 82 34 562 31 67 89 1210 114 5 | Output: 5 1 1 1 14 2 2 1 1 132 42 5 2 1 1 1
Expert
10
2,897
580
248
20
1,403
B
1403B
B. Spring cleaning
2,300
*special; data structures; dfs and similar; graphs; trees
Spring cleanings are probably the most boring parts of our lives, except this year, when Flóra and her mother found a dusty old tree graph under the carpet.This tree has \(N\) nodes (numbered from \(1\) to \(N\)), connected by \(N-1\) edges. The edges gathered too much dust, so Flóra's mom decided to clean them. Cleani...
The first line contains two space-separated integer, \(N\) and \(Q\) (\(3 \leq N \leq 10^{5}\), \(1 \leq Q \leq 10^{5}\)) – the number of nodes the tree has and the number of variations. Each of the next \(N-1\) lines contains two space-separated integers \(u\) and \(v\) denoting that nodes \(u\) and \(v\) are connecte...
You should print \(Q\) lines. In the \(i\)-th line, print a single integer: the minimum cost required to clean the \(i\)-th variation of the tree. If the tree cannot be cleaned, print \(-1\).
The following picture shows the second variation. A possible solution is to clean the path between leaves \(1 - 6\), \(A - 7\) and \( B - 3\).You can download the above example and an additional (bigger) sample input here: https://gofile.io/d/8QlbsS
Input: 7 3 1 2 2 4 4 5 5 6 5 7 3 4 1 4 2 2 4 1 1 | Output: -1 10 8
Expert
5
1,319
863
191
14
1,200
A
1200A
A. Hotelier
800
brute force; data structures; implementation
Amugae has a hotel consisting of \(10\) rooms. The rooms are numbered from \(0\) to \(9\) from left to right.The hotel has two entrances — one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entranc...
The first line consists of an integer \(n\) (\(1 \le n \le 10^5\)), the number of events in Amugae's memory.The second line consists of a string of length \(n\) describing the events in chronological order. Each character represents: 'L': A customer arrives from the left entrance. 'R': A customer arrives from the right...
In the only line, output the hotel room's assignment status, from room \(0\) to room \(9\). Represent an empty room as '0', and an occupied room as '1', without spaces.
In the first example, hotel room's assignment status after each action is as follows. First of all, all rooms are empty. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. L: one more customer from the left entrance. Assignment status is 11000...
Input: 8 LLRL1RL1 | Output: 1010000011
Beginner
3
772
636
168
12
585
F
585F
F. Digits of Number Pi
3,200
dp; implementation; strings
Vasily has recently learned about the amazing properties of number π. In one of the articles it has been hypothesized that, whatever the sequence of numbers we have, in some position, this sequence is found among the digits of number π. Thus, if you take, for example, the epic novel ""War and Peace"" of famous Russian ...
The first line contains string s consisting of decimal digits (1 ≤ |s| ≤ 1000) that Vasily will use to search substrings in. According to hypothesis, this sequence of digis indeed occurs in the decimal representation of π, although we can't guarantee that.The second and third lines contain two positive integers x, y of...
Print how many numbers in the segment from x to y that are half-occurrences in s modulo 109 + 7.
Input: 021019 | Output: 2
Master
3
1,495
403
96
5
1,208
B
1208B
B. Uniqueness
1,500
binary search; brute force; implementation; two pointers
You are given an array \(a_{1}, a_{2}, \ldots, a_{n}\). You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)) and delete integers \(a_l, a_{l+1}, \ldots, a_r\) from the a...
The first line of the input contains a single integer \(n\) (\(1 \le n \le 2000\)) — the number of elements in the given array.The next line contains \(n\) spaced integers \(a_{1}, a_{2}, \ldots, a_{n}\) (\(1 \le a_{i} \le 10^{9}\)) — the elements of the array.
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print \(0\).
In the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index \(2\) to \(3\).In the third example you can remove the subsegments from index \(1\) to \(2\), or from index \(2\) to \(3\), or from index \(3\) to \(4\).
Input: 3 1 2 3 | Output: 0
Medium
4
473
261
182
12
808
F
808F
F. Card Game
2,400
binary search; flows; graphs
Digital collectible card games have become very popular recently. So Vova decided to try one of these.Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck with total power not less than k, but magic numbers may not allow him t...
The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100000).Then n lines follow, each of these lines contains three numbers that represent the corresponding card: pi, ci and li (1 ≤ pi ≤ 1000, 1 ≤ ci ≤ 100000, 1 ≤ li ≤ n).
If Vova won't be able to build a deck with required power, print - 1. Otherwise print the minimum level Vova has to reach in order to build a deck.
Input: 5 85 5 11 5 44 6 31 12 43 12 1 | Output: 4
Expert
3
687
237
147
8
3
C
3C
C. Tic-tac-toe
1,800
brute force; games; implementation
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the g...
The input consists of three lines, each of the lines contains characters ""."", ""X"" or ""0"" (a period, a capital letter X, or a digit zero).
Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw.
Input: X0X.0..X. | Output: second
Medium
3
954
143
107
0
207
D10
207D10
D10. The Beaver's Problem - 3
2,100
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subj...
The first line contains integer id (0 ≤ id ≤ 106) — the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided in...
Print an integer from 1 to 3, inclusive — the number of the subject the given document corresponds to.
Hard
0
1,472
653
102
2
497
D
497D
2,900
brute force; geometry; math
Master
3
0
0
0
4
1,093
G
1093G
G. Multidimensional Queries
2,300
bitmasks; data structures
You are given an array \(a\) of \(n\) points in \(k\)-dimensional space. Let the distance between two points \(a_x\) and \(a_y\) be \(\sum \limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|\) (it is also known as Manhattan distance).You have to process \(q\) queries of the following two types: \(1\) \(i\) \(b_1\) \(b_2\) ... \(...
The first line contains two numbers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 5\)) — the number of elements in \(a\) and the number of dimensions of the space, respectively.Then \(n\) lines follow, each containing \(k\) integers \(a_{i, 1}\), \(a_{i, 2}\), ..., \(a_{i, k}\) (\(-10^6 \le a_{i, j} \le ...
Print the answer for each query of the second type.
Input: 5 2 1 2 2 3 3 4 4 5 5 6 7 2 1 5 2 1 3 2 3 5 1 5 -1 -2 2 1 5 1 4 -1 -2 2 1 5 | Output: 8 4 4 12 10
Expert
2
512
887
51
10
348
A
348A
A. Mafia
1,600
binary search; math; sortings
One day n friends gathered together to play ""Mafia"". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum numbe...
The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.
In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds.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.
You don't need to know the rules of ""Mafia"" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).
Input: 33 2 2 | Output: 4
Medium
3
433
216
287
3
1,286
C1
1286C1
C1. Madhouse (Easy version)
2,400
brute force; constructive algorithms; interactive; math
This problem is different with hard version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string \(s\) of length \(n\), consisting only of lowercase English letters. The player can as...
First line contains number \(n\) (\(1 \le n \le 100\)) — the length of the picked string.
Input: 4 a aa a cb b c c | Output: ? 1 2 ? 3 4 ? 4 4 ! aabc
Expert
4
1,441
89
0
12
2,064
A
2064A
A. Brogramming Contest
800
greedy; strings
One day after waking up, your friend challenged you to a brogramming contest. In a brogramming contest, you are given a binary string\(^{\text{∗}}\) \(s\) of length \(n\) and an initially empty binary string \(t\). During a brogramming contest, you can make either of the following moves any number of times: remove some...
The first line contains an integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The first line of each test case is an integer \(n\) (\(1 \le n \le 1000\)) — the length of the string \(s\).The second line of each test case contains the binary string \(s\).The sum of \(n\) across all test cases does not exceed...
For each testcase, output the minimum number of moves required.
An optimal solution to the first test case is as follows: \(s = \texttt{00}\color{red}{\texttt{110}}\), \(t =\) empty string. \(s = \texttt{00}\), \(t = \texttt{11}\color{red}{\texttt{0}}\). \(s = \texttt{000}\), \(t = \texttt{11}\). It can be proven that there is no solution using less than \(2\) moves.In the second t...
Input: 55001104111130015000003101 | Output: 2 1 1 0 3
Beginner
2
962
330
63
20
896
E
896E
E. Welcome home, Chtholly
3,100
data structures; dsu
— I... I survived.— Welcome home, Chtholly.— I kept my promise...— I made it... I really made it!After several days of fighting, Chtholly Nota Seniorious miraculously returned from the fierce battle.As promised, Willem is now baking butter cake for her.However, although Willem is skilled in making dessert, he rarely ba...
The first line contains two integers n and m (1 ≤ n, m ≤ 105).The second line contains n integers, i-th of them is ai (1 ≤ ai ≤ 105).The next m lines are the m operations described above. It is guaranteed that 1 ≤ l ≤ r ≤ n and 1 ≤ x ≤ 105.
For each operation of the second type, print the answer.
Input: 5 61 5 5 5 82 2 5 51 2 4 32 2 5 22 2 5 51 3 5 12 1 5 1 | Output: 3303
Master
2
1,121
240
56
8
1,660
F2
1660F2
F2. Promising String (hard version)
2,100
data structures; implementation; math; strings
This is the hard version of Problem F. The only difference between the easy version and the hard version is the constraints.We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings ""+--+"" and ""++-+--"" are balanced, and strings ""+--"", ""--"" and """" are...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) —the number of test cases in the test.Then the descriptions of test cases follow.Each test case of input data consists of two lines. The first line consists of the number \(n\) (\(1 \le n \le 2 \cdot 10^5\)): the length of \(s\).The second lin...
For each test case, print a single number: the number of the promising non-empty substrings of string \(s\). Each non-empty promising substring must be counted in the answer as many times as it occurs in string \(s\).
The following are the promising substrings for the first three test cases in the example: \(s[1 \dots 2]\)=""+-"", \(s[2 \dots 3]\)=""-+""; \(s[1 \dots 2]\)=""-+"", \(s[2 \dots 3]\)=""+-"", \(s[1 \dots 5]\)=""-+---"", \(s[3 \dots 5]\)=""---""; \(s[1 \dots 3]\)=""---"", \(s[2 \dots 4]\)=""---"".
Input: 53+-+5-+---4----7--+---+6+++--- | Output: 2 4 2 7 4
Hard
4
1,286
527
217
16
1,887
D
1887D
D. Split
2,700
binary search; data structures; divide and conquer; dsu; math; trees; two pointers
Let's call an array \(b_1, b_2, \ldots, b_m\) (\(m \ge 2\)) good if it can be split into two parts such that all elements in the left part are strictly smaller than all elements in the right part. In other words, there must exist an index \(1 \le i < m\) such that every element from \(b_1, \ldots, b_i\) is strictly sma...
The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) — the size of the array.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_n \le n\)) — the elements of the array \(a\).The third line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) — the num...
For each query, output ""Yes"" (without quotes) if the array \(a_l, a_{l+1}, \ldots, a_r\) is good, and ""No"" (without quotes) otherwise.You can output ""Yes"" and ""No"" in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
In the first example: The array \([3,2,1,4,5]\) can be split into two parts: \([3,2,1]\) and \([4,5]\). The array \([3,2,1]\) cannot be split into two parts such that all elements in the left part are smaller than all elements in the right part. The array \([3,2,1,4]\) can be split into two parts: \([3,2,1]\) and \([4]...
Input: 5 3 2 1 4 5 5 1 5 1 3 1 4 1 2 2 5 | Output: Yes No Yes No Yes
Master
7
626
474
290
18
1,426
E
1426E
E. Rock, Paper, Scissors
1,800
brute force; constructive algorithms; flows; greedy; math
Alice and Bob have decided to play the game ""Rock, Paper, Scissors"". The game consists of several rounds, each round is independent of each other. In each round, both players show one of the following things at the same time: rock, paper or scissors. If both players showed the same things then the round outcome is a ...
The first line of the input contains one integer \(n\) (\(1 \le n \le 10^{9}\)) — the number of rounds.The second line of the input contains three integers \(a_1, a_2, a_3\) (\(0 \le a_i \le n\)) — the number of times Alice will show rock, scissors and paper, respectively. It is guaranteed that \(a_1 + a_2 + a_3 = n\)....
Print two integers: the minimum and the maximum number of rounds Alice can win.
In the first example, Alice will not win any rounds if she shows scissors and then paper and Bob shows rock and then scissors. In the best outcome, Alice will win one round if she shows paper and then scissors, and Bob shows rock and then scissors.In the second example, Alice will not win any rounds if Bob shows the sa...
Input: 2 0 1 1 1 1 0 | Output: 0 1
Medium
5
1,406
534
79
14
182
A
182A
A. Battlefield
2,200
geometry; graphs; implementation; shortest paths
Vasya lagged behind at the University and got to the battlefield. Just joking! He's simply playing some computer game. The field is a flat platform with n trenches dug on it. The trenches are segments on a plane parallel to the coordinate axes. No two trenches intersect.There is a huge enemy laser far away from Vasya. ...
The first line contains two space-separated integers: a and b (1 ≤ a, b ≤ 1000), — the duration of charging and the duration of shooting, in seconds.The second line contains four space-separated integers: Ax, Ay, Bx, By ( - 104 ≤ Ax, Ay, Bx, By ≤ 104) — the coordinates of points А and B. It is guaranteed that points A ...
If Vasya can get from point A to point B, print the minimum time he will need for it. Otherwise, print number -1.The answer will be considered correct if the absolute or relative error does not exceed 10 - 4
Input: 2 40 5 6 530 0 0 41 1 4 16 0 6 4 | Output: 19.0000000000
Hard
4
1,535
796
207
1
2,106
A
2106A
A. Dr. TC
800
brute force; math
In order to test his patients' intelligence, Dr. TC created the following test.First, he creates a binary string\(^{\text{∗}}\) \(s\) having \(n\) characters. Then, he creates \(n\) binary strings \(a_1, a_2, \ldots, a_n\). It is known that \(a_i\) is created by first copying \(s\), then flipping the \(i\)'th character...
The first line of the input consists of a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10\)) — the length of the binary string \(s\).The second line of each test case contains a single binary string \(s\) of size \...
For each test case, output a single integer, the number of \(\texttt{1}\)s on the board.
The first example is explained in the statement.For the second example, the only string written on the board will be the string \(\texttt{0}\); therefore, the answer is \(0\).In the third example, the following strings will be written on the board: \([\texttt{10000}, \texttt{01000}, \texttt{00100}, \texttt{00010}, \tex...
Input: 53101115000002113010 | Output: 5 0 5 2 4
Beginner
2
885
325
88
21
306
A
306A
A. Candies
800
implementation
Polycarpus has got n candies and m friends (n ≥ m). He wants to make a New Year present with candies to each friend. Polycarpus is planning to present all candies and he wants to do this in the fairest (that is, most equal) manner. He wants to choose such ai, where ai is the number of candies in the i-th friend's prese...
The single line of the input contains a pair of space-separated positive integers n, m (1 ≤ n, m ≤ 100;n ≥ m) — the number of candies and the number of Polycarpus's friends.
Print the required sequence a1, a2, ..., am, where ai is the number of candies in the i-th friend's present. All numbers ai must be positive integers, total up to n, the maximum one should differ from the minimum one by the smallest possible value.
Print ai in any order, separate the numbers by spaces.
Input: 12 3 | Output: 4 4 4
Beginner
1
562
173
248
3
1,186
F
1186F
F. Vus the Cossack and a Graph
2,400
dfs and similar; graphs; greedy; implementation
Vus the Cossack has a simple graph with \(n\) vertices and \(m\) edges. Let \(d_i\) be a degree of the \(i\)-th vertex. Recall that a degree of the \(i\)-th vertex is the number of conected edges to the \(i\)-th vertex.He needs to remain not more than \(\lceil \frac{n+m}{2} \rceil\) edges. Let \(f_i\) be the degree of ...
The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^6\), \(0 \leq m \leq 10^6\)) — the number of vertices and edges respectively.Each of the next \(m\) lines contains two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\)) — vertices between which there is an edge.It is guaranteed that the gr...
In the first line, print one integer \(k\) (\(0 \leq k \leq \lceil \frac{n+m}{2} \rceil\)) — the number of edges which you need to remain.In each of the next \(k\) lines, print two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\)) — the vertices, the edge between which, you need to remain. You can not print the...
Input: 6 6 1 2 2 3 3 4 4 5 5 3 6 5 | Output: 5 2 1 3 2 5 3 5 4 6 5
Expert
4
574
416
346
11
106
A
106A
A. Card Game
1,000
implementation
There is a card game called ""Durak"", which means ""Fool"" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want.To play durak you need a pack of 36 cards. Each card has a suit (""S"", "...
The first line contains the tramp suit. It is ""S"", ""H"", ""D"" or ""C"".The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank (""6"", ""7"", ""8"", ""9"", ""T"", ""J"", ""Q"", ""K"" and ""A""), and the s...
Print ""YES"" (without the quotes) if the first cards beats the second one. Otherwise, print ""NO"" (also without the quotes).
Input: HQH 9S | Output: YES
Beginner
1
1,041
382
126
1
1,819
D
1819D
D. Misha and Apples
2,800
brute force; data structures; dp; two pointers
Schoolboy Misha got tired of doing sports programming, so he decided to quit everything and go to the magical forest to sell magic apples.His friend Danya came to the magical forest to visit Misha. What was his surprise when he found out that Misha found a lot of friends there, the same former sports programmers. And a...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) —the number of test cases. The description of test cases follows.The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 2 \cdot 10^5\)) —the number of stalls and kinds of apples.Eac...
For each test case, output a single integer — the maximum number of apples that could be in Dani's backpack after visiting all the shops at best.
In the first test case, Danya remembers all the shops, so the process will be deterministic. He will take two apples at the first shop and two more at the second, but after he puts them in his backpack, they will disappear. So at the end there will only be \(2\) apples left, which he will take at the third shop.In the ...
Input: 43 42 1 22 4 12 1 24 42 1 22 3 401 12 5005 303 1 2 32 3 101 3 | Output: 2 1 5 3
Master
4
1,629
945
145
18
367
C
367C
C. Sereja and the Arrangement of Numbers
2,000
graphs; greedy; sortings
Let's call an array consisting of n integer numbers a1, a2, ..., an, beautiful if it has the following property: consider all pairs of numbers x, y (x ≠ y), such that number x occurs in the array a and number y occurs in the array a; for each pair x, y must exist some position j (1 ≤ j < n), such that at least one of t...
The first line contains two integers n and m (1 ≤ n ≤ 2·106, 1 ≤ m ≤ 105). Next m lines contain pairs of integers. The i-th line contains numbers qi, wi (1 ≤ qi, wi ≤ 105).It is guaranteed that all qi are distinct.
In a single line print maximum amount of money (in rubles) Sereja can pay.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 Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1, 2, 1, 2, 2]. There are another optimal arrays for this test.In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2].
Input: 5 21 22 3 | Output: 5
Hard
3
1,092
214
221
3
1,641
A
1641A
A. Great Sequence
1,200
brute force; greedy; sortings
A sequence of positive integers is called great for a positive integer \(x\), if we can split it into pairs in such a way that in each pair the first number multiplied by \(x\) is equal to the second number. More formally, a sequence \(a\) of size \(n\) is great for a positive integer \(x\), if \(n\) is even and there ...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 20\,000\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\), \(x\) (\(1 \le n \le 2 \cdot 10^5\), \(2 \le x \le 10^6\)).The next line contai...
For each test case print a single integer — the minimum number of integers that can be added to the end of \(a\) to make it a great sequence for the number \(x\).
In the first test case, Sam got lucky and the sequence is already great for the number \(4\) because you can divide it into such pairs: \((1, 4)\), \((4, 16)\). Thus we can add \(0\) numbers.In the second test case, you can add numbers \(1\) and \(14\) to the sequence, then you can divide all \(8\) integers into such p...
Input: 44 41 16 4 46 21 2 2 2 4 75 35 2 3 5 159 1010 10 10 20 1 100 200 2000 3 | Output: 0 2 3 3
Easy
3
683
481
162
16
1,158
A
1158A
A. The Party and Sweets
1,500
binary search; constructive algorithms; greedy; implementation; math; sortings; two pointers
\(n\) boys and \(m\) girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from \(1\) to \(n\) and all girls are numbered with integers from \(1\) to \(m\). For all \(1 \leq i \leq n\) the minimal number of sweets, which \(i\)-th boy pre...
The first line contains two integers \(n\) and \(m\), separated with space — the number of boys and girls, respectively (\(2 \leq n, m \leq 100\,000\)). The second line contains \(n\) integers \(b_1, \ldots, b_n\), separated by spaces — \(b_i\) is equal to the minimal number of sweets, which \(i\)-th boy presented to s...
If the described situation is impossible, print \(-1\). In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
In the first test, the minimal total number of sweets, which boys could have presented is equal to \(12\). This can be possible, for example, if the first boy presented \(1\) and \(4\) sweets, the second boy presented \(3\) and \(2\) sweets and the third boy presented \(1\) and \(1\) sweets for the first and the second...
Input: 3 2 1 2 1 3 4 | Output: 12
Medium
7
1,078
560
187
11
1,705
D
1705D
D. Mark and Lightbulbs
1,800
combinatorics; constructive algorithms; greedy; math; sortings
Mark has just purchased a rack of \(n\) lightbulbs. The state of the lightbulbs can be described with binary string \(s = s_1s_2\dots s_n\), where \(s_i=\texttt{1}\) means that the \(i\)-th lightbulb is turned on, while \(s_i=\texttt{0}\) means that the \(i\)-th lightbulb is turned off.Unfortunately, the lightbulbs are...
The first line of the input contains a single integer \(q\) (\(1\leq q\leq 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(3\leq n\leq 2\cdot 10^5\)) — the number of lightbulbs.The second line of each test case contains a binary string \(s\) of length \(n\) — the i...
For each test case, print a line containing the minimum number of operations Mark needs to perform to transform \(s\) to \(t\). If there is no such sequence of operations, print \(-1\).
In the first test case, one sequence of operations that achieves the minimum number of operations is the following. Select \(i=3\), changing \(\texttt{01}\color{red}{\texttt{0}}\texttt{0}\) to \(\texttt{01}\color{red}{\texttt{1}}\texttt{0}\). Select \(i=2\), changing \(\texttt{0}\color{red}{\texttt{1}}\texttt{10}\) to ...
Input: 4401000010410100100501001000116000101010011 | Output: 2 -1 -1 5
Medium
5
737
560
185
17
1,844
C
1844C
C. Particles
1,300
dp; greedy; implementation; math
You have discovered \(n\) mysterious particles on a line with integer charges of \(c_1,\dots,c_n\). You have a device that allows you to perform the following operation: Choose a particle and remove it from the line. The remaining particles will shift to fill in the gap that is created. If there were particles with cha...
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 \(c_1...
For each test case, output one integer, the maximum charge of the remaining particle.
In the first test case, the best strategy is to use the device on the \(4\)th particle, then on the \(1\)st particle (as described in the statement), and finally use the device on the new \(3\)rd particle followed by the \(1\)st particle.In the second test case, the best strategy is to use the device on the \(4\)th par...
Input: 36-3 1 4 -1 5 -95998244353 998244353 998244353 998244353 9982443531-2718 | Output: 9 2994733059 -2718
Easy
4
869
454
85
18
593
C
593C
C. Beautiful Function
2,200
constructive algorithms; math
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the i...
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Correct functions: 10 (1+2) ((t-3)+(t*4)) abs((t-10)) (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13))) abs((t-(abs((t*31))+14))))Incorrect functions: 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7))) abs(t-3) (not enough brackets, it should be abs((t-3)) 2+(2-3 (one bracket too many) 1(t+5) (n...
Input: 30 10 410 0 420 10 4 | Output: t abs((t-10))
Hard
2
1,592
272
300
5
1,840
B
1840B
B. Binary Cafe
1,100
bitmasks; combinatorics; math
Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place.The cafe offers visitors \(k\) different delicious desserts. The desserts are numbered from \(0\) to \(k-1\). The cost of the \(i\)-th dessert is \(2^i\) coins, because it is a binary cafe! Toma is willing to spend no more tha...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.Then follows \(t\) lines, each of which describes one test case.Each test case is given on a single line and consists of two integers \(n\) and \(k\) (\(1 \le n, k \le 10^9\)) — the number of coins Toma is will...
Output \(t\) integers, the \(i\)-th of which should be equal to the answer for the \(i\)-th test case — the number of ways to buy desserts for tasting.
Variants for 1st sample: {}, {1}Variants for 2nd sample: {}, {1}Variants for 3rd sample: {}, {1}, {2}Variants for 4th sample: {}, {1}, {2}, {1, 2}
Input: 51 22 12 210 2179 100 | Output: 2 2 3 4 180
Easy
3
559
379
151
18