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,739
C
1739C
C. Card Game
1,500
combinatorics; constructive algorithms; dp; games
Consider a game with \(n\) cards (\(n\) is even). Each card has a number written on it, between \(1\) and \(n\). All numbers on the cards are different. We say that a card with number \(x\) is stronger than a card with number \(y\) if \(x > y\).Two players, Alex and Boris, play this game. In the beginning, each of them...
The first line contains one integer \(t\) (\(1 \le t \le 30\)) β€” the number of test cases.Then, \(t\) lines follow. The \(i\)-th line contains one even integer \(n\) (\(2 \le n \le 60\)).
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo \(998244353\).
In the first test case, Alex wins if he receives the card \(2\) (he plays it, and Boris cannot respond). If Alex receives the card \(1\), the game ends in a draw.In the second test case: Alex wins if he receives the cards \([3, 4]\), \([2, 4]\) or \([1, 4]\); Boris wins if Alex receives the cards \([1, 2]\) or \([1, 3]...
Input: 5246860 | Output: 1 0 1 3 2 1 12 7 1 42 27 1 341102826 248150916 1
Medium
4
2,236
187
307
17
1,463
F
1463F
F. Max Correct Set
3,100
bitmasks; dp; math
Let's call the set of positive integers \(S\) correct if the following two conditions are met: \(S \subseteq \{1, 2, \dots, n\}\); if \(a \in S\) and \(b \in S\), then \(|a-b| \neq x\) and \(|a-b| \neq y\). For the given values \(n\), \(x\), and \(y\), you have to find the maximum size of the correct set.
A single line contains three integers \(n\), \(x\) and \(y\) (\(1 \le n \le 10^9\); \(1 \le x, y \le 22\)).
Print one integer β€” the maximum size of the correct set.
Input: 10 2 5 | Output: 5
Master
3
306
107
56
14
125
C
125C
C. Hobbits' Party
1,600
constructive algorithms; greedy
Everyone knows that hobbits love to organize all sorts of parties and celebrations. There are n hobbits living in the Shire. They decided to organize the Greatest Party (GP) that would last for several days. Next day the hobbits wrote a guest list, some non-empty set containing all the inhabitants of the Shire. To ensu...
The first line contains an integer n (3 ≀ n ≀ 10000), representing the number of hobbits.
In the first output line print a number k β€” the maximum duration of GP in days. Then on k lines print the guest lists, (the guests should be separated by spaces). Print each guest list on the single line. Each list can contain an arbitrary positive number of hobbits. The hobbits are numbered with integers from 1 to n.
Input: 4 | Output: 31 2 1 3 2 3
Medium
2
814
89
319
1
1,176
D
1176D
D. Recover it!
1,800
dfs and similar; graphs; greedy; number theory; sortings
Authors guessed an array \(a\) consisting of \(n\) integers; each integer is not less than \(2\) and not greater than \(2 \cdot 10^5\). You don't know the array \(a\), but you know the array \(b\) which is formed from it with the following sequence of operations: Firstly, let the array \(b\) be equal to the array \(a\)...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in \(a\).The second line of the input contains \(2n\) integers \(b_1, b_2, \dots, b_{2n}\) (\(2 \le b_i \le 2750131\)), where \(b_i\) is the \(i\)-th element of \(b\). \(2750131\) is the \(199999\)-th prime nu...
In the only line of the output print \(n\) integers \(a_1, a_2, \dots, a_n\) (\(2 \le a_i \le 2 \cdot 10^5\)) in any order β€” the array \(a\) from which the array \(b\) can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.
Input: 3 3 5 2 3 2 4 | Output: 3 4 2
Medium
5
1,103
325
293
11
1,967
A
1967A
A. Permutation Counting
1,400
binary search; greedy; implementation; math; sortings
You have some cards. An integer between \(1\) and \(n\) is written on each card: specifically, for each \(i\) from \(1\) to \(n\), you have \(a_i\) cards which have the number \(i\) written on them.There is also a shop which contains unlimited cards of each type. You have \(k\) coins, so you can buy \(k\) new cards in ...
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\), \(k\) (\(1\le n \le 2 \cdot 10^5\), \(0\le k \le 10^{12}\)) β€” the number of distinct types of cards and...
For each test case, output a single line containing an integer: the maximum score you can get.
In the first test case, the final (and only) array we can get is \([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\) (including \(11\) single \(1\)s), which contains \(11\) subarrays consisting of a permutation of \([1]\).In the second test case, we can buy \(0\) cards of type \(1\) and \(4\) cards of type \(2\), and then we rearran...
Input: 81 1012 48 43 46 1 83 97 6 25 36 6 7 4 69 77 6 1 7 6 2 4 3 310 101 3 1 2 1 9 3 5 7 59 85 8 7 5 1 3 2 9 8 | Output: 11 15 15 22 28 32 28 36
Easy
5
637
607
94
19
1,498
C
1498C
C. Planar Reflections
1,600
brute force; data structures; dp
Gaurang has grown up in a mystical universe. He is faced by \(n\) consecutive 2D planes. He shoots a particle of decay age \(k\) at the planes.A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age \(k-1\). If a part...
The first line of the input contains the number of test cases \(t\) (\(1 \le t \le 100\)). Then, \(t\) lines follow, each containing two integers \(n\) and \(k\) (\(1 \le n, k \le 1000\)). Additionally, the sum of \(n\) over all test cases will not exceed \(1000\), and the sum of \(k\) over all test cases will not exce...
Output \(t\) integers. The \(i\)-th of them should be equal to the answer to the \(i\)-th test case.
Let us explain the first example with four test cases. Test case 1: (\(n = 2\), \(k = 3\)) is already explained in the problem statement.See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in th...
Input: 4 2 3 2 2 3 1 1 3 | Output: 4 3 1 2
Medium
3
1,444
374
100
14
1,648
C
1648C
C. Tyler and Strings
1,900
combinatorics; data structures; implementation
While looking at the kitchen fridge, the little boy Tyler noticed magnets with symbols, that can be aligned into a string \(s\).Tyler likes strings, and especially those that are lexicographically smaller than another string, \(t\). After playing with magnets on the fridge, he is wondering, how many distinct strings ca...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 200\,000\)) β€” the lengths of strings \(s\) and \(t\) respectively.The second line contains \(n\) integers \(s_1, s_2, s_3, \ldots, s_n\) (\(1 \le s_i \le 200\,000\)) β€” letters of the string \(s\).The third line contains \(m\) integers \(t_1, t_2, t_...
Print a single number β€” the number of strings lexicographically smaller than \(t\) that can be obtained by rearranging the letters in \(s\), modulo \(998\,244\,353\).
In the first example, the strings we are interested in are \([1\, 2\, 2]\) and \([2\, 1\, 2]\). The string \([2\, 2\, 1]\) is lexicographically larger than the string \([2\, 1\, 2\, 1]\), so we don't count it.In the second example, all strings count except \([4\, 3\, 2\, 1]\), so the answer is \(4! - 1 = 23\).In the th...
Input: 3 4 1 2 2 2 1 2 1 | Output: 2
Hard
3
1,210
396
166
16
1,612
F
1612F
F. Armor and Weapons
2,800
brute force; dp; greedy; shortest paths
Monocarp plays a computer game. There are \(n\) different sets of armor and \(m\) different weapons in this game. If a character equips the \(i\)-th set of armor and wields the \(j\)-th weapon, their power is usually equal to \(i + j\); but some combinations of armor and weapons synergize well. Formally, there is a lis...
The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)) β€” the number of armor sets and the number of weapons, respectively.The second line contains one integer \(q\) (\(0 \le q \le \min(2 \cdot 10^5, nm)\)) β€” the number of combinations that synergize well.Then \(q\) lines follow, the \(i\...
Print one integer β€” the minimum number of hours Monocarp has to spend to obtain both the \(n\)-th armor set and the \(m\)-th weapon.
In the first example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: Obtain the \(2\)-nd weapon using the \(1\)-st armor set and the \(1\)-st weapon; Obtain the \(3\)-rd armor set using the \(1\)-st armor set and the \(2\)-nd weapon; Obtain the \(4\)-th weapon using the \(3\)-rd armor ...
Input: 3 4 0 | Output: 3
Master
4
1,173
535
132
16
1,205
B
1205B
B. Shortest Cycle
1,900
bitmasks; brute force; graphs; shortest paths
You are given \(n\) integer numbers \(a_1, a_2, \dots, a_n\). Consider graph on \(n\) nodes, in which nodes \(i\), \(j\) (\(i\neq j\)) are connected if and only if, \(a_i\) AND \(a_j\neq 0\), where AND denotes the bitwise AND operation.Find the length of the shortest cycle in this graph or determine that it doesn't hav...
The first line contains one integer \(n\) \((1 \le n \le 10^5)\) β€” number of numbers.The second line contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^{18}\)).
If the graph doesn't have any cycles, output \(-1\). Else output the length of the shortest cycle.
In the first example, the shortest cycle is \((9, 3, 6, 28)\).In the second example, the shortest cycle is \((5, 12, 9)\).The graph has no cycles in the third example.
Input: 4 3 6 28 9 | Output: 4
Hard
4
336
185
98
12
1,454
F
1454F
F. Array Partition
2,100
binary search; data structures; greedy; two pointers
You are given an array \(a\) consisting of \(n\) integers.Let \(min(l, r)\) be the minimum value among \(a_l, a_{l + 1}, \ldots, a_r\) and \(max(l, r)\) be the maximum value among \(a_l, a_{l + 1}, \ldots, a_r\).Your task is to choose three positive (greater than \(0\)) integers \(x\), \(y\) and \(z\) such that: \(x + ...
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the length of \(a\).The second line of the test case contains \(n\) integers \(a_1, a...
For each test case, print the answer: NO in the only line if there is no such partition of \(a\) that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers \(x\), \(y\) and \(z\) (\(x + y + z = n\)) in the second line.If there are several answers, you can print a...
Input: 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 | Output: YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4
Hard
4
778
513
323
14
1,165
F2
1165F2
F2. Microtransactions (hard version)
2,000
binary search; greedy; implementation
The only difference between easy and hard versions is constraints.Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β€” and he won't start playing until he gets all of them.Each ...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β€” the number of types of microtransactions and the number of special offers in the game shop.The second line of the input contains \(n\) integers \(k_1, k_2, \dots, k_n\) (\(0 \le k_i \le 2 \cdot 10^5\)), where \(k_i\) i...
Print one integer β€” the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Input: 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 | Output: 8
Hard
3
1,261
808
114
11
1,896
H1
1896H1
H1. Cyclic Hamming (Easy Version)
3,400
brute force; divide and conquer; dp; fft; math; number theory
This is the easy version of the problem. The only difference between the two versions is the constraint on \(k\). You can make hacks only if all versions of the problem are solved.In this statement, all strings are \(0\)-indexed.For two strings \(a\), \(b\) of the same length \(p\), we define the following definitions:...
The first line of the input contains a single integer \(k\) (\(1 \le k \le 7\)).The second line of the input contains string \(s\) of size \(2^{k+1}\), consisting of the characters '0', '1' and '?'.The third line of the input contains string \(t\) of size \(2^{k+1}\), consisting of the characters '0', '1' and '?'.It is...
Print a single integer β€” the answer to the problem modulo \(998\,244\,353\).
In the first example, we can check that the condition \(h(s, c) \ge 2^k\) for all cyclic shift \(c\) of \(t\) is satisfied. In particular: for \(c = \mathtt{0101}\), \(h(s, c) = h(\mathtt{0110}, \mathtt{0101}) = 2 \ge 2^1\); for \(c = \mathtt{1010}\), \(h(s, c) = h(\mathtt{0110}, \mathtt{1010}) = 2 \ge 2^1\). In the se...
Input: 1 0011 0101 | Output: 1
Master
6
1,228
417
76
18
257
A
257A
A. Sockets
1,100
greedy; implementation; sortings
Vasya has got many devices that work on electricity. He's got n supply-line filters to plug the devices, the i-th supply-line filter has ai sockets.Overall Vasya has got m devices and k electrical sockets in his flat, he can plug the devices or supply-line filters directly. Of course, he can plug the supply-line filter...
The first line contains three integers n, m, k (1 ≀ n, m, k ≀ 50) β€” the number of supply-line filters, the number of devices and the number of sockets that he can plug to directly, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 50) β€” number ai stands for the number of soc...
Print a single number β€” the minimum number of supply-line filters that is needed to plug all the devices to electricity. If it is impossible to plug all the devices even using all the supply-line filters, print -1.
In the first test case he can plug the first supply-line filter directly to electricity. After he plug it, he get 5 (3 on the supply-line filter and 2 remaining sockets for direct plugging) available sockets to plug. Thus, one filter is enough to plug 5 devices.One of the optimal ways in the second test sample is to pl...
Input: 3 5 33 1 2 | Output: 1
Easy
3
856
356
214
2
1,761
F2
1761F2
F2. Anti-median (Hard Version)
3,500
combinatorics; dp; math
This is the hard version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if all versions of the problem are solved.Let's call an array \(a\) of odd length \(2m+1\) (with \(m \ge 1\)) bad, if element \(a_{m+1}\) is equal to the median of this array. In oth...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) \((2 \le n \le 10^6)\) β€” the length of the permutation.The second line of each test case contains \(n\) integers \(p_1,...
For each test case, output a single integer β€” the number of ways to set unknown values to obtain an anti-median permutation, modulo \(10^9+7\).
In the first test case, both \([1, 2]\) and \([2, 1]\) are anti-median.In the second test case, permutations \([1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2]\) are anti-median. The remaining two permutations, \([1, 2, 3]\), \([3, 2, 1]\), are bad arrays on their own, as their median, \(2\), is in their middle.In the third ...
Input: 52-1 -13-1 -1 -141 2 3 46-1 -1 3 4 -1 -18-1 -1 -1 -1 -1 -1 -1 -1 | Output: 2 4 0 1 316
Master
3
764
653
143
17
840
B
840B
B. Leha and another game about graph
2,100
constructive algorithms; data structures; dfs and similar; dp; graphs
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or - 1. To pass the level, he needs to find a Β«goodΒ» subset of edges of the graph or say,...
The first line contains two integers n, m (1 ≀ n ≀ 3Β·105, n - 1 ≀ m ≀ 3Β·105) β€” number of vertices and edges.The second line contains n integers d1, d2, ..., dn ( - 1 ≀ di ≀ 1) β€” numbers on the vertices.Each of the next m lines contains two integers u and v (1 ≀ u, v ≀ n) β€” edges. It's guaranteed, that graph in the inpu...
Print - 1 in a single line, if solution doesn't exist. Otherwise in the first line k β€” number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1.
In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
Input: 1 01 | Output: -1
Hard
5
659
335
231
8
1,114
C
1114C
C. Trailing Loves (or L'oeufs?)
1,700
brute force; implementation; math; number theory
The number ""zero"" is called ""love"" (or ""l'oeuf"" to be precise, literally means ""egg"" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number \(9200\) has two trailing zeros. Aki thinks the more trailing zero ...
The only line of the input contains two integers \(n\) and \(b\) (\(1 \le n \le 10^{18}\), \(2 \le b \le 10^{12}\)).
Print an only integer β€” the number of trailing zero digits in the \(b\)-ary representation of \(n!\)
In the first example, \(6!_{(10)} = 720_{(10)} = 880_{(9)}\).In the third and fourth example, \(5!_{(10)} = 120_{(10)} = 1111000_{(2)}\).The representation of the number \(x\) in the \(b\)-ary base is \(d_1, d_2, \ldots, d_k\) if \(x = d_1 b^{k - 1} + d_2 b^{k - 2} + \ldots + d_k b^0\), where \(d_i\) are integers and \...
Input: 6 9 | Output: 1
Medium
4
897
116
100
11
246
E
246E
E. Blood Cousins Return
2,400
binary search; data structures; dfs and similar; dp; sortings
Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.We call the man with a number a a 1-ancestor of the man with a ...
The first line of the input contains a single integer n (1 ≀ n ≀ 105) β€” the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string si and integer ri (0 ≀ ri ≀ n), where si is the name of the man with a number i, and ri is either the number...
Print m whitespace-separated integers β€” the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.
Input: 6pasha 0gerald 1gerald 1valera 2igor 3olesya 151 11 21 33 16 1 | Output: 22010
Expert
5
1,224
804
164
2
1,023
A
1023A
A. Single Wildcard Pattern Matching
1,200
brute force; implementation; strings
You are given two strings \(s\) and \(t\). The string \(s\) consists of lowercase Latin letters and at most one wildcard character '*', the string \(t\) consists only of lowercase Latin letters. The length of the string \(s\) equals \(n\), the length of the string \(t\) equals \(m\).The wildcard character '*' in the st...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β€” the length of the string \(s\) and the length of the string \(t\), respectively.The second line contains string \(s\) of length \(n\), which consists of lowercase Latin letters and at most one wildcard character '*'.The third line ...
Print ""YES"" (without quotes), if you can obtain the string \(t\) from the string \(s\). Otherwise print ""NO"" (without quotes).
In the first example a wildcard character '*' can be replaced with a string ""force"". So the string \(s\) after this replacement is ""codeforces"" and the answer is ""YES"".In the second example a wildcard character '*' can be replaced with an empty string. So the string \(s\) after this replacement is ""vkcup"" and t...
Input: 6 10code*scodeforces | Output: YES
Easy
3
938
406
130
10
1,592
A
1592A
A. Gamer Hemose
800
binary search; greedy; math; sortings
One day, Ahmed_Hossam went to Hemose and said ""Let's solve a gym contest!"". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has \(n\) weapons. The \(i\)...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) \((1 \leq t \leq 10^5)\). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(H\) \((2 \leq n \leq 10^3, 1 \leq H \leq 10^9)\) β€” the number of available weapons and the ini...
For each test case, print a single integer β€” the minimum number of times that the Agent will have to use the weapons to kill the enemy.
In the first test case, the Agent can use the second weapon, making health value of the enemy equal to \(4-7=-3\). \(-3 \le 0\), so the enemy is dead, and using weapon \(1\) time was enough.In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will d...
Input: 3 2 4 3 7 2 6 4 2 3 11 2 1 7 | Output: 1 2 3
Beginner
4
846
587
135
15
1,912
I
1912I
3,300
geometry; math; two pointers
Master
3
0
0
0
19
180
B
180B
B. Divisibility Rules
2,300
math; number theory
Vasya studies divisibility rules at school. Here are some of them: Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even. Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3. Divisibility by 4. A number i...
The first input line contains two integers b and d (2 ≀ b, d ≀ 100) β€” the notation system base and the divisor. Both numbers are given in the decimal notation.
On the first output line print the type of the rule in the b-based notation system, where the divisor is d: ""2-type"", ""3-type"", ""11-type"", ""6-type"" or ""7-type"". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line t...
The divisibility rule for number 3 in binary notation looks as follows: ""A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3"". That's an 11-type rule. For example, 2110 = 101012. For i...
Input: 10 10 | Output: 2-type1
Expert
2
2,823
159
414
1
913
H
913H
H. Don't Exceed
3,400
math; probabilities
You generate real numbers s1, s2, ..., sn as follows: s0 = 0; si = si - 1 + ti, where ti is a real number chosen independently uniformly at random between 0 and 1, inclusive. You are given real numbers x1, x2, ..., xn. You are interested in the probability that si ≀ xi is true for all i simultaneously.It can be shown t...
The first line contains integer n (1 ≀ n ≀ 30).The next n lines contain real numbers x1, x2, ..., xn, given with at most six digits after the decimal point (0 < xi ≀ n).
Print a single integer, the answer to the problem.
In the first example, the sought probability is 1 since the sum of i real numbers which don't exceed 1 doesn't exceed i.In the second example, the probability is x1 itself.In the third example, the sought probability is 3 / 8.
Input: 41.0023.0000004.0 | Output: 1
Master
2
439
169
50
9
1,857
B
1857B
B. Maximum Rounding
1,100
greedy; implementation; math
Given a natural number \(x\). You can perform the following operation: choose a positive integer \(k\) and round \(x\) to the \(k\)-th digit Note that the positions are numbered from right to left, starting from zero. If the number has \(k\) digits, it is considered that the digit at the \(k\)-th position is equal to \...
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases.Each test case consists of positive integer \(x\) with a length of up to \(2 \cdot 10^5\). It is guaranteed that there are no leading zeros in the integer.It is guaranteed that the sum of the lengths of all integers \(x\) ove...
For each set of input data, output the maximum possible value of \(x\) after the operations. The number should not have leading zeros in its representation.
In the first sample, it is better not to perform any operations.In the second sample, you can perform one operation and obtain \(10\).In the third sample, you can choose \(k=1\) or \(k=2\). In both cases the answer will be \(100\).
Input: 101599913198020444204456094741986040862016542130810467 | Output: 1 10 100 1000 2000 20444 21000 100000 420000 41000000000000000000
Easy
3
1,522
370
156
18
720
B
720B
B. Cactusophobia
2,400
dfs and similar; flows
Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle.Vasya has an edge cactus, each edge of this graph has some color.Vasya would like to remove the minimal number of edges in such way that...
The first line contains two integers: n, m (2 ≀ n ≀ 10 000) β€” the number of vertices and the number of edges in Vasya's graph, respectively.The following m lines contain three integers each: u, v, c (1 ≀ u, v ≀ n, u β‰  v, 1 ≀ c ≀ m) β€” the numbers of vertices connected by the corresponding edge, and its color. It is guar...
Output one integer: the maximal number of different colors that the resulting tree can have.
Input: 4 41 2 42 3 13 4 24 2 3 | Output: 3
Expert
2
544
377
92
7
2,038
N
2038N
N. Fixing the Expression
800
implementation
An expression is a string consisting of three characters, where the first and the last characters are digits (from \(0\) to \(9\)), and the middle character is a comparison symbol (<, = or >).An expression is true if the comparison symbol matches the digits (for example, if the first digit is strictly less than the las...
The first line contains one integer \(t\) (\(1 \le t \le 300\)) β€” the number of test cases.Each test case consists of one line containing the string \(s\) (\(|s| = 3\), the first and the last characters of \(s\) are digits, the second character is a comparison symbol).
For each test case, print a string consisting of \(3\) characters β€” a true expression which can be obtained by changing as few characters as possible in \(s\). If there are multiple answers, print any of them.
Input: 53<73>78=90=05<3 | Output: 3<7 8>7 8<9 0=0 0<3
Beginner
1
637
269
209
20
248
D
248D
D. Sweets for Everyone!
2,300
binary search; greedy; implementation
For he knew every Who down in Whoville beneath, Was busy now, hanging a mistletoe wreath. ""And they're hanging their stockings!"" he snarled with a sneer, ""Tomorrow is Christmas! It's practically here!""Dr. Suess, How The Grinch Stole ChristmasChristmas celebrations are coming to Whoville. Cindy Lou Who and her paren...
The first line of the input contains two space-separated integers n and t (2 ≀ n ≀ 5Β·105, 1 ≀ t ≀ 109). The second line of the input contains n characters, the i-th of them equals ""H"" (if the i-th segment contains a house), ""S"" (if the i-th segment contains a shop) or ""."" (if the i-th segment doesn't contain a ho...
If there isn't a single value of k that makes it possible to give sweets to everybody in at most t units of time, print in a single line ""-1"" (without the quotes). Otherwise, print on a single line the minimum possible value of k.
In the first example, there are as many stores, as houses. If the family do not take a single kilo of sweets from home, in order to treat the inhabitants of the first house, they will need to make at least one step back, and they have absolutely no time for it. If they take one kilogram of sweets, they won't need to go...
Input: 6 6HSHSHS | Output: 1
Expert
3
2,410
401
232
2
89
A
89A
A. Robbery
1,800
greedy
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.Unfortunately, Joe di...
The first line contains integers n, m and k (1 ≀ n ≀ 104, 1 ≀ m, k ≀ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β€” it is an integer from 0 to 105.
Print a single number β€” the maximum number of diamonds Joe can steal.
In the second sample Joe can act like this:The diamonds' initial positions are 4 1 3.During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and t...
Input: 2 3 12 3 | Output: 0
Medium
1
1,763
210
69
0
1,506
E
1506E
E. Restoring the Permutation
1,500
constructive algorithms; implementation
A permutation is a sequence of \(n\) integers from \(1\) to \(n\), in which all numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) are permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) are not.Polycarp was presented with a permutation \(p\) of numbers from \(1\) to \(n\). However...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le n\)).It is guaranteed that the ar...
For each test case, output two lines: on the first line output \(n\) integers β€” lexicographically minimal permutation that could have been originally presented to Polycarp; on the second line print \(n\) integers β€” lexicographically maximal permutation that could have been originally presented to Polycarp;
Input: 4 7 3 3 4 4 7 7 7 4 1 2 3 4 7 3 4 5 5 5 7 7 1 1 | Output: 3 1 4 2 7 5 6 3 2 4 1 7 6 5 1 2 3 4 1 2 3 4 3 4 5 1 2 7 6 3 4 5 2 1 7 6 1 1
Medium
2
1,550
501
307
15
178
B1
178B1
B1. Greedy Merchants
1,600
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities....
The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 ≀ ai, bi ≀ n, ai β‰  bi), separated by a space β€” the numbers of cities connected by the i-th road. It is guaranteed that...
Print exactly k lines, the i-th line should contain a single integer di β€” the number of dinars that the i-th merchant paid.
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is des...
Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120
Medium
0
1,676
1,052
123
1
1,773
B
1773B
B. BinCoin
2,200
binary search; divide and conquer; hashing; implementation; probabilities; trees
There are \(n\) employees in the BinCoin company numbered from \(1\) to \(n\). The subordination structure in this company is a rooted tree. In other words: There is one CEO in the company β€” the main boss. Each other employee has exactly one direct superior. There are no cycles in the subordination structure. Moreover,...
The first line contains two integers \(n\) and \(k\) β€” the number of employees and the number of records in the journal (\(1 \le n \le 999\); \(50 \le k \le 100\)).Each of the next \(k\) lines contains a permutation of integers from \(1\) to \(n\) β€” the order of employees in the corresponding record.It is guaranteed th...
Output \(n\) integers \(p_i\). If \(i\) is a CEO, then \(p_i\) should be \(-1\). Otherwise, \(p_i\) should be the index of the direct superior of \(i\)-th employee.Your output should correspond to a binary rooted tree. If there are several trees satisfying the input, you can output any one of them.
In order to fit on the page, several consecutive lines in the examples were joined into one. The real inputs follow the input description.
Input: 3 50 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 1 2 3 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 3 2 1 1 2 3 1 2 3 3 2 1 3 2 1 | Output...
Hard
6
1,662
402
299
17
1,601
A
1601A
A. Array Elimination
1,300
bitmasks; greedy; math; number theory
You are given array \(a_1, a_2, \ldots, a_n\), consisting of non-negative integers.Let's define operation of ""elimination"" with integer parameter \(k\) (\(1 \leq k \leq n\)) as follows: Choose \(k\) distinct array indices \(1 \leq i_1 < i_2 < \ldots < i_k \le n\). Calculate \(x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ \ldots ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 200\,000\)) β€” the length of array \(a\).The second line of each test case contains \...
For each test case, print all values \(k\), such that it's possible to make all elements of \(a\) equal to \(0\) in a finite number of elimination operations with the given parameter \(k\).Print them in increasing order.
In the first test case: If \(k = 1\), we can make four elimination operations with sets of indices \(\{1\}\), \(\{2\}\), \(\{3\}\), \(\{4\}\). Since \(\&\) of one element is equal to the element itself, then for each operation \(x = a_i\), so \(a_i - x = a_i - a_i = 0\). If \(k = 2\), we can make two elimination operat...
Input: 5 4 4 4 4 4 4 13 7 25 19 6 3 5 3 1 7 1 1 1 5 0 0 0 0 0 | Output: 1 2 4 1 2 1 1 1 2 3 4 5
Easy
4
917
493
220
16
1,662
I
1662I
I. Ice Cream Shop
0
brute force; implementation; sortings
On a beach there are \(n\) huts in a perfect line, hut \(1\) being at the left and hut \(i+1\) being \(100\) meters to the right of hut \(i\), for all \(1 \le i \le n - 1\). In hut \(i\) there are \(p_i\) people.There are \(m\) ice cream sellers, also aligned in a perfect line with all the huts. The \(i\)-th ice cream ...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 200\,000\), \(1 \le m \le 200\,000\)) β€” the number of huts and the number of ice cream sellers.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le 10^9\)) β€” the number of people in each hut.The third line contains \(m\) i...
Print the maximum number of ice creams that can be sold by choosing optimally the location of the new shop.
In the first sample, you can place the shop (coloured orange in the picture below) \(150\) meters to the right of the first hut (for example) so that it is the closest shop to the first two huts, which have \(2\) and \(5\) people, for a total of \(7\) sold ice creams. In the second sample, you can place the shop \(170\...
Input: 3 1 2 5 6 169 | Output: 7
Beginner
3
1,116
450
107
16
1,466
E
1466E
E. Apollo versus Pan
1,800
bitmasks; brute force; math
Only a few know that Pan and Apollo weren't only battling for the title of the GOAT musician. A few millenniums later, they also challenged each other in math (or rather in fast calculations). The task they got to solve is the following:Let \(x_1, x_2, \ldots, x_n\) be the sequence of \(n\) non-negative integers. Find ...
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1\,000\)) denoting the number of test cases, then \(t\) test cases follow.The first line of each test case consists of a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)), the length of the sequence. The second one contains \(n\) non-negat...
Print \(t\) lines. The \(i\)-th line should contain the answer to the \(i\)-th text case.
Input: 8 2 1 7 3 1 2 4 4 5 5 5 5 5 6 2 2 1 0 1 0 1 1 6 1 12 123 1234 12345 123456 5 536870912 536870911 1152921504606846975 1152921504606846974 1152921504606846973 | Output: 128 91 1600 505 0 1 502811676 264880351
Medium
3
615
481
89
14
1,475
A
1475A
A. Odd Divisor
900
math; number theory
You are given an integer \(n\). Check if \(n\) has an odd divisor, greater than one (does there exist such a number \(x\) (\(x > 1\)) that \(n\) is divisible by \(x\) and \(x\) is odd).For example, if \(n=6\), then there is \(x=3\). If \(n=4\), then such a number does not exist.
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case contains one integer \(n\) (\(2 \le n \le 10^{14}\)).Please note, that the input for some test cases won't fit into \(32\)-bit integer type, so you should use at least \(64\)-bit inte...
For each test case, output on a separate line: ""YES"" if \(n\) has an odd divisor, greater than one; ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Input: 6 2 3 4 5 998244353 1099511627776 | Output: NO YES NO YES YES NO
Beginner
2
279
358
246
14
1,616
G
1616G
G. Just Add an Edge
3,500
dfs and similar; dp; graphs
You are given a directed acyclic graph with \(n\) vertices and \(m\) edges. For all edges \(a \to b\) in the graph, \(a < b\) holds.You need to find the number of pairs of vertices \(x\), \(y\), such that \(x > y\) and after adding the edge \(x \to y\) to the graph, it has a Hamiltonian path.
The first line of input contains one integer \(t\) (\(1 \leq t \leq 5\)): the number of test cases.The next lines contains the descriptions of the test cases.In the first line you are given two integers \(n\) and \(m\) (\(1 \leq n \leq 150\,000\), \(0 \leq m \leq \min(150\,000, \frac{n(n-1)}{2})\)): the number of verti...
For each test case, print one integer: the number of pairs of vertices \(x\), \(y\), \(x > y\), such that after adding the edge \(x \to y\) to the graph, it has a Hamiltonian path.
In the first example, any edge \(x \to y\) such that \(x > y\) is valid, because there already is a path \(1 \to 2 \to 3\).In the second example only the edge \(4 \to 1\) is valid. There is a path \(3 \to 4 \to 1 \to 2\) if this edge is added.In the third example you can add edges \(2 \to 1\), \(3 \to 1\), \(4 \to 1\),...
Input: 3 3 2 1 2 2 3 4 3 1 2 3 4 1 4 4 4 1 3 1 4 2 3 3 4 | Output: 3 1 4
Master
3
293
527
180
16
1,346
A
1346A
A. Color Revolution
1,000
*special; math
Hmm, how long has it been since the last color revolution? 5 years?! It's totally the time to make a new one!So the general idea is the following. Division \(1\) should have \(n_1\) participants. Division \(2\) should have \(n_2\) and be exactly \(k\) times bigger than division \(1\) (\(n_2 = k \cdot n_1\)). Division \...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of testcases.Each of the next \(t\) lines contains two integers \(n\) and \(k\) (\(4 \le n \le 10^9\); \(1 \le k \le 500\)) β€” the total number of participants on Codeforces and the size multiplier for the corresponding testcase. In each ...
For each testcase print four integers \(n_1, n_2, n_3\) and \(n_4\) such that \(n_2 = k \cdot n_1\), \(n_3 = k \cdot n_2\), \(n_4 = k \cdot n_3\) and \(n_1 + n_2 + n_3 + n_4 = n\).
Input: 4 40 3 1200 7 320802005 400 4 1 | Output: 1 3 9 27 3 21 147 1029 5 2000 800000 320000000 1 1 1 1
Beginner
2
866
394
180
13
1,080
C
1080C
C. Masha and two friends
1,500
implementation
Recently, Masha was presented with a chessboard with a height of \(n\) and a width of \(m\).The rows on the chessboard are numbered from \(1\) to \(n\) from bottom to top. The columns are numbered from \(1\) to \(m\) from left to right. Therefore, each cell can be specified with the coordinates \((x,y)\), where \(x\) i...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of test cases.Each of them is described in the following format:The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^9\)) β€” the size of the board.The second line contains four integers \(x_1\), \(y_1\), \(x_2\), \(y_2...
Output \(t\) lines, each of which contains two numbers β€” the number of white and black cells after spilling paint, respectively.
Explanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illust...
Input: 5 2 2 1 1 2 2 1 1 2 2 3 4 2 2 3 2 3 1 4 3 1 5 1 1 5 1 3 1 5 1 4 4 1 1 4 2 1 3 4 4 3 4 1 2 4 2 2 1 3 3 | Output: 0 4 3 9 2 3 8 8 4 8
Medium
1
1,607
640
128
10
5
C
5C
C. Longest Regular Bracket Sequence
1,900
constructive algorithms; data structures; dp; greedy; sortings; strings
This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. Y...
The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106.
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing ""0 1"".
Input: )((())))(()()) | Output: 6 2
Hard
6
497
131
185
0
1,091
H
1091H
H. New Year and the Tricolore Recreation
3,200
games
Alice and Bob play a game on a grid with \(n\) rows and infinitely many columns. In each row, there are three tokens, blue, white and red one. Before the game starts and after every move, the following two conditions must hold: Any two tokens are not in the same cell. In each row, the blue token is to the left of the w...
The first line contains a two integers \(n\) and \(f\) (\(1 \leq n \leq 10^5\), \(2 \leq f \leq 2 \cdot 10^5\)) β€” the number of rows and the forbidden move, respectively.Each of the next \(n\) lines contains three integers \(b_i\), \(w_i\), \(r_i\) (\(-10^5 \leq b_i < w_i < r_i \leq 10^5\)) β€” the number of column in wh...
Output two lines. The first line should contain the name of the winner when Alice starts, and the second line should contain the name of the winner when Bob starts.
The first example is as follows:When Alice starts, she can win by moving the blue and white token to right by \(2\) cells, getting into position \(2~5~9\). Regardless of what Bob does, Alice will have one more move and then the game is over. For instance, he can move both the red and white token by \(2\) cells to the l...
Input: 1 6 0 3 9 | Output: Alice Bob
Master
1
1,723
393
164
10
681
C
681C
C. Heap Operations
1,600
constructive algorithms; data structures; greedy
Petya has recently learned data structure named ""Binary heap"".The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap; Thus, at any moment of time the heap contains several intege...
The first line of the input contains the only integer n (1 ≀ n ≀ 100 000) β€” the number of the records left in Petya's journal.Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceedin...
The first line of the output should contain a single integer m β€” the minimum possible number of records in the modified sequence of operations.Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All...
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Input: 2insert 3getMin 4 | Output: 4insert 3removeMininsert 4getMin 4
Medium
3
2,059
350
599
6
1,497
D
1497D
D. Genius
2,500
bitmasks; dp; graphs; number theory
Please note the non-standard memory limit.There are \(n\) problems numbered with integers from \(1\) to \(n\). \(i\)-th problem has the complexity \(c_i = 2^i\), tag \(tag_i\) and score \(s_i\).After solving the problem \(i\) it's allowed to solve problem \(j\) if and only if \(\text{IQ} < |c_i - c_j|\) and \(tag_i \ne...
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 an integer \(n\) \((1 \le n \le 5000)\) β€” the number of problems.The second line of each test case contains \(n\) integers \(tag_1, tag_2, \ldots, tag_n\) \((1 \le tag_i \le n)\) β€” ...
For each test case print a single integer β€” the maximum number of points that can be earned.
In the first test case optimal sequence of solving problems is as follows: \(1 \rightarrow 2\), after that total score is \(5\) and \(\text{IQ} = 2\) \(2 \rightarrow 3\), after that total score is \(10\) and \(\text{IQ} = 4\) \(3 \rightarrow 1\), after that total score is \(20\) and \(\text{IQ} = 6\) \(1 \rightarrow 4\...
Input: 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 | Output: 35 30 42 0 0
Expert
4
634
553
92
14
1,215
B
1215B
B. The Number of Products
1,400
combinatorics; dp; implementation
You are given a sequence \(a_1, a_2, \dots, a_n\) consisting of \(n\) non-zero integers (i.e. \(a_i \ne 0\)). You have to calculate two following values: the number of pairs of indices \((l, r)\) \((l \le r)\) such that \(a_l \cdot a_{l + 1} \dots a_{r - 1} \cdot a_r\) is negative; the number of pairs of indices \((l, ...
The first line contains one integer \(n\) \((1 \le n \le 2 \cdot 10^{5})\) β€” the number of elements in the sequence.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((-10^{9} \le a_i \le 10^{9}; a_i \neq 0)\) β€” the elements of the sequence.
Print two integers β€” the number of subsegments with negative product and the number of subsegments with positive product, respectively.
Input: 5 5 -3 3 -1 1 | Output: 8 7
Easy
3
411
257
135
12
1,228
E
1228E
E. Another Filling the Grid
2,300
combinatorics; dp; math
You have \(n \times n\) square grid and an integer \(k\). Put an integer in each cell while satisfying the conditions below. All numbers in the grid should be between \(1\) and \(k\) inclusive. Minimum number of the \(i\)-th row is \(1\) (\(1 \le i \le n\)). Minimum number of the \(j\)-th column is \(1\) (\(1 \le j \le...
The only line contains two integers \(n\) and \(k\) (\(1 \le n \le 250\), \(1 \le k \le 10^{9}\)).
Print the answer modulo \((10^{9} + 7)\).
In the first example, following \(7\) cases are possible. In the second example, make sure you print the answer modulo \((10^{9} + 7)\).
Input: 2 2 | Output: 7
Expert
3
521
98
41
12
622
D
622D
D. Optimal Number Permutation
1,900
constructive algorithms
You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi β€” the distance between the positions of the number i. Permute the numbers in array a to minimize the value ...
The only line contains integer n (1 ≀ n ≀ 5Β·105).
Print 2n integers β€” the permuted array a that minimizes the value of the sum s.
Input: 2 | Output: 1 1 2 2
Hard
1
332
49
79
6
1,990
F
1990F
F. Polygonal Segments
2,800
brute force; data structures; divide and conquer; dp; greedy; two pointers
You are given an array \(a\) of size \(n\).A segment \([l, r](1 \le l < r \le n)\) is called a polygonal segment only if the following conditions hold: \((r-l+1) \geq 3\); Considering \(a_l, a_{l+1}, \ldots, a_r\) as side lengths, these sides can form a polygon with \((r-l+1)\) sides. Process \(q\) queries of two types...
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.For each test case: The first line of each testcase contains two integers \(n\), \(q\) (\(4 \le n \le 2\cdot 10^5\), \(1 \le q \le 10^5\)); The second line of each testcase contains \(n\) integers \(a_1,a_2,\ldots, a_n\) (\(1 \...
For each query, if there is no suitable segment, output \(-1\) in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line.
In the first query of the first test case, there is no polygonal segment under the given condition. For example, considering segment \([1,3]\), you can not form a triangle with side lengths of \(a_1=3\), \(a_2=1\), and \(a_3=2\).In the second query of the first test case, the longest polygonal segment is \([1,4]\). You...
Input: 25 63 1 2 2 81 1 31 1 41 1 52 1 51 1 41 1 54 10500000000000 500000000000 1000000000000 5000000000001 1 31 2 41 1 42 1 4999999999992 3 9999999999991 1 31 2 41 1 42 3 10000000000001 1 3 | Output: -1 4 4 3 5 -1 -1 4 -1 3 4 -1
Master
6
548
705
175
19
344
B
344B
B. Simple Molecules
1,200
brute force; graphs; math
Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number β€” the number of bonds the atom must form with other atoms. An atom can form one o...
The single line of the input contains three space-separated integers a, b and c (1 ≀ a, b, c ≀ 106) β€” the valence numbers of the given atoms.
If such a molecule can be built, print three space-separated integers β€” the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print ""Impossible"" (without the quotes).
The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with...
Input: 1 1 2 | Output: 0 1 1
Easy
3
645
141
312
3
1,446
E
1446E
E. Long Recovery
3,500
constructive algorithms; dfs and similar
A patient has been infected with an unknown disease. His body can be seen as an infinite grid of triangular cells which looks as follows: Two cells are neighboring if they share a side. Therefore, each cell (\(x\), \(y\)) has exactly three neighbors: (\(x+1\), \(y\)) (\(x-1\), \(y\)) (\(x+1\), \(y-1\)) if \(x\) is even...
The first line contains one integer \(n\) \((1 \leq n \leq 250000)\) β€” the number of infected cells. The \(i\)-th of the next \(n\) lines contains two space-separated integers \(x_i\) and \(y_i\) \((0 \leq x_i, y_i < 500)\), meaning that cell \((x_i, y_i)\) is infected. All cells \((x_i, y_i)\) are distinct, and all ot...
If it is possible that the organism never fully recovers from the disease, print SICK. Otherwise, you should print RECOVERED and in the next line an integer \(k\) β€” the longest possible recovery period, modulo \(998244353\).
For the first testcase, the following drawings describe the longest possible recovery process. It can be proven that there are no recovery periods of length \(5\) or longer, and the organism always recovers in this testcase. \(\hspace{40pt} \downarrow\) \(\hspace{40pt} \downarrow\) \(\hspace{40pt} \downarrow\) \(\hspac...
Input: 4 0 0 1 0 2 0 0 1 | Output: RECOVERED 4
Master
2
1,074
353
224
14
1,767
E
1767E
E. Algebra Flash
2,500
bitmasks; brute force; dp; graphs; math; meet-in-the-middle; trees
Algebra Flash 2.2 has just been released!Changelog: New gamemode! Thank you for the continued support of the game! Huh, is that it? Slightly disappointed, you boot up the game and click on the new gamemode. It says ""Colored platforms"".There are \(n\) platforms, numbered from \(1\) to \(n\), placed one after another. ...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le m \le 40\)) β€” the number of platforms and the number of colors, respectively.The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le m\)) β€” the colors of the platforms.The third line contains \(m\) inte...
Print the smallest amount of coins you can spend to activate some platforms so that you could start on an activated platform \(1\), jump through some activated platforms and reach an activated platform \(n\).
Input: 5 3 1 3 2 3 1 1 10 100 | Output: 11
Expert
7
991
428
208
17
1,297
F
1297F
F. Movie Fan
0
*special; data structures; greedy; implementation; sortings
Recently, Polycarp has been a fan of cinema novelties and is trying not to miss them!In the near future, \(n\) new movies will be released: the \(i\)-th of them will be airing from the day \(a_i\) and to the day \(b_i\). This means that if Polycarp wants to watch the \(i\)-th movie in the cinema, he must do so in the p...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the input. The following are descriptions of the \(t\) test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β€” the number of movies and the maxim...
Print \(t\) answers to given test cases in the order in which they appear in the input: the \(i\)-th answer should consist of two lines. Print the integer \(d\) in the first line of each test case answer: \(d=0\), if there is a schedule such that all movies are watched during airing, \(d>0\), if such a schedule does no...
Input: 3 7 2 1 2 1 3 2 2 2 3 1 1 2 3 1 2 5 3 1 1 1 1 1 1 1 1 1 1 6 1 13 13 31 31 25 25 12 12 14 14 10 10 | Output: 1 1 3 2 3 1 4 2 1 1 1 1 2 2 0 13 31 25 12 14 10
Beginner
5
1,138
690
717
12
1,365
D
1365D
D. Solve The Maze
1,700
constructive algorithms; dfs and similar; dsu; graphs; greedy; implementation; shortest paths
Vivek has encountered a problem. He has a maze that can be represented as an \(n \times m\) grid. Each of the grid cells may represent the following: Empty β€” '.' Wall β€” '#' Good person β€” 'G' Bad person β€” 'B' The only escape from the maze is at cell \((n, m)\).A person can move to a cell only if it shares a side with th...
The first line contains one integer \(t\) \((1 \le t \le 100)\) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) \((1 \le n, m \le 50)\) β€” the number of rows and columns in the maze.Each of the next \(n\) lines contain \(m\) charac...
For each test case, print ""Yes"" if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print ""No""You may print every letter in any case (upper or lower).
For the first and second test cases, all conditions are already satisfied.For the third test case, there is only one empty cell \((2,2)\), and if it is replaced with a wall then the good person at \((1,2)\) will not be able to escape.For the fourth test case, the good person at \((1,1)\) cannot escape.For the fifth tes...
Input: 6 1 1 . 1 2 G. 2 2 #B G. 2 3 G.# B#. 3 3 #B. #.. GG. 2 2 #B B. | Output: Yes Yes No No Yes Yes
Medium
7
840
542
206
13
6
D
6D
D. Lizards and Basements 2
2,600
brute force; dp
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.Polycarp likes to play computer role-playing game Β«Lizards and BasementsΒ». At the moment he is playing it as a magician. At one of the last ...
The first line of the input contains three integers n, a, b (3 ≀ n ≀ 10; 1 ≀ b < a ≀ 10). The second line contains a sequence of n integers β€” h1, h2, ..., hn (1 ≀ hi ≀ 15), where hi is the amount of health points the i-th archer has.
In the first line print t β€” the required minimum amount of fire balls.In the second line print t numbers β€” indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them...
Input: 3 2 12 2 2 | Output: 32 2 2
Expert
2
1,103
233
349
0
1,419
A
1419A
A. Digit Game
900
games; greedy; implementation
Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play \(t\) matches of a digit game...In each of \(t\) matches of the digit game, a positive integer is generated. It consists of \(n\) digits. The digits of this integer are n...
First line of input contains an integer \(t\) \((1 \le t \le 100)\) β€” the number of matches.The first line of each match description contains an integer \(n\) \((1 \le n \le 10^3)\) β€” the number of digits of the generated number.The second line of each match description contains an \(n\)-digit positive integer without ...
For each match print \(1\), if Raze wins, and \(2\), if Breach wins.
In the first match no one can make a turn, the only digit left is \(2\), it's even, so Breach wins.In the second match the only digit left is \(3\), it's odd, so Raze wins.In the third match Raze can mark the last digit, after that Breach can only mark \(0\). \(1\) will be the last digit left, it's odd, so Raze wins.In...
Input: 4 1 2 1 3 3 102 4 2069 | Output: 2 1 1 2
Beginner
3
1,182
334
68
14
1,938
B
1938B
3,000
Master
0
0
0
0
19
687
E
687E
E. TOF
2,900
dfs and similar; graphs
Today Pari gave Arya a cool graph problem. Arya wrote a non-optimal solution for it, because he believes in his ability to optimize non-optimal solutions. In addition to being non-optimal, his code was buggy and he tried a lot to optimize it, so the code also became dirty! He keeps getting Time Limit Exceeds and he is ...
The first line of the input contains two integers n and m (1 ≀ n, m ≀ 5000) β€” the number of vertices and then number of directed edges in the input graph.Each of the next m lines contains a pair of integers ui and vi (1 ≀ ui, vi ≀ n), meaning there is a directed edge in the input graph. You may assume that the graph wo...
Print a single integer β€” the minimum possible number of dfs calls that can be achieved with permuting the edges.
Input: 3 31 22 33 1 | Output: 2998
Master
2
1,177
416
112
6
2,040
E
2040E
E. Control of Randomness
2,100
combinatorics; dfs and similar; dp; graphs; greedy; math; probabilities; trees
You are given a tree with \(n\) vertices.Let's place a robot in some vertex \(v \ne 1\), and suppose we initially have \(p\) coins. Consider the following process, where in the \(i\)-th step (starting from \(i = 1\)): If \(i\) is odd, the robot moves to an adjacent vertex in the direction of vertex \(1\); Else, \(i\) i...
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 two integers \(n\) and \(q\) (\(2 \le n \le 2 \cdot 10^3\); \(1 \le q \le 2 \cdot 10^3\)) β€” the number of vertices in...
For each test case, print \(q\) integers: the values of \(f(v_i, p_i)\) modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal...
The tree in the first test case: In the first query, the expected value is equal to \(1\), since the robot starts moving from vertex \(2\) to vertex \(1\) in the first step and the process stops.Let's calculate the expected value in the second query (\(x\) is the number of steps): \(P(x < 2) = 0\), the distance to vert...
Input: 24 41 22 32 42 03 04 03 112 101 22 32 41 55 66 76 86 98 1010 1110 126 09 010 011 03 17 110 112 112 211 12 | Output: 1 6 6 2 4 9 8 15 2 3 6 9 5 5
Hard
8
1,235
935
454
20
556
B
556B
B. Case of Fake Numbers
1,100
brute force; implementation
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers fro...
The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of gears.The second line contains n digits a1, a2, ..., an (0 ≀ ai ≀ n - 1) β€” the sequence of active teeth: the active tooth of the i-th gear contains number ai.
In a single line print ""Yes"" (without the quotes), if the given Stolp's gears puzzle is real, and ""No"" (without the quotes) otherwise.
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
Input: 31 0 0 | Output: Yes
Easy
2
1,213
223
138
5
2,093
B
2093B
B. Expensive Number
900
greedy; math
The cost of a positive integer \(n\) is defined as the result of dividing the number \(n\) by the sum of its digits.For example, the cost of the number \(104\) is \(\frac{104}{1 + 0 + 4} = 20.8\), and the cost of the number \(111\) is \(\frac{111}{1 + 1 + 1} = 37\).You are given a positive integer \(n\) that does not c...
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The only line of each test case contains a positive integer \(n\) (\(1 \leq n < 10^{100}\)) without leading zeros.
For each test case, output one integer on a new line β€” the number of digits that need to be removed from the number so that its cost becomes minimal.
Input: 4666137001020307 | Output: 2 4 3 0
Beginner
2
944
207
149
20
1,780
A
1780A
A. Hayato and School
800
constructive algorithms; greedy
Today Hayato came home from school with homework.In the assignment, Hayato was given an array \(a\) of length \(n\). The task was to find \(3\) numbers in this array whose sum is odd. At school, he claimed that there are such \(3\) numbers, but Hayato was not sure, so he asked you for help.Answer if there are such thre...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.For each test case, the first line contains one integer \(n\) (\(3 \le n \le 300\)) β€” the length of \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)) β€” the array \(a\).It is gu...
For each test case, in the first line print one word ""YES"" (without quotes) if there are \(3\) numbers with an odd sum or ""NO"" (without quotes) if there are no such \(3\) numbers.If the answer exists, then on the second line print \(3\) distinct integers \(i, j, k\) (\(1 \le i, j, k \le n\)) β€” the indices of the nu...
In the first test case, there is one way to choose \(3\) numbers, and since \(1 + 1 + 1 = 3\), this triple is fine for us.In the second test case, you need to choose the numbers \(1, 2, 2\), since \(1 + 2 + 2 = 5\).In the third test case, there is one way to choose three numbers, but \(1 + 2 + 3 = 6\) is an even number...
Input: 631 1 141 1 2 231 2 351 4 5 1 242 6 2 455 6 3 2 1 | Output: YES 1 2 3 YES 3 4 1 NO YES 1 3 4 NO YES 1 3 5
Beginner
2
511
402
368
17
334
B
334B
B. Eight Point Sets
1,400
sortings
Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three int...
The input consists of eight lines, the i-th line contains two space-separated integers xi and yi (0 ≀ xi, yi ≀ 106). You do not have any other conditions for these points.
In a single line print word ""respectable"", if the given set of points corresponds to Gerald's decency rules, and ""ugly"" otherwise.
Input: 0 00 10 21 01 22 02 12 2 | Output: respectable
Easy
1
576
171
134
3
1,810
G
1810G
G. The Maximum Prefix
3,200
dp
You're going to generate an array \(a\) with a length of at most \(n\), where each \(a_{i}\) equals either \(1\) or \(-1\).You generate this array in the following way. First, you choose some integer \(k\) (\(1\le k \le n\)), which decides the length of \(a\). Then, for each \(i\) (\(1\le i \le k\)), you set \(a_{i} = ...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β€” the number of test cases. Their description follows.The first line contains an integer \(n\) (\(1\le n \le 5000\)).Then for the following \(n\) lines, each line contains two integers \(x_{i}\) and \(y_{i}\) (...
For each test case, output \(n\) integers in one single line, the \(i\)-th of which denotes the expected score for an array of length \(i\), modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers...
In the first test case, if we choose \(k=1\), there are \(2\) possible arrays with equal probabilities: \([1]\) and \([-1]\). The \(S\) values for them are \(1\) and \(0\). So the expected score is \(\frac{1}{2}h_{0} + \frac{1}{2}h_{1} = \frac{3}{2}\). If we choose \(k=2\), there are \(4\) possible arrays with equal pr...
Input: 421 21 21 2 331 31 45 51 1 1 132 54 60 24 3 2 155 65 71 61 34 79 0 4 5 2 4 | Output: 500000005 750000007 1 1 1 200000005 333333339 333333339 500000005 880952391 801587311 781746041 789304620
Master
1
906
624
513
18
1,951
I
1951I
I. Growing Trees
3,200
binary search; constructive algorithms; flows; graphs; greedy
wowaka ft. Hatsune Miku - Ura-Omote Lovers࢞You are given an undirected connected simple graph with \(n\) nodes and \(m\) edges, where edge \(i\) connects node \(u_i\) and \(v_i\), with two positive parameters \(a_i\) and \(b_i\) attached to it. Additionally, you are also given an integer \(k\).A non-negative array \(x\...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 500\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n \le 50, n - 1 \le m \le \min(50, \frac{n(n - 1)}{2}), 1...
For each test case, output a single integer: the minimum cost of a \(k\)-spanning-tree generator.
In the first test case, a valid \(1\)-spanning-tree generator is \(x = [1, 1, 1, 1, 0]\), as indicated by the following figure. The cost of this generator is \((1^2 \cdot 5 + 1 \cdot 5) + (1^2 \cdot 5 + 1 \cdot 7) + (1^2 \cdot 6 + 1 \cdot 2) + (1^2 \cdot 3 + 1 \cdot 5) + (0^2 \cdot 4 + 0 \cdot 9) = 38\). It can be prov...
Input: 45 5 14 3 5 52 1 5 72 4 6 25 3 3 52 5 2 95 5 34 3 5 52 1 5 72 4 6 25 3 3 52 5 2 92 1 100000001 2 1000 100010 15 107 1 7 65 8 6 64 8 2 24 3 10 910 8 3 44 6 6 15 4 1 39 3 4 38 3 9 97 5 10 32 1 3 46 1 6 42 5 7 310 7 2 18 2 6 8 | Output: 38 191 100000010000000000 2722
Master
5
986
863
97
19
518
A
518A
A. Vitaly and Strings
1,600
constructive algorithms; strings
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time. During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English let...
The first line contains string s (1 ≀ |s| ≀ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.The second line contains string t (|t| = |s|), consisting of lowercase English letters.It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically...
If the string that meets the given requirements doesn't exist, print a single string ""No such string"" (without the quotes).If such string exists, print it. If there are multiple valid strings, you may print any of them.
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
Input: ac | Output: b
Medium
2
690
340
221
5
1,673
A
1673A
A. Subtle Substring Subtraction
800
games; greedy; strings
Alice and Bob are playing a game with strings. There will be \(t\) rounds in the game. In each round, there will be a string \(s\) consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is all...
The first line of input contains a single integer \(t\) (\(1\leq t\leq 5\cdot 10^4\)) denoting the number of rounds.Each of the next \(t\) lines contain a single string \(s\) (\(1\leq |s|\leq 2\cdot 10^5\)) consisting of lowercase English letters, denoting the string used for the round. Here \(|s|\) denotes the length ...
For each round, print a single line containing a string and an integer. If Alice wins the round, the string must be ""Alice"". If Bob wins the round, the string must be ""Bob"". The integer must be the difference between their scores assuming both players play optimally.
For the first round, \(\texttt{""aba""}\xrightarrow{\texttt{Alice}}\texttt{""}{\color{red}{\texttt{ab}}}\texttt{a""}\xrightarrow{} \texttt{""a""}\xrightarrow{\texttt{Bob}}\texttt{""}{\color{red}{\texttt{a}}}\texttt{""}\xrightarrow{}\texttt{""""}\). Alice's total score is \(1+2=3\). Bob's total score is \(1\).For the se...
Input: 5abaabccbancodeforces | Output: Alice 2 Alice 4 Alice 4 Bob 14 Alice 93
Beginner
3
1,234
429
271
16
2,060
G
2060G
G. Bugged Sort
2,400
dp; greedy; sortings
Today, Alice has given Bob arrays for him to sort in increasing order again! At this point, no one really knows how many times she has done this.Bob is given two sequences \(a\) and \(b\), both of length \(n\). All integers in the range from \(1\) to \(2n\) appear exactly once in either \(a\) or \(b\). In other words, ...
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\) (\(3 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(a_1,a_2,\ldots,a_n...
If it is possible to sort both sequences simultaneously, print ""YES"" on a new line. Otherwise, print ""NO"" on a new line.You can output the answer in any case. For example, the strings ""yEs"", ""yes"", and ""Yes"" will also be recognized as positive responses.
In the first test case, it can be shown that it is impossible.In the second test case, Bob can perform one operation with indices \(i=1\) and \(j=2\). The arrays become \([3,4,5]\) and \([1,2,6]\) respectively. Both arrays are now sorted.
Input: 532 1 34 6 532 1 54 3 641 6 4 35 2 8 745 3 7 18 6 4 275 1 9 12 3 13 72 4 11 14 6 10 8 | Output: NO YES NO YES YES
Expert
3
1,069
630
264
20
375
C
375C
C. Circling Round Treasures
2,600
bitmasks; shortest paths
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, ent...
The first line contains two integers n and m (1 ≀ n, m ≀ 20) β€” the sizes of the table. Next n lines each contains m characters β€” the description of the table. The description means the following: character ""B"" is a cell with a bomb; character ""S"" is the starting cell, you can assume that it's empty; digit c (1-8) i...
Print a single integer β€” the maximum possible profit you can get.
In the first example the answer will look as follows. In the second example the answer will look as follows. In the third example you cannot get profit.In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell.
Input: 4 4.....S1.........10 | Output: 2
Expert
2
1,696
810
65
3
231
B
231B
B. Magic, Wizardry and Wonders
1,500
constructive algorithms; greedy
Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into...
The single line contains three space-separated integers: n (2 ≀ n ≀ 100) β€” the initial number of cards on the table, d (|d| ≀ 104) β€” the number on the card that was left on the table after all the magical actions, and l (1 ≀ l ≀ 100) β€” the limits for the initial integers.
If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from ...
Input: 3 3 2 | Output: 2 1 2
Medium
2
1,646
272
408
2
33
A
33A
A. What is for dinner?
1,200
greedy; implementation
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are ""relaxing""....
The first line contains three integers n, m, k (1 ≀ m ≀ n ≀ 1000, 0 ≀ k ≀ 106) β€” total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≀ r ≀ m) β€” index of the row, where belongs the corresponding tooth, and c...
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
Input: 4 3 182 31 23 62 3 | Output: 11
Easy
2
1,776
425
92
0
1,811
G1
1811G1
G1. Vlad and the Nice Paths (easy version)
2,100
combinatorics; dp; math
This is an easy version of the problem, it differs from the hard one only by constraints on \(n\) and \(k\).Vlad found a row of \(n\) tiles and the integer \(k\). The tiles are indexed from left to right and the \(i\)-th tile has the color \(c_i\). After a little thought, he decided what to do with it.You can start fro...
The first line of each test contains the integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)) β€” the number of tiles in a row and the length of the block.The second line of each test case contains \(...
Print \(t\) numbers, each of which is the answer to the corresponding test case β€” the number of nice paths of maximum length modulo \(10^9 + 7\).
In the first sample, it is impossible to make a nice path with a length greater than \(0\).In the second sample, we are interested in the following paths: \(1 \rightarrow 3 \rightarrow 4 \rightarrow 5\) \(2 \rightarrow 4 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \ri...
Input: 55 21 2 3 4 57 21 3 1 3 3 1 311 41 1 1 1 1 1 1 1 1 1 15 21 1 2 2 25 11 2 3 4 5 | Output: 1 4 165 3 1
Hard
3
833
493
145
18
1,366
C
1366C
C. Palindromic Paths
1,500
greedy; math
You are given a matrix with \(n\) rows (numbered from \(1\) to \(n\)) and \(m\) columns (numbered from \(1\) to \(m\)). A number \(a_{i, j}\) is written in the cell belonging to the \(i\)-th row and the \(j\)-th column, each number is either \(0\) or \(1\).A chip is initially in the cell \((1, 1)\), and it will be move...
The first line contains one integer \(t\) (\(1 \le t \le 200\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n, m \le 30\)) β€” the dimensions of the matrix.Then \(n\) lines follow, the \(i\)-th line contains \(m\) integers \(a_{i, 1}\), \(a_{i, 2}\), ..., \(a...
For each test case, print one integer β€” the minimum number of cells you have to change so that every path in the matrix is palindromic.
The resulting matrices in the first three test cases: \(\begin{pmatrix} 1 & 1\\ 0 & 1 \end{pmatrix}\) \(\begin{pmatrix} 0 & 0 & 0\\ 0 & 0 & 0 \end{pmatrix}\) \(\begin{pmatrix} 1 & 0 & 1 & 1 & 1 & 1 & 1\\ 0 & 1 & 1 & 0 & 1 & 1 & 0\\ 1 & 1 & 1 & 1 & 1 & 0 & 1 \end{pmatrix}\)
Input: 4 2 2 1 1 0 1 2 3 1 1 0 1 0 0 3 7 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1 3 5 1 0 1 0 0 1 1 1 1 0 0 0 1 0 0 | Output: 0 3 4 4
Medium
2
937
357
135
13
1,248
B
1248B
B. Grow The Tree
900
greedy; math; sortings
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.The tree looks like a polyline on the plane, consisting of ...
The first line contains an integer \(n\) (\(1 \le n \le 100\,000\)) β€” the number of sticks Alexey got as a present.The second line contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10\,000\)) β€” the lengths of the sticks.
Print one integer β€” the square of the largest possible distance from \((0, 0)\) to the tree end.
The following pictures show optimal trees for example tests. The squared distance in the first example equals \(5 \cdot 5 + 1 \cdot 1 = 26\), and in the second example \(4 \cdot 4 + 2 \cdot 2 = 20\).
Input: 3 1 2 3 | Output: 26
Beginner
3
1,064
232
96
12
1,275
C
1275C
C. #define Π—Π°Π΄Π°Ρ‡Π° B ...
0
*special
Компания Β«Π’ΠšΠΎΠ½Ρ‚Π°ΠΊΡ‚Π΅Β» Π°ΠΊΡ‚ΠΈΠ²Π½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ языки C/C++ для Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ Π΄Π²ΠΈΠΆΠΊΠΎΠ². Π‘Ρ‚Π°Ρ€ΡˆΠΈΠΉ Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Вася β€” большой Π»ΡŽΠ±ΠΈΡ‚Π΅Π»ΡŒ языка C, вСдь Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π² Π½Π΅ΠΌ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ Ρ€Π°ΡΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΏΠΎΡ‚Π΅Π½Ρ†ΠΈΠ°Π» Define-Oriented Programming. Π’ языкС C Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Π° #define позволяСт ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΡ€ΡΠΌΡƒΡŽ подстановку куска ΠΊΠΎΠ΄Π° Π² любоС мСсто ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹. НапримСр...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число \(n\) (\(1 \le n \le 1\,000\)) β€” количСство элСмСнтов массива, значСния ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… ΠΈΠ½Ρ‚Π΅Ρ€Π΅ΡΡƒΡŽΡ‚ Π’Π°ΡΡŽ.Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ \(n\) строк содСрТат \(n\) Ρ†Π΅Π»Ρ‹Ρ… чисСл \(pos_i\) (\(0 \le pos_i \le 1\,125\,899\,906\,842\,623\)) β€” ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ, для ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π½ΡƒΠΆΠ½ΠΎ ΡƒΠ·Π½Π°Ρ‚ΡŒ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² массивС.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ \(n\) строк. Π’ \(i\)-ΠΉ строкС Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒΡΡ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ элСмСнта массива Π½Π° ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ \(pos_i\).
Начало этого массива выглядит ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ:const long long values[1125899906842624] = { 0, 1, 3, 4, 1, 2, 4, ... };
Input: 5 0 1 3 5 8 | Output: 0 1 4 2 3
Beginner
1
1,264
297
105
12
1,310
A
1310A
A. Recommendations
1,700
data structures; greedy; sortings
VK news recommendation system daily selects interesting publications of one of \(n\) disjoint categories for each user. Each publication belongs to exactly one category. For each category \(i\) batch algorithm selects \(a_i\) publications.The latest A/B test suggests that users are reading recommended publications more...
The first line of input consists of single integer \(n\) β€” the number of news categories (\(1 \le n \le 200\,000\)).The second line of input consists of \(n\) integers \(a_i\) β€” the number of publications of \(i\)-th category selected by the batch algorithm (\(1 \le a_i \le 10^9\)).The third line of input consists of \...
Print one integer β€” the minimal required time for the targeted algorithm to get rid of categories with the same size.
In the first example, it is possible to find three publications of the second type, which will take 6 seconds.In the second example, all news categories contain a different number of publications.
Input: 5 3 7 9 7 8 5 2 5 7 5 | Output: 6
Medium
3
755
452
117
13
430
A
430A
A. Points and Segments (easy)
1,600
constructive algorithms; sortings
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.Iahub wants to draw n distinct points and m segment...
The first line of input contains two integers: n (1 ≀ n ≀ 100) and m (1 ≀ m ≀ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≀ xi ≀ 100) β€” the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≀ li ≀ ri ≀...
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue).If there are multiple good drawings you can output any of them.
Input: 3 33 7 141 56 1011 15 | Output: 0 0 0
Medium
2
828
409
273
4
71
D
71D
D. Solitaire
2,200
brute force; implementation
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them.Vasya lays out nm cards as a rectangle n Γ— m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are...
The first line contains integers n and m (3 ≀ n, m ≀ 17, n Γ— m ≀ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as ""J1"" and ""J2"" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one β€” for the suit. The possible ranks a...
If the Solitaire can be solved, print on the first line ""Solution exists."" without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: ""There are no jokers."", if there are no jokers in the input data. ""Replace Jx with y."", if there is one joker. x is its numbe...
The pretests cover all the possible output formats.
Input: 4 62S 3S 4S 7S 8S AS5H 6H 7H 5S TC AC8H 9H TH 7C 8C 9C2D 2C 3C 4C 5C 6C | Output: No solution.
Hard
2
934
501
1,116
0
431
E
431E
E. Chemistry Experiment
2,200
binary search; data structures; ternary search
One day two students, Grisha and Diana, found themselves in the university chemistry lab. In the lab the students found n test tubes with mercury numbered from 1 to n and decided to conduct an experiment.The experiment consists of q steps. On each step, one of the following actions occurs: Diana pours all the contents ...
The first line contains two integers n and q (1 ≀ n, q ≀ 105) β€” the number of tubes ans the number of experiment steps. The next line contains n space-separated integers: h1, h2, ..., hn (0 ≀ hi ≀ 109), where hi is the volume of mercury in the Ρ–-th tube at the beginning of the experiment.The next q lines contain the ga...
For each action of the second type print the calculated value. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Input: 3 31 2 02 21 2 12 3 | Output: 1.500001.66667
Hard
3
949
668
157
4
512
E
512E
E. Fox And Polygon
2,900
constructive algorithms; divide and conquer
Fox Ciel just designed a puzzle game called ""Polygon""! It is played using triangulations of a regular n-edge polygon. The goal is to transform one triangulation to another by some tricky rules.Triangulation of an n-edge poylgon is a set of n - 3 diagonals satisfying the condition that no two diagonals share a common ...
The first line contain an integer n (4 ≀ n ≀ 1000), number of edges of the regular polygon. Then follows two groups of (n - 3) lines describing the original triangulation and goal triangulation.Description of each triangulation consists of (n - 3) lines. Each line contains 2 integers ai and bi (1 ≀ ai, bi ≀ n), describ...
First, output an integer k (0 ≀ k ≀ 20, 000): number of steps.Then output k lines, each containing 2 integers ai and bi: the endpoints of a diagonal you are going to flip at step i. You may output ai and bi in any order.If there are several possible solutions, output any of them.
Sample test 2 is discussed above and shown on the picture.
Input: 41 32 4 | Output: 11 3
Master
2
1,273
502
280
5
1,002
B3
1002B3
B3. Distinguish four 2-qubit states
1,600
*special
You are given 2 qubits which are guaranteed to be in one of the four orthogonal states: Your task is to perform necessary operations and measurements to figure out which state it was and to return the index of that state (0 for , 1 for etc.). The state of the qubits after the operations does not matter.You have to impl...
Medium
1
607
0
0
10
1,686
A
1686A
A. Everything Everywhere All But One
800
greedy
You are given an array of \(n\) integers \(a_1, a_2, \ldots, a_n\). After you watched the amazing film ""Everything Everywhere All At Once"", you came up with the following operation.In one operation, you choose \(n-1\) elements of the array and replace each of them with their arithmetic mean (which doesn't have to be ...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 200\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 50\)) β€” the number of integers.The second line of each test case contains \(n\) integers...
For each test case, if it is possible to make all elements equal after some number of operations, output \(\texttt{YES}\). Otherwise, output \(\texttt{NO}\).You can output \(\texttt{YES}\) and \(\texttt{NO}\) in any case (for example, strings \(\texttt{yEs}\), \(\texttt{yes}\), \(\texttt{Yes}\) will be recognized as a ...
In the first test case, all elements are already equal.In the second test case, you can choose all elements except the third, their average is \(\frac{1 + 2 + 4 + 5}{4} = 3\), so the array will become \([3, 3, 3, 3, 3]\).It's possible to show that it's impossible to make all elements equal in the third and fourth test ...
Input: 4342 42 4251 2 3 4 544 3 2 1324 2 22 | Output: YES YES NO NO
Beginner
1
675
371
339
16
1,676
D
1676D
D. X-Sum
1,000
brute force; greedy; implementation
Timur's grandfather gifted him a chessboard to practice his chess skills. This chessboard is a grid \(a\) with \(n\) rows and \(m\) columns with each cell having a non-negative integer written on it. Timur's challenge is to place a bishop on the board such that the sum of all cells attacked by the bishop is maximal. Th...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains the integers \(n\) and \(m\) (\(1 \le n \le 200\), \(1 \leq m \leq 200\)).The following \(n\) lines contain \(m\) integers each, ...
For each test case output a single integer, the maximum sum over all possible placements of the bishop.
For the first test case here the best sum is achieved by the bishop being in this position:
Input: 44 41 2 2 12 4 2 42 2 3 12 4 2 42 1103 31 1 11 1 11 1 13 30 1 11 0 11 1 0 | Output: 20 1 5 3
Beginner
3
553
561
103
16
276
D
276D
D. Little Girl and Maximum XOR
1,700
bitmasks; dp; greedy; implementation; math
A little girl loves problems on bitwise operations very much. Here's one of them.You are given two integers l and r. Let's consider the values of for all pairs of integers a and b (l ≀ a ≀ b ≀ r). Your task is to find the maximum value among all considered ones.Expression means applying bitwise excluding or operation t...
The single line contains space-separated integers l and r (1 ≀ l ≀ r ≀ 1018).Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
In a single line print a single integer β€” the maximum value of for all pairs of integers a, b (l ≀ a ≀ b ≀ r).
Input: 1 2 | Output: 3
Medium
5
494
224
110
2
46
E
46E
E. Comb
1,900
data structures; dp
Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table n Γ— m panels in size with integers written on the panels. There also was a huge number of stones lying by the w...
The first line contains a pair of integers n, m (2 ≀ n, m ≀ 1500). Next n lines contain m integers each β€” that is the table itself. The absolute value of the numbers in the table does not exceed 10000.
Print the single number β€” the maximum number of coins Lara can get.
Input: 2 2-1 21 3 | Output: 2
Hard
2
1,365
201
67
0
83
C
83C
C. Track
2,400
graphs; greedy; shortest paths
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest.The track's map is represented by a rectangle n Γ— m in size divide...
The first input line contains three integers n, m and k (1 ≀ n, m ≀ 50, nΒ·m β‰₯ 2, 1 ≀ k ≀ 4). Then n lines contain the map. Each line has the length of exactly m characters and consists of lowercase Latin letters and characters S and T. It is guaranteed that the map contains exactly one character S and exactly one chara...
If there is a path that satisfies the condition, print it as a sequence of letters β€” the plot types. Otherwise, print ""-1"" (without quotes). You shouldn't print the character S in the beginning and T in the end.Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print...
Input: 5 3 2SbacccaaccccabT | Output: bcccc
Expert
3
1,463
383
374
0
2,095
A
2095A
A. Piecing It Together
0
*special; string suffix structures
Beginner
2
0
0
0
20
773
C
773C
C. Prairie Partition
2,200
binary search; constructive algorithms; greedy; math
It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k β‰₯ 0, 0 < r ≀ 2k. Let's call that representation prairie partition of x.For example, the prairie partitions of 12, 17, 7 and 1 are: 12 = 1 + 2 + 4 + 5,17 = 1 + 2 + 4 + 8 + 2,7 = 1 + ...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of numbers given from Alice to Borys.The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1012; a1 ≀ a2 ≀ ... ≀ an) β€” the numbers given from Alice to Borys.
Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.If there are ...
In the first example, Alice could get the input sequence from [6, 20] as the original sequence.In the second example, Alice's original sequence could be either [4, 5] or [3, 3, 3].
Input: 81 1 2 2 3 4 5 8 | Output: 2
Hard
4
673
235
368
7
1,776
G
1776G
G. Another Wine Tasting Event
2,100
combinatorics; constructive algorithms; math; strings
After the first successful edition, Gabriella has been asked to organize a second wine tasting event. There will be \(2n - 1\) bottles of wine arranged in a row, each of which is either red wine or white wine.This time, Gabriella has already chosen the type and order of all the bottles. The types of the wines are repre...
The first line contains the integer \(n\) (\(1 \le n \le 10^6\)) β€” where \(2n - 1\) is the number of bottles, and \(n\) is the number of critics.The second line contains a string \(s\) of length \(2n - 1\) that represents the arrangement of the wines β€” the \(i\)-th character of \(s\) (\(1 \le i \le 2n - 1\)) is \(\text...
Print an integer \(x\) β€” the number of white wines that each critic will taste.It can be proved that at least one solution exists. If multiple solutions exist, any of them will be accepted.
In the first sample, there are \(5\) critics and \(2 \cdot 5 - 1 = 9\) bottles of wine. A possible set of intervals that makes each critic taste \(2\) white wines is the following: \([2, 6],\) \([1, 6],\) \([4, 8],\) \([1, 5],\) \([3, 7]\). Note that all intervals contain at least \(5\) bottles.In the second sample, th...
Input: 5 RWWRRRWWW | Output: 2
Hard
4
1,674
379
189
17
490
B
490B
B. Queue
1,500
dsu; implementation
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of t...
The first line contains integer n (2 ≀ n ≀ 2Β·105) β€” the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≀ ai, bi ≀ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the ar...
Print a sequence of n integers x1, x2, ..., xn β€” the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
The picture illustrates the queue for the first sample.
Input: 492 310 731 07 141 | Output: 92 7 31 141
Medium
2
885
563
169
4
388
B
388B
B. Fox and Minimal path
1,900
bitmasks; constructive algorithms; graphs; implementation; math
Fox Ciel wants to write a task for a programming contest. The task is: ""You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2.""Same with some writers, she wants to make an example with some certain output...
The first line contains a single integer k (1 ≀ k ≀ 109).
You should output a graph G with n vertexes (2 ≀ n ≀ 1000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph.The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph ...
In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2.
Input: 2 | Output: 4NNYYNNYYYYNNYYNN
Hard
5
448
57
665
3
1,912
A
1912A
A. Accumulator Apex
1,900
data structures; implementation; sortings
Allyn is playing a new strategy game called ""Accumulator Apex"". In this game, Allyn is given the initial value of an integer \(x\), referred to as the accumulator, and \(k\) lists of integers. Allyn can make multiple turns. On each turn, Allyn can withdraw the leftmost element from any non-empty list and add it to th...
The first line of the input contains two integers \(x\) and \(k\) (\(0 \leq x \leq 10^9, 1 \leq k \leq 10^5\)) β€” the initial value of the accumulator \(x\) and the number of lists. The next \(k\) lines contain the description of lists: an integer \(l_i\) (\(l_i \ge 1\)) followed on the same line by \(l_i\) elements of ...
The sole line of the output should contain the largest value of the accumulator \(x\) Allyn can get.
In the first input, we start with \(x = 1\). Then, we can take the first integer from the first list and get \(x = 0\) β€” adding the next integer \(2\) from the first list we get \(x = 2\). After that, we can add the integers from the second list and obtain \(x = 3\). Finally, we can add the integers from the third list...
Input: 1 32 -1 22 -2 32 -3 4 | Output: 4
Hard
3
603
489
100
19
1,638
F
1638F
F. Two Posters
3,200
brute force; data structures; greedy; two pointers
You want to advertise your new business, so you are going to place two posters on a billboard in the city center. The billboard consists of \(n\) vertical panels of width \(1\) and varying integer heights, held together by a horizontal bar. The \(i\)-th of the \(n\) panels has height \(h_i\). Initially, all panels hang...
The first line of input contains one integer \(n\) (\(1 \le n \le 10^4\)) β€” the number of vertical panels.The second line of input contains \(n\) integers \(h_1, h_2, ..., h_n\) (\(1 \le h_i \le 10^{12}\)) β€” the heights of the \(n\) vertical panels.
Print a single integer β€” the maximum total area the two posters can cover together.
In the first sample test, we can choose an upper poster with area \(12\) and a lower poster of area \(6\) as in the image below. In the second sample test, we can cover the whole billboard using a single poster.
Input: 6 2 2 3 5 4 5 | Output: 18
Master
4
938
249
83
16
513
G2
513G2
G2. Inversions problem
2,400
dp; probabilities
You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≀ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.
The first line of input contains two integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 109). The next line contains n integers p1, p2, ..., pn β€” the given permutation. All pi are different and in range from 1 to n.The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some...
Output the answer with absolute or relative error no more than 1e - 9.
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two element...
Input: 3 11 2 3 | Output: 0.833333333333333
Expert
2
316
655
70
5
498
C
498C
C. Array and Operations
2,100
flows; graph matchings; number theory
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≀ ik < jk ≀ n.In one operation you can perform a sequence of actions: take one...
The first line contains two space-separated integers n, m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100).The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ 109) β€” the description of the array.The following m lines contain the description of good pairs. The k-th line contains two space-separated integers ...
Output the answer for the problem.
Input: 3 28 3 81 22 3 | Output: 0
Hard
3
648
425
34
4
1,842
I
1842I
I. Tenzing and Necklace
3,500
divide and conquer; dp; greedy
bright, sunny and innocent......Tenzing has a beautiful necklace. The necklace consists of \(n\) pearls numbered from \(1\) to \(n\) with a string connecting pearls \(i\) and \((i \text{ mod } n)+1\) for all \(1 \leq i \leq n\).One day Tenzing wants to cut the necklace into several parts by cutting some strings. But fo...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2\leq n\leq 5\cdot 10^5\), \(1\leq k <n\)).The second line of...
For each test case, output the minimum total time in minutes required.
In the first test case, the necklace will be cut into \(3\) parts: \([1,2][3,4][5]\), so the total time is \(3\).In the second test case, the necklace will be cut into \(3\) parts: \([5,1][2][3,4]\), Tenzing will cut the strings connecting \((1,2), (2,3)\) and \((4,5)\), so the total time is \(a_1+a_2+a_4=7\).
Input: 45 21 1 1 1 15 21 2 3 4 56 34 2 5 1 3 310 32 5 6 5 2 1 7 9 7 2 | Output: 3 7 5 15
Master
3
704
498
70
18
937
A
937A
A. Olympiad
800
implementation; sortings
The recent All-Berland Olympiad in Informatics featured n participants with each scoring a certain amount of points.As the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria: At least one participant should get a diploma. No...
The first line contains a single integer n (1 ≀ n ≀ 100) β€” the number of participants.The next line contains a sequence of n integers a1, a2, ..., an (0 ≀ ai ≀ 600) β€” participants' scores.It's guaranteed that at least one participant has non-zero score.
Print a single integer β€” the desired number of ways.
There are three ways to choose a subset in sample case one. Only participants with 3 points will get diplomas. Participants with 2 or 3 points will get diplomas. Everyone will get a diploma! The only option in sample case two is to award everyone.Note that in sample case three participants with zero scores cannot get a...
Input: 41 3 3 2 | Output: 3
Beginner
2
573
253
52
9
887
B
887B
B. Cubes for Masha
1,300
brute force; implementation
Absent-minded Masha got set of n cubes for her birthday.At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.To make a number Masha can rotate her cubes and put them in a row. After t...
In first line integer n is given (1 ≀ n ≀ 3) β€” the number of cubes, Masha got for her birthday.Each of next n lines contains 6 integers aij (0 ≀ aij ≀ 9) β€” number on j-th face of i-th cube.
Print single integer β€” maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.
In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.
Input: 30 1 2 3 4 56 7 8 9 0 12 3 4 5 6 7 | Output: 87
Easy
2
581
189
133
8
1,996
G
1996G
G. Penacony
2,200
brute force; data structures; graphs; greedy; hashing
On Penacony, The Land of the Dreams, there exists \(n\) houses and \(n\) roads. There exists a road between house \(i\) and \(i+1\) for all \(1 \leq i \leq n-1\) and a road between house \(n\) and house \(1\). All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt ...
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) – the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5\)) – the number of houses and the number of friendships.The next \(m\) lines contain two integers \(a\) and \...
For each test case, output an integer, the minimum number of roads that must be maintained.
For the first test case, the following roads must be maintained: \(8 \leftarrow \rightarrow 1\) \(7 \leftarrow \rightarrow 8\) \(1 \leftarrow \rightarrow 2\) \(4 \leftarrow \rightarrow 5\)
Input: 78 31 82 74 513 41 132 123 114 1010 22 33 410 43 85 102 104 104 11 35 23 51 45 22 51 3 | Output: 4 7 2 7 2 3 3
Hard
5
662
568
91
19
1,555
E
1555E
E. Boring Segments
2,100
data structures; sortings; trees; two pointers
You are given \(n\) segments on a number line, numbered from \(1\) to \(n\). The \(i\)-th segments covers all integer points from \(l_i\) to \(r_i\) and has a value \(w_i\).You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two intege...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5\); \(2 \le m \le 10^6\)) β€” the number of segments and the number of integer points.Each of the next \(n\) lines contains three integers \(l_i\), \(r_i\) and \(w_i\) (\(1 \le l_i < r_i \le m\); \(1 \le w_i \le 10^6\)) β€” the description of t...
Print a single integer β€” the minimum cost of a good subset.
Input: 5 12 1 5 5 3 4 10 4 10 6 11 12 5 10 12 3 | Output: 3
Hard
4
694
392
59
15
1,102
C
1102C
C. Doors Breaking and Repairing
1,200
games
You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.There are \(n\) doors, the \(i\)-th door initially has durability equal to \(a_i\).During your move you can t...
The first line of the input contains three integers \(n\), \(x\) and \(y\) (\(1 \le n \le 100\), \(1 \le x, y \le 10^5\)) β€” the number of doors, value \(x\) and value \(y\), respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)), where \(a_i\) is the initial ...
Print one integer β€” the number of doors with durability equal to \(0\) at the end of the game, if you and Slavik both play optimally.
Clarifications about the optimal strategy will be ignored.
Input: 6 3 2 2 3 1 3 4 2 | Output: 6
Easy
1
1,093
352
133
11
846
E
846E
E. Chemistry in Berland
2,300
dfs and similar; greedy; trees
Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.Fortunately, chemical laws allow material transformations (yes, chemistry in ...
The first line contains one integer number n (1 ≀ n ≀ 105) β€” the number of materials discovered by Berland chemists.The second line contains n integer numbers b1, b2... bn (1 ≀ bi ≀ 1012) β€” supplies of BerSU laboratory.The third line contains n integer numbers a1, a2... an (1 ≀ ai ≀ 1012) β€” the amounts required for the...
Print YES if it is possible to conduct an experiment. Otherwise print NO.
Input: 31 2 33 2 11 11 1 | Output: YES
Expert
3
1,208
494
73
8
1,704
H2
1704H2
H2. Game of AI (hard version)
3,500
combinatorics; fft; math
This is the hard version of this problem. The difference between easy and hard versions is the constraint on \(k\) and the time limit. Notice that you need to calculate the answer for all positive integers \(n \in [1,k]\) in this version. You can make hacks only if both versions of the problem are solved.Cirno is playi...
The only line contains two positive integers \(k\) and \(M\) (\(1\le k\le 10^5\), \(2\le M\le 10^9\) ). It is guaranteed that \(2^{18}\) is a divisor of \(M-1\) and \(M\) is a prime number.
Output \(k\) lines, where the \(i\)-th line contains a non-negative integer, which is the answer for \(n=i\) modulo \(M\).
For \(n=1\), no valid sequence \(a\) exists. We regard the answer as \(0\).For \(n=2\), there is only one possible array \(a\): \([2, 1]\). For array \(a\) is \([2, 1]\) and permutation \(p\) is \([1, 2]\), the sequence \(b\) will be \([1, 1]\) after all rounds have finished. The details for each rounds: In the first r...
Input: 8 998244353 | Output: 0 2 24 360 6800 153150 4057452 123391016
Master
3
2,036
189
122
17
2,002
D2
2002D2
D2. DFS Checker (Hard Version)
2,300
binary search; data structures; dfs and similar; graphs; hashing; trees
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on \(n\) and \(q\) are higher. You can make hacks only if both versions of the problem are solved.You are given a rooted tree consisting of \(n\) vertices. The vertices are numbered from \(1\) to \(n\), and the ro...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(q\) (\(2\le n\le 3\cdot 10^5\), \(2\le q\le 10^5\)) β€” the number of vertices in the tree and the ...
For each test case, print \(q\) lines corresponding to the \(q\) queries. For each query, output \(\texttt{YES}\) if there is a DFS order that exactly equals the current permutation, and output \(\texttt{NO}\) otherwise.You can output \(\texttt{Yes}\) and \(\texttt{No}\) in any case (for example, strings \(\texttt{yEs}...
In the first test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,3,2],[1,2,3],[3,2,1]\), respectively. The first two permutations are valid DFS orders; the third is not a DFS order.In the second test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,2,5,4,3...
Input: 33 31 11 2 32 33 21 37 41 1 2 2 3 31 2 3 4 5 6 73 52 53 74 65 41 1 3 42 3 4 5 15 14 53 42 3 | Output: YES YES NO YES NO NO YES YES NO NO YES
Expert
6
992
876
424
20