question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
This is the easy version of the problem. The only difference between the two versions is the constraint on n. You can make hacks only if all versions of the problem are solved.Let's call an array a of odd length 2m+1 (with m >= 1) bad, if element a_{m+1} is equal to the median of this array. In other words, the array is bad if, after sorting it, the element at m+1-st position remains the same.Let's call a permutation p of integers from 1 to n anti-median, if every its subarray of odd length >= 3 is not bad.You are already given values of some elements of the permutation. Find the number of ways to set unknown values to obtain an anti-median permutation. As this number can be very large, find it modulo 10^9+7. | Input: ['52-1 -13-1 -1 -141 2 3 46-1 -1 3 4 -1 -18-1 -1 -1 -1 -1 -1 -1 -1'] Output:['2', '4', '0', '1', '316', ''] | [
3
] |
You are given a simple undirected graph consisting of n vertices. The graph doesn't contain self-loops, there is at most one edge between each pair of vertices. Your task is simple: make the graph connected.You can do the following operation any number of times (possibly zero): Choose a vertex u arbitrarily. For each vertex v satisfying v\ne u in the graph individually, if v is adjacent to u, remove the edge between u and v, otherwise add an edge between u and v. Find the minimum number of operations required to make the graph connected. Also, find any sequence of operations with the minimum length that makes the graph connected. | Input: ['430111001003000001010401001000000100106001100000011100100101000010001010010'] Output:['0', '1', '1', '2', '3 4 ', '3', '2 5 6 ', ''] | [
0,
2,
4
] |
Let f(x,y) be the number of carries of x+y in binary (i. e. f(x,y)=g(x)+g(y)-g(x+y), where g(x) is the number of ones in the binary representation of x).Given two integers n and k, find the number of ordered pairs (a,b) such that 0 <=q a,b < 2^n, and f(a,b) equals k. Note that for a\ne b, (a,b) and (b,a) are considered as two different pairs. As this number may be large, output it modulo 10^9+7. | Input: ['3 1', ''] Output:['15', ''] | [
3
] |
You are given a binary matrix b (all elements of the matrix are 0 or 1) of n rows and n columns.You need to construct a n sets A_1, A_2, ..., A_n, for which the following conditions are satisfied: Each set is nonempty and consists of distinct integers between 1 and n inclusive. All sets are distinct. For all pairs (i,j) satisfying 1<=q i, j<=q n, b_{i,j}=1 if and only if A_i\subsetneq A_j. In other words, b_{i, j} is 1 if A_i is a proper subset of A_j and 0 otherwise. Set X is a proper subset of set Y, if X is a nonempty subset of Y, and X \neq Y.It's guaranteed that for all test cases in this problem, such n sets exist. Note that it doesn't mean that such n sets exist for all possible inputs.If there are multiple solutions, you can output any of them. | Input: ['2', '4', '0001', '1001', '0001', '0000', '3', '011', '001', '000', ''] Output:['3 1 2 3', '2 1 3', '2 2 4', '4 1 2 3 4', '1 1', '2 1 2', '3 1 2 3', ''] | [
2
] |
Define a cyclic sequence of size n as an array s of length n, in which s_n is adjacent to s_1.Muxii has a ring represented by a cyclic sequence a of size n.However, the ring itself hates equal adjacent elements. So if two adjacent elements in the sequence are equal at any time, one of them will be erased immediately. The sequence doesn't contain equal adjacent elements initially.Muxii can perform the following operation until the sequence becomes empty: Choose an element in a and erase it. For example, if ring is [1, 2, 4, 2, 3, 2], and Muxii erases element 4, then ring would erase one of the elements equal to 2, and the ring will become [1, 2, 3, 2].Muxii wants to find the maximum number of operations he could perform.Note that in a ring of size 1, its only element isn't considered adjacent to itself (so it's not immediately erased). | Input: ['341 2 3 241 2 1 211'] Output:['4', '3', '1', ''] | [
2
] |
You are given three integers n, a, and b. Determine if there exist two permutations p and q of length n, for which the following conditions hold: The length of the longest common prefix of p and q is a. The length of the longest common suffix of p and q is b. A permutation of length n is an array containing each integer from 1 to n exactly once. 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). | Input: ['41 1 12 1 23 1 14 1 1'] Output:['Yes', 'No', 'No', 'Yes', ''] | [
0
] |
There are n quests. If you complete the i-th quest, you will gain a_i coins. You can only complete at most one quest per day. However, once you complete a quest, you cannot do the same quest again for k days. (For example, if k=2 and you do quest 1 on day 1, then you cannot do it on day 2 or 3, but you can do it again on day 4.)You are given two integers c and d. Find the maximum value of k such that you can gain at least c coins over d days. If no such k exists, output Impossible. If k can be arbitrarily large, output Infinity. | Input: ['62 5 41 22 20 10100 103 100 37 2 64 20 34 5 6 74 100000000000 20228217734 927368 26389746 6278969742 20 45 1'] Output:['2', 'Infinity', 'Impossible', '1', '12', '0', ''] | [
2,
4
] |
You are given a binary array of length n. You are allowed to perform one operation on it at most once. In an operation, you can choose any element and flip it: turn a 0 into a 1 or vice-versa.What is the maximum number of inversions^{\ddagger} the array can have after performing at most one operation?^ A binary array is an array that contains only zeroes and ones.^\ddagger The number of inversions in an array is the number of pairs of indices i,j such that i<j and a_i > a_j. | Input: ['541 0 1 060 1 0 0 1 020 081 0 1 1 0 0 0 131 1 1'] Output:['3', '7', '1', '13', '2', ''] | [
2,
3
] |
In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string s. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size x (1 <=q x <=q 26) contains only the first x Latin letters. For example an alphabet of size 4 contains only the characters \texttt{a}, \texttt{b}, \texttt{c} and \texttt{d}. | Input: ['51a4down10codeforces3bcf5zzzzz'] Output:['1', '23', '19', '6', '26', ''] | [
2
] |
A sequence of n numbers is called permutation if it contains all numbers from 1 to n exactly once. For example, the sequences [3, 1, 4, 2], [1] and [2,1] are permutations, but [1,2,1], [0,1] and [1,3,4] β are not.For a permutation p of even length n you can make an array b of length \frac{n}{2} such that: b_i = \max(p_{2i - 1}, p_{2i}) for 1 <= i <= \frac{n}{2} For example, if p = [2, 4, 3, 1, 5, 6], then: b_1 = \max(p_1, p_2) = \max(2, 4) = 4 b_2 = \max(p_3, p_4) = \max(3,1)=3 b_3 = \max(p_5, p_6) = \max(5,6) = 6 As a result, we made b = [4, 3, 6].For a given array b, find the lexicographically minimal permutation p such that you can make the given array b from it.If b = [4,3,6], then the lexicographically minimal permutation from which it can be made is p = [1,4,2,3,5,6], since: b_1 = \max(p_1, p_2) = \max(1, 4) = 4 b_2 = \max(p_3, p_4) = \max(2, 3) = 3 b_3 = \max(p_5, p_6) = \max(5, 6) = 6 A permutation x_1, x_2, ..., x_n is lexicographically smaller than a permutation y_1, y_2 ..., y_n if and only if there exists such i (1 <= i <= n) that x_1=y_1, x_2=y_2, ..., x_{i-1}=y_{i-1} and x_i<y_i. | Input: ['664 3 642 488 7 2 366 4 244 488 7 4 5'] Output:['1 4 2 3 5 6 ', '1 2 3 4 ', '-1', '5 6 3 4 1 2 ', '-1', '1 8 6 7 2 4 3 5 ', ''] | [
2,
3,
4
] |
A positive number x of length n in base p (2 <= p <= 10^9) is written on the blackboard. The number x is given as a sequence a_1, a_2, ..., a_n (0 <= a_i < p) β the digits of x in order from left to right (most significant to least significant).Dmitry is very fond of all the digits of this number system, so he wants to see each of them at least once.In one operation, he can: take any number x written on the board, increase it by 1, and write the new value x + 1 on the board. For example, p=5 and x=234_5. Initially, the board contains the digits 2, 3 and 4; Dmitry increases the number 234_5 by 1 and writes down the number 240_5. On the board there are digits 0, 2, 3, 4; Dmitry increases the number 240_5 by 1 and writes down the number 241_5. Now the board contains all the digits from 0 to 4. Your task is to determine the minimum number of operations required to make all the digits from 0 to p-1 appear on the board at least once. | Input: ['112 31 24 21 1 1 16 61 2 3 4 5 05 21 0 1 0 13 101 2 35 10004 1 3 2 53 52 3 44 43 2 3 01 325 51 2 2 2 43 41 0 1'] Output:['1', '1', '0', '0', '7', '995', '2', '1', '1', '1', '2', ''] | [
2,
3,
4
] |
There are n astronauts working on some space station. An astronaut with the number i (1 <= i <= n) has power a_i.An evil humanoid has made his way to this space station. The power of this humanoid is equal to h. Also, the humanoid took with him two green serums and one blue serum.In one second , a humanoid can do any of three actions: to absorb an astronaut with power strictly less humanoid power; to use green serum, if there is still one left; to use blue serum, if there is still one left. When an astronaut with power a_i is absorbed, this astronaut disappears, and power of the humanoid increases by \lfloor \frac{a_i}{2} \rfloor, that is, an integer part of \frac{a_i}{2}. For example, if a humanoid absorbs an astronaut with power 4, its power increases by 2, and if a humanoid absorbs an astronaut with power 7, its power increases by 3.After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by 2 times.After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by 3 times.The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally. | Input: ['84 12 1 8 93 36 2 604 55 1 100 53 238 6 31 1124 612 12 36 1004 12 1 1 153 515 1 13'] Output:['4', '3', '3', '3', '0', '4', '4', '3', ''] | [
0
] |
Inflation has occurred in Berlandia, so the store needs to change the price of goods.The current price of good n is given. It is allowed to increase the price of the good by k times, with 1 <= k <= m, k is an integer. Output the roundest possible new price of the good. That is, the one that has the maximum number of zeros at the end.For example, the number 481000 is more round than the number 1000010 (three zeros at the end of 481000 and only one at the end of 1000010).If there are several possible variants, output the one in which the new price is maximal.If it is impossible to get a rounder price, output n \cdot m (that is, the maximum possible price). | Input: ['106 115 4313 54 1610050 123452 64 3025 102 811 7'] Output:['60', '200', '65', '60', '120600000', '10', '100', '200', '100', '7', ''] | [
0
] |
Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of a.The thermostat can only be set to a temperature from l to r inclusive, the temperature cannot change by less than x. Formally, in one operation you can reconfigure the thermostat from temperature a to temperature b if |a - b| >= x and l <= b <= r.You are given l, r, x, a and b. Find the minimum number of operations required to get temperature b from temperature a, or say that it is impossible. | Input: ['103 5 63 30 15 54 50 10 53 73 5 63 4-10 10 11-5 6-3 3 41 0-5 10 89 21 5 12 5-1 4 30 2-6 3 6-1 -4'] Output:['0', '2', '3', '-1', '1', '-1', '3', '1', '3', '-1', ''] | [
2,
3
] |
A sequence of n numbers is called a permutation if it contains all integers from 1 to n exactly once. For example, the sequences [3, 1, 4, 2], [1] and [2,1] are permutations, but [1,2,1], [0,1] and [1,3,4] β are not.Polycarp lost his favorite permutation and found only some of its elements β the numbers b_1, b_2, ... b_m. He is sure that the sum of the lost elements equals s.Determine whether one or more numbers can be appended to the given sequence b_1, b_2, ... b_m such that the sum of the added numbers equals s, and the resulting new array is a permutation? | Input: ['53 133 1 41 113 31 4 22 14 35 61 2 3 4 5'] Output:['YES', 'NO', 'YES', 'NO', 'YES', ''] | [
3
] |
You are given an integer n. Find a sequence of n distinct integers a_1, a_2, ..., a_n such that 1 <=q a_i <=q 10^9 for all i and \max(a_1, a_2, ..., a_n) - \min(a_1, a_2, ..., a_n)= \sqrt{a_1 + a_2 + ... + a_n}.It can be proven that there exists a sequence of distinct integers that satisfies all the conditions above. | Input: ['3254'] Output:['3 1', '20 29 18 26 28', '25 21 23 31', ''] | [
0,
3,
4
] |
Given two integers n and x, a permutation p of length n is called funny if p_i is a multiple of i for all 1 <=q i <=q n - 1, p_n = 1, and p_1 = x.Find the lexicographically minimal^{\ddagger} funny permutation, or report that no such permutation exists. A permutation of length n is an array consisting of each of the integers from 1 to n exactly once.^{\ddagger} Let a and b be permutations of length n. Then a is lexicographically smaller than b if in the first position i where a and b differ, a_i < b_i. A permutation is lexicographically minimal if it is lexicographically smaller than all other permutations. | Input: ['33 34 25 4'] Output:['3 2 1 ', '2 4 3 1 ', '-1', ''] | [
2
] |
For his birthday, Kevin received the set of pairwise distinct numbers 1, 2, 3, ..., n as a gift.He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order p_1, p_2, ..., p_n, he wants to maximize the value \min \limits_{i=1}^{n - 1} \lvert p_{i + 1} - p_i \rvert, where |x| denotes the absolute value of x.Help Kevin to do that. | Input: ['243'] Output:['2 4 1 3', '1 2 3', ''] | [
2,
3
] |
You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached. | Input: ['54QQAA4QQAQ3QAA1Q14QAQQAQAAQQQAAA'] Output:['Yes', 'No', 'Yes', 'No', 'Yes', ''] | [
2
] |
Little Misha goes to the programming club and solves nothing there. It may seem strange, but when you find out that Misha is filming a Minecraft series, everything will fall into place...Misha is inspired by Manhattan, so he built a city in Minecraft that can be imagined as a table of size n * m. k students live in a city, the i-th student lives in the house, located at the intersection of the x_i-th row and the y_i-th column. Also, each student has a degree of his aggressiveness w_i. Since the city turned out to be very large, Misha decided to territorially limit the actions of his series to some square s, which sides are parallel to the coordinate axes. The length of the side of the square should be an integer from 1 to \min(n, m) cells.According to the plot, the main hero will come to the city and accidentally fall into the square s. Possessing a unique degree of aggressiveness 0, he will be able to show his leadership qualities and assemble a team of calm, moderate and aggressive students.In order for the assembled team to be versatile and close-knit, degrees of aggressiveness of all students of the team must be pairwise distinct and must form a single segment of consecutive integers. Formally, if there exist students with degrees of aggressiveness l, l+1, ..., -1, 1, ..., r-1, r inside the square s, where l <= 0 <= r, the main hero will be able to form a team of r-l+1 people (of course, he is included in this team).Notice, that it is not required to take all students from square s to the team.Misha thinks that the team should consist of at least t people. That is why he is interested, how many squares are there in the table in which the main hero will be able to form a team of at least t people. Help him to calculate this. | Input: ['2 2 1 2', '1 1 2', ''] Output:['0', ''] | [
0
] |
You have been invited as a production process optimization specialist to some very large company. The company has n machines at its factory, standing one behind another in the production chain. Each machine can be described in one of the following two ways: (+,~a_i) or (*,~a_i).If a workpiece with the value x is supplied to the machine of kind (+,~a_i), then the output workpiece has value x + a_i.If a workpiece with the value x is supplied to the machine of kind (*,~a_i), then the output workpiece has value x \cdot a_i.The whole production process is as follows. The workpiece with the value 1 is supplied to the first machine, then the workpiece obtained after the operation of the first machine is supplied to the second machine, then the workpiece obtained after the operation of the second machine is supplied to the third machine, and so on. The company is not doing very well, so now the value of the resulting product does not exceed 2 \cdot 10^9.The directors of the company are not satisfied with the efficiency of the production process and have given you a budget of b coins to optimize it.To optimize production you can change the order of machines in the chain. Namely, by spending p coins, you can take any machine of kind (+,~a_i) and move it to any place in the chain without changing the order of other machines. Also, by spending m coins, you can take any machine of kind (*,~a_i) and move it to any place in the chain.What is the maximum value of the resulting product that can be achieved if the total cost of movements that are made should not exceed b coins? | Input: ['3 2 1 3', '* 2', '+ 1', '+ 1', ''] Output:['6', ''] | [
0,
2,
4
] |
You are given a binary array a (all elements of the array are 0 or 1) of length n. You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until a is sorted: Choose two random indices i and j such that i < j. Indices are chosen equally probable among all pairs of indices (i, j) such that 1 <= i < j <= n. If a_i > a_j, then swap elements a_i and a_j. What is the expected number of such operations you will perform before the array becomes sorted?It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{998\,244\,353}. Output the integer equal to p \cdot q^{-1} \bmod 998\,244\,353. In other words, output such an integer x that 0 <= x < 998\,244\,353 and x \cdot q \equiv p \pmod{998\,244\,353}. | Input: ['330 1 050 0 1 1 161 1 1 0 0 1'] Output:['3', '0', '249561107', ''] | [
3
] |
You are given an integer x and an array of integers a_1, a_2, ..., a_n. You have to determine if the number a_1! + a_2! + ... + a_n! is divisible by x!.Here k! is a factorial of k β the product of all positive integers less than or equal to k. For example, 3! = 1 \cdot 2 \cdot 3 = 6, and 5! = 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120. | Input: ['6 4', '3 2 2 2 3 3', ''] Output:['Yes', ''] | [
3
] |
This is the hard version of the problem. The difference is that in this version the array contains zeros. You can make hacks only if both versions of the problem are solved.You are given an array [a_1, a_2, ... a_n] consisting of integers -1, 0 and 1. You have to build a partition of this array into the set of segments [l_1, r_1], [l_2, r_2], ..., [l_k, r_k] with the following property: Denote the alternating sum of all elements of the i-th segment as s_i: s_i = a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + ... \pm a_{r_i}. For example, the alternating sum of elements of segment [2, 4] in array [1, 0, -1, 1, 1] equals to 0 - (-1) + 1 = 2. The sum of s_i over all segments of partition should be equal to zero. Note that each s_i does not have to be equal to zero, this property is about sum of s_i over all segments of partition.The set of segments [l_1, r_1], [l_2, r_2], ..., [l_k, r_k] is called a partition of the array a of length n if 1 = l_1 <= r_1, l_2 <= r_2, ..., l_k <= r_k = n and r_i + 1 = l_{i+1} for all i = 1, 2, ... k-1. In other words, each element of the array must belong to exactly one segment.You have to build a partition of the given array with properties described above or determine that such partition does not exist.Note that it is not required to minimize the number of segments in the partition. | Input: ['540 0 0 07-1 1 0 1 0 1 050 -1 1 0 131 0 111'] Output:['4', '1 1', '2 2', '3 3', '4 4', '4', '1 1', '2 2', '3 5', '6 7', '-1', '2', '1 1', '2 3', '-1', ''] | [
2
] |
This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.You are given an array [a_1, a_2, ... a_n] consisting of integers -1 and 1. You have to build a partition of this array into the set of segments [l_1, r_1], [l_2, r_2], ..., [l_k, r_k] with the following property: Denote the alternating sum of all elements of the i-th segment as s_i: s_i = a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + ... \pm a_{r_i}. For example, the alternating sum of elements of segment [2, 4] in array [1, 0, -1, 1, 1] equals to 0 - (-1) + 1 = 2. The sum of s_i over all segments of partition should be equal to zero. Note that each s_i does not have to be equal to zero, this property is about sum of s_i over all segments of partition.The set of segments [l_1, r_1], [l_2, r_2], ..., [l_k, r_k] is called a partition of the array a of length n if 1 = l_1 <= r_1, l_2 <= r_2, ..., l_k <= r_k = n and r_i + 1 = l_{i+1} for all i = 1, 2, ... k-1. In other words, each element of the array must belong to exactly one segment.You have to build a partition of the given array with properties described above or determine that such partition does not exist.Note that it is not required to minimize the number of segments in the partition. | Input: ['441 1 1 16-1 1 1 1 1 131 -1 111'] Output:['1', '1 4', '2', '1 3', '4 6', '-1', '-1', ''] | [
2
] |
We call an array a of length n fancy if for each 1 < i <= n it holds that a_i = a_{i-1} + 1.Let's call f(p) applied to a permutation^ of length n as the minimum number of subarrays it can be partitioned such that each one of them is fancy. For example f([1,2,3]) = 1, while f([3,1,2]) = 2 and f([3,2,1]) = 3.Given n and a permutation p of length n, we define a permutation p' of length n to be k-special if and only if: p' is lexicographically smaller^\ddagger than p, and f(p') = k. Your task is to count for each 1 <= k <= n the number of k-special permutations modulo m.^ A permutation 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).^\ddagger A permutation a of length n is lexicographically smaller than a permutation b of length n if and only if the following holds: in the first position where a and b differ, the permutation a has a smaller element than the corresponding element in b. | Input: ['4 666012', '1 3 4 2', ''] Output:['1 0 1 1 '] | [
3
] |
Everyone was happy coding, until suddenly a power shortage happened and the best competitive programming site went down. Fortunately, a system administrator bought some new equipment recently, including some UPSs. Thus there are some servers that are still online, but we need all of them to be working in order to keep the round rated.Imagine the servers being a binary string s of length n. If the i-th server is online, then s_i = 1, and s_i = 0 otherwise.A system administrator can do the following operation called electricity spread, that consists of the following phases: Select two servers at positions 1 <= i < j <= n such that both are online (i.e. s_i=s_j=1). The spread starts only from online servers. Check if we have enough power to make the spread. We consider having enough power if the number of turned on servers in range [i, j] is at least the number of turned off servers in range [i, j]. More formally, check whether 2 \cdot (s_i + s_{i+1} + ... + s_j) >= j - i + 1. If the check is positive, turn on all the offline servers in range [i, j]. More formally, make s_k := 1 for all k from i to j. We call a binary string s of length n rated if we can turn on all servers (i.e. make s_i = 1 for 1 <= i <= n) using the electricity spread operation any number of times (possibly, 0). Your task is to find the number of rated strings of length n modulo m. | Input: ['2 100', ''] Output:['1'] | [
3
] |
Daemon Targaryen decided to stop looking like a Metin2 character. He turned himself into the most beautiful thing, a bracket sequence.For a bracket sequence, we can do two kind of operations: Select one of its substrings^ and cyclic shift it to the right. For example, after a cyclic shift to the right, "(())" will become ")(()"; Insert any bracket, opening '(' or closing ')', wherever you want in the sequence. We define the cost of a bracket sequence as the minimum number of such operations to make it balanced^\ddagger.Given a bracket sequence s of length n, find the sum of costs across all its \frac{n(n+1)}{2} non-empty substrings. Note that for each substring we calculate the cost independently.^ A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.^\ddagger A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not. | Input: ['51)4)()(3())5(((((10)(())))())'] Output:['1', '9', '6', '35', '112', ''] | [
2,
4
] |
You are given two integers n and m and an array a of n integers. For each 1 <= i <= n it holds that 1 <= a_i <= m.Your task is to count the number of different arrays b of length n such that: 1 <= b_i <= m for each 1 <= i <= n, and \gcd(b_1,b_2,b_3,...,b_i) = a_i for each 1 <= i <= n. Here \gcd(a_1,a_2,...,a_i) denotes the greatest common divisor (GCD) of integers a_1,a_2,...,a_i.Since this number can be too large, print it modulo 998\,244\,353. | Input: ['53 54 2 12 11 15 502 3 5 2 34 100000000060 30 1 12 10000000001000000000 2'] Output:['3', '1', '0', '595458194', '200000000', ''] | [
3
] |
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string s.For some non-empty substring^ t of string s containing x characters 0 and y characters 1, define its cost as: x \cdot y, if x > 0 and y > 0; x^2, if x > 0 and y = 0; y^2, if x = 0 and y > 0. Given a binary string s of length n, find the maximum cost across all its non-empty substrings.^ A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | Input: ['6511100711001106011110710010104100010'] Output:['9', '12', '16', '12', '9', '1', ''] | [
0,
2
] |
You are given a permutation a_1, a_2, ..., a_n of size n, where each integer from 1 to n appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices i, j, k (1 <= i < j < k <= n). If a_i > a_k, replace a_i with a_i + a_j. Otherwise, swap a_j and a_k. Determine whether you can make the array a sorted in non-descending order. | Input: ['731 2 331 3 275 3 4 7 6 2 177 6 5 4 3 2 152 1 4 5 352 1 3 4 571 2 6 7 4 3 5'] Output:['Yes', 'Yes', 'No', 'No', 'No', 'No', 'Yes', ''] | [
3
] |
Consider an array a of length n with elements numbered from 1 to n. It is possible to remove the i-th element of a if gcd(a_i, i) = 1, where gcd denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position.An array b with n integers such that 1 <= b_i <= n - i + 1 is a removal sequence for the array a if it is possible to remove all elements of a, if you remove the b_1-th element, then the b_2-th, ..., then the b_n-th element. For example, let a = [42, 314]: [1, 1] is a removal sequence: when you remove the 1-st element of the array, the condition gcd(42, 1) = 1 holds, and the array becomes [314]; when you remove the 1-st element again, the condition gcd(314, 1) = 1 holds, and the array becomes empty. [2, 1] is not a removal sequence: when you try to remove the 2-nd element, the condition gcd(314, 2) = 1 is false. An array is ambiguous if it has at least two removal sequences. For example, the array [1, 2, 5] is ambiguous: it has removal sequences [3, 1, 1] and [1, 2, 1]. The array [42, 314] is not ambiguous: the only removal sequence it has is [1, 1].You are given two integers n and m. You have to calculate the number of ambiguous arrays a such that the length of a is from 1 to n and each a_i is an integer from 1 to m. | Input: ['2 3', ''] Output:['6', ''] | [
3
] |
Alice and Bob are playing a game. They have an array of positive integers a of size n.Before starting the game, Alice chooses an integer k >= 0. The game lasts for k stages, the stages are numbered from 1 to k. During the i-th stage, Alice must remove an element from the array that is less than or equal to k - i + 1. After that, if the array is not empty, Bob must add k - i + 1 to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the k-th stage ends and Alice hasn't lost yet, she wins.Your task is to determine the maximum value of k such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. | Input: ['431 1 244 4 4 41151 3 2 1 1'] Output:['2', '0', '1', '3', ''] | [
2,
4
] |
You are playing a computer game. To pass the current level, you have to kill a big horde of monsters. In this horde, there are n monsters standing in the row, numbered from 1 to n. The i-th monster has a_i health and a special "Death's Blessing" spell of strength b_i attached to it.You are going to kill all of them one by one. It takes exactly h seconds to kill a monster with health h.When the i-th monster dies, it casts its spell that increases the health of its neighbors by b_i (the neighbors of the j-th monster in the row are the monsters on places j - 1 and j + 1. The first and the last monsters have only one neighbor each).After each monster is killed, the row shrinks, so its former neighbors become adjacent to each other (so if one of them dies, the other one is affected by its spell). For example, imagine a situation with 4 monsters with health a = [2, 6, 7, 3] and spells b = [3, 6, 0, 5]. One of the ways to get rid of the monsters is shown below: 2673\xrightarrow{6\ s}8133\xrightarrow{13\ s}83\xrightarrow{8\ s}6\xrightarrow{6\ s}\{\}3605305355 The first row represents the health of each monster, the second one β the power of the spells. As a result, we can kill all monsters in 6 + 13 + 8 + 6 = 33 seconds. Note that it's only an example and may not be the fastest way to get rid of the monsters.What is the minimum time required to kill all monsters in the row? | Input: ['411003100 1 1001 100 142 6 7 33 6 0 521000000000 10000000001000000000 1000000000'] Output:['10', '203', '26', '3000000000', ''] | [
2
] |
There's a chessboard of size n * n. m rooks are placed on it in such a way that: no two rooks occupy the same cell; no two rooks attack each other. A rook attacks all cells that are in its row or column.Is it possible to move exactly one rook (you can choose which one to move) into a different cell so that no two rooks still attack each other? A rook can move into any cell in its row or column if no other rook stands on its path. | Input: ['22 21 22 13 12 2'] Output:['NO', 'YES', ''] | [
2
] |
The position of the leftmost maximum on the segment [l; r] of array x = [x_1, x_2, ..., x_n] is the smallest integer i such that l <= i <= r and x_i = \max(x_l, x_{l+1}, ..., x_r).You are given an array a = [a_1, a_2, ..., a_n] of length n. Find the number of integer arrays b = [b_1, b_2, ..., b_n] of length n that satisfy the following conditions: 1 <= b_i <= m for all 1 <= i <= n; for all pairs of integers 1 <= l <= r <= n, the position of the leftmost maximum on the segment [l; r] of the array b is equal to the position of the leftmost maximum on the segment [l; r] of the array a. Since the answer might be very large, print its remainder modulo 10^9+7. | Input: ['43 31 3 24 22 2 2 26 96 9 6 9 6 99 10010 40 20 20 100 60 80 60 60'] Output:['8', '5', '11880', '351025663', ''] | [
3,
4
] |
You are given three integers a, b, and d. Your task is to find any integer x which satisfies all of the following conditions, or determine that no such integers exist: 0 <= x \lt 2^{60}; a|x is divisible by d; b|x is divisible by d. Here, | denotes the bitwise OR operation. | Input: ['812 39 56 8 14100 200 2003 4 62 2 218 27 3420 666 69987654321 123456789 999999999'] Output:['18', '14', '-1', '-1', '0', '11', '25599', '184470016815529983'] | [
3
] |
The score of an array v_1,v_2,...,v_n is defined as the number of indices i (1 <= i <= n) such that v_1+v_2+...+v_i = 0.You are given an array a_1,a_2,...,a_n of length n. You can perform the following operation multiple times: select an index i (1 <= i <= n) such that a_i=0; then replace a_i by an arbitrary integer. What is the maximum possible score of a that can be obtained by performing a sequence of such operations? | Input: ['552 0 1 -1 031000000000 1000000000 040 0 0 083 0 2 -10 10 -30 30 091 0 0 1 -1 0 1 0 -1'] Output:['3', '1', '4', '4', '5', ''] | [
0,
2
] |
A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.For example: string "7" is diverse because 7 appears in it 1 time and the number of distinct characters in it is 1; string "77" is not diverse because 7 appears in it 2 times and the number of distinct characters in it is 1; string "1010" is diverse because both 0 and 1 appear in it 2 times and the number of distinct characters in it is 2; string "6668" is not diverse because 6 appears in it 3 times and the number of distinct characters in it is 2. You are given a string s of length n, consisting of only digits 0 to 9. Find how many of its \frac{n(n+1)}{2} substrings are diverse.A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Note that if the same diverse string appears in s multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "77" both equal to "7", so the answer for the string "77" is 2. | Input: ['71727741010501100639999652345618789987887987998798'] Output:['1', '2', '10', '12', '10', '15', '106', ''] | [
0
] |
You have n rectangular wooden blocks, which are numbered from 1 to n. The i-th block is 1 unit high and \lceil \frac{i}{2} \rceil units long.Here, \lceil \frac{x}{2} \rceil denotes the result of division of x by 2, rounded up. For example, \lceil \frac{4}{2} \rceil = 2 and \lceil \frac{5}{2} \rceil = \lceil 2.5 \rceil = 3.For example, if n=5, then the blocks have the following sizes: 1 * 1, 1 * 1, 1 * 2, 1 * 2, 1 * 3. The available blocks for n=5 Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks. One of the ways to create 3 * 3 square using blocks 1 through 5 | Input: ['325197654321'] Output:['1', '3', '98827161', ''] | [
3
] |
For given integers n and m, let's call a pair of arrays a and b of integers good, if they satisfy the following conditions: a and b have the same length, let their length be k. k >= 2 and a_1 = 0, a_k = n, b_1 = 0, b_k = m. For each 1 < i <= k the following holds: a_i >=q a_{i - 1}, b_i >=q b_{i - 1}, and a_i + b_i \neq a_{i - 1} + b_{i - 1}.Find the sum of |a| over all good pairs of arrays (a,b). Since the answer can be very large, output it modulo 10^9 + 7. | Input: ['41 11 22 2100 100'] Output:['8', '26', '101', '886336572', ''] | [
3
] |
You are given an array a of n integers a_1, a_2, a_3, ..., a_n.You have to answer q independent queries, each consisting of two integers l and r. Consider the subarray a[l:r] = [a_l, a_{l+1}, ..., a_r]. You can apply the following operation to the subarray any number of times (possibly zero)- Choose two integers L, R such that l <= L <= R <= r and R - L + 1 is odd. Replace each element in the subarray from L to R with the XOR of the elements in the subarray [L, R]. The answer to the query is the minimum number of operations required to make all elements of the subarray a[l:r] equal to 0 or -1 if it is impossible to make all of them equal to 0. You can find more details about XOR operation here. | Input: ['7 6', '3 0 3 3 1 2 3', '3 4', '4 6', '3 7', '5 6', '1 6', '2 2', ''] Output:['-1', '1', '1', '-1', '2', '0', ''] | [
4
] |
You are given an array a consisting of n integers. You want to distribute these n integers into two groups s_1 and s_2 (groups can be empty) so that the following conditions are satisfied: For each i (1 <=q i <=q n), a_i goes into exactly one group. The value |sum(s_1)| - |sum(s_2)| is the maximum possible among all such ways to distribute the integers.Here sum(s_1) denotes the sum of the numbers in the group s_1, and sum(s_2) denotes the sum of the numbers in the group s_2.Determine the maximum possible value of |sum(s_1)| - |sum(s_2)|. | Input: ['4210 -104-2 -1 11 032 3 25-9 2 0 0 -4'] Output:['0', '8', '7', '11', ''] | [
2
] |
Anton decided to get ready for an Olympiad in Informatics. Ilya prepared n tasks for him to solve. It is possible to submit the solution for the i-th task in the first d_{i} days only. Anton cannot solve more than one task a day. Ilya estimated the usefulness of the i-th tasks as r_{i} and divided the tasks into three topics, the topic of the i-th task is type_{i}.Anton wants to solve exactly a tasks on the first topic, b tasks on the second topic and c tasks on the third topic. Tell Anton if it is possible to do so, and if it is, calculate the maximum total usefulness of the tasks he may solve. | Input: ['44 1 1 01 2 11 1 10 1 21 2 23 1 1 11 1 27 2 19 3 24 2 1 0100 2 15 2 37 1 25 1 25 1 1 110 3 19 2 320 1 116 1 41 3 4'] Output:['2', '-1', '17', '35', ''] | [
4
] |
The only difference between this problem and the hard version is the maximum number of questions.This is an interactive problem.There is a hidden integer 1 <= x <= n which you have to find. In order to find it you can ask at most \mathbf{82} questions.In each question you can choose a non-empty integer set S and ask if x belongs to S or not, after each question, if x belongs to S, you'll receive "YES", otherwise "NO".But the problem is that not all answers are necessarily true (some of them are joking), it's just guaranteed that for each two consecutive questions, at least one of them is answered correctly.Additionally to the questions, you can make at most 2 guesses for the answer x. Each time you make a guess, if you guess x correctly, you receive ":)" and your program should terminate, otherwise you'll receive ":(".As a part of the joking, we will not fix the value of x in the beginning. Instead, it can change throughout the interaction as long as all the previous responses are valid as described above.Note that your answer guesses are always answered correctly. If you ask a question before and after a guess, at least one of these two questions is answered correctly, as normal. | Input: ['6NO:(NO:)'] Output:['? 5 1 2 5 4 3! 6? 4 1 2 3 4! 5'] | [
4,
4
] |
You are given a rooted tree consisting of n vertices. The vertices are numbered from 1 to n, and the root is the vertex 1. You are also given a score array s_1, s_2, ..., s_n.A multiset of k simple paths is called valid if the following two conditions are both true. Each path starts from 1. Let c_i be the number of paths covering vertex i. For each pair of vertices (u,v) (2<= u,v<= n) that have the same parent, |c_u-c_v|<= 1 holds. The value of the path multiset is defined as \sum\limits_{i=1}^n c_i s_i.It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets. | Input: ['25 41 2 1 36 2 1 5 75 31 2 1 36 6 1 4 10'] Output:['54', '56', ''] | [
2
] |
You are given a permutation a of size n and you should perform n operations on it. In the i-th operation, you can choose a non-empty suffix of a and increase all of its elements by i. How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array a is a pair of indices (i, j) such that i > j and a_{i} < a_{j}. | Input: ['441 2 3 451 3 2 4 532 3 111'] Output:['1 1 1 1', '1 4 3 2 1', '1 3 3', '1'] | [
2,
3
] |
You have an array a of size n consisting only of zeroes and ones. You can do the following operation: choose two indices 1 <= i , j <= n, i!=j, add a_{i} to a_{j}, remove a_{i} from a. Note that elements of a can become bigger than 1 after performing some operations. Also note that n becomes 1 less after the operation.What is the minimum number of operations needed to make a non-decreasing, i. e. that each element is not less than the previous element? | Input: ['480 0 1 1 1 1 1 151 0 0 1 121 0111 1 0 0 1 0 0 1 1 1 0'] Output:['0', '1', '1', '3', ''] | [
2
] |
You have an array a of size n consisting only of zeroes and ones and an integer k. In one operation you can do one of the following: Select 2 consecutive elements of a and replace them with their minimum (that is, let a := [a_{1}, a_{2}, ..., a_{i-1}, \min(a_{i}, a_{i+1}), a_{i+2}, ..., a_{n}] for some 1 <= i <= n-1). This operation decreases the size of a by 1. Select k consecutive elements of a and replace them with their maximum (that is, let a := [a_{1}, a_{2}, ..., a_{i-1}, \max(a_{i}, a_{i+1}, ..., a_{i+k-1}), a_{i+k}, ..., a_{n}] for some 1 <= i <= n-k+1). This operation decreases the size of a by k-1. Determine if it's possible to turn a into [1] after several (possibly zero) operations. | Input: ['73 20 1 05 31 0 1 1 02 21 14 40 0 0 06 30 0 1 0 0 17 51 1 1 1 1 1 15 30 0 1 0 0'] Output:['YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', ''] | [
2
] |
You are given a permutation p_1, p_2, ..., p_n of length n of numbers 0, ..., n - 1. Count the number of subsegments 1 <=q l <=q r <=q n of this permutation such that mex(p_l, p_{l+1}, ..., p_r) > med(p_l, p_{l+1}, ..., p_r).mex of S is the smallest non-negative integer that does not occur in S. For example: mex({0, 1, 2, 3}) = 4 mex({0, 4, 1, 3}) = 2 mex({5, 4, 0, 1, 2}) = 3med of the set S is the median of the set, i.e. the element that, after sorting the elements in non-decreasing order, will be at position number <=ft \lfloor{ \frac{|S| + 1}{2} } \right \rfloor (array elements are numbered starting from 1 and here <=ft \lfloor{v} \right \rfloor denotes rounding v down.). For example: med({0, 1, 2, 3}) = 1 med({0, 4, 1, 3}) = 1 med({5, 4, 0, 1, 2}) = 2A sequence of n numbers is called a permutation if it contains all the numbers from 0 to n - 1 exactly once. | Input: ['81021 031 0 240 2 1 353 1 0 2 462 0 4 1 3 583 7 2 6 0 1 5 442 0 1 3'] Output:['1', '2', '4', '4', '8', '8', '15', '6', ''] | [
3
] |
This is an hard version of the problem. The only difference between an easy and a hard version is the constraints on a, b, c and d.You are given 4 positive integers a, b, c, d with a < c and b < d. Find any pair of numbers x and y that satisfies the following conditions: a < x <=q c, b < y <=q d, x \cdot y is divisible by a \cdot b.Note that required x and y may not exist. | Input: ['101 1 2 23 4 5 78 9 15 1812 21 14 2436 60 48 661024 729 373248 7301024 729 373247 7305040 40320 40319 1000000000999999999 999999999 1000000000 1000000000268435456 268435456 1000000000 1000000000'] Output:['2 2', '4 6', '12 12', '-1 -1', '-1 -1', '373248 730', '-1 -1', '15120 53760', '-1 -1', '536870912 536870912', ''] | [
0,
3
] |
This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on a, b, c and d.You are given 4 positive integers a, b, c, d with a < c and b < d. Find any pair of numbers x and y that satisfies the following conditions: a < x <=q c, b < y <=q d, x \cdot y is divisible by a \cdot b.Note that required x and y may not exist. | Input: ['51 1 2 23 4 5 78 9 15 1812 21 14 2436 60 48 66'] Output:['2 2', '4 6', '12 12', '-1 -1', '-1 -1', ''] | [
0,
3
] |
You are given an array of positive integers a_1, a_2, ..., a_n.Make the product of all the numbers in the array (that is, a_1 \cdot a_2 \cdot ... \cdot a_n) divisible by 2^n.You can perform the following operation as many times as you like: select an arbitrary index i (1 <=q i <=q n) and replace the value a_i with a_i=a_i \cdot i. You cannot apply the operation repeatedly to a single index. In other words, all selected values of i must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by 2^n. Note that such a set of operations does not always exist. | Input: ['61223 2310 6 11413 17 1 151 1 12 1 1620 7 14 18 3 5'] Output:['0', '1', '1', '-1', '2', '1', ''] | [
2,
3
] |
You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every n seconds and at the i-th second the color s_i is on.That way, the order of the colors is described by a string. For example, if s="rggry", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.More formally, you are given a string s_1, s_2, ..., s_n of length n. At the first second the color s_1 is on, at the second β s_2, ..., at the n-th second the color s_n is on, at the n + 1-st second the color s_1 is on and so on.You need to cross the road and that can only be done when the green color is on. You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.You can assume that you cross the road immediately. For example, with s="rggry" and the current color r there are two options: either the green color will be on after 1 second, or after 3. That way, the answer is equal to 3 β that is the number of seconds that we are guaranteed to cross the road, if the current color is r. | Input: ['65 rrggry1 gg3 rrrg5 yyrrgy7 rrgrgyrg9 yrrrgyyygy'] Output:['3', '0', '2', '4', '1', '4', ''] | [
4
] |
You are given n of integers a_1, a_2, ..., a_n. Process q queries of two types: query of the form "0 x_j": add the value x_j to all even elements of the array a, query of the form "1 x_j": add the value x_j to all odd elements of the array a.Note that when processing the query, we look specifically at the odd/even value of a_i, not its index.After processing each query, print the sum of the elements of the array a.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). | Input: ['41 111 13 31 2 40 21 30 56 71 3 2 4 10 481 60 50 40 51 30 120 16 71000000000 1000000000 1000000000 11 15 170 171 100001 510 920 531 160 1'] Output:['2', '11', '14', '29', '80', '100', '100', '100', '118', '190', '196', '3000000094', '3000060094', '3000060400', '3000060952', '3000061270', '3000061366', '3000061366', ''] | [
3
] |
An integer array a_1, a_2, ..., a_n is being transformed into an array of lowercase English letters using the following prodecure:While there is at least one number in the array: Choose any number x from the array a, and any letter of the English alphabet y. Replace all occurrences of number x with the letter y. For example, if we initially had an array a = [2, 3, 2, 4, 1], then we could transform it the following way: Choose the number 2 and the letter c. After that a = [c, 3, c, 4, 1]. Choose the number 3 and the letter a. After that a = [c, a, c, 4, 1]. Choose the number 4 and the letter t. After that a = [c, a, c, t, 1]. Choose the number 1 and the letter a. After that a = [c, a, c, t, a]. After the transformation all letters are united into a string, in our example we get the string "cacta".Having the array a and the string s determine if the string s could be got from the array a after the described transformation? | Input: ['752 3 2 4 1cacta150a211 22ab41 2 2 1aaab51 2 3 2 1aaaaa61 10 2 9 3 8azzfdb71 2 3 4 1 1 2abababb'] Output:['YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'NO', ''] | [
2
] |
Note that the memory limit is unusual.Let's define the sequence of Fibonacci strings as follows: f_0 is 0, f_1 is 1, f_i is f_{i-1} + f_{i-2} for i>1 (+ denotes the concatenation of two strings). So, for example, f_2 is 10, f_3 is 101, f_4 is 10110.For a given string s, let's define g(s) as the number of ways to cut it into several (any number, possibly even just one) strings such that none of these strings are Fibonacci strings. For example, if s is 10110101, g(s) = 3 since there are three ways to cut it: 101101 + 01; 1011 + 0101; 1011 + 01 + 01. You are given a sequence of strings s_1, s_2, ..., s_n. Calculate g(s_1), g(s_1 + s_2), ..., g(s_1 + s_2 + ... + s_n). Since these values can be huge, print them modulo 998244353. | Input: ['1', '10110101', ''] Output:['3', ''] | [
3
] |
Monocarp is playing a video game. In the game, he controls a spaceship and has to destroy an enemy spaceship.Monocarp has two lasers installed on his spaceship. Both lasers 1 and 2 have two values: p_i β the power of the laser; t_i β the reload time of the laser. When a laser is fully charged, Monocarp can either shoot it or wait for the other laser to charge and shoot both of them at the same time.An enemy spaceship has h durability and s shield capacity. When Monocarp shoots an enemy spaceship, it receives (P - s) damage (i.βe. (P - s) gets subtracted from its durability), where P is the total power of the lasers that Monocarp shoots (i.βe. p_i if he only shoots laser i and p_1 + p_2 if he shoots both lasers at the same time). An enemy spaceship is considered destroyed when its durability becomes 0 or lower.Initially, both lasers are zero charged.What's the lowest amount of time it can take Monocarp to destroy an enemy spaceship? | Input: ['5 10', '4 9', '16 1', ''] Output:['20', ''] | [
4
] |
You are given a string s consisting of n characters. Each character of s is either 0 or 1.A substring of s is a contiguous subsequence of its characters.You have to choose two substrings of s (possibly intersecting, possibly the same, possibly non-intersecting β just any two substrings). After choosing them, you calculate the value of the chosen pair of substrings as follows: let s_1 be the first substring, s_2 be the second chosen substring, and f(s_i) be the integer such that s_i is its binary representation (for example, if s_i is 11010, f(s_i) = 26); the value is the bitwise OR of f(s_1) and f(s_2). Calculate the maximum possible value you can get, and print it in binary representation without leading zeroes. | Input: ['5', '11010', ''] Output:['11111', ''] | [
0,
2
] |
Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between n boxes, arranged in a row. The i-th box contains a_i magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the i-th box was covered with a lid initially, he can either move the lid from the i-th box to the box (i-1) (if it exists), or keep the lid on the i-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.You have to calculate the maximum number of magazines Monocarp can save from the rain. | Input: ['450111010 5 8 9 6601101120 10 9 30 20 1940000100 100 100 100401115 4 5 1'] Output:['27', '80', '0', '14', ''] | [
2
] |
You are given an integer n. You have to construct a permutation of size n.A permutation is an array where each integer from 1 to s (where s is the size of permutation) occurs exactly once. For example, [2, 1, 4, 3] is a permutation of size 4; [1, 2, 4, 5, 3] is a permutation of size 5; [1, 4, 3] is not a permutation (the integer 2 is absent), [2, 1, 3, 1] is not a permutation (the integer 1 appears twice).A subsegment of a permutation is a contiguous subsequence of that permutation. For example, the permutation [2, 1, 4, 3] has 10 subsegments: [2], [2, 1], [2, 1, 4], [2, 1, 4, 3], [1], [1, 4], [1, 4, 3], [4], [4, 3] and [3].The value of the permutation is the number of its subsegments which are also permutations. For example, the value of [2, 1, 4, 3] is 3 since the subsegments [2, 1], [1] and [2, 1, 4, 3] are permutations.You have to construct a permutation of size n with minimum possible value among all permutations of size n. | Input: ['256'] Output:['1 4 3 5 2', '4 1 6 2 5 3', ''] | [
2
] |
Monocarp has forgotten the password to his mobile phone. The password consists of 4 digits from 0 to 9 (note that it can start with the digit 0).Monocarp remembers that his password had exactly two different digits, and each of these digits appeared exactly two times in the password. Monocarp also remembers some digits which were definitely not used in the password.You have to calculate the number of different sequences of 4 digits that could be the password for Monocarp's mobile phone (i.βe. these sequences should meet all constraints on Monocarp's password). | Input: ['280 1 2 4 5 6 8 918'] Output:['6', '216', ''] | [
0,
3
] |
You are given an array a consisting of n nonnegative integers. Let's define the prefix OR array b as the array b_i = a_1~\mathsf{OR}~a_2~\mathsf{OR}~...~\mathsf{OR}~a_i, where \mathsf{OR} represents the bitwise OR operation. In other words, the array b is formed by computing the \mathsf{OR} of every prefix of a.You are asked to rearrange the elements of the array a in such a way that its prefix OR array is lexicographically maximum.An array x is lexicographically greater than an array y if in the first position where x and y differ, x_i > y_i. | Input: ['541 2 4 875 1 2 3 4 5 521 10162 3 4 2 3 481 4 2 3 4 5 7 1'] Output:['8 4 2 1 ', '5 2 1 3 4 5 5 ', '101 1 ', '4 3 2 2 3 4 ', '7 1 4 2 3 4 5 1 ', ''] | [
0,
2,
3
] |
Alperen has two strings, s and t which are both initially equal to "a". He will perform q operations of two types on the given strings: 1 \;\; k \;\; x β Append the string x exactly k times at the end of string s. In other words, s := s + \underbrace{x + ... + x}_{k \text{ times}}. 2 \;\; k \;\; x β Append the string x exactly k times at the end of string t. In other words, t := t + \underbrace{x + ... + x}_{k \text{ times}}. After each operation, determine if it is possible to rearrange the characters of s and t such that s is lexicographically smaller than t.Note that the strings change after performing each operation and don't go back to their initial states. Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string p is lexicographically smaller than string q if there exists a position i such that p_i < q_i, and for all j < i, p_j = q_j. If no such i exists, then p is lexicographically smaller than q if the length of p is less than the length of q. For example, \texttt{abdc} < \texttt{abe} and \texttt{abc} < \texttt{abcd}, where we write p < q if p is lexicographically smaller than q. | Input: ['352 1 aa1 2 a2 3 a1 2 b2 3 abca21 5 mihai2 2 buiucani31 5 b2 3 a2 4 paiu'] Output:['YES', 'NO', 'YES', 'NO', 'YES', 'NO', 'YES', 'NO', 'NO', 'YES', ''] | [
2
] |
Timur has a stairway with n steps. The i-th step is a_i meters higher than its predecessor. The first step is a_1 meters higher than the ground, and the ground starts at 0 meters. The stairs for the first test case. Timur has q questions, each denoted by an integer k_1, ..., k_q. For each question k_i, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length k_i. Timur can only climb the j-th step if his legs are of length at least a_j. In other words, k_i >=q a_j for each step j climbed.Note that you should answer each question independently. | Input: ['34 51 2 1 51 2 4 9 102 21 10 13 11000000000 1000000000 10000000001000000000'] Output:['1 4 4 9 9 ', '0 2 ', '3000000000 ', ''] | [
2,
3,
4
] |
Given an array of n positive integers a_1, a_2, ..., a_n (1 <= a_i <= 1000). Find the maximum value of i + j such that a_i and a_j are coprime, or -1 if no such i, j exist.For example consider the array [1, 3, 5, 2, 4, 7, 7]. The maximum value of i + j that can be obtained is 5 + 7, since a_5 = 4 and a_7 = 7 are coprime. Two integers p and q are coprime if the only positive integer that is a divisor of both of them is 1 (that is, their greatest common divisor is 1). | Input: ['633 2 171 3 5 2 4 7 751 2 3 4 532 2 465 4 3 15 12 1651 2 2 3 6'] Output:['6', '12', '9', '-1', '10', '7', ''] | [
0,
2
] |
You are given an array a of n positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that a_1 < a_2 < ... < a_n holds. | Input: ['341 1 1 158 7 1 3 415'] Output:['NO', 'YES', 'YES', ''] | [
2
] |
Kirill lives on a connected undirected graph of n vertices and m edges at vertex 1. One fine evening he gathered f friends, the i-th friend lives at the vertex h_i. So all friends are now in the vertex 1, the i-th friend must get to his home to the vertex h_i.The evening is about to end and it is time to leave. It turned out that k (k <= 6) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.For example, in the graph below, a friend from vertex h_i=5 can give a ride to friends from the following sets of vertices: [2, 3], [2, 4], [2], [3], [4], but can't give a ride to friend from vertex 6 or a set [3, 4]. The vertices where friends without cars live are highlighted in green, and with cars β in red. Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number. | Input: ['36 71 22 32 43 54 53 66 552 3 4 5 641 2 3 56 71 22 32 43 54 53 66 562 3 4 5 6 541 2 3 54 41 21 32 33 433 4 221 3'] Output:['2', '1', '1', ''] | [
0
] |
Dmitry has n segments of different colors on the coordinate axis Ox. Each segment is characterized by three integers l_i, r_i and c_i (1 <= l_i <= r_i <= 10^9, 1 <= c_i <= n), where l_i and r_i are are the coordinates of the ends of the i-th segment, and c_i is its color.Dmitry likes to find the minimum distances between segments. However, he considers pairs of segments of the same color uninteresting. Therefore, he wants to know for each segment the distance from this segment to the nearest differently colored segment.The distance between two segments is the minimum of the distances between a point of the first segment and a point of the second segment. In particular, if the segments intersect, then the distance between them is equal to 0.For example, Dmitry has 5 segments: The first segment intersects with the second (and these are segments of different colors), so the answers for them are equal to 0. For the 3-rd segment, the nearest segment of a different color is the 2-nd segment, the distance to which is equal to 2. For the 4-th segment, the nearest segment of a different color is the 5-th segment, the distance to which is equal to 1. The 5-th segment lies inside the 2-nd segment (and these are segments of different colors), so the answers for them are equal to 0. | Input: ['931 2 13 4 15 6 22100000000 200000000 1900000000 1000000000 251 2 12 3 23 4 34 5 45 6 551 5 14 9 21 2 18 9 25 7 321 100 210 90 131 1 110 10 21000000000 1000000000 333 4 12 5 11 6 265 6 211 12 37 8 23 4 21 2 19 10 221 3 12 3 2'] Output:['3 1 1 ', '700000000 700000000 ', '0 0 0 0 0 ', '0 0 2 1 0 ', '0 0 ', '9 9 999999990 ', '0 0 0 ', '3 1 3 1 1 1 ', '0 0 ', ''] | [
3,
4
] |
You are given a sequence a=[a_1,a_2,...,a_n] consisting of n positive integers.Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by a[l,r] a segment of the sequence a with the left end in l and the right end in r, i.e. a[l,r]=[a_l, a_{l+1}, ..., a_r].For example, if a=[31,4,15,92,6,5], then a[2,5]=[4,15,92,6], a[5,5]=[6], a[1,6]=[31,4,15,92,6,5] are segments.We split the given sequence a into segments so that: each element is in exactly one segment; the sums of elements for all segments are equal. For example, if a = [55,45,30,30,40,100], then such a sequence can be split into three segments: a[1,2]=[55,45], a[3,5]=[30, 30, 40], a[6,6]=[100]. Each element belongs to exactly segment, the sum of the elements of each segment is 100.Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is 3.Find the minimum thickness among all possible splits of the given sequence of a into segments in the required way. | Input: ['4655 45 30 30 40 100410 23 7 13510 55 35 30 6564 1 1 1 1 4'] Output:['3', '4', '2', '3', ''] | [
0,
2,
3
] |
A sequence of n numbers is called permutation if it contains all numbers from 1 to n exactly once. For example, the sequences [3, 1, 4, 2], [1] and [2,1] are permutations, but [1,2,1], [0,1] and [1,3,4] are not.For a given number n you need to make a permutation p such that two requirements are satisfied at the same time: For each element p_i, at least one of its neighbors has a value that differs from the value of p_i by one. That is, for each element p_i (1 <= i <= n), at least one of its neighboring elements (standing to the left or right of p_i) must be p_i + 1, or p_i - 1. the permutation must have no fixed points. That is, for every i (1 <= i <= n), p_i \neq i must be satisfied. Let's call the permutation that satisfies these requirements funny.For example, let n = 4. Then [4, 3, 1, 2] is a funny permutation, since: to the right of p_1=4 is p_2=p_1-1=4-1=3; to the left of p_2=3 is p_1=p_2+1=3+1=4; to the right of p_3=1 is p_4=p_3+1=1+1=2; to the left of p_4=2 is p_3=p_4-1=2-1=1. for all i is p_i!=i. For a given positive integer n, output any funny permutation of length n, or output -1 if funny permutation of length n does not exist. | Input: ['543752'] Output:['3 4 2 1', '-1', '6 7 4 5 3 2 1', '5 4 1 2 3', '2 1'] | [
3
] |
Pak Chanek is given an array a of n integers. For each i (1 <=q i <=q n), Pak Chanek will write the one-element set \{a_i\} on a whiteboard.After that, in one operation, Pak Chanek may do the following: Choose two different sets S and T on the whiteboard such that S \cap T = \varnothing (S and T do not have any common elements). Erase S and T from the whiteboard and write S \cup T (the union of S and T) onto the whiteboard. After performing zero or more operations, Pak Chanek will construct a multiset M containing the sizes of all sets written on the whiteboard. In other words, each element in M corresponds to the size of a set after the operations.How many distinct^ multisets M can be created by this process? Since the answer may be large, output it modulo 998\,244\,353.^ Multisets B and C are different if and only if there exists a value k such that the number of elements with value k in B is different than the number of elements with value k in C. | Input: ['6', '1 1 2 1 4 3', ''] Output:['7', ''] | [
3
] |
Pak Chanek has n blank heart-shaped cards. Card 1 is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card i (i > 1) is hanging onto card p_i (p_i < i).In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation a of [1, 2, ..., n]. Then, the number written on card i is a_i.After that, Pak Chanek must do the following operation n times while maintaining a sequence s (which is initially empty): Choose a card x such that no other cards are hanging onto it. Append the number written on card x to the end of s. If x \neq 1 and the number on card p_x is larger than the number on card x, replace the number on card p_x with the number on card x. Remove card x. After that, Pak Chanek will have a sequence s with n elements. What is the maximum length of the longest non-decreasing subsequence^ of s at the end if Pak Chanek does all the steps optimally?^ A sequence b is a subsequence of a sequence c if b can be obtained from c by deletion of several (possibly, zero or all) elements. For example, [3,1] is a subsequence of [3,2,1], [4,3,1] and [3,1], but not [1,3,3,7] and [3,10,4]. | Input: ['6', '1 2 1 4 2', ''] Output:['4', ''] | [
2
] |
There are n bricks numbered from 1 to n. Brick i has a weight of a_i.Pak Chanek has 3 bags numbered from 1 to 3 that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let w_j be the weight of the brick Bu Dengklek takes from bag j. The score is calculated as |w_1 - w_2| + |w_2 - w_3|, where |x| denotes the absolute value of x.It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally? | Input: ['353 1 5 2 3417 8 19 458265 265 265 265 265 265 265 265'] Output:['6', '63', '0', ''] | [
2
] |
Pak Chanek has n two-dimensional slices of cheese. The i-th slice of cheese can be represented as a rectangle of dimensions a_i * b_i. We want to arrange them on the two-dimensional plane such that: Each edge of each cheese is parallel to either the x-axis or the y-axis. The bottom edge of each cheese is a segment of the x-axis. No two slices of cheese overlap, but their sides can touch. They form one connected shape. Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.Find the minimum possible perimeter of the constructed shape. | Input: ['344 14 51 12 332 42 62 312 65'] Output:['26', '24', '134', ''] | [
2
] |
You are given a rooted tree, consisting of n vertices. The vertices are numbered from 1 to n, the root is the vertex 1.You can perform the following operation at most k times: choose an edge (v, u) of the tree such that v is a parent of u; remove the edge (v, u); add an edge (1, u) (i.βe. make u with its subtree a child of the root). The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex 1 is 0, since it's the root, and the depth of all its children is 1.What's the smallest height of the tree that can be achieved? | Input: ['55 11 1 2 25 21 1 2 26 01 2 3 4 56 11 2 3 4 54 31 1 1'] Output:['2', '1', '5', '3', '1', ''] | [
2,
4
] |
For an array of non-negative integers a of size n, we construct another array d as follows: d_1 = a_1, d_i = |a_i - a_{i - 1}| for 2 <= i <= n.Your task is to restore the array a from a given array d, or to report that there are multiple possible arrays. | Input: ['341 0 2 532 6 350 0 0 0 0'] Output:['1 1 3 8', '-1', '0 0 0 0 0', ''] | [
2,
3
] |
You are given an n * n grid. We write (i, j) to denote the cell in the i-th row and j-th column. For each cell, you are told whether yon can delete it or not. Given an integer k, you are asked to delete exactly (n-k+1)^2 cells from the grid such that the following condition holds. You cannot find k not deleted cells (x_1, y_1), (x_2, y_2), ..., (x_k, y_k) that are strictly increasing, i.e., x_i < x_{i+1} and y_i < y_{i+1} for all 1 <=q i < k. Your task is to find a solution, or report that it is impossible. | Input: ['42 210014 311100101101001115 501111101111101111101111105 21000001111011110111101111'] Output:['YES', '01', '11', 'YES', '0011', '1111', '1111', '1100', 'NO', 'YES', '01111', '11000', '10000', '10000', '10000', ''] | [
2,
3
] |
This is an interactive problem.Given a simple undirected graph with n vertices numbered from 1 to n, your task is to color all the vertices such that for every color c, the following conditions hold: The set of vertices with color c is connected; s_c <=q n_c^2, where n_c is the number of vertices with color c, and s_c is the sum of degrees of vertices with color c. It can be shown that there always exists a way to color all the vertices such that the above conditions hold. Initially, you are only given the number n of vertices and the degree of each vertex. In each query, you can choose a vertex u. As a response, you will be given the k-th edge incident to u, if this is the k-th query on vertex u. You are allowed to make at most n queries.An undirected graph is simple if it does not contain multiple edges or self-loops.The degree of a vertex is the number of edges incident to it. A set S of vertices is connected if for every two different vertices u, v \in S, there is a path, which only passes through vertices in S, that connects u and v. That is, there is a sequence of edges (u_1, v_1), (u_2, v_2), ..., (u_k, v_k) with k >=q 1 such that u_1 = u, v_k = v, and v_i = u_{i+1} for every 1 <=q i < k; and u_k \in S and v_k \in S for every 1 <=q i <=q k. Especially, a set containing only one vertex is connected. | Input: ['1', '5', '2 2 2 2 0', '', '2', '', '4', '', '2', '', '4', ''] Output:['', '', '', '? 1', '', '? 1', '', '? 3', '', '? 3', '', '! 1 1 2 2 3', ''] | [
2
] |
Given an integer sequence a_1, a_2, ..., a_n of length n, your task is to compute the number, modulo 998244353, of ways to partition it into several non-empty continuous subsequences such that the sums of elements in the subsequences form a balanced sequence.A sequence s_1, s_2, ..., s_k of length k is said to be balanced, if s_{i} = s_{k-i+1} for every 1 <=q i <=q k. For example, [1, 2, 3, 2, 1] and [1,3,3,1] are balanced, but [1,5,15] is not. Formally, every partition can be described by a sequence of indexes i_1, i_2, ..., i_k of length k with 1 = i_1 < i_2 < ... < i_k <=q n such that k is the number of non-empty continuous subsequences in the partition; For every 1 <=q j <=q k, the j-th continuous subsequence starts with a_{i_j}, and ends exactly before a_{i_{j+1}}, where i_{k+1} = n + 1. That is, the j-th subsequence is a_{i_j}, a_{i_j+1}, ..., a_{i_{j+1}-1}. There are 2^{n-1} different partitions in total. Let s_1, s_2, ..., s_k denote the sums of elements in the subsequences with respect to the partition i_1, i_2, ..., i_k. Formally, for every 1 <=q j <=q k, s_j = \sum_{i=i_{j}}^{i_{j+1}-1} a_i = a_{i_j} + a_{i_j+1} + ... + a_{i_{j+1}-1}. For example, the partition [1\,|\,2,3\,|\,4,5,6] of sequence [1,2,3,4,5,6] is described by the sequence [1,2,4] of indexes, and the sums of elements in the subsequences with respect to the partition is [1,5,15].Two partitions i_1, i_2, ..., i_k and i'_1, i'_2, ..., i'_{k'} (described by sequences of indexes) are considered to be different, if at least one of the following holds. k \neq k', i_j \neq i'_j for some 1 <=q j <=q \min<=ft\{ k, k' \right\}. | Input: ['61100000000021 140 0 1 051 2 3 2 151 3 5 7 9320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'] Output:['1', '2', '3', '4', '2', '150994942', ''] | [
3
] |
Alice and Bob are playing a game on a sequence a_1, a_2, ..., a_n of length n. They move in turns and Alice moves first.In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her selected integers is even; otherwise, Bob wins. Your task is to determine who will win the game, if both players play optimally. | Input: ['431 3 541 3 5 741 2 3 4410 20 30 40'] Output:['Alice', 'Alice', 'Bob', 'Alice', ''] | [
2,
3
] |
Suppose a_1, a_2, ..., a_n is a sorted integer sequence of length n such that a_1 <=q a_2 <=q ... <=q a_n. For every 1 <=q i <=q n, the prefix sum s_i of the first i terms a_1, a_2, ..., a_i is defined by s_i = \sum_{k=1}^i a_k = a_1 + a_2 + ... + a_i. Now you are given the last k terms of the prefix sums, which are s_{n-k+1}, ..., s_{n-1}, s_{n}. Your task is to determine whether this is possible. Formally, given k integers s_{n-k+1}, ..., s_{n-1}, s_{n}, the task is to check whether there is a sequence a_1, a_2, ..., a_n such that a_1 <=q a_2 <=q ... <=q a_n, and s_i = a_1 + a_2 + ... + a_i for all n-k+1 <=q i <=q n. | Input: ['45 51 2 3 4 57 4-6 -5 -3 03 32 3 43 23 4'] Output:['Yes', 'Yes', 'No', 'No', ''] | [
2,
3
] |
The hero is addicted to glory, and is fighting against a monster. The hero has n skills. The i-th skill is of type a_i (either fire or frost) and has initial damage b_i. The hero can perform all of the n skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows: If the current skill immediately follows another skill of a different type, then its damage is doubled. In other words, If a skill of type fire and with initial damage c is performed immediately after a skill of type fire, then it will deal c damage; If a skill of type fire and with initial damage c is performed immediately after a skill of type frost, then it will deal 2c damage; If a skill of type frost and with initial damage c is performed immediately after a skill of type fire, then it will deal 2c damage; If a skill of type frost and with initial damage c is performed immediately after a skill of type frost , then it will deal c damage. Your task is to find the maximum damage the hero can deal. | Input: ['440 1 1 11 10 100 100060 0 0 1 1 13 4 5 6 7 831 1 11000000000 1000000000 1000000000111'] Output:['2112', '63', '3000000000', '1', ''] | [
2
] |
DTL engineers love partying in the weekend. Ela does, too! Unfortunately, she didn't know how to dance yet. Therefore, she decided to take a dancing class.There are n students in the dancing class, including Ela. In the final project, n students will participate in a choreography described below.n students are positioned on the positive side of the Ox-axis. The i-th dancer is located at a_i > 0. Some dancers will change positions during the dance (we'll call them movable dancers), and others will stay in the same place during a choreography (we'll call them immovable dancers). We distinguish the dancers using a binary string s of length n: if s_i equals '1', then the i-th dancer is movable, otherwise the i-th dancer is immovable.Let's call the "positive energy value" of the choreography d > 0. The dancers will perform "movements" based on this value.Each minute after the dance begins, the movable dancer with the smallest x-coordinate will start moving to the right and initiate a "movement". At the beginning of the movement, the dancer's energy level will be initiated equally to the positive energy value of the choreography, which is d. Each time they move from some y to y+1, the energy level will be decreased by 1. At some point, the dancer might meet other fellow dancers in the same coordinates. If it happens, then the energy level of the dancer will be increased by 1. A dancer will stop moving to the right when his energy level reaches 0, and he doesn't share a position with another dancer.The dancers are very well-trained, and each "movement" will end before the next minute begins.To show her understanding of this choreography, Ela has to answer q queries, each consisting of two integers k and m. The answer to this query is the coordinate of the m-th dancer of both types from the left at k-th minute after the choreography begins. In other words, denote x_{k, 1}, x_{k, 2}, ..., x_{k, n} as the sorted coordinates of the dancers at k-th minute from the beginning, you need to print x_{k, m}. | Input: ['4 3 8', '1 3 6 7', '1011', '1 1', '1 2', '1 3', '1 4', '2 1', '2 2', '2 3', '2 4', ''] Output:['3', '5', '6', '7', '3', '6', '7', '10', ''] | [
4
] |
After a long, tough, but fruitful day at DTL, Ela goes home happily. She entertains herself by solving Competitive Programming problems. She prefers short statements, because she already read too many long papers and documentation at work. The problem of the day reads:You are given an integer c. Suppose that c has n divisors. You have to find a sequence with n - 1 integers [a_1, a_2, ... a_{n - 1}], which satisfies the following conditions: Each element is strictly greater than 1. Each element is a divisor of c. All elements are distinct. For all 1 <= i < n - 1, \gcd(a_i, a_{i + 1}) is a prime number. In this problem, because c can be too big, the result of prime factorization of c is given instead. Note that \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y and a prime number is a positive integer which has exactly 2 divisors. | Input: ['521 11131 1 11422 1'] Output:['0 1 ', '1 1 ', '1 0 ', '1 ', '0 1 1 ', '0 0 1 ', '1 0 1 ', '1 1 0 ', '0 1 0 ', '1 1 1 ', '1 0 0 ', '-1', '2 0 ', '1 1 ', '0 1 ', '2 1 ', '1 0 ', ''] | [
3
] |
Ela likes to go hiking a lot. She loves nature and exploring the various creatures it offers. One day, she saw a strange type of ant, with a cannibalistic feature. More specifically, an ant would eat any ants that it sees which is smaller than it.Curious about this feature from a new creature, Ela ain't furious. She conducts a long, non-dubious, sentimental experiment.She puts n cannibalistic ants in a line on a long wooden stick. Initially, the ants have the same weight of 1. The distance between any two consecutive ants is the same. The distance between the first ant in the line to the left end and the last ant in the line to the right end is also the same as the distance between the ants. Each ant starts moving towards the left-end or the right-end randomly and equiprobably, at the same constant pace throughout the experiment. Two ants will crash if they are standing next to each other in the line and moving in opposite directions, and ants will change direction immediately when they reach the end of the stick. Ela can't determine the moving direction of each ant, but she understands very well their behavior when crashes happen. If a crash happens between two ants of different weights, the heavier one will eat the lighter one, and gain the weight of the lighter one. After that, the heavier and will continue walking in the same direction. In other words, if the heavier one has weight x and walking to the right, the lighter one has weight y and walking to the left (x > y), then after the crash, the lighter one will diminish, and the heavier one will have weight x + y and continue walking to the right. If a crash happens between two ants with the same weight, the one walking to the left end of the stick will eat the one walking to the right, and then continue walking in the same direction. In other words, if one ant of weight x walking to the left, crashes with another ant of weight x walking to the right, the one walking to the right will disappear, and the one walking to the left will have to weight 2x and continue walking to the left. Please, check the example in the "Note" section, which will demonstrate the ants' behavior as above.We can prove that after a definite amount of time, there will be only one last ant standing. Initially, each ant can randomly and equiprobably move to the left or the right, which generates 2^n different cases of initial movements for the whole pack. For each position in the line, calculate the probability that the ant begins in that position and survives. Output it modulo 10^9 + 7.Formally, let M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output such an integer x that 0 <= x < M and x \cdot q \equiv p \pmod{M}. | Input: ['3452'] Output:['0', '250000002', '250000002', '500000004', '0', '250000002', '250000002', '250000002', '250000002', '0', '1', ''] | [
3
] |
Ela needs to send a large package from machine 1 to machine n through a network of machines. Currently, with the network condition, she complains that the network is too slow and the package can't arrive in time. Luckily, a Wiring Wizard offered her a helping hand.The network can be represented as an undirected connected graph with n nodes, each node representing a machine. m wires are used to connect them. Wire i is used to connect machines u_i and v_i, and has a weight w_i. The aforementioned large package, if going through wire i, will move from machine u_i to machine v_i (or vice versa) in exactly w_i microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index i, connecting machine u_i and v_i, and rewire it following these steps: Choose one machine that is connected by this wire. Without loss of generality, let's choose v_i. Choose a machine that is currently connecting to v_i (including u_i), call it t_i. Disconnect the wire indexed i from v_i, then using it to connect u_i and t_i. The rewiring of wire i will takes w_i microseconds, and the weight of the wire will not change after this operation. After a rewiring, a machine might have some wire connect it with itself. Also, the Wiring Wizard has warned Ela that rewiring might cause temporary disconnections between some machines, but Ela just ignores it anyway. Her mission is to send the large package from machine 1 to machine n as fast as possible. Note that the Wizard can use his spell on a wire zero, one, or many times. To make sure the network works seamlessly while transferring the large package, once the package starts transferring from machine 1, the Wiring Wizard cannot use his spell to move wires around anymore.Ela wonders, with the help of the Wiring Wizard, what is the least amount of time needed to transfer the large package from machine 1 to n. | Input: ['38 91 2 36 4 53 5 66 1 37 4 43 8 42 3 37 8 54 5 24 51 2 12 4 13 4 13 1 11 3 28 84 6 927 1 656 5 436 7 964 3 744 8 547 4 992 5 22'] Output:['9', '2', '154', ''] | [
0,
2
] |
Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game.The problem, which involves a non-standard chess pieces type that is described below, reads: given 3 white crickets on a n \cdot n board, arranged in an "L" shape next to each other, there are no other pieces on the board. Ela wants to know with a finite number of moves, can she put any white cricket on the square on row x, column y?An "L"-shape piece arrangement can only be one of the below: For simplicity, we describe the rules for crickets on the board where only three white crickets are. It can move horizontally, vertically, or diagonally, but only to a square in some direction that is immediately after another cricket piece (so that it must jump over it). If the square immediately behind the piece is unoccupied, the cricket will occupy the square. Otherwise (when the square is occupied by another cricket, or does not exist), the cricket isn't allowed to make such a move.See an example of valid crickets' moves on the pictures in the Note section. | Input: ['687 2 8 2 7 15 182 2 1 2 2 15 582 2 1 2 2 16 681 1 1 2 2 15 582 2 1 2 2 18 888 8 8 7 7 84 8'] Output:['YES', 'NO', 'YES', 'NO', 'YES', 'YES', ''] | [
3
] |
While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being "Luxury" days. She counts the days since she started these activities, in which the day she starts is numbered as day 1. A "Luxury" day is the day in which the number of this day is a luxurious number. An integer x is called a luxurious number if it is divisible by {\lfloor \sqrt{x} \rfloor}.Here \lfloor r \rfloor denotes the "floor" of a real number r. In other words, it's the largest integer not greater than r.For example: 8, 56, 100 are luxurious numbers, since 8 is divisible by \lfloor \sqrt{8} \rfloor = \lfloor 2.8284 \rfloor = 2, 56 is divisible \lfloor \sqrt{56} \rfloor = \lfloor 7.4833 \rfloor = 7, and 100 is divisible by \lfloor \sqrt{100} \rfloor = \lfloor 10 \rfloor = 10, respectively. On the other hand 5, 40 are not, since 5 are not divisible by \lfloor \sqrt{5} \rfloor = \lfloor 2.2361 \rfloor = 2, and 40 are not divisible by \lfloor \sqrt{40} \rfloor = \lfloor 6.3246 \rfloor = 6.Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day l and day r, you want to know how many times she changes the activities. | Input: ['58 198 20119 1211 1000000000000000001234567891011 1000000000000000000'] Output:['5', '6', '2', '948683296', '2996666667', ''] | [
3,
4
] |
Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.n books must be split into k compartments on the bookshelf (n is divisible by k). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.Ela must stack exactly \frac{n}{k} books in each compartment. After the books are stacked, for each compartment indexed from 1 to k, she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.What is the lexicographically greatest resulting string possible that Ela can create?A string a is lexicographically greater than a string b if and only if one of the following holds: b is a prefix of a, but b!=a; in the first position where a and b differ, the string a has a letter that appears later in the alphabet than the corresponding letter in b. The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains 7 letters 'b', 'a', 'b', 'c', 'e', 'c', 'f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a', 'b' and 'c', are included in the multiset. | Input: ['512 3cabccadabaac12 6cabccadabaac12 12cabccadabaac25 1abcdefghijklmnopqrstuvwxy10 5bcdxedbcfg'] Output:['edb', 'ccbbba', 'bbbbbaaaaaaa', 'z', 'aaaaa', ''] | [
2
] |
You're given an array consisting of n integers. You have to perform n turns.Initially your score is 0.On the i-th turn, you are allowed to leave the array as it is or swap any one pair of 2 adjacent elements in the array and change exactly one of them to 0(and leave the value of other element unchanged) after swapping. In either case(whether you swap or not), after this you add a_i to your score. What's the maximum possible score you can get? | Input: ['2', '3 1', ''] Output:['6', ''] | [
2
] |
Everool has a binary string s of length 2n. Note that a binary string is a string consisting of only characters 0 and 1. He wants to partition s into two disjoint equal subsequences. He needs your help to do it.You are allowed to do the following operation exactly once. You can choose any subsequence (possibly empty) of s and rotate it right by one position. In other words, you can select a sequence of indices b_1, b_2, ..., b_m, where 1 <= b_1 < b_2 < ... < b_m <= 2n. After that you simultaneously set s_{b_1} := s_{b_m}, s_{b_2} := s_{b_1}, ..., s_{b_m} := s_{b_{m-1}}.Can you partition s into two disjoint equal subsequences after performing the allowed operation exactly once?A partition of s into two disjoint equal subsequences s^p and s^q is two increasing arrays of indices p_1, p_2, ..., p_n and q_1, q_2, ..., q_n, such that each integer from 1 to 2n is encountered in either p or q exactly once, s^p = s_{p_1} s_{p_2} ... s_{p_n}, s^q = s_{q_1} s_{q_2} ... s_{q_n}, and s^p = s^q.If it is not possible to partition after performing any kind of operation, report -1. If it is possible to do the operation and partition s into two disjoint subsequences s^p and s^q, such that s^p = s^q, print elements of b and indices of s^p, i. e. the values p_1, p_2, ..., p_n. | Input: ['4', '2', '1010', '3', '100010', '2', '1111', '2', '1110', ''] Output:['0', '1 2', '2 3 5', '1 2 5', '3 2 3 4', '1 4', '-1', ''] | [
2
] |
This is the hard version of this problem. In this version, we have queries. Note that we do not have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.An array b of length m is good if for all i the i-th element is greater than or equal to i. In other words, b is good if and only if b_i >=q i for all i (1 <=q i <=q m).You are given an array a consisting of n positive integers, and you are asked q queries. In each query, you are given two integers p and x (1 <=q p,x <=q n). You have to do a_p := x (assign x to a_p). In the updated array, find the number of pairs of indices (l, r), where 1 <= l <= r <= n, such that the array [a_l, a_{l+1}, ..., a_r] is good.Note that all queries are independent, which means after each query, the initial array a is restored. | Input: ['4', '2 4 1 4', '3', '2 4', '3 3', '2 1', ''] Output:['6', '10', '5', ''] | [
4
] |
This is the easy version of this problem. In this version, we do not have queries. Note that we have multiple test cases in this version. You can make hacks only if both versions of the problem are solved.An array b of length m is good if for all i the i-th element is greater than or equal to i. In other words, b is good if and only if b_i >=q i for all i (1 <=q i <=q m).You are given an array a consisting of n positive integers. Find the number of pairs of indices (l, r), where 1 <= l <= r <= n, such that the array [a_l, a_{l+1}, ..., a_r] is good. | Input: ['331 2 331 1 142 1 4 3'] Output:['6', '3', '7', ''] | [
4
] |
You are given an integer array a of length n. Does there exist an array b consisting of n+1 positive integers such that a_i=\gcd (b_i,b_{i+1}) for all i (1 <=q i <=q n)? Note that \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y. | Input: ['4134324 234 2 441 1 1 1'] Output:['YES', 'YES', 'NO', 'YES', ''] | [
3
] |
You are given two arrays a and b of n elements, each element is either 0 or 1.You can make operations of 2 kinds. Pick an index i and change a_i to 1-a_i. Rearrange the array a however you want. Find the minimum number of operations required to make a equal to b. | Input: ['531 0 10 0 141 1 0 00 1 1 121 11 141 0 0 10 1 1 0101'] Output:['1', '2', '0', '1', '1', ''] | [
0,
2
] |
There are n houses in your city arranged on an axis at points h_1, h_2, ..., h_n. You want to build a new house for yourself and consider two options where to place it: points p_1 and p_2.As you like visiting friends, you have calculated in advance the distances from both options to all existing houses. More formally, you have calculated two arrays d_1, d_2: d_{i, j} = <=ft|p_i - h_j\right|, where |x| defines the absolute value of x.After a long time of inactivity you have forgotten the locations of the houses h and the options p_1, p_2. But your diary still keeps two arrays β d_1, d_2, whose authenticity you doubt. Also, the values inside each array could be shuffled, so values at the same positions of d_1 and d_2 may correspond to different houses. Pay attention, that values from one array could not get to another, in other words, all values in the array d_1 correspond the distances from p_1 to the houses, and in the array d_2 β from p_2 to the houses.Also pay attention, that the locations of the houses h_i and the considered options p_j could match. For example, the next locations are correct: h = \{1, 0, 3, 3\}, p = \{1, 1\}, that could correspond to already shuffled d_1 = \{0, 2, 1, 2\}, d_2 = \{2, 2, 1, 0\}.Check whether there are locations of houses h and considered points p_1, p_2, for which the founded arrays of distances would be correct. If it is possible, find appropriate locations of houses and considered options. | Input: ['4155210 125 20210 3326 6940 2 1 22 2 1 0'] Output:['YES', '5 ', '0 10', 'NO', 'YES', '0 43 ', '33 69', 'YES', '1 0 3 3', '1 1'] | [
2
] |
You like the card board game "Set". Each card contains k features, each of which is equal to a value from the set \{0, 1, 2\}. The deck contains all possible variants of cards, that is, there are 3^k different cards in total.A feature for three cards is called good if it is the same for these cards or pairwise distinct. Three cards are called a set if all k features are good for them.For example, the cards (0, 0, 0), (0, 2, 1), and (0, 1, 2) form a set, but the cards (0, 2, 2), (2, 1, 2), and (1, 2, 0) do not, as, for example, the last feature is not good.A group of five cards is called a meta-set, if there is strictly more than one set among them. How many meta-sets there are among given n distinct cards? | Input: ['8 4', '0 0 0 0', '0 0 0 1', '0 0 0 2', '0 0 1 0', '0 0 2 0', '0 1 0 0', '1 0 0 0', '2 2 0 0', ''] Output:['1', ''] | [
0,
3
] |
There was a string s which was supposed to be encrypted. For this reason, all 26 lowercase English letters were arranged in a circle in some order, afterwards, each letter in s was replaced with the one that follows in clockwise order, in that way the string t was obtained. You are given a string t. Determine the lexicographically smallest string s that could be a prototype of the given string t.A string a is lexicographically smaller than a string b of the same length if and only if: in the first position where a and b differ, the string a has a letter, that appears earlier in the alphabet than the corresponding letter in b. | Input: ['51a2ba10codeforces26abcdefghijklmnopqrstuvwxyz26abcdefghijklmnopqrstuvwxzy'] Output:['b', 'ac', 'abcdebfadg', 'bcdefghijklmnopqrstuvwxyza', 'bcdefghijklmnopqrstuvwxyaz', ''] | [
2
] |
There are n pieces of tangerine peel, the i-th of them has size a_i. In one step it is possible to divide one piece of size x into two pieces of positive integer sizes y and z so that y + z = x.You want that for each pair of pieces, their sizes differ strictly less than twice. In other words, there should not be two pieces of size x and y, such that 2x <= y. What is the minimum possible number of steps needed to satisfy the condition? | Input: ['351 2 3 4 5110335600 900 1300 2000 2550'] Output:['10', '0', '4', ''] | [
2,
3
] |