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,567 |
F
|
1567F
|
F. One-Four Overload
| 2,700 |
2-sat; constructive algorithms; dfs and similar; dsu; graphs; implementation
|
Alice has an empty grid with \(n\) rows and \(m\) columns. Some of the cells are marked, and no marked cells are adjacent to the edge of the grid. (Two squares are adjacent if they share a side.) Alice wants to fill each cell with a number such that the following statements are true: every unmarked cell contains either the number \(1\) or \(4\); every marked cell contains the sum of the numbers in all unmarked cells adjacent to it (if a marked cell is not adjacent to any unmarked cell, this sum is \(0\)); every marked cell contains a multiple of \(5\). Alice couldn't figure it out, so she asks Bob to help her. Help Bob find any such grid, or state that no such grid exists.
|
The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 500\)) β the number of rows and the number of columns in the grid, respectively.Then \(n\) lines follow, each containing \(m\) characters. Each of these characters is either '.' or 'X' β an unmarked and a marked cell, respectively. No marked cells are adjacent to the edge of the grid.
|
Output ""'NO"" if no suitable grid exists. Otherwise, output ""'YES""'. Then output \(n\) lines of \(m\) space-separated integers β the integers in the grid.
|
It can be shown that no such grid exists for the second test.
|
Input: 5 5 ..... .XXX. .X.X. .XXX. ..... | Output: YES 4 1 4 4 1 4 5 5 5 1 4 5 1 5 4 1 5 5 5 4 1 4 4 1 4
|
Master
| 6 | 681 | 367 | 157 | 15 |
1,930 |
D1
|
1930D1
|
D1. Sum over all Substrings (Easy Version)
| 1,800 |
brute force; dp; greedy; strings
|
This is the easy version of the problem. The only difference between the two versions is the constraint on \(t\) and \(n\). You can make hacks only if both versions of the problem are solved.For a binary\(^\dagger\) pattern \(p\) and a binary string \(q\), both of length \(m\), \(q\) is called \(p\)-good if for every \(i\) (\(1 \leq i \leq m\)), there exist indices \(l\) and \(r\) such that: \(1 \leq l \leq i \leq r \leq m\), and \(p_i\) is a mode\(^\ddagger\) of the string \(q_l q_{l+1} \ldots q_{r}\). For a pattern \(p\), let \(f(p)\) be the minimum possible number of \(\mathtt{1}\)s in a \(p\)-good binary string (of the same length as the pattern).You are given a binary string \(s\) of size \(n\). Find $$$\(\sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j).\)\( In other words, you need to sum the values of \)f\( over all \)\frac{n(n+1)}{2}\( substrings of \)s\(.\)^\dagger\( A binary pattern is a string that only consists of characters \)\mathtt{0}\( and \)\mathtt{1}\(.\)^\ddagger\( Character \)c\( is a mode of string \)t\( of length \)m\( if the number of occurrences of \)c\( in \)t\( is at least \)\lceil \frac{m}{2} \rceil\(. For example, \)\mathtt{0}\( is a mode of \)\mathtt{010}\(, \)\mathtt{1}\( is not a mode of \)\mathtt{010}\(, and both \)\mathtt{0}\( and \)\mathtt{1}\( are modes of \)\mathtt{011010}$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \(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 a single integer \(n\) (\(1 \le n \le 100\)) β the length of the binary string \(s\).The second line of each test case contains a binary string \(s\) of length \(n\) consisting of only characters \(\mathtt{0}\) and \(\mathtt{1}\).It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(10^4\).
|
For each test case, output the sum of values of \(f\) over all substrings of \(s\).
|
In the first test case, the only \(\mathtt{1}\)-good string is \(\mathtt{1}\). Thus, \(f(\mathtt{1})=1\).In the second test case, \(f(\mathtt{10})=1\) because \(\mathtt{01}\) is \(\mathtt{10}\)-good, and \(\mathtt{00}\) is not \(\mathtt{10}\)-good. Thus, the answer is \(f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2\).In the third test case, \(f\) equals to \(0\) for all \(1 \leq i \leq j \leq 5\). Thus, the answer is \(0\).
|
Input: 4112105000002011110110000000111111 | Output: 1 2 0 346
|
Medium
| 4 | 1,340 | 545 | 83 | 19 |
1,967 |
B2
|
1967B2
|
B2. Reverse Card (Hard Version)
| 2,200 |
brute force; math; number theory
|
The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.You are given two positive integers \(n\), \(m\).Calculate the number of ordered pairs \((a, b)\) satisfying the following conditions: \(1\le a\le n\), \(1\le b\le m\); \(b \cdot \gcd(a,b)\) is a multiple of \(a+b\).
|
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 two integers \(n\), \(m\) (\(1\le n,m\le 2 \cdot 10^6\)).It is guaranteed that neither the sum of \(n\) nor the sum of \(m\) over all test cases exceeds \(2 \cdot 10^6\).
|
For each test case, print a single integer: the number of valid pairs.
|
In the first test case, no pair satisfies the conditions.In the fourth test case, \((2,2),(3,6),(4,4),(6,3),(6,6),(8,8)\) satisfy the conditions.
|
Input: 61 12 33 510 8100 12331000000 1145141 | Output: 0 1 1 6 423 5933961
|
Hard
| 3 | 345 | 371 | 70 | 19 |
1,042 |
B
|
1042B
|
B. Vitamins
| 1,200 |
bitmasks; brute force; dp; implementation
|
Berland shop sells \(n\) kinds of juices. Each juice has its price \(c_i\). Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin ""A"", vitamin ""B"" and vitamin ""C"". Each juice can contain one, two or all three types of vitamins in it.Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.
|
The first line contains a single integer \(n\) \((1 \le n \le 1\,000)\) β the number of juices.Each of the next \(n\) lines contains an integer \(c_i\) \((1 \le c_i \le 100\,000)\) and a string \(s_i\) β the price of the \(i\)-th juice and the vitamins it contains. String \(s_i\) contains from \(1\) to \(3\) characters, and the only possible characters are ""A"", ""B"" and ""C"". It is guaranteed that each letter appears no more than once in each string \(s_i\). The order of letters in strings \(s_i\) is arbitrary.
|
Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.
|
In the first example Petya buys the first, the second and the fourth juice. He spends \(5 + 6 + 4 = 15\) and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is \(16\), which isn't optimal.In the second example Petya can't obtain all three vitamins, as no juice contains vitamin ""C"".
|
Input: 45 C6 B16 BAC4 A | Output: 15
|
Easy
| 4 | 526 | 520 | 159 | 10 |
1,360 |
D
|
1360D
|
D. Buying Shovels
| 1,300 |
math; number theory
|
Polycarp wants to buy exactly \(n\) shovels. The shop sells packages with shovels. The store has \(k\) types of packages: the package of the \(i\)-th type consists of exactly \(i\) shovels (\(1 \le i \le k\)). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly \(n\) shovels?For example, if \(n=8\) and \(k=7\), then Polycarp will buy \(2\) packages of \(4\) shovels.Help Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly \(n\) shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from \(1\) to \(k\), inclusive.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then, \(t\) test cases follow, one per line.Each test case consists of two positive integers \(n\) (\(1 \le n \le 10^9\)) and \(k\) (\(1 \le k \le 10^9\)) β the number of shovels and the number of types of packages.
|
Print \(t\) answers to the test cases. Each answer is a positive integer β the minimum number of packages.
|
The answer to the first test case was explained in the statement.In the second test case, there is only one way to buy \(8\) shovels β \(8\) packages of one shovel.In the third test case, you need to buy a \(1\) package of \(6\) shovels.
|
Input: 5 8 7 8 1 6 10 999999733 999999732 999999733 999999733 | Output: 2 8 1 999999733 1
|
Easy
| 2 | 829 | 319 | 106 | 13 |
2,084 |
B
|
2084B
|
B. MIN = GCD
| 1,100 |
greedy; math; number theory
|
You are given a positive integer sequence \(a\) of length \(n\). Determine if it is possible to rearrange \(a\) such that there exists an integer \(i\) (\(1 \le i<n\)) satisfying $$$\( \min([a_1,a_2,\ldots,a_i])=\gcd([a_{i+1},a_{i+2},\ldots,a_n]). \)\(Here \)\gcd(c)\( denotes the greatest common divisor of \)c\(, which is the maximum positive integer that divides all integers in \)c$$$.
|
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\) (\(2 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^{18}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output ""Yes"" if it is possible, and ""No"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, rearrange \(a\) to \([1,1]\) and let \(i=1\), then \(\min([1])=\gcd([1])\).In the second test case, it can be shown that it is impossible.In the third test case, rearrange \(a\) to \([3,2,2]\) and let \(i=2\), then \(\min([3,2])=\gcd([2])\).In the fifth test case, rearrange \(a\) to \([3,4,5,6,9]\) and let \(i=3\), then \(\min([3,4,5])=\gcd([6,9])\).
|
Input: 721 121 232 2 332 3 454 5 6 9 33998244359987710471 99824435698771045 100000000761 1 4 5 1 4 | Output: Yes No Yes No Yes Yes Yes
|
Easy
| 3 | 389 | 428 | 236 | 20 |
638 |
D
|
638D
|
D. Three-dimensional Turtle Super Computer
| 1,800 |
brute force; dfs and similar; graphs
|
A super computer has been built in the Turtle Academy of Sciences. The computer consists of nΒ·mΒ·k CPUs. The architecture was the paralellepiped of size n Γ m Γ k, split into 1 Γ 1 Γ 1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to n, the line number from 1 to m and the column number from 1 to k.In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (x, y, z) can send messages to CPUs (x + 1, y, z), (x, y + 1, z) and (x, y, z + 1) (of course, if they exist), there is no feedback, that is, CPUs (x + 1, y, z), (x, y + 1, z) and (x, y, z + 1) cannot send messages to CPU (x, y, z).Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (a, b, c) controls CPU (d, e, f) , if there is a chain of CPUs (xi, yi, zi), such that (x1 = a, y1 = b, z1 = c), (xp = d, yp = e, zp = f) (here and below p is the length of the chain) and the CPU in the chain with number i (i < p) can send messages to CPU i + 1.Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (x, y, z) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (x, y, z) CPUs: (a, b, c) and (d, e, f), such that (a, b, c) controls (d, e, f) before (x, y, z) is turned off and stopped controlling it after the turning off.
|
The first line contains three integers n, m and k (1 β€ n, m, k β€ 100) β the dimensions of the Super Computer. Then n blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to n. Each block consists of m lines, k characters in each β the description of a layer in the format of an m Γ k table. Thus, the state of the CPU (x, y, z) is corresponded to the z-th character of the y-th line of the block number x. Character ""1"" corresponds to a working CPU and character ""0"" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line.
|
Print a single integer β the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control.
|
In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2, 1, 2) turns off, it disrupts the control by CPU (2, 1, 3) over CPU (2, 1, 1), and when CPU (2, 2, 2) is turned off, it disrupts the control over CPU (2, 2, 3) by CPU (2, 2, 1).In the second sample all processors except for the corner ones are critical.In the third sample there is not a single processor controlling another processor, so the answer is 0.
|
Input: 2 2 3000000111111 | Output: 2
|
Medium
| 3 | 1,630 | 646 | 125 | 6 |
171 |
D
|
171D
|
D. Broken checker
| 1,300 |
*special; brute force
|
""This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!"" β people complained in the comments to one round on Codeforces. And even more... No, wait, the checker for the problem was alright, that's a mercy.
|
The only line of the input contains an integer between 1 and 5, inclusive. All tests for this problem are different. The contents of the test case doesn't need to be equal to its index.
|
The only line of the output contains an integer between 1 and 3, inclusive.
|
This problem has no samples, since there so few test cases.
|
Easy
| 2 | 285 | 185 | 75 | 1 |
|
534 |
B
|
534B
|
B. Covered Path
| 1,400 |
dp; greedy; math
|
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
|
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.It is guaranteed that there is a way to complete the segment so that: the speed in the first second equals v1, the speed in the last second equals v2, the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
|
Print the maximum possible length of the path segment in meters.
|
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
|
Input: 5 64 2 | Output: 26
|
Easy
| 3 | 569 | 614 | 64 | 5 |
1,490 |
C
|
1490C
|
C. Sum of Cubes
| 1,100 |
binary search; brute force; brute force; math
|
You are given a positive integer \(x\). Check whether the number \(x\) is representable as the sum of the cubes of two positive integers.Formally, you need to check if there are two integers \(a\) and \(b\) (\(1 \le a, b\)) such that \(a^3+b^3=x\).For example, if \(x = 35\), then the numbers \(a=2\) and \(b=3\) are suitable (\(2^3+3^3=8+27=35\)). If \(x=4\), then no pair of numbers \(a\) and \(b\) is suitable.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.Each test case contains one integer \(x\) (\(1 \le x \le 10^{12}\)).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 integer type in your programming language.
|
For each test case, output on a separate line: ""YES"" if \(x\) is representable as the sum of the cubes of two positive integers. ""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).
|
The number \(1\) is not representable as the sum of two cubes.The number \(2\) is represented as \(1^3+1^3\).The number \(4\) is not representable as the sum of two cubes.The number \(34\) is not representable as the sum of two cubes.The number \(35\) is represented as \(2^3+3^3\).The number \(16\) is represented as \(2^3+2^3\).The number \(703657519796\) is represented as \(5779^3+7993^3\).
|
Input: 7 1 2 4 34 35 16 703657519796 | Output: NO YES NO NO YES YES YES
|
Easy
| 4 | 413 | 357 | 275 | 14 |
1,252 |
L
|
1252L
|
L. Road Construction
| 2,300 |
flows; graphs
|
There are \(N\) cities in the country of Numbata, numbered from \(1\) to \(N\). Currently, there is no road connecting them. Therefore, each of these \(N\) cities proposes a road candidate to be constructed.City \(i\) likes to connect with city \(A_i\), so city \(i\) proposes to add a direct bidirectional road connecting city \(i\) and city \(A_i\). It is guaranteed that no two cities like to connect with each other. In other words, there is no pair of integers \(i\) and \(j\) where \(A_i = j\) and \(A_j = i\). It is also guaranteed that any pair of cities are connected by a sequence of road proposals. In other words, if all proposed roads are constructed, then any pair of cities are connected by a sequence of constructed road.City \(i\) also prefers the road to be constructed using a specific material. Each material can be represented by an integer (for example, \(0\) for asphalt, \(1\) for wood, etc.). The material that can be used for the road connecting city \(i\) and city \(A_i\) is represented by an array \(B_i\) containing \(M_i\) integers: \([(B_i)_1, (B_i)_2, \dots, (B_i)_{M_i}]\). This means that the road connecting city \(i\) and city \(A_i\) can be constructed with either of the material in \(B_i\).There are \(K\) workers to construct the roads. Each worker is only familiar with one material, thus can only construct a road with a specific material. In particular, the \(i^{th}\) worker can only construct a road with material \(C_i\). Each worker can only construct at most one road. You want to assign each worker to construct a road such that any pair of cities are connected by a sequence of constructed road.
|
Input begins with a line containing two integers: \(N\) \(K\) (\(3 \le N \le 2000\); \(1 \le K \le 2000\)) representing the number of cities and the number of workers, respectively. The next \(N\) lines each contains several integers: \(A_i\) \(M_i\) \((B_i)_1\), \((B_i)_2\), \(\cdots\), \((B_i)_{M_i}\) (\(1 \le A_i \le N\); \(A_i \ne i\); \(1 \le M_i \le 10\,000\); \(0 \le (B_i)_1 < (B_i)_2 < \dots < (B_i)_{M_i} \le 10^9\)) representing the bidirectional road that city \(i\) likes to construct. It is guaranteed that the sum of \(M_i\) does not exceed \(10\,000\). It is also guaranteed that no two cities like to connect with each other and any pair of cities are connected by a sequence of road proposals. The next line contains \(K\) integers: \(C_i\) (\(0 \le C_i \le 10^9\)) representing the material that is familiarized by the workers.
|
If it is not possible to assign each worker to construct a road such that any pair of cities are connected by a sequence of constructed road, simply output -1 in a line. Otherwise, for each worker in the same order as input, output in a line two integers (separated by a single space): \(u\) and \(v\) in any order. This means that the worker constructs a direct bidirectional road connecting city \(u\) and \(v\). If the worker does not construct any road, output ""0 0"" (without quotes) instead. Each pair of cities can only be assigned to at most one worker. You may output any assignment as long as any pair of cities are connected by a sequence of constructed road.
|
Explanation for the sample input/output #1We can assign the workers to construct the following roads: The first worker constructs a road connecting city \(1\) and city \(2\). The second worker constructs a road connecting city \(2\) and city \(3\). The third worker constructs a road connecting city \(3\) and city \(4\). The fourth worker does not construct any road. The fifth worker constructs a road connecting city \(4\) and city \(2\). Therefore, any pair of cities are now connected by a sequence of constructed road.Explanation for the sample input/output #2There is no worker that can construct a road connecting city \(1\), thus city \(1\) is certainly isolated.
|
Input: 4 5 2 2 1 2 3 2 2 3 4 2 3 4 2 2 4 5 1 2 3 4 5 | Output: 1 2 2 3 3 4 0 0 4 2
|
Expert
| 2 | 1,646 | 848 | 671 | 12 |
359 |
B
|
359B
|
B. Permutation
| 1,400 |
constructive algorithms; dp; math
|
A permutation p is an ordered group of numbers p1, p2, ..., pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1, p2, ..., pn.Simon has a positive integer n and a non-negative integer k, such that 2k β€ n. Help him find permutation a of length 2n, such that it meets this equation: .
|
The first line contains two integers n and k (1 β€ n β€ 50000, 0 β€ 2k β€ n).
|
Print 2n integers a1, a2, ..., a2n β the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.
|
Record |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0.In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.
|
Input: 1 0 | Output: 1 2
|
Easy
| 3 | 355 | 73 | 167 | 3 |
884 |
A
|
884A
|
A. Book Reading
| 800 |
implementation
|
Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.Help Luba to determine the minimum number of day when she finishes reading.It is guaranteed that the answer doesn't exceed n.Remember that there are 86400 seconds in a day.
|
The first line contains two integers n and t (1 β€ n β€ 100, 1 β€ t β€ 106) β the number of days and the time required to read the book.The second line contains n integers ai (0 β€ ai β€ 86400) β the time Luba has to spend on her work during i-th day.
|
Print the minimum day Luba can finish reading the book.It is guaranteed that answer doesn't exceed n.
|
Input: 2 286400 86398 | Output: 2
|
Beginner
| 1 | 505 | 245 | 101 | 8 |
|
766 |
D
|
766D
|
D. Mahmoud and a Dictionary
| 2,000 |
data structures; dfs and similar; dp; dsu; graphs
|
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words.He know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on.Sometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time.After Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations.After adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help.
|
The first line of input contains three integers n, m and q (2 β€ n β€ 105, 1 β€ m, q β€ 105) where n is the number of words in the dictionary, m is the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.The second line contains n distinct words a1, a2, ..., an consisting of small English letters with length not exceeding 20, which are the words in the dictionary.Then m lines follow, each of them contains an integer t (1 β€ t β€ 2) followed by two different words xi and yi which has appeared in the dictionary words. If t = 1, that means xi has a synonymy relation with yi, otherwise xi has an antonymy relation with yi.Then q lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered.All words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different.
|
First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print ""NO"" (without quotes) and ignore it, otherwise print ""YES"" (without quotes).After that print q lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3.See the samples for better understanding.
|
Input: 3 3 4hate love like1 love like2 love hate1 hate likelove likelove hatelike hatehate like | Output: YESYESNO1222
|
Hard
| 5 | 1,619 | 1,066 | 458 | 7 |
|
477 |
D
|
477D
|
D. Dreamoon and Binary
| 2,700 |
dp; strings
|
Dreamoon saw a large integer x written on the ground and wants to print its binary form out. Dreamoon has accomplished the part of turning x into its binary format. Now he is going to print it in the following manner.He has an integer n = 0 and can only perform the following two operations in any order for unlimited times each: Print n in binary form without leading zeros, each print will append to the right of previous prints. Increase n by 1. Let's define an ideal sequence as a sequence of operations that can successfully print binary representation of x without leading zeros and ends with a print operation (i.e. operation 1). Dreamoon wants to know how many different ideal sequences are there and the length (in operations) of the shortest ideal sequence.The answers might be large so please print them modulo 1000000007 (109 + 7).Let's define the string representation of an ideal sequence as a string of '1' and '2' where the i-th character in the string matches the i-th operation performed. Two ideal sequences are called different if their string representations are different.
|
The single line of the input contains a binary integer representing x (1 β€ x < 25000) without leading zeros.
|
The first line of the output should contain an integer representing the number of different ideal sequences modulo 1000000007 (109 + 7).The second line of the output contains an integer representing the minimal length of an ideal sequence modulo 1000000007 (109 + 7).
|
For the first sample, the shortest and the only ideal sequence is Β«222221Β» of length 6.For the second sample, there are three ideal sequences Β«21211Β», Β«212222222221Β», Β«222222222222222222222222221Β». Among them the shortest one has length 5.
|
Input: 101 | Output: 16
|
Master
| 2 | 1,094 | 108 | 267 | 4 |
1,873 |
D
|
1873D
|
D. 1D Eraser
| 800 |
greedy; implementation; two pointers
|
You are given a strip of paper \(s\) that is \(n\) cells long. Each cell is either black or white. In an operation you can take any \(k\) consecutive cells and make them all white.Find the minimum number of operations needed to remove all black cells.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)) β the length of the paper and the integer used in the operation.The second line of each test case contains a string \(s\) of length \(n\) consisting of characters \(\texttt{B}\) (representing a black cell) or \(\texttt{W}\) (representing a white cell).The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations needed to remove all black cells.
|
In the first test case you can perform the following operations: $$$\(\color{red}{\texttt{WBW}}\texttt{WWB} \to \texttt{WWW}\color{red}{\texttt{WWB}} \to \texttt{WWWWWW}\)\(In the second test case you can perform the following operations: \)\(\texttt{WW}\color{red}{\texttt{BWB}}\texttt{WW} \to \texttt{WWWWWWW}\)\(In the third test case you can perform the following operations: \)\(\texttt{B}\color{red}{\texttt{WBWB}} \to \color{red}{\texttt{BWWW}}\texttt{W} \to \texttt{WWWWW}\)$$$
|
Input: 86 3WBWWWB7 3WWBWBWW5 4BWBWB5 5BBBBB8 2BWBWBBBB10 2WBBWBBWBBW4 1BBBB3 2WWW | Output: 2 1 2 1 4 3 4 0
|
Beginner
| 3 | 251 | 532 | 112 | 18 |
1,213 |
C
|
1213C
|
C. Book Reading
| 1,200 |
math
|
Polycarp is reading a book consisting of \(n\) pages numbered from \(1\) to \(n\). Every time he finishes the page with the number divisible by \(m\), he writes down the last digit of this page number. For example, if \(n=15\) and \(m=5\), pages divisible by \(m\) are \(5, 10, 15\). Their last digits are \(5, 0, 5\) correspondingly, their sum is \(10\).Your task is to calculate the sum of all digits Polycarp has written down.You have to answer \(q\) independent queries.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β the number of queries.The following \(q\) lines contain queries, one per line. Each query is given as two integers \(n\) and \(m\) (\(1 \le n, m \le 10^{16}\)) β the number of pages in the book and required divisor, respectively.
|
For each query print the answer for it β the sum of digits written down by Polycarp.
|
Input: 7 1 1 10 1 100 3 1024 14 998244353 1337 123 144 1234312817382646 13 | Output: 1 45 153 294 3359835 0 427262129093995
|
Easy
| 1 | 474 | 309 | 84 | 12 |
|
1,976 |
D
|
1976D
|
D. Invertible Bracket Sequences
| 2,000 |
binary search; combinatorics; data structures; divide and conquer; implementation; two pointers
|
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)""); bracket sequences "")("", ""("" and "")"" are not. Let's define the inverse of the bracket sequence as follows: replace all brackets '(' with ')', and vice versa (all brackets ')' with '('). For example, strings ""()(("" and "")())"" are inverses of each other.You are given a regular bracket sequence \(s\). Calculate the number of pairs of integers \((l,r)\) (\(1 \le l \le r \le |s|\)) such that if you replace the substring of \(s\) from the \(l\)-th character to the \(r\)-th character (inclusive) with its inverse, \(s\) will still be a regular bracket sequence.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.Additional constraint on the input: the total length of the regular bracket sequences over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the number of pairs \((l,r)\) meeting the conditions from the statement.
|
In the first example, there is only one pair: \((2, 3)\): (()) \(\rightarrow\) ()(). In the second example, there are no pairs.In the third example, there are three pairs: \((2, 3)\): ()()() \(\rightarrow\) (())(); \((4, 5)\): ()()() \(\rightarrow\) ()(()); \((2, 5)\): ()()() \(\rightarrow\) (()());
|
Input: 4(())()()()()(()())(()) | Output: 1 0 3 13
|
Hard
| 6 | 892 | 360 | 117 | 19 |
1,983 |
F
|
1983F
|
F. array-value
| 2,500 |
binary search; bitmasks; data structures; greedy; two pointers
|
You have an array of non-negative integers \(a_1, a_2, \ldots, a_n\).The value of a sub-array of length \(\ge 2\), \(a[l, r] = [a_l, a_{l+1}, \ldots, a_r]\) is the minimum value of \(a_i \oplus a_j\) such that \(l \le i < j \le r\), where \(\oplus\) is the xor (exclusive-or) operator.You have to find the \(k\)-th smallest value over all sub-arrays of length \(\ge 2\).
|
The first line of the input contains multiple test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)).The first line of each test case contains integer numbers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le \frac{n\cdot(n-1)}{2}\)).The second line of the input contains \(n\) non-negative integer numbers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the array itself.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
Print the \(k\)-th smallest value obtained over all subarrays of length at least \(2\).
|
In the first testcase, we have subarrays with their smallest exclusive-or pair as:\([1,2]: 3\)\([2,3]: 1\)\([3,4]: 7\)\([4,5]: 1\)\([1,2,3]: 1\)\([2,3,4]: 1\)\([3,4,5]: 1\)\([1,2,3,4]: 1\)\([2,3,4,5]: 1\)\([1,2,3,4,5]: 1\)The sorted order would be: \(1, 1, 1, 1, 1, 1, 1, 1, 3, 7\). Therefore, the second smallest element would be \(1\).
|
Input: 45 21 2 3 4 52 14 34 61 2 4 85 91 2 3 4 5 | Output: 1 7 12 3
|
Expert
| 5 | 370 | 457 | 87 | 19 |
2,084 |
G1
|
2084G1
|
G1. Wish Upon a Satellite (Easy Version)
| 2,600 |
dp; games
|
This is the easy version of the problem. The difference between the versions is that in this version, \(t \le 1000\), \(n \le 5000\) and the sum of \(n\) does not exceed \(5000\). You can hack only if you solved all versions of this problem. For a non-empty sequence \(c\) of length \(k\), define \(f(c)\) as follows: Turtle and Piggy are playing a game on a sequence. They are given the sequence \(c_1, c_2, \ldots, c_k\), and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). The game goes as follows: Let the current length of the sequence be \(m\). If \(m = 1\), the game ends. If the game does not end and it's Turtle's turn, then Turtle must choose an integer \(i\) such that \(1 \le i \le m - 1\), set \(c_i\) to \(\min(c_i, c_{i + 1})\), and remove \(c_{i + 1}\). If the game does not end and it's Piggy's turn, then Piggy must choose an integer \(i\) such that \(1 \le i \le m - 1\), set \(c_i\) to \(\max(c_i, c_{i + 1})\), and remove \(c_{i + 1}\). Turtle wants to maximize the value of \(c_1\) in the end, while Piggy wants to minimize the value of \(c_1\) in the end. \(f(c)\) is the value of \(c_1\) in the end if both players play optimally. For a permutation \(p\) of length \(n\)\(^{\text{β}}\), Turtle defines the beauty of the permutation as \(\sum\limits_{i = 1}^n \sum\limits_{j = i}^n f([p_i, p_{i + 1}, \ldots, p_j])\) (i.e., the sum of \(f(c)\) where \(c\) is a non-empty subsegment\(^{\text{β }}\) of \(p\)).Piggy gives Turtle a permutation \(a\) of length \(n\) where some elements are missing and represented by \(0\).Turtle asks you to determine a permutation \(b\) of length \(n\) such that: \(b\) can be formed by filling in the missing elements of \(a\) (i.e., for all \(1 \le i \le n\), if \(a_i \ne 0\), then \(b_i = a_i\)). The beauty of the permutation \(b\) is maximized. For convenience, you only need to find the maximum beauty of such permutation \(b\).\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array). \(^{\text{β }}\)A sequence \(a\) is a subsegment of a sequence \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)). It is guaranteed that the elements of \(a\) that are not \(0\) are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, output a single integer β the maximum beauty of the permutation \(b\).
|
In the first test case, the permutation \(b\) with the maximum beauty is \([1, 2]\). The beauty of \([1, 2]\) is \(4\) since \(f([1]) + f([2]) + f([1, 2]) = 1 + 2 + 1 = 4\). If \(c = [1, 2]\), then \(f(c) = 1\) since Turtle can only choose \(i = 1\) and he will set \(c_1\) to \(\min(c_1, c_2) = 1\).In the second test case, one of the permutations \(b\) with the maximum beauty is \([3, 2, 1]\). The beauty of \([3, 2, 1]\) is \(12\) since \(f([3]) + f([2]) + f([1]) + f([3, 2]) + f([2, 1]) + f([3, 2, 1]) = 3 + 2 + 1 + 2 + 1 + 3 = 12\).In the third test case, one of the permutations \(b\) with the maximum beauty is \([2, 1, 3]\).In the fourth test case, if \(c = [3, 2, 4, 5, 1]\), then \(f(c) = 3\). One of the possible game processes is as follows: Turtle can choose \(i = 3\). Then he will set \(c_3\) to \(\min(c_3, c_4) = 4\) and remove \(c_4\). The sequence \(c\) will become \([3, 2, 4, 1]\). Piggy can choose \(i = 1\). Then he will set \(c_1\) to \(\max(c_1, c_2) = 3\) and remove \(c_2\). The sequence \(c\) will become \([3, 4, 1]\). Turtle can choose \(i = 2\). Then he will set \(c_2\) to \(\min(c_2, c_3) = 1\) and remove \(c_3\). The sequence \(c\) will become \([3, 1]\). Piggy can choose \(i = 1\). Then he will set \(c_1\) to \(\max(c_1, c_2) = 3\) and remove \(c_2\). The sequence \(c\) will become \([3]\). The length of the sequence becomes \(1\), so the game will end. The value of \(c_1\) will be \(3\) in the end. In the fifth test case, one of the permutations \(b\) with the maximum beauty is \([1, 3, 2, 5, 6, 4, 7]\).
|
Input: 821 030 0 030 1 053 2 4 5 170 3 2 5 0 0 0101 2 6 5 8 9 0 0 0 050 4 1 0 050 1 5 2 3 | Output: 4 12 11 44 110 300 45 40
|
Expert
| 2 | 2,563 | 517 | 90 | 20 |
1,575 |
I
|
1575I
|
I. Illusions of the Desert
| 2,300 |
data structures; trees
|
Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions.The map of the labyrinth forms a tree with \(n\) rooms numbered from \(1\) to \(n\) and \(n - 1\) tunnels connecting them such that it is possible to travel between each pair of rooms through several tunnels.The \(i\)-th room (\(1 \leq i \leq n\)) has \(a_i\) illusion rate. To go from the \(x\)-th room to the \(y\)-th room, there must exist a tunnel between \(x\) and \(y\), and it takes \(\max(|a_x + a_y|, |a_x - a_y|)\) energy. \(|z|\) denotes the absolute value of \(z\).To prevent grave robbers, the maze can change the illusion rate of any room in it. Chanek and Indiana would ask \(q\) queries.There are two types of queries to be done: \(1\ u\ c\) β The illusion rate of the \(x\)-th room is changed to \(c\) (\(1 \leq u \leq n\), \(0 \leq |c| \leq 10^9\)). \(2\ u\ v\) β Chanek and Indiana ask you the minimum sum of energy needed to take the secret treasure at room \(v\) if they are initially at room \(u\) (\(1 \leq u, v \leq n\)). Help them, so you can get a portion of the treasure!
|
The first line contains two integers \(n\) and \(q\) (\(2 \leq n \leq 10^5\), \(1 \leq q \leq 10^5\)) β the number of rooms in the maze and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq |a_i| \leq 10^9\)) β inital illusion rate of each room.The \(i\)-th of the next \(n-1\) lines contains two integers \(s_i\) and \(t_i\) (\(1 \leq s_i, t_i \leq n\)), meaning there is a tunnel connecting \(s_i\)-th room and \(t_i\)-th room. The given edges form a tree.The next \(q\) lines contain the query as described. The given queries are valid.
|
For each type \(2\) query, output a line containing an integer β the minimum sum of energy needed for Chanek and Indiana to take the secret treasure.
|
In the first query, their movement from the \(1\)-st to the \(2\)-nd room is as follows. \(1 \rightarrow 5\) β takes \(\max(|10 + 4|, |10 - 4|) = 14\) energy. \(5 \rightarrow 6\) β takes \(\max(|4 + (-6)|, |4 - (-6)|) = 10\) energy. \(6 \rightarrow 2\) β takes \(\max(|-6 + (-9)|, |-6 - (-9)|) = 15\) energy. In total, it takes \(39\) energy.In the second query, the illusion rate of the \(1\)-st room changes from \(10\) to \(-3\).In the third query, their movement from the \(1\)-st to the \(2\)-nd room is as follows. \(1 \rightarrow 5\) β takes \(\max(|-3 + 4|, |-3 - 4|) = 7\) energy. \(5 \rightarrow 6\) β takes \(\max(|4 + (-6)|, |4 - (-6)|) = 10\) energy. \(6 \rightarrow 2\) β takes \(\max(|-6 + (-9)|, |-6 - (-9)|) = 15\) energy. Now, it takes \(32\) energy.
|
Input: 6 4 10 -9 2 -1 4 -6 1 5 5 4 5 6 6 2 6 3 2 1 2 1 1 -3 2 1 2 2 3 3 | Output: 39 32 0
|
Expert
| 2 | 1,142 | 587 | 149 | 15 |
1,666 |
F
|
1666F
|
F. Fancy Stack
| 2,200 |
combinatorics; dp; implementation
|
Little Fiona has a collection of \(n\) blocks of various sizes \(a_1, a_2, \ldots, a_n\), where \(n\) is even. Some of the blocks can be equal in size. She would like to put all these blocks one onto another to form a fancy stack.Let \(b_1, b_2, \ldots, b_n\) be the sizes of blocks in the stack from top to bottom. Since Fiona is using all her blocks, \(b_1, b_2, \ldots, b_n\) must be a permutation of \(a_1, a_2, \ldots, a_n\). Fiona thinks the stack is fancy if both of the following conditions are satisfied: The second block is strictly bigger than the first one, and then each block is alternately strictly smaller or strictly bigger than the previous one. Formally, \(b_1 < b_2 > b_3 < b_4 > \ldots > b_{n-1} < b_n\). The sizes of the blocks on even positions are strictly increasing. Formally, \(b_2 < b_4 < b_6 < \ldots < b_n\) (remember that \(n\) is even). Two stacks are considered different if their corresponding sequences \(b_1, b_2, \ldots, b_n\) differ in at least one position.Fiona wants to know how many different fancy stacks she can build with all of her blocks. Since large numbers scare Fiona, find this number modulo \(998\,244\,353\).
|
Each input contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2500\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) β the number of blocks at Fiona's disposal (\(2 \le n \le 5000\); \(n\) is even). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) β the sizes of the blocks in non-decreasing order (\(1 \le a_1 \le a_2 \le \dotsb \le a_n \le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, print the number of ways to build a fancy stack, modulo \(998\,244\,353\).
|
Input: 241 2 3 481 1 2 3 4 4 6 7 | Output: 2 4
|
Hard
| 3 | 1,161 | 554 | 94 | 16 |
|
214 |
B
|
214B
|
B. Hometask
| 1,600 |
brute force; constructive algorithms; greedy; math
|
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set.
|
A single line contains a single integer n (1 β€ n β€ 100000) β the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
|
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
|
In the first sample there is only one number you can make β 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
|
Input: 10 | Output: 0
|
Medium
| 4 | 584 | 172 | 106 | 2 |
1,582 |
B
|
1582B
|
B. Luntik and Subsequences
| 900 |
combinatorics; math
|
Luntik came out for a morning stroll and found an array \(a\) of length \(n\). He calculated the sum \(s\) of the elements of the array (\(s= \sum_{i=1}^{n} a_i\)). Luntik calls a subsequence of the array \(a\) nearly full if the sum of the numbers in that subsequence is equal to \(s-1\).Luntik really wants to know the number of nearly full subsequences of the array \(a\). But he needs to come home so he asks you to solve that problem!A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by deletion of several (possibly, zero or all) elements.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The next \(2 \cdot t\) lines contain descriptions of test cases. The description of each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 60\)) β the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the elements of the array \(a\).
|
For each test case print the number of nearly full subsequences of the array.
|
In the first test case, \(s=1+2+3+4+5=15\), only \((2,3,4,5)\) is a nearly full subsequence among all subsequences, the sum in it is equal to \(2+3+4+5=14=15-1\).In the second test case, there are no nearly full subsequences.In the third test case, \(s=1+0=1\), the nearly full subsequences are \((0)\) and \(()\) (the sum of an empty subsequence is \(0\)).
|
Input: 5 5 1 2 3 4 5 2 1000 1000 2 1 0 5 3 0 2 1 1 5 2 1 0 3 0 | Output: 1 0 2 4 4
|
Beginner
| 2 | 585 | 456 | 77 | 15 |
330 |
A
|
330A
|
A. Cakeminator
| 800 |
brute force; implementation
|
You are given a rectangular cake, represented as an r Γ c grid. Each cell either has an evil strawberry, or is empty. For example, a 3 Γ 4 cake may look as follows: The cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.Please output the maximum number of cake cells that the cakeminator can eat.
|
The first line contains two integers r and c (2 β€ r, c β€ 10), denoting the number of rows and the number of columns of the cake. The next r lines each contains c characters β the j-th character of the i-th line denotes the content of the cell at row i and column j, and is either one of these: '.' character denotes a cake cell with no evil strawberry; 'S' character denotes a cake cell with an evil strawberry.
|
Output the maximum number of cake cells that the cakeminator can eat.
|
For the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats).
|
Input: 3 4S.........S. | Output: 8
|
Beginner
| 2 | 519 | 411 | 69 | 3 |
638 |
C
|
638C
|
C. Road Improvement
| 1,800 |
*special; dfs and similar; graphs; greedy; trees
|
In Berland there are n cities and n - 1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are.
|
The first line of the input contains a positive integer n (2 β€ n β€ 200 000) β the number of cities in Berland.Each of the next n - 1 lines contains two numbers ui, vi, meaning that the i-th road connects city ui and city vi (1 β€ ui, vi β€ n, ui β vi).
|
First print number k β the minimum number of days needed to repair all the roads in Berland.In next k lines print the description of the roads that should be repaired on each of the k days. On the i-th line print first number di β the number of roads that should be repaired on the i-th day, and then di space-separated integers β the numbers of the roads that should be repaired on the i-th day. The roads are numbered according to the order in the input, starting from one.If there are multiple variants, you can print any of them.
|
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 β on the second day.
|
Input: 41 23 43 2 | Output: 22 2 11 3
|
Medium
| 5 | 628 | 250 | 533 | 6 |
955 |
E
|
955E
|
E. Icicles
| 2,900 |
Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back!At the moment the refugee is inside an icy cave with n icicles dangling from the ceiling located in integer coordinates numbered from 1 to n. The distance between floor and the i-th icicle is equal to ai.Andrew is free to choose an arbitrary integer point T in range from 1 to n inclusive and at time instant 0 launch a sound wave spreading into both sides (left and right) at the speed of one point per second. Any icicle touched by the wave starts falling at the same speed (that means that in a second the distance from floor to icicle decreases by one but cannot become less that zero). While distance from icicle to floor is more than zero, it is considered passable; as soon as it becomes zero, the icicle blocks the path and prohibits passing.Krakozyabra is initially (i.e. at time instant 0) is located at point and starts running in the right direction at the speed of one point per second. You can assume that events in a single second happen in the following order: first Krakozyabra changes its position, and only then the sound spreads and icicles fall; in particular, that means that if Krakozyabra is currently at point and the falling (i.e. already touched by the sound wave) icicle at point i is 1 point from the floor, then Krakozyabra will pass it and find itself at and only after that the icicle will finally fall and block the path.Krakozyabra is considered entrapped if there are fallen (i.e. with ai = 0) icicles both to the left and to the right of its current position. Help Andrew find the minimum possible time it takes to entrap Krakozyabra by choosing the optimal value of T or report that this mission is impossible.
|
The first line contains the number of icicles n (2 β€ n β€ 105).The next line contains n space-separated numbers ai (1 β€ ai β€ 105) β the distances from floor to icicles.
|
Print an only integer β the minimum time it takes to entrap Krakozyabra between two fallen icicles. If it is impossible, print - 1.
|
In sample case one it's optimal to launch the sound wave from point 3. Then in two seconds icicles 1 and 5 will start falling, and in one more seconds they will block the paths. Krakozyabra will be located at at that time. Note that icicle number 3 will also be fallen, so there will actually be two icicles blocking the path to the left.In sample case two it is optimal to launch the wave from point 2 and entrap Krakozyabra in 2 seconds.In sample case four the answer is impossible.
|
Input: 51 4 3 5 1 | Output: 3
|
Master
| 0 | 1,736 | 167 | 131 | 9 |
|
1,260 |
C
|
1260C
|
C. Infinite Fence
| 1,700 |
greedy; math; number theory
|
You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.You must paint a fence which consists of \(10^{100}\) planks in two colors in the following way (suppose planks are numbered from left to right from \(0\)): if the index of the plank is divisible by \(r\) (such planks have indices \(0\), \(r\), \(2r\) and so on) then you must paint it red; if the index of the plank is divisible by \(b\) (such planks have indices \(0\), \(b\), \(2b\) and so on) then you must paint it blue; if the index is divisible both by \(r\) and \(b\) you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). Furthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are \(k\) consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed.The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs.
|
The first line contains single integer \(T\) (\(1 \le T \le 1000\)) β the number of test cases.The next \(T\) lines contain descriptions of test cases β one per line. Each test case contains three integers \(r\), \(b\), \(k\) (\(1 \le r, b \le 10^9\), \(2 \le k \le 10^9\)) β the corresponding coefficients.
|
Print \(T\) words β one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise.
|
Input: 4 1 1 2 2 10 4 5 2 3 3 2 2 | Output: OBEY REBEL OBEY OBEY
|
Medium
| 3 | 1,380 | 307 | 153 | 12 |
|
1,374 |
B
|
1374B
|
B. Multiply by 2, divide by 6
| 900 |
math
|
You are given an integer \(n\). In one move, you can either multiply \(n\) by two or divide \(n\) by \(6\) (if it is divisible by \(6\) without the remainder).Your task is to find the minimum number of moves needed to obtain \(1\) from \(n\) or determine if it's impossible to do that.You have to answer \(t\) independent test cases.
|
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 only line of the test case contains one integer \(n\) (\(1 \le n \le 10^9\)).
|
For each test case, print the answer β the minimum number of moves needed to obtain \(1\) from \(n\) if it's possible to do that or -1 if it's impossible to obtain \(1\) from \(n\).
|
Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer \(15116544\): Divide by \(6\) and get \(2519424\); divide by \(6\) and get \(419904\); divide by \(6\) and get \(69984\); divide by \(6\) and get \(11664\); multiply by \(2\) and get \(23328\); divide by \(6\) and get \(3888\); divide by \(6\) and get \(648\); divide by \(6\) and get \(108\); multiply by \(2\) and get \(216\); divide by \(6\) and get \(36\); divide by \(6\) and get \(6\); divide by \(6\) and get \(1\).
|
Input: 7 1 2 3 12 12345 15116544 387420489 | Output: 0 -1 2 -1 -1 12 36
|
Beginner
| 1 | 333 | 225 | 181 | 13 |
1,146 |
B
|
1146B
|
B. Hate ""A""
| 1,100 |
implementation; strings
|
Bob has a string \(s\) consisting of lowercase English letters. He defines \(s'\) to be the string after removing all ""a"" characters from \(s\) (keeping all other characters in the same order). He then generates a new string \(t\) by concatenating \(s\) and \(s'\). In other words, \(t=s+s'\) (look at notes for an example).You are given a string \(t\). Your task is to find some \(s\) that Bob could have used to generate \(t\). It can be shown that if an answer exists, it will be unique.
|
The first line of input contains a string \(t\) (\(1 \leq |t| \leq 10^5\)) consisting of lowercase English letters.
|
Print a string \(s\) that could have generated \(t\). It can be shown if an answer exists, it is unique. If no string exists, print "":("" (without double quotes, there is no space between the characters).
|
In the first example, we have \(s = \) ""aaaaa"", and \(s' = \) """".In the second example, no such \(s\) can work that will generate the given \(t\).In the third example, we have \(s = \) ""ababacac"", and \(s' = \) ""bbcc"", and \(t = s + s' = \) ""ababacacbbcc"".
|
Input: aaaaa | Output: aaaaa
|
Easy
| 2 | 492 | 115 | 205 | 11 |
884 |
E
|
884E
|
E. Binary Matrix
| 2,500 |
dsu
|
You are given a matrix of size n Γ m. Each element of the matrix is either 1 or 0. You have to determine the number of connected components consisting of 1's. Two cells belong to the same component if they have a common border, and both elements in these cells are 1's.Note that the memory limit is unusual!
|
The first line contains two numbers n and m (1 β€ n β€ 212, 4 β€ m β€ 214) β the number of rows and columns, respectively. It is guaranteed that m is divisible by 4.Then the representation of matrix follows. Each of n next lines contains one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from A to F). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number B is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.Elements are not separated by whitespaces.
|
Print the number of connected components consisting of 1's.
|
In the first example the matrix is: 000110101000It is clear that it has three components.The second example: 0101111111100011It is clear that the number of components is 2.There are no 1's in the third example, so the answer is 0.
|
Input: 3 41A8 | Output: 3
|
Expert
| 1 | 307 | 679 | 59 | 8 |
1,592 |
F2
|
1592F2
|
F2. Alice and Recoloring 2
| 2,800 |
constructive algorithms; flows; graph matchings; greedy
|
The difference between the versions is in the costs of operations. Solution for one version won't work for another!Alice has a grid of size \(n \times m\), initially all its cells are colored white. The cell on the intersection of \(i\)-th row and \(j\)-th column is denoted as \((i, j)\). Alice can do the following operations with this grid:Choose any subrectangle containing cell \((1, 1)\), and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white). This operation costs \(1\) coin.Choose any subrectangle containing cell \((n, 1)\), and flip the colors of all its cells. This operation costs \(3\) coins.Choose any subrectangle containing cell \((1, m)\), and flip the colors of all its cells. This operation costs \(4\) coins.Choose any subrectangle containing cell \((n, m)\), and flip the colors of all its cells. This operation costs \(2\) coins. As a reminder, subrectangle is a set of all cells \((x, y)\) with \(x_1 \le x \le x_2\), \(y_1 \le y \le y_2\) for some \(1 \le x_1 \le x_2 \le n\), \(1 \le y_1 \le y_2 \le m\).Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other.
|
The first line of the input contains \(2\) integers \(n, m\) (\(1 \le n, m \le 500\)) β the dimensions of the grid.The \(i\)-th of the next \(n\) lines contains a string \(s_i\) of length \(m\), consisting of letters W and B. The \(j\)-th character of string \(s_i\) is W if the cell \((i, j)\) is colored white in the favorite coloring of Alice, and B if it's colored black.
|
Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.
|
In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells \((2, 2), (2, 3), (3, 2), (3, 3)\). This would cost \(2\) coins.
|
Input: 3 3 WWW WBB WBB | Output: 2
|
Master
| 4 | 1,312 | 375 | 95 | 15 |
1,579 |
E2
|
1579E2
|
E2. Array Optimization by Deque
| 1,700 |
data structures; greedy
|
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.You are given an integer array \(a[1 \ldots n] = [a_1, a_2, \ldots, a_n]\).Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements \([3, 4, 4]\) currently in the deque, adding an element \(1\) to the beginning will produce the sequence \([\color{red}{1}, 3, 4, 4]\), and adding the same element to the end will produce \([3, 4, 4, \color{red}{1}]\).The elements of the array are sequentially added to the initially empty deque, starting with \(a_1\) and finishing with \(a_n\). Before adding each element to the deque, you may choose whether to add it to the beginning or to the end.For example, if we consider an array \(a = [3, 7, 5, 5]\), one of the possible sequences of actions looks like this: \(\quad\) 1.add \(3\) to the beginning of the deque:deque has a sequence \([\color{red}{3}]\) in it;\(\quad\) 2.add \(7\) to the end of the deque:deque has a sequence \([3, \color{red}{7}]\) in it;\(\quad\) 3.add \(5\) to the end of the deque:deque has a sequence \([3, 7, \color{red}{5}]\) in it;\(\quad\) 4.add \(5\) to the beginning of the deque:deque has a sequence \([\color{red}{5}, 3, 7, 5]\) in it;Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence \(d\) is a pair of indices \((i, j)\) such that \(i < j\) and \(d_i > d_j\). For example, the array \(d = [5, 3, 7, 5]\) has exactly two inversions β \((1, 2)\) and \((3, 4)\), since \(d_1 = 5 > 3 = d_2\) and \(d_3 = 7 > 5 = d_4\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases. The first line of each test case description contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β array size. The second line of the description contains \(n\) space-separated integers \(a_i\) (\(-10^9 \le a_i \le 10^9\)) β elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β the minimal possible number of inversions in the deque after executing the described algorithm.
|
One of the ways to get the sequence \([5, 3, 7, 5]\) in the deque, containing only two inversions, from the initial array \([3, 7, 5, 5]\) (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence \([3, 7, 5, 5]\), also containing exactly two inversions, will be in the deque as-is.
|
Input: 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 | Output: 2 0 1 0 1 2
|
Medium
| 2 | 1,693 | 499 | 233 | 15 |
1,551 |
E
|
1551E
|
E. Fixed Points
| 2,000 |
binary search; brute force; dp
|
Consider a sequence of integers \(a_1, a_2, \ldots, a_n\). In one move, you can select any element of the sequence and delete it. After an element is deleted, all elements to the right are shifted to the left by \(1\) position, so there are no empty spaces in the sequence. So after you make a move, the sequence's length decreases by \(1\). The indices of the elements after the move are recalculated.E. g. let the sequence be \(a=[3, 2, 2, 1, 5]\). Let's select the element \(a_3=2\) in a move. Then after the move the sequence will be equal to \(a=[3, 2, 1, 5]\), so the \(3\)-rd element of the new sequence will be \(a_3=1\) and the \(4\)-th element will be \(a_4=5\).You are given a sequence \(a_1, a_2, \ldots, a_n\) and a number \(k\). You need to find the minimum number of moves you have to make so that in the resulting sequence there will be at least \(k\) elements that are equal to their indices, i. e. the resulting sequence \(b_1, b_2, \ldots, b_m\) will contain at least \(k\) indices \(i\) such that \(b_i = i\).
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of two consecutive lines. The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)). The second line contains a sequence of integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)). The numbers in the sequence are not necessarily different.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2000\).
|
For each test case output in a single line: \(-1\) if there's no desired move sequence; otherwise, the integer \(x\) (\(0 \le x \le n\)) β the minimum number of the moves to be made so that the resulting sequence will contain at least \(k\) elements that are equal to their indices.
|
In the first test case the sequence doesn't satisfy the desired condition, but it can be provided by deleting the first element, hence the sequence will be \([1, 2, 3, 4, 5, 6]\) and \(6\) elements will be equal to their indices.In the second test case there are two ways to get the desired result in \(2\) moves: the first one is to delete the \(1\)-st and the \(3\)-rd elements so that the sequence will be \([1, 2, 3]\) and have \(3\) elements equal to their indices; the second way is to delete the \(2\)-nd and the \(3\)-rd elements to get the sequence \([5, 2, 3]\) with \(2\) desired elements.
|
Input: 4 7 6 1 1 2 3 4 5 6 5 2 5 1 3 2 3 5 2 5 5 5 5 4 8 4 1 2 3 3 2 2 5 5 | Output: 1 2 -1 2
|
Hard
| 3 | 1,029 | 492 | 282 | 15 |
2,063 |
E
|
2063E
|
E. Triangle Tree
| 2,300 |
data structures; dfs and similar; dp; greedy; trees
|
One day, a giant tree grew in the countryside. Little John, with his childhood eagle, decided to make it his home. Little John will build a structure on the tree with galvanized square steel. However, little did he know, he could not build what is physically impossible. You are given a rooted tree\(^{\text{β}}\) containing \(n\) vertices rooted at vertex \(1\). A pair of vertices \((u,v)\) is called a good pair if \(u\) is not an ancestor\(^{\text{β }}\) of \(v\) and \(v\) is not an ancestor of \(u\). For any two vertices, \(\text{dist}(u,v)\) is defined as the number of edges on the unique simple path from \(u\) to \(v\), and \(\text{lca}(u,v)\) is defined as their lowest common ancestor.A function \(f(u,v)\) is defined as follows. If \((u,v)\) is a good pair, \(f(u,v)\) is the number of distinct integer values \(x\) such that there exists a non-degenerate triangle\(^{\text{β‘}}\) formed by side lengths \(\text{dist}(u,\text{lca}(u,v))\), \(\text{dist}(v,\text{lca}(u,v))\), and \(x\). Otherwise, \(f(u,v)\) is \(0\). You need to find the following value: $$$\(\sum_{i = 1}^{n-1} \sum_{j = i+1}^n f(i,j).\)$$$\(^{\text{β}}\)A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root. \(^{\text{β }}\)An ancestor of vertex \(v\) is any vertex on the simple path from \(v\) to the root, including the root, but not including \(v\). The root has no ancestors. \(^{\text{β‘}}\)A triangle with side lengths \(a\), \(b\), \(c\) is non-degenerate when \(a+b > c\), \(a+c > b\), \(b+c > a\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).Each of the next \(n-1\) lines contains two integers \(u_i\) and \(v_i\), denoting the two vertices connected by an edge (\(1 \le u_i,v_i \le n\), \(u_i \neq v_i\)).It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output the answer on a separate line.
|
On the first test case, the only good pair \((i,j)\) satisfying \(i<j\) is \((2,3)\). Here, \(\text{lca}(2,3)\) is \(1\), and the two distances are \(1\) and \(1\).There is only one value of \(x\) for two side lengths \(1\) and \(1\), which is \(1\). Therefore, the answer for the first test case is \(1\).On the second test case, there is no good pair. Therefore, the answer for the second test case is \(0\).On the third test case, the good pairs \((i,j)\) satisfying \(i<j\) are as follows. \((2,5)\): \(\text{lca}(2,5)\) is \(1\), distances are \(1\) and \(1\). There is only one possible value of \(x\), which is \(1\). \((3,4)\): \(\text{lca}(3,4)\) is \(2\), distances are \(1\) and \(1\). There is only one possible value of \(x\), which is \(1\). \((3,5)\): \(\text{lca}(3,5)\) is \(1\), distances are \(2\) and \(1\). There is only one possible value of \(x\), which is \(2\). \((4,5)\): \(\text{lca}(4,5)\) is \(1\), distances are \(2\) and \(1\). There is only one possible value of \(x\), which is \(2\). Therefore, the answer for the third test case is \(1+1+1+1=4\).
|
Input: 431 21 331 23 252 31 54 21 2112 12 32 44 56 55 74 88 97 1010 11 | Output: 1 0 4 29
|
Expert
| 5 | 1,551 | 565 | 57 | 20 |
855 |
E
|
855E
|
E. Salazar Slytherin's Locket
| 2,200 |
bitmasks; dp
|
Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher. Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l and r (both inclusive). Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b, all the digits from 0 to b - 1 appear even number of times in its representation without any leading zeros.You have to answer q queries to unlock the office. Each query has three integers bi, li and ri, the base and the range for which you have to find the count of magic numbers.
|
First line of input contains q (1 β€ q β€ 105) β number of queries.Each of the next q lines contain three space separated integers bi, li, ri (2 β€ bi β€ 10, 1 β€ li β€ ri β€ 1018).
|
You have to output q lines, each containing a single integer, the answer to the corresponding query.
|
In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: 4 = 1002, 5 = 1012, 6 = 1102, 7 = 1112, 8 = 10002, 9 = 10012. Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.
|
Input: 22 4 93 1 10 | Output: 12
|
Hard
| 2 | 972 | 174 | 100 | 8 |
574 |
A
|
574A
|
A. Bear and Elections
| 1,200 |
greedy; implementation
|
Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get ai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?
|
The first line contains single integer n (2 β€ n β€ 100) - number of candidates.The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 1000) - number of votes for each candidate. Limak is candidate number 1.Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.
|
Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.
|
In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.In the third sample Limak is a winner without bribing any citizen.
|
Input: 55 1 11 2 8 | Output: 4
|
Easy
| 2 | 700 | 331 | 107 | 5 |
611 |
G
|
611G
|
G. New Year and Cake
| 2,900 |
geometry; two pointers
|
Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes.As you may know, a New Year cake is a strictly convex polygon with n vertices.Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking they decided to cut a cake along one of nΒ·(n - 3) / 2 diagonals. Then Limak will get a non-greater piece.Limak understands rules but he won't be happy if the second piece happens to be much bigger. Limak's disappointment will be equal to the difference between pieces' areas, multiplied by two. It can be proved that it will be integer for the given constraints.There are nΒ·(n - 3) / 2 possible scenarios. Consider them all and find the sum of values of Limak's disappointment, modulo 109 + 7.
|
The first line of the input contains a single integer n (4 β€ n β€ 500 000) β the number of vertices in the polygon denoting the cake.Each of the next n lines contains two integers xi and yi (|xi|, |yi| β€ 109) β coordinates of the i-th point.It's guaranteed that all points are distinct, polygon is strictly convex and points are given in the clockwise order.
|
Print the sum of values of Limak's disappointment over all possible scenarios modulo 109 + 7.
|
In the first sample possible values of Limak's disappointment are 0, 18, 18, 24, 30.
|
Input: 52 42 75 75 43 -2 | Output: 90
|
Master
| 2 | 804 | 357 | 93 | 6 |
1,650 |
E
|
1650E
|
E. Rescheduling the Exam
| 1,900 |
binary search; data structures; greedy; implementation; math; sortings
|
Now Dmitry has a session, and he has to pass \(n\) exams. The session starts on day \(1\) and lasts \(d\) days. The \(i\)th exam will take place on the day of \(a_i\) (\(1 \le a_i \le d\)), all \(a_i\) β are different. Sample, where \(n=3\), \(d=12\), \(a=[3,5,9]\). Orange β exam days. Before the first exam Dmitry will rest \(2\) days, before the second he will rest \(1\) day and before the third he will rest \(3\) days. For the session schedule, Dmitry considers a special value \(\mu\) β the smallest of the rest times before the exam for all exams. For example, for the image above, \(\mu=1\). In other words, for the schedule, he counts exactly \(n\) numbers β how many days he rests between the exam \(i-1\) and \(i\) (for \(i=0\) between the start of the session and the exam \(i\)). Then it finds \(\mu\) β the minimum among these \(n\) numbers.Dmitry believes that he can improve the schedule of the session. He may ask to change the date of one exam (change one arbitrary value of \(a_i\)). Help him change the date so that all \(a_i\) remain different, and the value of \(\mu\) is as large as possible.For example, for the schedule above, it is most advantageous for Dmitry to move the second exam to the very end of the session. The new schedule will take the form: Now the rest periods before exams are equal to \([2,2,5]\). So, \(\mu=2\). Dmitry can leave the proposed schedule unchanged (if there is no way to move one exam so that it will lead to an improvement in the situation).
|
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of input test cases. The descriptions of test cases follow.An empty line is written in the test before each case.The first line of each test case contains two integers \(n\) and \(d\) (\(2 \le n \le 2 \cdot 10^5, 1 \le d \le 10^9\)) β the number of exams and the length of the session, respectively.The second line of each test case contains \(n\) integers \(a_i\) (\(1 \le a_i \le d, a_i < a_{i+1}\)), where the \(i\)-th number means the date of the \(i\)-th exam.It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the maximum possible value of \(\mu\) if Dmitry can move any one exam to an arbitrary day. All values of \(a_i\) should remain distinct.
|
The first sample is parsed in statement.One of the optimal schedule changes for the second sample: Initial schedule. New schedule.In the third sample, we need to move the exam from day \(1\) to any day from \(4\) to \(100\).In the fourth sample, any change in the schedule will only reduce \(\mu\), so the schedule should be left as it is.In the fifth sample, we need to move the exam from day \(1\) to any day from \(100000000\) to \(300000000\).One of the optimal schedule changes for the sixth sample: Initial schedule. New schedule.In the seventh sample, every day is exam day, and it is impossible to rearrange the schedule.
|
Input: 93 123 5 92 51 52 1001 25 153 6 9 12 153 10000000001 400000000 5000000002 103 42 21 24 156 11 12 132 2017 20 | Output: 2 1 1 2 99999999 3 0 1 9
|
Hard
| 6 | 1,499 | 647 | 163 | 16 |
1,872 |
F
|
1872F
|
F. Selling a Menagerie
| 1,800 |
dfs and similar; dsu; graphs; implementation; math
|
You are the owner of a menagerie consisting of \(n\) animals numbered from \(1\) to \(n\). However, maintaining the menagerie is quite expensive, so you have decided to sell it!It is known that each animal is afraid of exactly one other animal. More precisely, animal \(i\) is afraid of animal \(a_i\) (\(a_i \neq i\)). Also, the cost of each animal is known, for animal \(i\) it is equal to \(c_i\).You will sell all your animals in some fixed order. Formally, you will need to choose some permutation\(^\dagger\) \(p_1, p_2, \ldots, p_n\), and sell animal \(p_1\) first, then animal \(p_2\), and so on, selling animal \(p_n\) last.When you sell animal \(i\), there are two possible outcomes: If animal \(a_i\) was sold before animal \(i\), you receive \(c_i\) money for selling animal \(i\). If animal \(a_i\) was not sold before animal \(i\), you receive \(2 \cdot c_i\) money for selling animal \(i\). (Surprisingly, animals that are currently afraid are more valuable). Your task is to choose the order of selling the animals in order to maximize the total profit. For example, if \(a = [3, 4, 4, 1, 3]\), \(c = [3, 4, 5, 6, 7]\), and the permutation you choose is \([4, 2, 5, 1, 3]\), then: The first animal to be sold is animal \(4\). Animal \(a_4 = 1\) was not sold before, so you receive \(2 \cdot c_4 = 12\) money for selling it. The second animal to be sold is animal \(2\). Animal \(a_2 = 4\) was sold before, so you receive \(c_2 = 4\) money for selling it. The third animal to be sold is animal \(5\). Animal \(a_5 = 3\) was not sold before, so you receive \(2 \cdot c_5 = 14\) money for selling it. The fourth animal to be sold is animal \(1\). Animal \(a_1 = 3\) was not sold before, so you receive \(2 \cdot c_1 = 6\) money for selling it. The fifth animal to be sold is animal \(3\). Animal \(a_3 = 4\) was sold before, so you receive \(c_3 = 5\) money for selling it.Your total profit, with this choice of permutation, is \(12 + 4 + 14 + 6 + 5 = 41\). Note that \(41\) is not the maximum possible profit in this example.\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\), but \(4\) is present in the array).
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follow the descriptions of the test cases.The first line of each test case description contains an integer \(n\) (\(2 \le n \le 10^5\)) β the number of animals.The second line of the test case description contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\), \(a_i \neq i\)) β \(a_i\) means the index of the animal that animal \(i\) is afraid of.The third line of the test case description contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le 10^9\)) β the costs of the animals.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
Output \(t\) lines, each containing the answer to the corresponding test case. The answer should be \(n\) integers β the permutation \(p_1, p_2, \ldots, p_n\), indicating in which order to sell the animals in order to maximize the profit. If there are multiple possible answers, you can output any of them.
|
Input: 832 3 26 6 182 1 4 3 6 5 8 71 2 1 2 2 1 2 152 1 1 1 19 8 1 1 122 11000000000 99999999972 3 2 6 4 4 31 2 3 4 5 6 753 4 4 1 33 4 5 6 732 1 11 2 242 1 4 11 1 1 1 | Output: 1 2 3 2 4 5 1 6 3 7 8 3 4 5 1 2 1 2 7 5 1 3 2 6 4 5 3 2 4 1 3 2 1 3 4 1 2
|
Medium
| 5 | 2,372 | 701 | 306 | 18 |
|
605 |
C
|
605C
|
C. Freelancer's Dreams
| 2,400 |
geometry
|
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least p experience points, and a desired flat in Moscow costs q dollars. Mikhail is determined to follow his dreams and registered at a freelance site.He has suggestions to work on n distinct projects. Mikhail has already evaluated that the participation in the i-th project will increase his experience by ai per day and bring bi dollars per day. As freelance work implies flexible working hours, Mikhail is free to stop working on one project at any time and start working on another project. Doing so, he receives the respective share of experience and money. Mikhail is only trying to become a cool programmer, so he is able to work only on one project at any moment of time.Find the real value, equal to the minimum number of days Mikhail needs to make his dream come true.For example, suppose Mikhail is suggested to work on three projects and a1 = 6, b1 = 2, a2 = 1, b2 = 3, a3 = 2, b3 = 6. Also, p = 20 and q = 20. In order to achieve his aims Mikhail has to work for 2.5 days on both first and third projects. Indeed, a1Β·2.5 + a2Β·0 + a3Β·2.5 = 6Β·2.5 + 1Β·0 + 2Β·2.5 = 20 and b1Β·2.5 + b2Β·0 + b3Β·2.5 = 2Β·2.5 + 3Β·0 + 6Β·2.5 = 20.
|
The first line of the input contains three integers n, p and q (1 β€ n β€ 100 000, 1 β€ p, q β€ 1 000 000) β the number of projects and the required number of experience and money.Each of the next n lines contains two integers ai and bi (1 β€ ai, bi β€ 1 000 000) β the daily increase in experience and daily income for working on the i-th project.
|
Print a real value β the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
First sample corresponds to the example in the problem statement.
|
Input: 3 20 206 21 32 6 | Output: 5.000000000000000
|
Expert
| 1 | 1,274 | 342 | 347 | 6 |
2,069 |
F
|
2069F
|
F. Graph Inclusion
| 2,800 |
data structures; dfs and similar; divide and conquer; dsu; graphs
|
A connected component of an undirected graph is defined as a set of vertices \(S\) of this graph such that: for every pair of vertices \((u, v)\) in \(S\), there exists a path between vertices \(u\) and \(v\); there is no vertex outside \(S\) that has a path to a vertex within \(S\). For example, the graph in the picture below has three components: \(\{1, 3, 7, 8\}\), \(\{2\}\), \(\{4, 5, 6\}\). We say that graph \(A\) includes graph \(B\) if every component of graph \(B\) is a subset of some component of graph \(A\).You are given two graphs, \(A\) and \(B\), both consisting of \(n\) vertices numbered from \(1\) to \(n\). Initially, there are no edges in the graphs. You must process queries of two types: add an edge to one of the graphs; remove an edge from one of the graphs. After each query, you have to calculate the minimum number of edges that have to be added to \(A\) so that \(A\) includes \(B\), and print it. Note that you don't actually add these edges, you just calculate their number.
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 4 \cdot 10^5\); \(1 \le q \le 4 \cdot 10^5\)) β the number of vertices and queries, respectively.Next, there are \(q\) lines, where the \(i\)-th line describes the \(i\)-th query. The description of the query begins with the character \(c_i\) (either A or B) β the graph to which the query should be applied. Then, two integers \(x_i\) and \(y_i\) follow (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)). If there is an edge \((x_i, y_i)\) in the corresponding graph, it should be removed; otherwise, it should be added to that graph.
|
For each query, print one integer β the minimum number of edges that you have to add to the graph \(A\) so that it includes \(B\).
|
Input: 6 9A 2 3B 1 3A 2 1A 3 2B 5 6A 6 5A 3 4A 4 2A 4 3 | Output: 0 1 0 1 2 1 1 0 1
|
Master
| 5 | 1,008 | 597 | 130 | 20 |
|
1,572 |
E
|
1572E
|
E. Polygon
| 3,000 |
binary search; dp; geometry
|
You are given a strictly convex polygon with \(n\) vertices.You will make \(k\) cuts that meet the following conditions: each cut is a segment that connects two different nonadjacent vertices; two cuts can intersect only at vertices of the polygon. Your task is to maximize the area of the smallest region that will be formed by the polygon and those \(k\) cuts.
|
The first line contains two integers \(n\), \(k\) (\(3 \le n \le 200\), \(0 \le k \le n-3\)).The following \(n\) lines describe vertices of the polygon in anticlockwise direction. The \(i\)-th line contains two integers \(x_i\), \(y_i\) (\(|x_i|, |y_i| \le 10^8\)) β the coordinates of the \(i\)-th vertex.It is guaranteed that the polygon is convex and that no two adjacent sides are parallel.
|
Print one integer: the maximum possible area of the smallest region after making \(k\) cuts multiplied by \(2\).
|
In the first example, it's optimal to make cuts between the following pairs of vertices: \((-2, -4)\) and \((4, 2)\), \((-2, -4)\) and \((1, 5)\), \((-5, -1)\) and \((1, 5)\), \((-5, 0)\) and \((0, 5)\). Points \((-5, -1)\), \((1, 5)\), \((0, 5)\), \((-5, 0)\) determine the smallest region with double area of \(11\). In the second example, it's optimal to make cuts between the following pairs of vertices: \((2, -1)\) and \((0, 2)\), \((2, -1)\) and \((1, 0)\), \((-1, 0)\) and \((0, 2)\). Points \((2, -2)\), \((2, -1)\), \((-1, 0)\) determine one of the smallest regions with double area of \(3\).
|
Input: 8 4 -2 -4 2 -2 4 2 1 5 0 5 -4 4 -5 0 -5 -1 | Output: 11
|
Master
| 3 | 362 | 394 | 112 | 15 |
1,167 |
E
|
1167E
|
E. Range Deleting
| 2,100 |
binary search; combinatorics; data structures; two pointers
|
You are given an array consisting of \(n\) integers \(a_1, a_2, \dots , a_n\) and an integer \(x\). It is guaranteed that for every \(i\), \(1 \le a_i \le x\).Let's denote a function \(f(l, r)\) which erases all values such that \(l \le a_i \le r\) from the array \(a\) and returns the resulting array. For example, if \(a = [4, 1, 1, 4, 5, 2, 4, 3]\), then \(f(2, 4) = [1, 1, 5]\).Your task is to calculate the number of pairs \((l, r)\) such that \(1 \le l \le r \le x\) and \(f(l, r)\) is sorted in non-descending order. Note that the empty array is also considered sorted.
|
The first line contains two integers \(n\) and \(x\) (\(1 \le n, x \le 10^6\)) β the length of array \(a\) and the upper limit for its elements, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots a_n\) (\(1 \le a_i \le x\)).
|
Print the number of pairs \(1 \le l \le r \le x\) such that \(f(l, r)\) is sorted in non-descending order.
|
In the first test case correct pairs are \((1, 1)\), \((1, 2)\), \((1, 3)\) and \((2, 3)\).In the second test case correct pairs are \((1, 3)\), \((1, 4)\), \((2, 3)\), \((2, 4)\), \((3, 3)\) and \((3, 4)\).
|
Input: 3 3 2 3 1 | Output: 4
|
Hard
| 4 | 576 | 244 | 106 | 11 |
1,067 |
B
|
1067B
|
B. Multihedgehog
| 1,800 |
dfs and similar; graphs; shortest paths
|
Someone give a strange birthday present to Ivan. It is hedgehog β connected undirected graph in which one vertex has degree at least \(3\) (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself \(k\)-multihedgehog.Let us define \(k\)-multihedgehog as follows: \(1\)-multihedgehog is hedgehog: it has one vertex of degree at least \(3\) and some vertices of degree 1. For all \(k \ge 2\), \(k\)-multihedgehog is \((k-1)\)-multihedgehog in which the following changes has been made for each vertex \(v\) with degree 1: let \(u\) be its only neighbor; remove vertex \(v\), create a new hedgehog with center at vertex \(w\) and connect vertices \(u\) and \(w\) with an edge. New hedgehogs can differ from each other and the initial gift. Thereby \(k\)-multihedgehog is a tree. Ivan made \(k\)-multihedgehog but he is not sure that he did not make any mistakes. That is why he asked you to check if his tree is indeed \(k\)-multihedgehog.
|
First line of input contains \(2\) integers \(n\), \(k\) (\(1 \le n \le 10^{5}\), \(1 \le k \le 10^{9}\)) β number of vertices and hedgehog parameter.Next \(n-1\) lines contains two integers \(u\) \(v\) (\(1 \le u, \,\, v \le n; \,\, u \ne v\)) β indices of vertices connected by edge.It is guaranteed that given graph is a tree.
|
Print ""Yes"" (without quotes), if given graph is \(k\)-multihedgehog, and ""No"" (without quotes) otherwise.
|
2-multihedgehog from the first example looks like this:Its center is vertex \(13\). Hedgehogs created on last step are: [4 (center), 1, 2, 3], [6 (center), 7, 8, 9], [5 (center), 10, 11, 12, 13].Tree from second example is not a hedgehog because degree of center should be at least \(3\).
|
Input: 14 21 42 43 44 1310 511 512 514 55 136 78 613 69 6 | Output: Yes
|
Medium
| 3 | 1,009 | 329 | 109 | 10 |
1,572 |
F
|
1572F
|
F. Stations
| 3,400 |
data structures
|
There are \(n\) cities in a row numbered from \(1\) to \(n\).The cities will be building broadcasting stations. The station in the \(i\)-th city has height \(h_i\) and range \(w_i\). It can broadcast information to city \(j\) if the following constraints are met: \(i \le j \le w_i\), and for each \(k\) such that \(i < k \le j\), the following condition holds: \(h_k < h_i\). In other words, the station in city \(i\) can broadcast information to city \(j\) if \(j \ge i\), \(j\) is in the range of \(i\)-th station, and \(i\) is strictly highest on the range from \(i\) to \(j\) (including city \(j\)).At the beginning, for every city \(i\), \(h_i = 0\) and \(w_i = i\).Then \(q\) events will take place. During \(i\)-th event one of the following will happen: City \(c_i\) will rebuild its station so that its height will be strictly highest among all stations and \(w_{c_i}\) will be set to \(g_i\). Let \(b_j\) be the number of stations that can broadcast information to city \(j\). Print the sum of \(b_j\) over all \(j\) satisfying \(l_i \le j \le r_i\). Your task is to react to all events and print answers to all queries.
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2\cdot10^5\)) β number of cities and number of events.Then \(q\) lines follow. The \(i\)-th line begins with an integer \(p_i\) (\(p_i = 1\) or \(p_i = 2\)).If \(p_i = 1\) a station will be rebuilt. Then two integers \(c_i\) and \(g_i\) (\(1 \le c_i \le g_i \le n\)) follow β the city in which the station is rebuilt and its new broadcasting range.If \(p_i = 2\) you are given a query. Then two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) follow β the range of cities in the query.
|
For each query, print in a single line the sum of \(b_j\) over the given interval.
|
In the first test case, only station \(1\) reaches city \(1\) before and after it is rebuilt.In the second test case, after each rebuild, the array \(b\) looks as follows: \([1, 1, 1, 2, 1]\); \([1, 2, 2, 3, 2]\); \([1, 2, 2, 1, 2]\); \([1, 1, 2, 1, 2]\); \([1, 1, 2, 1, 1]\).
|
Input: 1 3 2 1 1 1 1 1 2 1 1 | Output: 1 1
|
Master
| 1 | 1,131 | 567 | 82 | 15 |
1,454 |
A
|
1454A
|
A. Special Permutation
| 800 |
constructive algorithms; probabilities
|
You are given one integer \(n\) (\(n > 1\)).Recall that a permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2, 3, 1, 5, 4]\) is a permutation of length \(5\), but \([1, 2, 2]\) is not a permutation (\(2\) appears twice in the array) and \([1, 3, 4]\) is also not a permutation (\(n = 3\) but there is \(4\) in the array).Your task is to find a permutation \(p\) of length \(n\) that there is no index \(i\) (\(1 \le i \le n\)) such that \(p_i = i\) (so, for all \(i\) from \(1\) to \(n\) the condition \(p_i \ne i\) should be satisfied).You have to answer \(t\) independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each \(n > 1\).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(2 \le n \le 100\)) β the length of the permutation you have to find.
|
For each test case, print \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) β a permutation that there is no index \(i\) (\(1 \le i \le n\)) such that \(p_i = i\) (so, for all \(i\) from \(1\) to \(n\) the condition \(p_i \ne i\) should be satisfied).If there are several answers, you can print any. It can be proven that the answer exists for each \(n > 1\).
|
Input: 2 2 5 | Output: 2 1 2 1 5 3 4
|
Beginner
| 2 | 777 | 263 | 359 | 14 |
|
986 |
F
|
986F
|
F. Oppa Funcan Style Remastered
| 3,300 |
graphs; math; number theory; shortest paths
|
Surely you have seen insane videos by South Korean rapper PSY, such as ""Gangnam Style"", ""Gentleman"" and ""Daddy"". You might also hear that PSY has been recording video ""Oppa Funcan Style"" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description here):On the ground there are \(n\) platforms, which are numbered with integers from \(1\) to \(n\), on \(i\)-th platform there is a dancer with number \(i\). Further, every second all the dancers standing on the platform with number \(i\) jump to the platform with the number \(f(i)\). The moving rule \(f\) is selected in advance and is not changed throughout the clip.The duration of the clip was \(k\) seconds and the rule \(f\) was chosen in such a way that after \(k\) seconds all dancers were in their initial positions (i.e. the \(i\)-th dancer stood on the platform with the number \(i\)). That allowed to loop the clip and collect even more likes.PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video.In his case ""enhanced version"" means even more insanity, so the number of platforms can be up to \(10^{18}\)! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all \(x\) from \(1\) to \(n\) \(f(x) \neq x\) must hold.Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well.PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule \(f\) for different options.
|
In the first line of input there is one integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of options for \(n\) and \(k\) to check.In the next \(t\) lines the options are given: each option is described with two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(1 \le k \le 10^{15}\)) β the number of dancers and the duration in seconds.It is guaranteed that the number of different values of \(k\) in one test is not greater than \(50\).
|
Print \(t\) lines. If the \(i\)-th option of the video is feasible, print ""YES"" (without quotes) in \(i\)-th line, otherwise print ""NO"" (without quotes).
|
Input: 37 73 85 6 | Output: YESNOYES
|
Master
| 4 | 1,813 | 442 | 157 | 9 |
|
2,040 |
B
|
2040B
|
B. Paint a Strip
| 1,000 |
constructive algorithms; greedy; math
|
You have an array of zeros \(a_1, a_2, \ldots, a_n\) of length \(n\).You can perform two types of operations on it: Choose an index \(i\) such that \(1 \le i \le n\) and \(a_i = 0\), and assign \(1\) to \(a_i\); Choose a pair of indices \(l\) and \(r\) such that \(1 \le l \le r \le n\), \(a_l = 1\), \(a_r = 1\), \(a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil\), and assign \(1\) to \(a_i\) for all \(l \le i \le r\). What is the minimum number of operations of the first type needed to make all elements of the array equal to one?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array.Note that there is no limit on the sum of \(n\) over all test cases.
|
For each test case, print one integer β the minimum number of needed operations of first type.
|
In the first test case, you can perform an operation of the \(1\)st type with \(i = 1\).In the second test case, you can perform the following sequence of operations: Operation of \(1\)st type, \(i = 1\). After performing this operation, the array will look like this: \([1, 0]\). Operation of \(1\)st type, \(i = 2\). After performing this operation, the array will look like this: \([1, 1]\). The sequence of operations in the second test case In the third test case, you can perform the following sequence of operations: Operation of \(1\)st type, \(i = 1\). After performing this operation, the array will look like this: \([1, 0, 0, 0]\). Operation of \(1\)st type, \(i = 4\). After performing this operation, the array will look like this: \([1, 0, 0, 1]\). Operation of \(2\)nd type, \(l = 1\), \(r = 4\). On this segment, \(a_l + \ldots + a_r = a_1 + a_2 + a_3 + a_4 = 2\), which is not less than \(\lceil\frac{r - l + 1}{2}\rceil = 2\). After performing this operation, the array will look like this: \([1, 1, 1, 1]\). The sequence of operations in the third test case
|
Input: 412420 | Output: 1 2 2 4
|
Beginner
| 3 | 542 | 337 | 94 | 20 |
977 |
D
|
977D
|
D. Divide by three, multiply by two
| 1,400 |
dfs and similar; math; sortings
|
Polycarp likes to play with numbers. He takes some integer number \(x\), writes it down on the board, and then performs with it \(n - 1\) operations of the two kinds: divide the number \(x\) by \(3\) (\(x\) must be divisible by \(3\)); multiply the number \(x\) by \(2\). After each operation, Polycarp writes down the result on the board and replaces \(x\) by the result. So there will be \(n\) numbers on the board after all.You are given a sequence of length \(n\) β the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.It is guaranteed that the answer exists.
|
The first line of the input contatins an integer number \(n\) (\(2 \le n \le 100\)) β the number of the elements in the sequence. The second line of the input contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 3 \cdot 10^{18}\)) β rearranged (reordered) sequence that Polycarp can wrote down on the board.
|
Print \(n\) integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.It is guaranteed that the answer exists.
|
In the first example the given sequence can be rearranged in the following way: \([9, 3, 6, 12, 4, 8]\). It can match possible Polycarp's game which started with \(x = 9\).
|
Input: 64 8 6 3 12 9 | Output: 9 3 6 12 4 8
|
Easy
| 3 | 964 | 327 | 177 | 9 |
843 |
B
|
843B
|
B. Interactive LowerBound
| 2,000 |
brute force; interactive; probabilities
|
This is an interactive problem.You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β - 1, then valuenexti > valuei.You are given the number of elements in the list n, the index of the first element start, and the integer x.You can make up to 2000 queries of the following two types: ? i (1 β€ i β€ n) β ask the values valuei and nexti, ! ans β give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem.
|
The first line contains three integers n, start, x (1 β€ n β€ 50000, 1 β€ start β€ n, 0 β€ x β€ 109) β the number of elements in the list, the index of the first element and the integer x.
|
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
|
You can read more about singly linked list by the following link: https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list The illustration for the first sample case. Start and finish elements are marked dark.
|
Input: 5 3 8097 -158 516 281 179 4 | Output: ? 1? 2? 3? 4? 5! 81
|
Hard
| 3 | 967 | 182 | 158 | 8 |
1,986 |
B
|
1986B
|
B. Matrix Stabilization
| 1,000 |
brute force; data structures; greedy; sortings
|
You are given a matrix of size \(n \times m\), where the rows are numbered from \(1\) to \(n\) from top to bottom, and the columns are numbered from \(1\) to \(m\) from left to right. The element at the intersection of the \(i\)-th row and the \(j\)-th column is denoted by \(a_{ij}\).Consider the algorithm for stabilizing matrix \(a\): Find the cell \((i, j)\) such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of \(i\), and if there are still multiple cells, choose the one with the smallest value of \(j\). Set \(a_{ij} = a_{ij} - 1\). Go to step \(1\). In this problem, cells \((a, b)\) and \((c, d)\) are considered neighbors if they share a common side, i.e., \(|a - c| + |b - d| = 1\).Your task is to output the matrix \(a\) after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations.
|
Each test consists of multiple sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of sets of input data. This is followed by their description.The first line of each set of input data contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 100, n \cdot m > 1\)) β the number of rows and columns of matrix \(a\). The next \(n\) lines describe the corresponding rows of the matrix. The \(i\)-th line contains \(m\) integers \(a_{i1}, a_{i2}, \ldots, a_{im}\) (\(1 \leq a_{ij} \leq 10^9\)).It is guaranteed that the sum of \(n \cdot m\) over all sets of input data does not exceed \(2 \cdot 10^5\).
|
For each set of input data, output \(n\) lines with \(m\) numbers in each line β the values of the cells of matrix \(a\) after the stabilization algorithm.
|
In the first set of input data, the algorithm will select the cell \((1, 1)\) twice in a row and then terminate. In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells.In the third set of input data, the algorithm will select the cell \((2, 2)\) and then terminate. In the fourth set of input data, the algorithm will select the cell \((1, 1)\) three times and then the cell \((2, 3)\) twice.
|
Input: 61 23 12 1112 21 23 42 37 4 51 8 105 492 74 31 7474 92 17 731 17 92 374 7 3 927 31 1 13 31000000000 1 10000000001 1000000000 11000000000 1 1000000000 | Output: 1 1 1 1 1 2 3 3 4 4 5 1 8 8 74 74 31 31 74 74 17 7 31 17 17 3 31 7 3 3 7 7 1 1 1 1 1 1 1 1 1 1 1
|
Beginner
| 4 | 1,031 | 651 | 155 | 19 |
1,034 |
E
|
1034E
|
E. Little C Loves 3 III
| 3,200 |
bitmasks; dp; math
|
Little C loves number Β«3Β» very much. He loves all things about it.Now he is interested in the following problem:There are two arrays of \(2^n\) intergers \(a_0,a_1,...,a_{2^n-1}\) and \(b_0,b_1,...,b_{2^n-1}\).The task is for each \(i (0 \leq i \leq 2^n-1)\), to calculate \(c_i=\sum a_j \cdot b_k\) (\(j|k=i\) and \(j\&k=0\), where ""\(|\)"" denotes bitwise or operation and ""\(\&\)"" denotes bitwise and operation).It's amazing that it can be proved that there are exactly \(3^n\) triples \((i,j,k)\), such that \(j|k=i\), \(j\&k=0\) and \(0 \leq i,j,k \leq 2^n-1\). So Little C wants to solve this excellent problem (because it's well related to \(3\)) excellently.Help him calculate all \(c_i\). Little C loves \(3\) very much, so he only want to know each \(c_i \& 3\).
|
The first line contains one integer \(n (0 \leq n \leq 21)\).The second line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(a_{i-1}\).The third line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(b_{i-1}\).
|
Print one line contains \(2^n\) integers in \([0,3]\) without spaces β the \(i\)-th of them is \(c_{i-1}\&3\). (It's obvious that \(c_{i}\&3\) is in \([0,3]\)).
|
Input: 11111 | Output: 12
|
Master
| 3 | 775 | 276 | 160 | 10 |
|
804 |
E
|
804E
|
E. The same permutation
| 3,100 |
constructive algorithms
|
Seyyed and MoJaK are friends of Sajjad. Sajjad likes a permutation. Seyyed wants to change the permutation in a way that Sajjad won't like it. Seyyed thinks more swaps yield more probability to do that, so he makes MoJaK to perform a swap between every pair of positions (i, j), where i < j, exactly once. MoJaK doesn't like to upset Sajjad.Given the permutation, determine whether it is possible to swap all pairs of positions so that the permutation stays the same. If it is possible find how to do that.
|
The first line contains single integer n (1 β€ n β€ 1000) β the size of the permutation.As the permutation is not important, you can consider ai = i, where the permutation is a1, a2, ..., an.
|
If it is not possible to swap all pairs of positions so that the permutation stays the same, print ""NO"",Otherwise print ""YES"", then print lines: the i-th of these lines should contain two integers a and b (a < b) β the positions where the i-th swap is performed.
|
Input: 3 | Output: NO
|
Master
| 1 | 506 | 189 | 266 | 8 |
|
2,131 |
G
|
2131G
|
G. Wafu!
| 0 |
bitmasks; brute force; data structures; dfs and similar; dp; math
|
To help improve her math, Kudryavka is given a set \(S\) that consists of \(n\) distinct positive integers. Initially, her score is \(1\). She can perform an arbitrary number of the following operations on the set if it is not empty: Let the minimum value of \(S\) be \(m\). Multiply her score by \(m\). Remove \(m\) from \(S\). For every integer \(i\) such that \(1 \le i < m\), add \(i\) to the set \(S\). It can be shown that no duplicates are added during this step. She is addicted to performing operations, but after \(k\) operations, she realizes she forgot her score. Please help her determine her score, modulo \(10^9+7\).
|
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 two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^9\)). The second line of each test case contains \(n\) integers \(s_1, s_2, \dots, s_n\) (\(1 \le s_i \le 10^9\), \(s_i \neq s_j\)) β the elements of the initial set \(S\). It is guaranteed that the set \(S\) is not empty before each of the \(k\) operations is performed.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output an integer indicating the answer modulo \(10^9+7\).
|
Let us simulate the process in the first test case:$$$\( \{1,3\} \xrightarrow{\text{remove}\ 1} \{3\} \xrightarrow[\text{add}\ 1,2]{\text{remove}\ 3} \{1,2\} \xrightarrow{\text{remove}\ 1} \{2\} \)\(The removed values are \)1\(, \)3\( and \)1\( respectively, so her score is \)1\times 3\times 1 = 3\(.In the second test case, the answer is \)1 \times 4 \times 1 \times 2 \times 1 \times 3 = 24$$$.
|
Input: 42 31 33 65 1 42 1002 1005 151 2 3 4 5 | Output: 3 24 118143737 576
|
Beginner
| 6 | 631 | 644 | 78 | 21 |
547 |
B
|
547B
|
B. Mike and Feet
| 1,900 |
binary search; data structures; dp; dsu
|
Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.Mike is a curious to know for each x such that 1 β€ x β€ n the maximum strength among all groups of size x.
|
The first line of input contains integer n (1 β€ n β€ 2 Γ 105), the number of bears.The second line contains n integers separated by space, a1, a2, ..., an (1 β€ ai β€ 109), heights of bears.
|
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
|
Input: 101 2 3 4 5 4 3 2 1 6 | Output: 6 4 4 3 3 2 2 1 1 1
|
Hard
| 4 | 532 | 187 | 108 | 5 |
|
1,196 |
A
|
1196A
|
A. Three Piles of Candies
| 800 |
brute force; constructive algorithms; math
|
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer \(q\) independent queries.Let's see the following example: \([1, 3, 4]\). Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob β then Alice has \(4\) candies, and Bob has \(4\) candies.Another example is \([1, 10, 100]\). Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes \(54\) candies, and Alice takes \(46\) candies. Now Bob has \(55\) candies, and Alice has \(56\) candies, so she has to discard one candy β and after that, she has \(55\) candies too.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β the number of queries. Then \(q\) queries follow.The only line of the query contains three integers \(a, b\) and \(c\) (\(1 \le a, b, c \le 10^{16}\)) β the number of candies in the first, second and third piles correspondingly.
|
Print \(q\) lines. The \(i\)-th line should contain the answer for the \(i\)-th query β the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).
|
Input: 4 1 3 4 1 10 100 10000000000000000 10000000000000000 10000000000000000 23 34 45 | Output: 4 55 15000000000000000 51
|
Beginner
| 3 | 1,498 | 308 | 243 | 11 |
|
1,157 |
C2
|
1157C2
|
C2. Increasing Subsequence (hard version)
| 1,700 |
greedy
|
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence \(a\) consisting of \(n\) integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence \([1, 2, 4, 3, 2]\) the answer is \(4\) (you take \(1\) and the sequence becomes \([2, 4, 3, 2]\), then you take the rightmost element \(2\) and the sequence becomes \([2, 4, 3]\), then you take \(3\) and the sequence becomes \([2, 4]\) and then you take \(4\) and the sequence becomes \([2]\), the obtained increasing sequence is \([1, 2, 3, 4]\)).
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
In the first line of the output print \(k\) β the maximum number of elements in a strictly increasing sequence you can obtain.In the second line print a string \(s\) of length \(k\), where the \(j\)-th character of this string \(s_j\) should be 'L' if you take the leftmost element during the \(j\)-th move and 'R' otherwise. If there are multiple answers, you can print any.
|
The first example is described in the problem statement.
|
Input: 5 1 2 4 3 2 | Output: 4 LRRR
|
Medium
| 1 | 963 | 279 | 375 | 11 |
2,084 |
D
|
2084D
|
D. Arcology On Permafrost
| 1,600 |
binary search; brute force; constructive algorithms; greedy; math
|
You are given three integers \(n\), \(m\), and \(k\), where \(m \cdot k < n\).For a sequence \(b\) consisting of non-negative integers, define \(f(b)\) as follows: You may perform the following operation on \(b\): Let \(l\) denote the current length of \(b\). Choose a positive integer \(1 \leq i \leq l - k + 1\), remove the subarray from index \(i\) to \(i + k - 1\) and concatenate the remaining parts. In other words, replace \(b\) with $$$\([b_1, b_2, \ldots, b_{i - 1}, b_{i + k}, b_{i + k + 1}, \ldots, b_l].\)\( \)f(b)\( is defined as the minimum possible value of \)\operatorname{mex}(b)$$$\(^{\text{β}}\) after performing the above operation at most \(m\) times (possibly zero). You need to construct a sequence \(a\) of length \(n\) consisting of non-negative integers, such that: For all \(1 \le i \le n\), \(0 \le a_i \le 10^9\). Over all such sequences \(a\), \(f(a)\) is maximized. \(^{\text{β}}\)The minimum excluded (MEX) of a collection of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(x\) which does not occur in the collection \(c\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m < n\), \(1 \le k < n\), \(1 \le m \cdot k < n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)).If there are multiple answers, print any of them.
|
In the first test case, it can be shown that \(f(a) = 1\), which is maximized.In the second test case, it can be shown that \(f(a) = 1\), which is maximized. \(f(a) = 1\) since you can perform the following operations: Choose \(i = 3\), remove the subarray from index \(3\) to \(4\) and concatenate the remaining parts. The sequence \(a\) becomes \([0, 1, 0]\). Choose \(i = 1\), remove the subarray from index \(1\) to \(2\) and concatenate the remaining parts. The sequence \(a\) becomes \([0]\). In the third test case, it can be shown that \(f(a) = 2\), which is maximized. \(f(a) = 2\) since you can perform the following operation: Choose \(i = 2\), remove the subarray from index \(2\) to \(5\) and concatenate the remaining parts. The sequence \(a\) becomes \([0, 1]\). In the fourth test case, it can be shown that \(f(a) = 2\), which is maximized.In the fifth test case, it can be shown that \(f(a) = 3\), which is maximized.In the sixth test case, it can be shown that \(f(a) = 2\), which is maximized.
|
Input: 82 1 15 2 26 1 48 2 28 1 511 3 322 6 317 2 2 | Output: 0 0 0 1 0 0 0 0 1 2 2 0 1 0 2 1 0 1 0 8 1 0 1 2 1000000000 1 0 1 2 1 0 0 1 0 2 1 0 2 1 0 0 2 1 0 2 1 0 3 2 1 0 2 1 0 2 1 0 2 1 0 2 1 4 0 2 1 3 4 0 2 1 0 3 4 0 1 2 1 3
|
Medium
| 5 | 1,092 | 425 | 142 | 20 |
967 |
B
|
967B
|
B. Watering System
| 1,000 |
math; sortings
|
Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for \(n\) flowers and so it looks like a pipe with \(n\) holes. Arkady can only use the water that flows from the first hole.Arkady can block some of the holes, and then pour \(A\) liters of water into the pipe. After that, the water will flow out from the non-blocked holes proportionally to their sizes \(s_1, s_2, \ldots, s_n\). In other words, if the sum of sizes of non-blocked holes is \(S\), and the \(i\)-th hole is not blocked, \(\frac{s_i \cdot A}{S}\) liters of water will flow out of it.What is the minimum number of holes Arkady should block to make at least \(B\) liters of water flow out of the first hole?
|
The first line contains three integers \(n\), \(A\), \(B\) (\(1 \le n \le 100\,000\), \(1 \le B \le A \le 10^4\)) β the number of holes, the volume of water Arkady will pour into the system, and the volume he wants to get out of the first hole.The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le 10^4\)) β the sizes of the holes.
|
Print a single integer β the number of holes Arkady should block.
|
In the first example Arkady should block at least one hole. After that, \(\frac{10 \cdot 2}{6} \approx 3.333\) liters of water will flow out of the first hole, and that suits Arkady.In the second example even without blocking any hole, \(\frac{80 \cdot 3}{10} = 24\) liters will flow out of the first hole, that is not less than \(20\).In the third example Arkady has to block all holes except the first to make all water flow out of the first hole.
|
Input: 4 10 32 2 2 2 | Output: 1
|
Beginner
| 2 | 728 | 360 | 65 | 9 |
999 |
F
|
999F
|
F. Cards and Joy
| 2,000 |
dp
|
There are \(n\) players sitting at the card table. Each player has a favorite number. The favorite number of the \(j\)-th player is \(f_j\).There are \(k \cdot n\) cards on the table. Each card contains a single integer: the \(i\)-th card contains number \(c_i\). Also, you are given a sequence \(h_1, h_2, \dots, h_k\). Its meaning will be explained below.The players have to distribute all the cards in such a way that each of them will hold exactly \(k\) cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals \(h_t\) if the player holds \(t\) cards containing his favorite number. If a player gets no cards with his favorite number (i.e., \(t=0\)), his joy level is \(0\).Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence \(h_1, \dots, h_k\) is the same for all the players.
|
The first line of input contains two integers \(n\) and \(k\) (\(1 \le n \le 500, 1 \le k \le 10\)) β the number of players and the number of cards each player will get.The second line contains \(k \cdot n\) integers \(c_1, c_2, \dots, c_{k \cdot n}\) (\(1 \le c_i \le 10^5\)) β the numbers written on the cards.The third line contains \(n\) integers \(f_1, f_2, \dots, f_n\) (\(1 \le f_j \le 10^5\)) β the favorite numbers of the players.The fourth line contains \(k\) integers \(h_1, h_2, \dots, h_k\) (\(1 \le h_t \le 10^5\)), where \(h_t\) is the joy level of a player if he gets exactly \(t\) cards with his favorite number written on them. It is guaranteed that the condition \(h_{t - 1} < h_t\) holds for each \(t \in [2..k]\).
|
Print one integer β the maximum possible total joy levels of the players among all possible card distributions.
|
In the first example, one possible optimal card distribution is the following: Player \(1\) gets cards with numbers \([1, 3, 8]\); Player \(2\) gets cards with numbers \([2, 2, 8]\); Player \(3\) gets cards with numbers \([2, 2, 8]\); Player \(4\) gets cards with numbers \([5, 5, 5]\). Thus, the answer is \(2 + 6 + 6 + 7 = 21\).In the second example, no player can get a card with his favorite number. Thus, the answer is \(0\).
|
Input: 4 31 3 2 8 5 5 8 2 2 8 5 21 2 2 52 6 7 | Output: 21
|
Hard
| 1 | 946 | 734 | 111 | 9 |
394 |
E
|
394E
|
E. Lightbulb for Minister
| 0 |
geometry
|
The Minister for education is coming! Naturally, nobody wants to perform poorly in front of such a honored guest. However, two hours before the arrival it turned out that one of the classes has a malfunctioning lightbulb β for some reason it doesn't get enough energy. The solution was found quickly: all we've got to do is to change the location of the lightbulb so that it got the maximum amount of energy.Everybody knows that the power of the lightbulb equals , where C is some constant value and ri is the Euclidean distance from the bulb to the i-th generator. Consequently, our task is to minimize . Of course, we know the positions of all generators.The bulb should be on the ceiling of the class. The ceiling of the class is in the form of a strictly convex m-gon (the class itself has the form of a right prism with a strictly convex m-gon at the bottom). Help to find the optimum location for the bulb. Assume that all generators are in the plane of the class ceiling. Consider that the plane of the class ceiling has some Cartesian coordinate system introduced.
|
The first line contains integer n (2 β€ n β€ 105) β the number of generators. Each of the next n lines contains a pair of integers xi, yi, representing the coordinates of the i-th generator in the plane of the class ceiling. It's guaranteed that no two generators have the same location.The next line contains integer m (3 β€ m β€ 105) β the number of vertexes in the convex polygon that describes the ceiling of the class. Each of the following m lines contains a pair of integers pi, qi, representing the coordinates of the i-th point of the polygon in the clockwise order. It's guaranteed that the polygon is strictly convex.The absolute value of all the coordinates don't exceed 106.
|
Print a single real number β the minimum value of the sum of squares of distances from the generators to the point of the lightbulb's optimal position. The answer will be considered valid if its absolute or relative error doesn't exceed 10 - 4.
|
We'll define a strictly convex polygon as a convex polygon with the following property: no three vertices of the polygon lie on the same line.
|
Input: 43 23 45 45 243 34 45 34 2 | Output: 8.00000000
|
Beginner
| 1 | 1,072 | 683 | 244 | 3 |
765 |
G
|
765G
|
G. Math, math everywhere
| 3,200 |
brute force; dp; math; meet-in-the-middle; number theory
|
If you have gone that far, you'll probably skip unnecessary legends anyway...You are given a binary string and an integer . Find the number of integers k, 0 β€ k < N, such that for all i = 0, 1, ..., m - 1 Print the answer modulo 109 + 7.
|
In the first line of input there is a string s consisting of 0's and 1's (1 β€ |s| β€ 40).In the next line of input there is an integer n (1 β€ n β€ 5Β·105).Each of the next n lines contains two space-separated integers pi, Ξ±i (1 β€ pi, Ξ±i β€ 109, pi is prime). All pi are distinct.
|
A single integer β the answer to the problem.
|
Input: 122 13 1 | Output: 2
|
Master
| 5 | 237 | 275 | 45 | 7 |
|
1,740 |
A
|
1740A
|
A. Factorise N+M
| 800 |
constructive algorithms; number theory
|
Pak Chanek has a prime number\(^\dagger\) \(n\). Find a prime number \(m\) such that \(n + m\) is not prime.\(^\dagger\) A prime number is a number with exactly \(2\) factors. The first few prime numbers are \(2,3,5,7,11,13,\ldots\). In particular, \(1\) is not a prime number.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The following lines contain the description of each test case.The only line of each test case contains a prime number \(n\) (\(2 \leq n \leq 10^5\)).
|
For each test case, output a line containing a prime number \(m\) (\(2 \leq m \leq 10^5\)) such that \(n + m\) is not prime. It can be proven that under the constraints of the problem, such \(m\) always exists.If there are multiple solutions, you can output any of them.
|
In the first test case, \(m = 2\), which is prime, and \(n + m = 7 + 2 = 9\), which is not prime.In the second test case, \(m = 7\), which is prime, and \(n + m = 2 + 7 = 9\), which is not prime.In the third test case, \(m = 47837\), which is prime, and \(n + m = 75619 + 47837 = 123456\), which is not prime.
|
Input: 37275619 | Output: 2 7 47837
|
Beginner
| 2 | 277 | 283 | 270 | 17 |
1,933 |
C
|
1933C
|
C. Turtle Fingers: Count the Values of k
| 1,100 |
brute force; implementation; math; number theory
|
You are given three positive integers \(a\), \(b\) and \(l\) (\(a,b,l>0\)). It can be shown that there always exists a way to choose non-negative (i.e. \(\ge 0\)) integers \(k\), \(x\), and \(y\) such that \(l = k \cdot a^x \cdot b^y\). Your task is to find the number of distinct possible values of \(k\) across all such ways.
|
The first line contains the integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The following \(t\) lines contain three integers, \(a\), \(b\) and \(l\) (\(2 \le a, b \le 100\), \(1 \le l \le 10^6\)) β description of a test case.
|
Output \(t\) lines, with the \(i\)-th (\(1 \le i \le t\)) line containing an integer, the answer to the \(i\)-th test case.
|
In the first test case, \(a=2, b=5, l=20\). The possible values of \(k\) (and corresponding \(x,y\)) are as follows: Choose \(k = 1, x = 2, y = 1\). Then \(k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l\). Choose \(k = 2, x = 1, y = 1\). Then \(k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l\). Choose \(k = 4, x = 0, y = 1\). Then \(k \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l\). Choose \(k = 5, x = 2, y = 0\). Then \(k \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l\). Choose \(k = 10, x = 1, y = 0\). Then \(k \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l\). Choose \(k = 20, x = 0, y = 0\). Then \(k \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l\). In the second test case, \(a=2, b=5, l=21\). Note that \(l = 21\) is not divisible by either \(a = 2\) or \(b = 5\). Therefore, we can only set \(x = 0, y = 0\), which corresponds to \(k = 21\).In the third test case, \(a=4, b=6, l=48\). The possible values of \(k\) (and corresponding \(x,y\)) are as follows: Choose \(k = 2, x = 1, y = 1\). Then \(k \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l\). Choose \(k = 3, x = 2, y = 0\). Then \(k \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l\). Choose \(k = 8, x = 0, y = 1\). Then \(k \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l\). Choose \(k = 12, x = 1, y = 0\). Then \(k \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l\). Choose \(k = 48, x = 0, y = 0\). Then \(k \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l\).
|
Input: 112 5 202 5 214 6 482 3 723 5 752 2 10243 7 83349100 100 10000007 3 22 6 617 3 632043 | Output: 6 1 5 12 6 11 24 4 1 3 24
|
Easy
| 4 | 327 | 241 | 123 | 19 |
2,064 |
E
|
2064E
|
E. Mycraft Sand Sort
| 2,400 |
combinatorics; data structures; dsu; greedy; math; sortings
|
Steve has a permutation\(^{\text{β}}\) \(p\) and an array \(c\), both of length \(n\). Steve wishes to sort the permutation \(p\). Steve has an infinite supply of coloured sand blocks, and using them he discovered a physics-based way to sort an array of numbers called gravity sort. Namely, to perform gravity sort on \(p\), Steve will do the following: For all \(i\) such that \(1 \le i \le n\), place a sand block of color \(c_i\) in position \((i, j)\) for all \(j\) where \(1 \le j \le p_i\). Here, position \((x, y)\) denotes a cell in the \(x\)-th row from the top and \(y\)-th column from the left. Apply downwards gravity to the array, so that all sand blocks fall as long as they can fall. An example of gravity sort for the third testcase. \(p = [4, 2, 3, 1, 5]\), \(c = [2, 1, 4, 1, 5]\) Alex looks at Steve's sand blocks after performing gravity sort and wonders how many pairs of arrays \((p',c')\) where \(p'\) is a permutation would have resulted in the same layout of sand. Note that the original pair of arrays \((p, c)\) will always be counted.Please calculate this for Alex. As this number could be large, output it modulo \(998\,244\,353\).\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is a \(4\) in the array).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each testcase contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the lengths of the arrays.The second line of each testcase contains \(n\) distinct integers \(p_1,p_2,\ldots,p_n\) (\(1 \le p_i \le n\)) β the elements of \(p\).The following line contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(1 \le c_i \le n\)) β the elements of \(c\).The sum of \(n\) across all testcases does not exceed \(2 \cdot 10^5\).
|
For each testcase, output the number of pairs of arrays \((p', c')\) Steve could have started with, modulo \(998\,244\,353\).
|
The second test case is shown below. Gravity sort of the second testcase. It can be shown that permutations of \(p\) will yield the same result, and that \(c\) must equal \([1,1,1,1,1]\) (since all sand must be the same color), so the answer is \(5! = 120\).The third test case is shown in the statement above. It can be proven that no other arrays \(p\) and \(c\) will produce the same final result, so the answer is \(1\).
|
Input: 411155 3 4 1 21 1 1 1 154 2 3 1 52 1 4 1 54029 15 20 35 37 31 27 1 32 36 38 25 22 8 16 7 3 28 11 12 23 4 14 9 39 13 10 30 6 2 24 17 19 5 34 18 33 26 40 213 1 2 2 1 2 3 1 1 1 1 2 1 3 1 1 3 1 1 1 2 2 1 3 3 3 2 3 2 2 2 2 1 3 2 1 1 2 2 2 | Output: 1 120 1 143654893
|
Expert
| 6 | 1,501 | 526 | 125 | 20 |
70 |
C
|
70C
|
C. Lucky Tickets
| 2,200 |
binary search; data structures; sortings; two pointers
|
In Walrusland public transport tickets are characterized by two integers: by the number of the series and by the number of the ticket in the series. Let the series number be represented by a and the ticket number β by b, then a ticket is described by the ordered pair of numbers (a, b). The walruses believe that a ticket is lucky if a * b = rev(a) * rev(b). The function rev(x) reverses a number written in the decimal system, at that the leading zeroes disappear. For example, rev(12343) = 34321, rev(1200) = 21.The Public Transport Management Committee wants to release x series, each containing y tickets, so that at least w lucky tickets were released and the total number of released tickets (x * y) were minimum. The series are numbered from 1 to x inclusive. The tickets in each series are numbered from 1 to y inclusive. The Transport Committee cannot release more than maxx series and more than maxy tickets in one series.
|
The first line contains three integers maxx, maxy, w (1 β€ maxx, maxy β€ 105, 1 β€ w β€ 107).
|
Print on a single line two space-separated numbers, the x and the y. If there are several possible variants, print any of them. If such x and y do not exist, print a single number - 1.
|
Input: 2 2 1 | Output: 1 1
|
Hard
| 4 | 932 | 89 | 184 | 0 |
|
332 |
B
|
332B
|
B. Maximum Absurdity
| 1,500 |
data structures; dp; implementation
|
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 β€ a β€ b β€ n - k + 1, b - a β₯ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included).As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
|
The first line contains two integers n and k (2 β€ n β€ 2Β·105, 0 < 2k β€ n) β the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn β the absurdity of each law (1 β€ xi β€ 109).
|
Print two integers a, b β the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1] and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
|
In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.
|
Input: 5 23 6 1 1 6 | Output: 1 4
|
Medium
| 3 | 1,109 | 278 | 332 | 3 |
1,918 |
F
|
1918F
|
F. Caterpillar on a Tree
| 2,500 |
dfs and similar; graphs; greedy; implementation; sortings; trees
|
The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root.The tree is represented as a rooted tree with the root at the node \(1\). Each crawl to a neighboring node takes \(1\) minute for the caterpillar. And there is a trampoline under the tree. If the caterpillar detaches from the tree and falls onto the trampoline, it will end up at the root of the tree in \(0\) seconds. But the trampoline is old and can withstand no more than \(k\) caterpillar's falls.What is the minimum time the caterpillar can take to visit all the nodes of the tree?More formally, we need to find the minimum time required to visit all the nodes of the tree, if the caterpillar starts at the root (node \(1\)) and moves using two methods. Crawl along an edge to one of the neighboring nodes: takes \(1\) minute. Teleport to the root: takes no time, no new nodes become visited. The second method (teleportation) can be used at most \(k\) times. The caterpillar can finish the journey at any node.
|
The first line of the input contains two integers: \(n\) (\(2 \le n \le 2\cdot 10^5\)) β the number of nodes in the tree, and \(k\) (\(0 \le k \le 10^9\)) β the maximum number of teleports to the root.The second line contains \(n-1\) integers \(p_2\), \(p_3\), ..., \(p_n\) (\(1 \le p_i \le n\)) β the ancestors in the tree for nodes \(2, 3, \ldots, n\); node \(1\) is the root.
|
Print a single number in a single line β the minimum number of minutes required to visit all the nodes of the tree.
|
The figure shows one of the ways to traverse the tree from the first test in 9 minutes.
|
Input: 8 1 1 1 2 2 4 3 3 | Output: 9
|
Expert
| 6 | 1,011 | 378 | 115 | 19 |
388 |
A
|
388A
|
A. Fox and Box Accumulation
| 1,400 |
greedy; sortings
|
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct?
|
The first line contains an integer n (1 β€ n β€ 100). The next line contains n integers x1, x2, ..., xn (0 β€ xi β€ 100).
|
Output a single integer β the minimal possible number of piles.
|
In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).
|
Input: 30 0 10 | Output: 2
|
Easy
| 2 | 940 | 117 | 63 | 3 |
1,446 |
F
|
1446F
|
F. Line Distance
| 3,200 |
binary search; data structures; geometry
|
You are given an integer \(k\) and \(n\) distinct points with integer coordinates on the Euclidean plane, the \(i\)-th point has coordinates \((x_i, y_i)\).Consider a list of all the \(\frac{n(n - 1)}{2}\) pairs of points \(((x_i, y_i), (x_j, y_j))\) (\(1 \le i < j \le n\)). For every such pair, write out the distance from the line through these two points to the origin \((0, 0)\).Your goal is to calculate the \(k\)-th smallest number among these distances.
|
The first line contains two integers \(n\), \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le \frac{n(n - 1)}{2}\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^4 \le x_i, y_i \le 10^4\)) β the coordinates of the \(i\)-th point. It is guaranteed that all given points are pairwise distinct.
|
You should output one number β the \(k\)-th smallest distance from the origin. Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}\).
|
There are \(6\) pairs of points: Line \(1-2\) : distance \(0\) from the origin Line \(1-3\) : distance \(\frac{\sqrt{2}}{2} \approx 0.707106781\) from the origin Line \(1-4\) : distance \(2\) from the origin Line \(2-3\) : distance \(1\) from the origin Line \(2-4\) : distance \(2\) from the origin Line \(3-4\) : distance \(\frac{2}{\sqrt{29}} \approx 0.371390676\) from the origin The third smallest distance among those is approximately \(0.707106781\).
|
Input: 4 3 2 1 -2 -1 0 -1 -2 4 | Output: 0.707106780737
|
Master
| 3 | 461 | 324 | 329 | 14 |
1,809 |
C
|
1809C
|
C. Sum on Subarrays
| 1,500 |
constructive algorithms; greedy; math
|
For an array \(a = [a_1, a_2, \dots, a_n]\), let's denote its subarray \(a[l, r]\) as the array \([a_l, a_{l+1}, \dots, a_r]\).For example, the array \(a = [1, -3, 1]\) has \(6\) non-empty subarrays: \(a[1,1] = [1]\); \(a[1,2] = [1,-3]\); \(a[1,3] = [1,-3,1]\); \(a[2,2] = [-3]\); \(a[2,3] = [-3,1]\); \(a[3,3] = [1]\). You are given two integers \(n\) and \(k\). Construct an array \(a\) consisting of \(n\) integers such that: all elements of \(a\) are from \(-1000\) to \(1000\); \(a\) has exactly \(k\) subarrays with positive sums; the rest \(\dfrac{(n+1) \cdot n}{2}-k\) subarrays of \(a\) have negative sums.
|
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 30\); \(0 \le k \le \dfrac{(n+1) \cdot n}{2}\)).
|
For each test case, print \(n\) integers β the elements of the array meeting the constraints. It can be shown that the answer always exists. If there are multiple answers, print any of them.
|
Input: 43 22 02 24 6 | Output: 1 -3 1 -13 -42 -13 42 -3 -4 10 -2
|
Medium
| 3 | 615 | 231 | 190 | 18 |
|
404 |
C
|
404C
|
C. Restore Graph
| 1,800 |
dfs and similar; graphs; sortings
|
Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.
|
The first line contains two space-separated integers n and k (1 β€ k < n β€ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.The second line contains space-separated integers d[1], d[2], ..., d[n] (0 β€ d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.
|
If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 β€ m β€ 106) β the number of edges in the found graph.In each of the next m lines print two space-separated integers ai and bi (1 β€ ai, bi β€ n; ai β bi), denoting the edge that connects vertices with numbers ai and bi. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.
|
Input: 3 20 1 1 | Output: 31 21 33 2
|
Medium
| 3 | 676 | 408 | 493 | 4 |
|
1,201 |
A
|
1201A
|
A. Important Exam
| 900 |
implementation; strings
|
A class of students wrote a multiple-choice test.There are \(n\) students in the class. The test had \(m\) questions, each of them had \(5\) possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question \(i\) worth \(a_i\) points. Incorrect answers are graded with zero points.The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.
|
The first line contains integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of students in the class and the number of questions in the test.Each of the next \(n\) lines contains string \(s_i\) (\(|s_i| = m\)), describing an answer of the \(i\)-th student. The \(j\)-th character represents the student answer (A, B, C, D or E) on the \(j\)-th question.The last line contains \(m\) integers \(a_1, a_2, \ldots, a_m\) (\(1 \le a_i \le 1000\)) β the number of points for the correct answer for every question.
|
Print a single integer β the maximum possible total score of the class.
|
In the first example, one of the most optimal test answers is ""ABCD"", this way the total number of points will be \(16\).In the second example, one of the most optimal test answers is ""CCC"", this way each question will be answered by exactly one student and the total number of points is \(5 + 4 + 12 = 21\).
|
Input: 2 4 ABCD ABCE 1 2 3 4 | Output: 16
|
Beginner
| 2 | 563 | 519 | 71 | 12 |
965 |
B
|
965B
|
B. Battleship
| 1,300 |
implementation
|
Arkady is playing Battleship. The rules of this game aren't really important.There is a field of \(n \times n\) cells. There should be exactly one \(k\)-decker on the field, i. e. a ship that is \(k\) cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship.Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)) β the size of the field and the size of the ship.The next \(n\) lines contain the field. Each line contains \(n\) characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship).
|
Output two integers β the row and the column of a cell that belongs to the maximum possible number of different locations of the ship.If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell.
|
The picture below shows the three possible locations of the ship that contain the cell \((3, 2)\) in the first sample.
|
Input: 4 3#..##.#......### | Output: 3 2
|
Easy
| 1 | 534 | 321 | 263 | 9 |
706 |
C
|
706C
|
C. Hard problem
| 1,600 |
dp; strings
|
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
|
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of strings.The second line contains n integers ci (0 β€ ci β€ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string. Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.
|
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
|
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string ""aa"" should go before string ""aaa"", thus the answer is - 1.
|
Input: 21 2baac | Output: 1
|
Medium
| 2 | 1,007 | 405 | 192 | 7 |
1,909 |
B
|
1909B
|
B. Make Almost Equal With Mod
| 1,200 |
bitmasks; constructive algorithms; math; number theory
|
xi - Solar Stormβ You are given an array \(a_1, a_2, \dots, a_n\) of distinct positive integers. You have to do the following operation exactly once: choose a positive integer \(k\); for each \(i\) from \(1\) to \(n\), replace \(a_i\) with \(a_i \text{ mod } k^\dagger\). Find a value of \(k\) such that \(1 \leq k \leq 10^{18}\) and the array \(a_1, a_2, \dots, a_n\) contains exactly \(2\) distinct values at the end of the operation. It can be shown that, under the constraints of the problem, at least one such \(k\) always exists. If there are multiple solutions, you can print any of them.\(^\dagger\) \(a \text{ mod } b\) denotes the remainder after dividing \(a\) by \(b\). For example: \(7 \text{ mod } 3=1\) since \(7 = 3 \cdot 2 + 1\) \(15 \text{ mod } 4=3\) since \(15 = 4 \cdot 3 + 3\) \(21 \text{ mod } 1=0\) since \(21 = 21 \cdot 1 + 0\)
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^{17}\)) β the initial state of the array. It is guaranteed that all the \(a_i\) are distinct.Note that there are no constraints on the sum of \(n\) over all test cases.
|
For each test case, output a single integer: a value of \(k\) (\(1 \leq k \leq 10^{18}\)) such that the array \(a_1, a_2, \dots, a_n\) contains exactly \(2\) distinct values at the end of the operation.
|
In the first test case, you can choose \(k = 7\). The array becomes \([8 \text{ mod } 7, 15 \text{ mod } 7, 22 \text{ mod } 7, 30 \text{ mod } 7] = [1, 1, 1, 2]\), which contains exactly \(2\) distinct values (\(\{1, 2\}\)).In the second test case, you can choose \(k = 30\). The array becomes \([0, 0, 8, 0, 8]\), which contains exactly \(2\) distinct values (\(\{0, 8\}\)). Note that choosing \(k = 10\) would also be a valid solution.In the last test case, you can choose \(k = 10^{18}\). The array becomes \([2, 1]\), which contains exactly \(2\) distinct values (\(\{1, 2\}\)). Note that choosing \(k = 10^{18} + 1\) would not be valid, because \(1 \leq k \leq 10^{18}\) must be true.
|
Input: 548 15 22 30560 90 98 120 3086328 769 541 986 215 73451000 2000 7000 11000 1600022 1 | Output: 7 30 3 5000 1000000000000000000
|
Easy
| 4 | 851 | 551 | 202 | 19 |
507 |
E
|
507E
|
E. Breaking Good
| 2,100 |
dfs and similar; dp; graphs; shortest paths
|
Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n cities with m bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads. The roads aren't all working. There are some roads which need some more work to be performed to be completely functioning.The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city n. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan.First of all the path which they are going to use on their way back from city 1 to their headquarters n must be as short as possible, since it is important to finish operation as fast as possible.Then, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional. If the chosen path has some roads that doesn't work they'll have to repair those roads before the operation.Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).Can you help Walter complete his task and gain the gang's trust?
|
The first line of input contains two integers n, m (2 β€ n β€ 105, ), the number of cities and number of roads respectively.In following m lines there are descriptions of roads. Each description consists of three integers x, y, z (1 β€ x, y β€ n, ) meaning that there is a road connecting cities number x and y. If z = 1, this road is working, otherwise it is not.
|
In the first line output one integer k, the minimum possible number of roads affected by gang.In the following k lines output three integers describing roads that should be affected. Each line should contain three integers x, y, z (1 β€ x, y β€ n, ), cities connected by a road and the new state of a road. z = 1 indicates that the road between cities x and y should be repaired and z = 0 means that road should be blown up. You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it's original state should be different from z.After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and n.If there are multiple optimal answers output any.
|
In the first test the only path is 1 - 2In the second test the only shortest path is 1 - 3 - 4In the third test there are multiple shortest paths but the optimal is 1 - 4 - 6 - 8
|
Input: 2 11 2 0 | Output: 11 2 1
|
Hard
| 4 | 1,868 | 360 | 838 | 5 |
1,505 |
A
|
1505A
|
A. Is it rated - 2
| 900 |
*special; implementation; interactive
|
InteractionThis is an interactive problem. You need to read participants' queries from standard input and print your responses to standard output. You don't know the number of queries upfront, so you'll need to process them as you get them; you'll know you're done once you reach the end of the file.In each query, you will be asked the question, written in one line. You have to answer it correctly, patiently and without any display of emotions. Your response is case-insensitive.Please make sure to use the stream flushing operation after each response in order not to leave part of your output in some buffer.
|
Input: Is it rated? Is it rated? Is it rated? | Output: NO NO NO
|
Beginner
| 3 | 613 | 0 | 0 | 15 |
|||
1,728 |
F
|
1728F
|
F. Fishermen
| 3,100 |
flows; graph matchings; greedy
|
There are \(n\) fishermen who have just returned from a fishing trip. The \(i\)-th fisherman has caught a fish of size \(a_i\).The fishermen will choose some order in which they are going to tell the size of the fish they caught (the order is just a permutation of size \(n\)). However, they are not entirely honest, and they may ""increase"" the size of the fish they have caught.Formally, suppose the chosen order of the fishermen is \([p_1, p_2, p_3, \dots, p_n]\). Let \(b_i\) be the value which the \(i\)-th fisherman in the order will tell to the other fishermen. The values \(b_i\) are chosen as follows: the first fisherman in the order just honestly tells the actual size of the fish he has caught, so \(b_1 = a_{p_1}\); every other fisherman wants to tell a value that is strictly greater than the value told by the previous fisherman, and is divisible by the size of the fish that the fisherman has caught. So, for \(i > 1\), \(b_i\) is the smallest integer that is both strictly greater than \(b_{i-1}\) and divisible by \(a_{p_i}\). For example, let \(n = 7\), \(a = [1, 8, 2, 3, 2, 2, 3]\). If the chosen order is \(p = [1, 6, 7, 5, 3, 2, 4]\), then: \(b_1 = a_{p_1} = 1\); \(b_2\) is the smallest integer divisible by \(2\) and greater than \(1\), which is \(2\); \(b_3\) is the smallest integer divisible by \(3\) and greater than \(2\), which is \(3\); \(b_4\) is the smallest integer divisible by \(2\) and greater than \(3\), which is \(4\); \(b_5\) is the smallest integer divisible by \(2\) and greater than \(4\), which is \(6\); \(b_6\) is the smallest integer divisible by \(8\) and greater than \(6\), which is \(8\); \(b_7\) is the smallest integer divisible by \(3\) and greater than \(8\), which is \(9\). You have to choose the order of fishermen in a way that yields the minimum possible \(\sum\limits_{i=1}^{n} b_i\).
|
The first line contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of fishermen.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)).
|
Print one integer β the minimum possible value of \(\sum\limits_{i=1}^{n} b_i\) you can obtain by choosing the order of fishermen optimally.
|
Input: 7 1 8 2 3 2 2 3 | Output: 33
|
Master
| 3 | 1,848 | 181 | 140 | 17 |
|
1,155 |
B
|
1155B
|
B. Game with Telephone Numbers
| 1,200 |
games; greedy; implementation
|
A telephone number is a sequence of exactly \(11\) digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string \(s\) of length \(n\) (\(n\) is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string \(s\) becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).
|
The first line contains one integer \(n\) (\(13 \le n < 10^5\), \(n\) is odd) β the length of string \(s\).The second line contains the string \(s\) (\(|s| = n\)) consisting only of decimal digits.
|
If Vasya has a strategy that guarantees him victory, print YES.Otherwise print NO.
|
In the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.In the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, because there is no digit 8 at all.
|
Input: 13 8380011223344 | Output: YES
|
Easy
| 3 | 735 | 197 | 82 | 11 |
1,500 |
A
|
1500A
|
A. Going Home
| 1,800 |
brute force; hashing; implementation; math
|
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array \(a\). Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices \(x, y, z, w\) such that \(a_x + a_y = a_z + a_w\).Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery?
|
The first line contains the single integer \(n\) (\(4 \leq n \leq 200\,000\)) β the size of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2.5 \cdot 10^6\)).
|
Print ""YES"" if there are such four indices, and ""NO"" otherwise.If such indices exist, print these indices \(x\), \(y\), \(z\) and \(w\) (\(1 \le x, y, z, w \le n\)).If there are multiple answers, print any of them.
|
In the first example \(a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6\). Note that there are other answer, for example, 2 3 4 6.In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that \(a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3\)
|
Input: 6 2 1 5 2 7 4 | Output: YES 2 3 1 6
|
Medium
| 4 | 565 | 205 | 218 | 15 |
1,921 |
G
|
1921G
|
G. Mischievous Shooter
| 2,200 |
brute force; data structures; divide and conquer; dp; implementation
|
Once the mischievous and wayward shooter named Shel found himself on a rectangular field of size \(n \times m\), divided into unit squares. Each cell either contains a target or not.Shel only had a lucky shotgun with him, with which he can shoot in one of the four directions: right-down, left-down, left-up, or right-up. When fired, the shotgun hits all targets in the chosen direction, the Manhattan distance to which does not exceed a fixed constant \(k\). The Manhattan distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) is equal to \(|x_1 - x_2| + |y_1 - y_2|\). Possible hit areas for \(k = 3\). Shel's goal is to hit as many targets as possible. Please help him find this value.
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains field dimensions \(n\), \(m\), and the constant for the shotgun's power \(k\) (\(1 \le n, m, k \le 10^5, 1 \le n \cdot m \le 10^5\)).Each of the next \(n\) lines contains \(m\) characters β the description of the next field row, where the character '.' means the cell is empty, and the character '#' indicates the presence of a target. It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single integer on a separate line, which is equal to the maximum possible number of hit targets with one shot.
|
Possible optimal shots for the examples in the statement:
|
Input: 43 3 1.#.###.#.2 5 3###.....##4 4 2..#####.#..#####2 1 3## | Output: 3 4 5 2
|
Hard
| 5 | 696 | 657 | 139 | 19 |
1,729 |
D
|
1729D
|
D. Friends and the Restaurant
| 1,200 |
greedy; sortings; two pointers
|
A group of \(n\) friends decide to go to a restaurant. Each of the friends plans to order meals for \(x_i\) burles and has a total of \(y_i\) burles (\(1 \le i \le n\)). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all \(x_i\) values in the group must not exceed the sum of \(y_i\) values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be \(n = 6\) friends for whom \(x\) = [\(8, 3, 9, 2, 4, 5\)] and \(y\) = [\(5, 3, 1, 4, 5, 10\)]. Then: first and sixth friends can go to the restaurant on the first day. They will spend \(8+5=13\) burles at the restaurant, and their total budget is \(5+10=15\) burles. Since \(15 \ge 13\), they can actually form a group. friends with indices \(2, 4, 5\) can form a second group. They will spend \(3+2+4=9\) burles at the restaurant, and their total budget will be \(3+4+5=12\) burles (\(12 \ge 9\)). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is \(2\). Friends will visit the restaurant for a maximum of two days. Note that the \(3\)-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given \(n\), \(x\) and \(y\).
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the number of friends.The second line of each test case contains exactly \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \le x_i \le 10^9\)). The value of \(x_i\) corresponds to the number of burles that the friend numbered \(i\) plans to spend at the restaurant.The third line of each test case contains exactly \(n\) integers \(y_1, y_2, \dots, y_n\) (\(1 \le y_i \le 10^9\)). The value \(y_i\) corresponds to the number of burles that the friend numbered \(i\) has.It is guaranteed that the sum of \(n\) values over all test cases does not exceed \(10^5\).
|
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
|
The first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends (\(1+3+10 \ge 2+3+7\)). Note that they do not have the option of splitting into two groups.
|
Input: 668 3 9 2 4 55 3 1 4 5 1041 2 3 41 1 2 232 3 71 3 1062 3 6 9 5 73 2 7 10 6 1065 4 2 1 8 1001 1 1 1 1 20061 4 1 2 4 21 3 3 2 3 4 | Output: 2 0 1 3 1 3
|
Easy
| 3 | 1,753 | 804 | 149 | 17 |
1,428 |
A
|
1428A
|
A. Box is Pull
| 800 |
math
|
Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point \((x_1,y_1)\) to the point \((x_2,y_2)\).He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly \(1\) unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by \(1\) unit. For example, if the box is at the point \((1,2)\) and Wabbit is standing at the point \((2,2)\), he can pull the box right by \(1\) unit, with the box ending up at the point \((2,2)\) and Wabbit ending at the point \((3,2)\).Also, Wabbit can move \(1\) unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly \(1\) unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.Wabbit can start at any point. It takes \(1\) second to travel \(1\) unit right, left, up, or down, regardless of whether he pulls the box while moving.Determine the minimum amount of time he needs to move the box from \((x_1,y_1)\) to \((x_2,y_2)\). Note that the point where Wabbit ends up at does not matter.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 1000)\): the number of test cases. The description of the test cases follows.Each of the next \(t\) lines contains four space-separated integers \(x_1, y_1, x_2, y_2\) \((1 \leq x_1, y_1, x_2, y_2 \leq 10^9)\), describing the next test case.
|
For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from \((x_1,y_1)\) to \((x_2,y_2)\).
|
In the first test case, the starting and the ending points of the box are \((1,2)\) and \((2,2)\) respectively. This is the same as the picture in the statement. Wabbit needs only \(1\) second to move as shown in the picture in the statement.In the second test case, Wabbit can start at the point \((2,1)\). He pulls the box to \((2,1)\) while moving to \((3,1)\). He then moves to \((3,2)\) and then to \((2,2)\) without pulling the box. Then, he pulls the box to \((2,2)\) while moving to \((2,3)\). It takes \(4\) seconds.
|
Input: 2 1 2 2 2 1 1 2 2 | Output: 1 4
|
Beginner
| 1 | 1,289 | 345 | 138 | 14 |
935 |
F
|
935F
|
F. Fafa and Array
| 2,600 |
data structures; greedy
|
Fafa has an array A of n positive integers, the function f(A) is defined as . He wants to do q queries of two types: 1 l r x β find the maximum possible value of f(A), if x is to be added to one element in the range [l, r]. You can choose to which element to add x. 2 l r x β increase all the elements in the range [l, r] by value x. Note that queries of type 1 don't affect the array elements.
|
The first line contains one integer n (3 β€ n β€ 105) β the length of the array.The second line contains n positive integers a1, a2, ..., an (0 < ai β€ 109) β the array elements.The third line contains an integer q (1 β€ q β€ 105) β the number of queries. Then q lines follow, line i describes the i-th query and contains four integers ti li ri xi . It is guaranteed that at least one of the queries is of type 1.
|
For each query of type 1, print the answer to the query.
|
Input: 51 1 1 1 151 2 4 12 2 3 12 4 4 22 3 4 11 3 3 2 | Output: 28
|
Expert
| 2 | 394 | 408 | 56 | 9 |
|
342 |
A
|
342A
|
A. Xenia and Divisors
| 1,200 |
greedy; implementation
|
Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: a < b < c; a divides b, b divides c. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three.Help Xenia, find the required partition or else say that it doesn't exist.
|
The first line contains integer n (3 β€ n β€ 99999) β the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.It is guaranteed that n is divisible by 3.
|
If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.If there is no solution, print -1.
|
Input: 61 1 1 2 2 2 | Output: -1
|
Easy
| 2 | 519 | 204 | 310 | 3 |
|
746 |
A
|
746A
|
A. Compote
| 800 |
implementation; math
|
Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1: 2: 4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits β lemons, apples and pears β should be put in the compote as whole fruits.Your task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0.
|
The first line contains the positive integer a (1 β€ a β€ 1000) β the number of lemons Nikolay has. The second line contains the positive integer b (1 β€ b β€ 1000) β the number of apples Nikolay has. The third line contains the positive integer c (1 β€ c β€ 1000) β the number of pears Nikolay has.
|
Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote.
|
In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1 + 2 + 4 = 7.In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21.In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0.
|
Input: 257 | Output: 7
|
Beginner
| 2 | 575 | 293 | 99 | 7 |
203 |
E
|
203E
|
E. Transportation
| 2,300 |
greedy; sortings; two pointers
|
Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment.The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other robots. More precisely, for the i-th robot we know value ci β the number of robots it can carry. In this case, each of ci transported robots can additionally carry other robots.However, the robots need to be filled with fuel to go, so Valera spent all his last money and bought S liters of fuel. He learned that each robot has a restriction on travel distances. Thus, in addition to features ci, the i-th robot has two features fi and li β the amount of fuel (in liters) needed to move the i-th robot, and the maximum distance that the robot can go.Due to the limited amount of time and fuel, Valera wants to move the maximum number of robots to the luggage compartment. He operates as follows. First Valera selects some robots that will travel to the luggage compartment on their own. In this case the total amount of fuel required to move all these robots must not exceed S. Then Valera seats the robots into the compartments, so as to transport as many robots as possible. Note that if a robot doesn't move by itself, you can put it in another not moving robot that is moved directly or indirectly by a moving robot. After that all selected and seated robots along with Valera go to the luggage compartment and the rest robots will be lost. There are d meters to the luggage compartment. Therefore, the robots that will carry the rest, must have feature li of not less than d. During the moving Valera cannot stop or change the location of the robots in any way.Help Valera calculate the maximum number of robots that he will be able to take home, and the minimum amount of fuel he will have to spend, because the remaining fuel will come in handy in Valera's research.
|
The first line contains three space-separated integers n, d, S (1 β€ n β€ 105, 1 β€ d, S β€ 109). The first number represents the number of robots, the second one β the distance to the luggage compartment and the third one β the amount of available fuel.Next n lines specify the robots. The i-th line contains three space-separated integers ci, fi, li (0 β€ ci, fi, li β€ 109) β the i-th robot's features. The first number is the number of robots the i-th robot can carry, the second number is the amount of fuel needed for the i-th robot to move and the third one shows the maximum distance the i-th robot can go.
|
Print two space-separated integers β the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes.
|
Input: 3 10 100 12 101 6 100 1 1 | Output: 2 6
|
Expert
| 3 | 1,988 | 608 | 254 | 2 |
|
1,977 |
B
|
1977B
|
B. Binary Colouring
| 1,100 |
bitmasks; constructive algorithms; greedy; math
|
You are given a positive integer \(x\). Find any array of integers \(a_0, a_1, \ldots, a_{n-1}\) for which the following holds: \(1 \le n \le 32\), \(a_i\) is \(1\), \(0\), or \(-1\) for all \(0 \le i \le n - 1\), \(x = \displaystyle{\sum_{i=0}^{n - 1}{a_i \cdot 2^i}}\), There does not exist an index \(0 \le i \le n - 2\) such that both \(a_{i} \neq 0\) and \(a_{i + 1} \neq 0\). It can be proven that under the constraints of the problem, a valid array always exists.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single positive integer \(x\) (\(1 \le x < 2^{30}\)).
|
For each test case, output two lines.On the first line, output an integer \(n\) (\(1 \le n \le 32\)) β the length of the array \(a_0, a_1, \ldots, a_{n-1}\).On the second line, output the array \(a_0, a_1, \ldots, a_{n-1}\).If there are multiple valid arrays, you can output any of them.
|
In the first test case, one valid array is \([1]\), since \((1) \cdot 2^0 = 1\).In the second test case, one possible valid array is \([0,-1,0,0,1]\), since \((0) \cdot 2^0 + (-1) \cdot 2^1 + (0) \cdot 2^2 + (0) \cdot 2^3 + (1) \cdot 2^4 = -2 + 16 = 14\).
|
Input: 71142415271119 | Output: 1 1 5 0 -1 0 0 1 6 0 0 0 -1 0 1 5 -1 0 0 0 1 6 -1 0 -1 0 0 1 5 -1 0 -1 0 1 5 -1 0 1 0 1
|
Easy
| 4 | 470 | 285 | 287 | 19 |
1,867 |
B
|
1867B
|
B. XOR Palindromes
| 1,100 |
bitmasks; constructive algorithms; strings
|
You are given a binary string \(s\) of length \(n\) (a string that consists only of \(0\) and \(1\)). A number \(x\) is good if there exists a binary string \(l\) of length \(n\), containing \(x\) ones, such that if each symbol \(s_i\) is replaced by \(s_i \oplus l_i\) (where \(\oplus\) denotes the bitwise XOR operation), then the string \(s\) becomes a palindrome.You need to output a binary string \(t\) of length \(n+1\), where \(t_i\) (\(0 \leq i \leq n\)) is equal to \(1\) if number \(i\) is good, and \(0\) otherwise.A palindrome is a string that reads the same from left to right as from right to left. For example, 01010, 1111, 0110 are palindromes.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains a binary string \(s\) of length \(n\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single line containing a binary string \(t\) of length \(n+1\) - the answer to the problem.
|
Consider the first example. \(t_2 = 1\) because we can choose \(l = \) 010100, then the string \(s\) becomes 111111, which is a palindrome. \(t_4 = 1\) because we can choose \(l = \) 101011. It can be shown that for all other \(i\), there is no answer, so the remaining symbols are \(0\).
|
Input: 561010115000009100100011310011 | Output: 0010100 111111 0011111100 0110 11
|
Easy
| 3 | 660 | 414 | 120 | 18 |
979 |
E
|
979E
|
E. Kuro and Topological Parity
| 2,400 |
dp
|
Kuro has recently won the ""Most intelligent cat ever"" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games.Kuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot of arrows (you can assume that the number of arrows is infinite). Immediately, Katie came up with the game called Topological Parity.The paper is divided into \(n\) pieces enumerated from \(1\) to \(n\). Shiro has painted some pieces with some color. Specifically, the \(i\)-th piece has color \(c_{i}\) where \(c_{i} = 0\) defines black color, \(c_{i} = 1\) defines white color and \(c_{i} = -1\) means that the piece hasn't been colored yet.The rules of the game is simple. Players must put some arrows between some pairs of different pieces in such a way that for each arrow, the number in the piece it starts from is less than the number of the piece it ends at. Also, two different pieces can only be connected by at most one arrow. After that the players must choose the color (\(0\) or \(1\)) for each of the unpainted pieces. The score of a valid way of putting the arrows and coloring pieces is defined as the number of paths of pieces of alternating colors. For example, \([1 \to 0 \to 1 \to 0]\), \([0 \to 1 \to 0 \to 1]\), \([1]\), \([0]\) are valid paths and will be counted. You can only travel from piece \(x\) to piece \(y\) if and only if there is an arrow from \(x\) to \(y\).But Kuro is not fun yet. He loves parity. Let's call his favorite parity \(p\) where \(p = 0\) stands for ""even"" and \(p = 1\) stands for ""odd"". He wants to put the arrows and choose colors in such a way that the score has the parity of \(p\).It seems like there will be so many ways which satisfy Kuro. He wants to count the number of them but this could be a very large number. Let's help him with his problem, but print it modulo \(10^{9} + 7\).
|
The first line contains two integers \(n\) and \(p\) (\(1 \leq n \leq 50\), \(0 \leq p \leq 1\)) β the number of pieces and Kuro's wanted parity.The second line contains \(n\) integers \(c_{1}, c_{2}, ..., c_{n}\) (\(-1 \leq c_{i} \leq 1\)) β the colors of the pieces.
|
Print a single integer β the number of ways to put the arrows and choose colors so the number of valid paths of alternating colors has the parity of \(p\).
|
In the first example, there are \(6\) ways to color the pieces and add the arrows, as are shown in the figure below. The scores are \(3, 3, 5\) for the first row and \(5, 3, 3\) for the second row, both from left to right.
|
Input: 3 1-1 0 1 | Output: 6
|
Expert
| 1 | 1,965 | 268 | 155 | 9 |
282 |
C
|
282C
|
C. XOR and OR
| 1,500 |
constructive algorithms; implementation; math
|
The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.A Bitlandish string is a string made only of characters ""0"" and ""1"".BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p = x xor y, q = x or y. Then he replaces one of the two taken characters by p and the other one by q.The xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.You've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.
|
The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths.It is guaranteed that the given strings only consist of characters ""0"" and ""1"". The strings are not empty, their length doesn't exceed 106.
|
Print ""YES"" if a can be transformed into b, otherwise print ""NO"". Please do not print the quotes.
|
Input: 1110 | Output: YES
|
Medium
| 3 | 1,069 | 273 | 101 | 2 |
|
1,819 |
F
|
1819F
|
F. Willy-nilly, Crack, Into Release!
| 3,500 |
data structures; dp
|
You have long dreamed of working in a large IT company and finally got a job there. You have studied all existing modern technologies for a long time and are ready to apply all your knowledge in practice. But then you sit down at your desk and see a sheet of paper with the company's motto printed in large letters: abcdabcdabcdabcd....The company's motto contains four main principlesβ a (Willi), b (Nilli), c (Crack), d (Release). Therefore, you consider strings of length \(n\) consisting of these four Latin letters. Unordered pairs of letters ""ab"", ""bc"", ""cd"", and ""da"" in this motto are adjacent, so we will call such pairs of symbols good. So, if you are given a string \(s\) of length \(n\), and it is known that the unordered pair of symbols \(\{ x, y \}\) is good, then you can perform one of the following operations on the string: if \(s_n = x\), then you are allowed to replace this symbol with \(y\), if there exists \(1 \le i < n\) such that \(s_i = x\) and \(s_{i+1} = \ldots = s_n = y\), then you are allowed to replace the \(i\)-th symbol of the string with \(y\), and all subsequent symbols with \(x\). For example, the string bacdd can be replaced with one of the strings bacda, bacdc, or badcc, and the string aac can be replaced with aab or aad.A non-empty sequence of operations for the string \(s\) will be called correct if the following two conditions are met: after performing all operations, the string becomes \(s\) again, no string, except for \(s\), will occur more than once during the operations. At the same time, the string \(s\) can occur exactly twice - before the start of the operations and after performing all operations. Now we are ready to move on to the problem statement! You have a set of strings that is initially empty. Then, each of \(q\) queries adds another string \(t_i\) to the set, or removes the string \(t_i\) from the set. After each query, you need to output the minimum and maximum size of a correct sequence of operations in which each word occurs at least once. The choice of the initial string \(s\) is up to you.
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 20\), \(1 \le q \le 100\,000\)) β the length of the strings under consideration and the number of queries to modify the set of strings.Each of the next \(q\) lines contains a string \(t_i\) (\(\lvert t_i \rvert = n\)). All strings consist of characters ""a"", ""b"", ""c"" and ""d"". If the string \(t_i\) was not in the set before the query, it is added to the set, otherwise it is removed from the set.
|
For each of the \(q\) queries, output two integers: the minimum and maximum size of a correct sequence of operations in which each word from the set appears at least once.If there is no sequence of operations that satisfies the condition of the problem, output a single number \(-1\).
|
Let's consider the first test example. After the first query, the set of important words is equal to \(\{\)aa\(\}\), the minimum sequence of actions has the following form: aa, ab, aa. The maximum sequence of actions that fits is aa, ab, ba, bb, bc, cb, cc, cd, dc, dd, da, ad, aa. After the second query, the set of important words is equal to \(\{\)aa, ac\(\}\). The minimum and maximum sequences of actions are: aa, ab, ac, ad, aa. After the third query, the set of important words is equal to \(\{\)aa, ac, dd\(\}\). There is no sequence of actions that fits the condition, so \(-1\) should be outputted. After the fourth query, the set of important words is equal to \(\{\)aa, dd\(\}\). The minimum and maximum sequences of actions are as follows: aa, ab, ba, bb, bc, cb, cc, cd, dc, dd, da, ad, aa.
|
Input: 2 4 aa ac dd ac | Output: 2 12 4 4 -1 12 12
|
Master
| 2 | 2,083 | 472 | 284 | 18 |
1,165 |
A
|
1165A
|
A. Remainder
| 1,100 |
implementation; math
|
You are given a huge decimal number consisting of \(n\) digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.You are also given two integers \(0 \le y < x < n\). Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder \(10^y\) modulo \(10^x\). In other words, the obtained number should have remainder \(10^y\) when divided by \(10^x\).
|
The first line of the input contains three integers \(n, x, y\) (\(0 \le y < x < n \le 2 \cdot 10^5\)) β the length of the number and the integers \(x\) and \(y\), respectively.The second line of the input contains one decimal number consisting of \(n\) digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.
|
Print one integer β the minimum number of operations you should perform to obtain the number having remainder \(10^y\) modulo \(10^x\). In other words, the obtained number should have remainder \(10^y\) when divided by \(10^x\).
|
In the first example the number will be \(11010100100\) after performing one operation. It has remainder \(100\) modulo \(100000\).In the second example the number will be \(11010100010\) after performing three operations. It has remainder \(10\) modulo \(100000\).
|
Input: 11 5 2 11010100101 | Output: 1
|
Easy
| 2 | 754 | 363 | 228 | 11 |
1,089 |
L
|
1089L
|
L. Lazyland
| 900 |
The kingdom of Lazyland is the home to \(n\) idlers. These idlers are incredibly lazy and create many problems to their ruler, the mighty King of Lazyland. Today \(k\) important jobs for the kingdom (\(k \le n\)) should be performed. Every job should be done by one person and every person can do at most one job. The King allowed every idler to choose one job they wanted to do and the \(i\)-th idler has chosen the job \(a_i\). Unfortunately, some jobs may not be chosen by anyone, so the King has to persuade some idlers to choose another job. The King knows that it takes \(b_i\) minutes to persuade the \(i\)-th idler. He asked his minister of labour to calculate the minimum total time he needs to spend persuading the idlers to get all the jobs done. Can you help him?
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) β the number of idlers and the number of jobs.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le k\)) β the jobs chosen by each idler.The third line of the input contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10^9\)) β the time the King needs to spend to persuade the \(i\)-th idler.
|
The only line of the output should contain one number β the minimum total time the King needs to spend persuading the idlers to get all the jobs done.
|
In the first example the optimal plan is to persuade idlers 1, 6, and 8 to do jobs 2, 4, and 6.In the second example each job was chosen by some idler, so there is no need to persuade anyone.
|
Input: 8 7 1 1 3 1 5 3 7 1 5 7 4 8 1 3 5 2 | Output: 10
|
Beginner
| 0 | 775 | 443 | 150 | 10 |
|
1,475 |
F
|
1475F
|
F. Unusual Matrix
| 1,900 |
2-sat; brute force; constructive algorithms
|
You are given two binary square matrices \(a\) and \(b\) of size \(n \times n\). A matrix is called binary if each of its elements is equal to \(0\) or \(1\). You can do the following operations on the matrix \(a\) arbitrary number of times (0 or more): vertical xor. You choose the number \(j\) (\(1 \le j \le n\)) and for all \(i\) (\(1 \le i \le n\)) do the following: \(a_{i, j} := a_{i, j} \oplus 1\) (\(\oplus\) β is the operation xor (exclusive or)). horizontal xor. You choose the number \(i\) (\(1 \le i \le n\)) and for all \(j\) (\(1 \le j \le n\)) do the following: \(a_{i, j} := a_{i, j} \oplus 1\). Note that the elements of the \(a\) matrix change after each operation.For example, if \(n=3\) and the matrix \(a\) is: $$$\( \begin{pmatrix} 1 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 1 & 0 \end{pmatrix} \)\( Then the following sequence of operations shows an example of transformations: vertical xor, \)j=1\(. \)\( a= \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix} \)\( horizontal xor, \)i=2\(. \)\( a= \begin{pmatrix} 0 & 1 & 0 \\ 0 & 1 & 0 \\ 0 & 1 & 0 \end{pmatrix} \)\( vertical xor, \)j=2\(. \)\( a= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix} \)\( Check if there is a sequence of operations such that the matrix \)a\( becomes equal to the matrix \)b$$$.
|
The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 1000\)) β the size of the matrices.The following \(n\) lines contain strings of length \(n\), consisting of the characters '0' and '1' β the description of the matrix \(a\).An empty line follows.The following \(n\) lines contain strings of length \(n\), consisting of the characters '0' and '1' β the description of the matrix \(b\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output on a separate line: ""YES"", there is such a sequence of operations that the matrix \(a\) becomes equal to the matrix \(b\); ""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).
|
The first test case is explained in the statements.In the second test case, the following sequence of operations is suitable: horizontal xor, \(i=1\); horizontal xor, \(i=2\); horizontal xor, \(i=3\); It can be proved that there is no sequence of operations in the third test case so that the matrix \(a\) becomes equal to the matrix \(b\).
|
Input: 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 | Output: YES YES NO
|
Hard
| 3 | 1,302 | 618 | 296 | 14 |
174 |
A
|
174A
|
A. Problem About Equation
| 1,100 |
math
|
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously: there were b milliliters poured in total. That is, the bottle need to be emptied; after the process is over, the volumes of the drink in the mugs should be equal.
|
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
|
Print a single number ""-1"" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
|
Input: 5 501 2 3 4 5 | Output: 12.00000011.00000010.0000009.0000008.000000
|
Easy
| 1 | 924 | 316 | 533 | 1 |
|
362 |
D
|
362D
|
D. Fools and Foolproof Roads
| 2,100 |
data structures; dfs and similar; dsu; graphs; greedy
|
You must have heard all about the Foolland on your Geography lessons. Specifically, you must know that federal structure of this country has been the same for many centuries. The country consists of n cities, some pairs of cities are connected by bidirectional roads, each road is described by its length li.The fools lived in their land joyfully, but a recent revolution changed the king. Now the king is Vasily the Bear. Vasily divided the country cities into regions, so that any two cities of the same region have a path along the roads between them and any two cities of different regions don't have such path. Then Vasily decided to upgrade the road network and construct exactly p new roads in the country. Constructing a road goes like this: We choose a pair of distinct cities u, v that will be connected by a new road (at that, it is possible that there already is a road between these cities). We define the length of the new road: if cities u, v belong to distinct regions, then the length is calculated as min(109, S + 1) (S β the total length of all roads that exist in the linked regions), otherwise we assume that the length equals 1000. We build a road of the specified length between the chosen cities. If the new road connects two distinct regions, after construction of the road these regions are combined into one new region. Vasily wants the road constructing process to result in the country that consists exactly of q regions. Your task is to come up with such road constructing plan for Vasily that it meets the requirement and minimizes the total length of the built roads.
|
The first line contains four integers n (1 β€ n β€ 105), m (0 β€ m β€ 105), p (0 β€ p β€ 105), q (1 β€ q β€ n) β the number of cities in the Foolland, the number of existing roads, the number of roads that are planned to construct and the required number of regions.Next m lines describe the roads that exist by the moment upgrading of the roads begun. Each of these lines contains three integers xi, yi, li: xi, yi β the numbers of the cities connected by this road (1 β€ xi, yi β€ n, xi β yi), li β length of the road (1 β€ li β€ 109). Note that one pair of cities can be connected with multiple roads.
|
If constructing the roads in the required way is impossible, print a single string ""NO"" (without the quotes). Otherwise, in the first line print word ""YES"" (without the quotes), and in the next p lines print the road construction plan. Each line of the plan must consist of two distinct integers, giving the numbers of the cities connected by a road. The road must occur in the plan in the order they need to be constructed. If there are multiple optimal solutions, you can print any of them.
|
Consider the first sample. Before the reform the Foolland consists of four regions. The first region includes cities 1, 2, 3, the second region has cities 4 and 6, the third region has cities 5, 7, 8, the fourth region has city 9. The total length of the roads in these cities is 11, 20, 5 and 0, correspondingly. According to the plan, we first build the road of length 6 between cities 5 and 9, then the road of length 23 between cities 1 and 9. Thus, the total length of the built roads equals 29.
|
Input: 9 6 2 21 2 23 2 14 6 201 3 87 8 35 7 2 | Output: YES9 51 9
|
Hard
| 5 | 1,599 | 592 | 496 | 3 |
391 |
C2
|
391C2
|
C2. The Tournament
| 0 |
greedy
|
This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points.Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined, there were n contestants in the tournament, numbered from 1 to n. Each of them had already obtained some amount of tournament points, namely the i-th fighter had pi points.Manao is going to engage in a single fight against each contestant. Each of Manao's fights ends in either a win or a loss. A win grants Manao one point, and a loss grants Manao's opponent one point. For each i, Manao estimated the amount of effort ei he needs to invest to win against the i-th contestant. Losing a fight costs no effort.After Manao finishes all of his fights, the ranklist will be determined, with 1 being the best rank and n + 1 being the worst. The contestants will be ranked in descending order of their tournament points. The contestants with the same number of points as Manao will be ranked better than him if they won the match against him and worse otherwise. The exact mechanism of breaking ties for other fighters is not relevant here.Manao's objective is to have rank k or better. Determine the minimum total amount of effort he needs to invest in order to fulfill this goal, if it is possible.
|
The first line contains a pair of integers n and k (1 β€ k β€ n + 1). The i-th of the following n lines contains two integers separated by a single space β pi and ei (0 β€ pi, ei β€ 200000).The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem C1 (4 points), the constraint 1 β€ n β€ 15 will hold. In subproblem C2 (4 points), the constraint 1 β€ n β€ 100 will hold. In subproblem C3 (8 points), the constraint 1 β€ n β€ 200000 will hold.
|
Print a single number in a single line β the minimum amount of effort Manao needs to use to rank in the top k. If no amount of effort can earn Manao such a rank, output number -1.
|
Consider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant has 2 points and victory against him costs Manao 2 units of effort. Manao's goal is top be in top 2. The optimal decision is to win against fighters 1 and 3, after which Manao, fighter 2, and fighter 3 will all have 2 points. Manao will rank better than fighter 3 and worse than fighter 2, thus finishing in second place.Consider the second test case. Even if Manao wins against both opponents, he will still rank third.
|
Input: 3 21 11 42 2 | Output: 3
|
Beginner
| 1 | 1,413 | 602 | 179 | 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.