question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
You are given an array a_1, a_2, ..., a_n, consisting of n integers. You are also given an integer value x.Let f(k) be the maximum sum of a contiguous subarray of a after applying the following operation: add x to the elements on exactly k distinct positions. An empty subarray should also be considered, it has sum 0.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of f(k) for all k from 0 to n independently.
Input: ['34 24 1 3 23 5-2 -7 -110 2-6 -1 -2 4 -6 -1 -4 4 -5 -4'] Output:['10 12 14 16 18', '0 4 4 5', '4 6 6 7 7 7 7 8 8 8 8', '']
[ 0, 2 ]
Let's call a permutation p of length n anti-Fibonacci if the condition p_{i-2} + p_{i-1}!=p_i holds for all i (3 <= i <= n). Recall that the permutation is the array of length n which contains each integer from 1 to n exactly once.Your task is for a given number n print n distinct anti-Fibonacci permutations of length n.
Input: ['243'] Output:['4 1 3 2', '1 2 4 3', '3 4 1 2', '2 4 1 3', '3 2 1', '1 3 2', '3 1 2', '']
[ 0 ]
Sam is a kindergartener, and there are n children in his group. He decided to create a team with some of his children to play "brawl:go 2".Sam has n power-ups, the i-th has type a_i. A child's strength is equal to the number of different types among power-ups he has.For a team of size k, Sam will distribute all n power-ups to k children in such a way that each of the k children receives at least one power-up, and each power-up is given to someone.For each integer k from 1 to n, find the minimum sum of strengths of a team of k children Sam can get.
Input: ['231 1 265 1 2 2 2 4'] Output:['2 2 3 ', '4 4 4 4 5 6 ', '']
[ 2 ]
You are given an array a of length n. Also you are given m distinct positions p_1, p_2, ..., p_m (1 <=q p_i <=q n).A non-empty subset of these positions T is randomly selected with equal probability and the following value is calculated: \sum_{i=1}^{n} (a_i \cdot \min_{j \in T} <=ft|i - j\right|). In other word, for each index of the array, a_i and the distance to the closest chosen position are multiplied, and then these values are summed up.Find the expected value of this sum.This value must be found modulo 998\,244\,353. More formally, let M = 998\,244\,353. It can be shown that the answer can be represented as an irreducible fraction \frac{p}{q}, where p and q are integers and q \neq 0 (mod M). Output the integer equal to p \cdot q^{-1} (mod M). In other words, output such integer x that 0 <=q x < M and x \cdot q = p (mod M).
Input: ['4 2', '1 2 3 4', '1 4', ''] Output:['665496247', '']
[ 3 ]
Sam changed his school and on the first biology lesson he got a very interesting task about genes.You are given n arrays, the i-th of them contains m different integers β€” a_{i,1}, a_{i,2},...,a_{i,m}. Also you are given an array of integers w of length n.Find the minimum value of w_i + w_j among all pairs of integers (i, j) (1 <= i, j <= n), such that the numbers a_{i,1}, a_{i,2},...,a_{i,m}, a_{j,1}, a_{j,2},...,a_{j,m} are distinct.
Input: ['4 2', '1 2 5', '4 3 1', '2 3 2', '4 5 3', ''] Output:['5', '']
[ 0, 2, 3 ]
In the work of a doctor, it is important to maintain the anonymity of clients and the results of tests. The test results are sent to everyone personally by email, but people are very impatient and they want to know the results right away.That's why in the testing lab "De-vitro" doctors came up with an experimental way to report the results. Let's assume that n people took the tests in the order of the queue. Then the chief doctor Sam can make several statements, in each telling if there is a sick person among the people in the queue from l-th to r-th (inclusive), for some values l and r.During the process, Sam will check how well this scheme works and will be interested in whether it is possible to find out the test result of i-th person from the information he announced. And if it can be done, then is that patient sick or not.Help Sam to test his scheme.
Input: ['6 9', '0 4 5 0', '1 5', '1 6', '0 4 6 1', '1 6', '0 2 5 1', '0 2 2 0', '1 3', '1 2', ''] Output:['NO', 'N/A', 'YES', 'YES', 'NO', '']
[ 0, 2, 4 ]
A sequence of positive integers is called great for a positive integer x, if we can split it into pairs in such a way that in each pair the first number multiplied by x is equal to the second number. More formally, a sequence a of size n is great for a positive integer x, if n is even and there exists a permutation p of size n, such that for each i (1 <= i <= \frac{n}{2}) a_{p_{2i-1}} \cdot x = a_{p_{2i}}. Sam has a sequence a and a positive integer x. Help him to make the sequence great: find the minimum possible number of positive integers that should be added to the sequence a to make it great for the number x.
Input: ['44 41 16 4 46 21 2 2 2 4 75 35 2 3 5 159 1010 10 10 20 1 100 200 2000 3'] Output:['0', '2', '3', '3', '']
[ 0, 2 ]
You want to advertise your new business, so you are going to place two posters on a billboard in the city center. The billboard consists of n vertical panels of width 1 and varying integer heights, held together by a horizontal bar. The i-th of the n panels has height h_i. Initially, all panels hang down from the bar (their top edges lie on it), but before placing the two posters, you are allowed to move each panel up by any integer length, as long as it is still connected to the bar (its bottom edge lies below or on it).After the moves are done, you will place two posters: one below the bar and one above it. They are not allowed to go over the bar and they must be positioned completely inside of the panels.What is the maximum total area the two posters can cover together if you make the optimal moves? Note that you can also place a poster of 0 area. This case is equivalent to placing a single poster.
Input: ['6', '2 2 3 5 4 5', ''] Output:['18', '']
[ 0, 2 ]
You have an array a_1,a_2, ..., a_n. Each element initially has value 0 and color 1. You are also given q queries to perform: Color l r c: Change the color of elements a_l,a_{l+1},\cdots,a_r to c (1 <= l <= r <= n, 1 <= c <= n). Add c x: Add x to values of all elements a_i (1 <= i <= n) of color c (1 <= c <= n, -10^9 <= x <= 10^9). Query i: Print a_i (1 <= i <= n).
Input: ['5 8', 'Color 2 4 2', 'Add 2 2', 'Query 3', 'Color 4 5 3', 'Color 2 2 3', 'Add 3 3', 'Query 2', 'Query 5', ''] Output:['2', '5', '3', '']
[ 0 ]
You found a painting on a canvas of size n * m. The canvas can be represented as a grid with n rows and m columns. Each cell has some color. Cell (i, j) has color c_{i,j}.Near the painting you also found a brush in the shape of a 2 * 2 square, so the canvas was surely painted in the following way: initially, no cell was painted. Then, the following painting operation has been performed some number of times: Choose two integers i and j (1 <= i < n, 1 <= j < m) and some color k (1 <= k <= nm). Paint cells (i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1) in color k. All cells must be painted at least once. A cell can be painted multiple times. In this case, its final color will be the last one.Find any sequence of at most nm operations that could have led to the painting you found or state that it's impossible.
Input: ['4 4', '5 5 3 3', '1 1 5 3', '2 2 5 4', '2 2 4 4', ''] Output:['6', '1 3 3', '3 3 4', '2 2 5', '1 1 5', '2 1 1', '3 1 2', '']
[ 2 ]
You are given a permutation p_1, p_2, ..., p_n. Then, an undirected graph is constructed in the following way: add an edge between vertices i, j such that i < j if and only if p_i > p_j. Your task is to count the number of connected components in this graph.Two vertices u and v belong to the same connected component if and only if there is at least one path along edges connecting u and v.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).
Input: ['631 2 352 1 4 3 566 1 4 2 5 31163 2 1 6 5 453 1 5 2 4'] Output:['3', '3', '1', '1', '2', '1', '']
[ 3 ]
You are given an array a_1, a_2, ..., a_n. You can perform operations on the array. In each operation you can choose an integer i (1 <= i < n), and swap elements a_i and a_{i+1} of the array, if a_i + a_{i+1} is odd.Determine whether it can be sorted in non-decreasing order using this operation any number of times.
Input: ['441 6 31 1424 252 9 6 7 1036 6 6'] Output:['Yes', 'No', 'No', 'Yes', '']
[ 3 ]
You are given a permutation p_1, p_2, ..., p_n of length n. You have to choose two integers l,r (1 <= l <= r <= n) and reverse the subsegment [l,r] of the permutation. The permutation will become p_1,p_2, ..., p_{l-1},p_r,p_{r-1}, ..., p_l,p_{r+1},p_{r+2}, ... ,p_n.Find the lexicographically smallest permutation that can be obtained by performing exactly one reverse operation on the initial permutation.Note that for two distinct permutations of equal length a and b, a is lexicographically smaller than b if at the first position they differ, a has the smaller element.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).
Input: ['41132 1 341 4 2 351 2 3 4 5'] Output:['1 ', '1 2 3 ', '1 2 4 3 ', '1 2 3 4 5 ', '']
[ 2, 3 ]
You are given a permutation p of length n.You can choose any subsequence, remove it from the permutation, and insert it at the beginning of the permutation keeping the same order.For every k from 0 to n, find the minimal possible number of inversions in the permutation after you choose a subsequence of length exactly k.
Input: ['31144 2 1 355 1 3 2 4'] Output:['0 0', '4 2 2 1 4', '5 4 2 2 1 5', '']
[ 2, 3 ]
Vitaly gave Maxim n numbers 1, 2, ..., n for his 16-th birthday. Maxim was tired of playing board games during the celebration, so he decided to play with these numbers. In one step Maxim can choose two numbers x and y from the numbers he has, throw them away, and add two numbers x + y and |x - y| instead. He wants all his numbers to be equal after several steps and the sum of the numbers to be minimal.Help Maxim to find a solution. Maxim's friends don't want to wait long, so the number of steps in the solution should not exceed 20n. It is guaranteed that under the given constraints, if a solution exists, then there exists a solution that makes all numbers equal, minimizes their sum, and spends no more than 20n moves.
Input: ['223'] Output:['-1', '3', '1 3', '2 2', '4 0', '']
[ 2, 3 ]
You are given a tree with n vertices numbered from 1 to n. The height of the i-th vertex is h_i. You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency e costs e coins, where e > 0.It is considered that a vertex x gets a signal if for some pair of towers at the vertices u and v (u \neq v, but it is allowed that x = u or x = v) with efficiencies e_u and e_v, respectively, it is satisfied that \min(e_u, e_v) >=q h_x and x lies on the path between u and v.Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.
Input: ['3', '1 2 1', '1 2', '2 3', ''] Output:['4']
[ 2 ]
You are given an array a of length n. Let cnt_x be the number of elements from the array which are equal to x. Let's also define f(x, y) as (cnt_x + cnt_y) \cdot (x + y).Also you are given m bad pairs (x_i, y_i). Note that if (x, y) is a bad pair, then (y, x) is also bad.Your task is to find the maximum value of f(u, v) over all pairs (u, v), such that u \neq v, that this pair is not bad, and also that u and v each occur in the array a. It is guaranteed that such a pair exists.
Input: ['36 16 3 6 7 3 33 62 03 47 41 2 2 3 1 5 11 53 51 32 5'] Output:['40', '14', '15', '']
[ 0, 4 ]
You are given two arrays a and b, both of length n.You can perform the following operation any number of times (possibly zero): select an index i (1 <=q i <=q n) and swap a_i and b_i.Let's define the cost of the array a as \sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2. Similarly, the cost of the array b is \sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2.Your task is to minimize the total cost of two arrays.
Input: ['313643 6 6 62 7 4 146 7 2 42 5 3 5'] Output:['0', '987', '914', '']
[ 2, 3 ]
Andrew has n piles with stones. The i-th pile contains a_i stones. He wants to make his table clean so he decided to put every stone either to the 1-st or the n-th pile.Andrew can perform the following operation any number of times: choose 3 indices 1 <= i < j < k <= n, such that the j-th pile contains at least 2 stones, then he takes 2 stones from the pile j and puts one stone into pile i and one stone into pile k. Tell Andrew what is the minimum number of operations needed to move all the stones to piles 1 and n, or determine if it's impossible.
Input: ['451 2 2 3 631 3 131 2 143 1 1 2'] Output:['4', '-1', '1', '-1 ', '']
[ 2 ]
Let there be an array b_1, b_2, ..., b_k. Let there be a partition of this array into segments [l_1; r_1], [l_2; r_2], ..., [l_c; r_c], where l_1 = 1, r_c = k, and for any 2 <=q i <=q c holds that r_{i-1} + 1 = l_i. In other words, each element of the array belongs to exactly one segment.Let's define the cost of a partition as c + \sum_{i = 1}^{c} \operatorname{mex}(\{b_{l_i}, b_{l_i + 1}, ..., b_{r_i}\}), where \operatorname{mex} of a set of numbers S is the smallest non-negative integer that does not occur in the set S. In other words, the cost of a partition is the number of segments plus the sum of MEX over all segments. Let's define the value of an array b_1, b_2, ..., b_k as the maximum possible cost over all partitions of this array.You are given an array a of size n. Find the sum of values of all its subsegments.An array x is a subsegment of an array y if x can be obtained from y by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input: ['421 232 0 142 0 5 150 1 1 0 1'] Output:['4', '14', '26', '48', '']
[ 0, 2, 3 ]
You have an array a of length n. You can exactly once select an integer len between 1 and n - 1 inclusively, and then sort in non-decreasing order the prefix of the array of length len and the suffix of the array of length n - len independently.For example, if the array is a = [3, 1, 4, 5, 2], and you choose len = 2, then after that the array will be equal to [1, 3, 2, 4, 5].Could it be that after performing this operation, the array will not be sorted in non-decreasing order?
Input: ['332 2 143 1 2 151 2 2 4 4'] Output:['YES', 'YES', 'NO', '']
[ 0 ]
There are n weighted points on the OX-axis. The coordinate and the weight of the i-th point is x_i and w_i, respectively. All points have distinct coordinates and positive weights. Also, x_i < x_{i + 1} holds for any 1 <=q i < n. The weighted distance between i-th point and j-th point is defined as |x_i - x_j| \cdot (w_i + w_j), where |val| denotes the absolute value of val.You should answer q queries, where the i-th query asks the following: Find the minimum weighted distance among all pairs of distinct points among the points in subarray [l_i,r_i].
Input: ['5 5', '-2 2', '0 10', '1 1', '9 2', '12 7', '1 3', '2 3', '1 5', '3 5', '2 4', ''] Output:['9', '11', '9', '24', '11', '']
[ 2 ]
There are n cars on a coordinate axis OX. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment.More formally, we can describe the i-th car with a letter and an integer: its orientation ori_i and its location x_i. If ori_i = L, then x_i is decreasing at a constant rate with respect to time. Similarly, if ori_i = R, then x_i is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment.We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment.Unfortunately, we lost all information about our cars, but we do remember m relationships. There are two types of relationships:1 i j β€”i-th car and j-th car are irrelevant.2 i j β€”i-th car and j-th car are destined.Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any.Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions.
Input: ['4 4', '1 1 2', '1 2 3', '2 3 4', '2 4 1', ''] Output:['YES', 'R 0', 'L -3', 'R 5', 'L 6', '']
[ 2 ]
You are given an array a consisting of n distinct positive integers.Let's consider an infinite integer set S which contains all integers x that satisfy at least one of the following conditions: x = a_i for some 1 <=q i <=q n. x = 2y + 1 and y is in S. x = 4y and y is in S.For example, if a = [1,2] then the 10 smallest elements in S will be \{1,2,3,4,5,7,8,9,11,12\}.Find the number of elements in S that are strictly smaller than 2^p. Since this number may be too large, print it modulo 10^9 + 7.
Input: ['2 4', '6 1', ''] Output:['9', '']
[ 3 ]
You are given an array a of n elements. Your can perform the following operation no more than n times: Select three indices x,y,z (1 <=q x < y < z <=q n) and replace a_x with a_y - a_z. After the operation, |a_x| need to be less than 10^{18}.Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well.
Input: ['3', '5', '5 -4 2 -1 2', '3', '4 3 2', '3', '-3 -2 -1', ''] Output:['2', '1 2 3', '3 4 5', '-1', '0', '']
[ 2 ]
You are given an array a of size n. Each element in this array is an integer between 1 and 10^9.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between 1 and 10^9. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element a_i is a local maximum if it is strictly larger than both of its neighbors (that is, a_i > a_{i - 1} and a_i > a_{i + 1}). Since a_1 and a_n have only one neighbor each, they will never be a local maximum.
Input: ['5', '3', '2 1 2', '4', '1 2 3 1', '5', '1 2 1 2 1', '9', '1 2 1 3 2 3 1 2 1', '9', '2 1 3 1 3 1 3 1 3', ''] Output:['0', '2 1 2', '1', '1 3 3 1', '1', '1 2 2 2 1', '2', '1 2 3 3 2 3 3 2 1', '2', '2 1 3 3 3 1 1 1 3', '']
[ 2 ]
You are given an array a of size n.You can perform the following operation on the array: Choose two different integers i, j (1 <=q i < j <=q n), replace a_i with x and a_j with y. In order not to break the array, a_i | a_j = x | y must be held, where | denotes the bitwise OR operation. Notice that x and y are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
Input: ['431 3 251 2 4 8 1626 633 5 6'] Output:['3', '31', '6', '7', '']
[ 2 ]
One of my most productive days was throwing away 1,000 lines of code.β€” Ken ThompsonFibonacci addition is an operation on an array X of integers, parametrized by indices l and r. Fibonacci addition increases X_l by F_1, increases X_{l + 1} by F_2, and so on up to X_r which is increased by F_{r - l + 1}.F_i denotes the i-th Fibonacci number (F_1 = 1, F_2 = 1, F_{i} = F_{i - 1} + F_{i - 2} for i > 2), and all operations are performed modulo MOD.You are given two arrays A and B of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays A and B are equal modulo MOD.
Input: ['3 5 3', '2 2 1', '0 0 0', 'A 1 3', 'A 1 3', 'B 1 1', 'B 2 2', 'A 3 3', ''] Output:['YES', 'NO', 'NO', 'NO', 'YES', '']
[ 0, 3 ]
This is an interactive problem.We picked an array of whole numbers a_1, a_2, ..., a_n (0 <= a_i <= 10^9) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find i such that a_i = 0.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices i, j, k, and we will tell you the value of \max(a_i, a_j, a_k) - \min(a_i, a_j, a_k). In other words, we will tell you the difference between the maximum and the minimum number among a_i, a_j and a_k.You are allowed to make no more than 2 \cdot n - 2 queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers i and j and you win if a_i = 0 or a_j = 0.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive.
Input: ['1', '', '4', '', '2', '', '3', '', '3', '', '2', ''] Output:['', '', '', '? 1 2 3', '', '? 2 3 4', '', '? 3 4 1', '', '? 4 1 2', '', '! 2 3']
[ 3 ]
Haha, try to solve this, SelectorUnlimited!β€” antontrygubO_oYour friends Alice and Bob practice fortune telling.Fortune telling is performed as follows. There is a well-known array a of n non-negative integers indexed from 1 to n. The tellee starts with some non-negative number d and performs one of the two operations for each i = 1, 2, ..., n, in the increasing order of i. The possible operations are: replace their current number d with d + a_i replace their current number d with d \oplus a_i (hereinafter \oplus denotes the bitwise XOR operation)Notice that the chosen operation may be different for different i and for different tellees.One time, Alice decided to start with d = x and Bob started with d = x + 3. Each of them performed fortune telling and got a particular number in the end. Notice that the friends chose operations independently of each other, that is, they could apply different operations for the same i.You learnt that either Alice or Bob ended up with number y in the end, but you don't know whose of the two it was. Given the numbers Alice and Bob started with and y, find out who (Alice or Bob) could get the number y after performing the operations. It is guaranteed that on the jury tests, exactly one of your friends could have actually gotten that number.HacksYou cannot make hacks in this problem.
Input: ['41 7 922 0 21 34 0 11 2 3 42 1000000000 30000000001000000000 1000000000'] Output:['Alice', 'Alice', 'Bob', 'Alice', '']
[ 3 ]
Real stupidity beats artificial intelligence every time.β€” Terry Pratchett, Hogfather, DiscworldYou are given a string s of length n and a number k. Let's denote by rev(s) the reversed string s (i.e. rev(s) = s_n s_{n-1} ... s_1). You can apply one of the two kinds of operations to the string: replace the string s with s + rev(s) replace the string s with rev(s) + sHow many different strings can you get as a result of performing exactly k operations (possibly of different kinds) on the original string s?In this statement we denoted the concatenation of strings s and t as s + t. In other words, s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m, where n and m are the lengths of strings s and t respectively.
Input: ['43 2aab3 3aab7 1abacaba2 0ab'] Output:['2', '2', '1', '1', '']
[ 2 ]
You are given a connected weighted undirected graph, consisting of n vertices and m edges.You are asked k queries about it. Each query consists of a single integer x. For each query, you select a spanning tree in the graph. Let the weights of its edges be w_1, w_2, ..., w_{n-1}. The cost of a spanning tree is \sum \limits_{i=1}^{n-1} |w_i - x| (the sum of absolute differences between the weights and x). The answer to a query is the lowest cost of a spanning tree.The queries are given in a compressed format. The first p (1 <= p <= k) queries q_1, q_2, ..., q_p are provided explicitly. For queries from p+1 to k, q_j = (q_{j-1} \cdot a + b) \mod c.Print the xor of answers to all queries.
Input: ['5 8', '4 1 4', '3 1 0', '3 5 3', '2 5 4', '3 4 8', '4 3 4', '4 2 8', '5 3 9', '3 11 1 1 10', '0 1 2', ''] Output:['4', '']
[ 2, 3, 4 ]
You have an array of integers a of size n. Initially, all elements of the array are equal to 1. You can perform the following operation: choose two integers i (1 <= i <= n) and x (x > 0), and then increase the value of a_i by <=ft\lfloor\frac{a_i}{x}\right\rfloor (i.e. make a_i = a_i + <=ft\lfloor\frac{a_i}{x}\right\rfloor).After performing all operations, you will receive c_i coins for all such i that a_i = b_i.Your task is to determine the maximum number of coins that you can receive by performing no more than k operations.
Input: ['44 41 7 5 22 6 5 23 03 5 25 4 75 95 2 5 6 35 9 1 9 76 1411 4 6 2 8 1643 45 9 41 15 38'] Output:['9', '0', '30', '167', '']
[ 2 ]
Monocarp is playing a computer game. In this game, his character fights different monsters.A fight between a character and a monster goes as follows. Suppose the character initially has health h_C and attack d_C; the monster initially has health h_M and attack d_M. The fight consists of several steps: the character attacks the monster, decreasing the monster's health by d_C; the monster attacks the character, decreasing the character's health by d_M; the character attacks the monster, decreasing the monster's health by d_C; the monster attacks the character, decreasing the character's health by d_M; and so on, until the end of the fight. The fight ends when someone's health becomes non-positive (i. e. 0 or less). If the monster's health becomes non-positive, the character wins, otherwise the monster wins.Monocarp's character currently has health equal to h_C and attack equal to d_C. He wants to slay a monster with health equal to h_M and attack equal to d_M. Before the fight, Monocarp can spend up to k coins to upgrade his character's weapon and/or armor; each upgrade costs exactly one coin, each weapon upgrade increases the character's attack by w, and each armor upgrade increases the character's health by a.Can Monocarp's character slay the monster if Monocarp spends coins on upgrades optimally?
Input: ['425 49 201 1 1025 412 201 1 10100 145 20 4 109 269 24 2 7'] Output:['YES', 'NO', 'YES', 'YES', '']
[ 0, 3 ]
You are given a string s, consisting only of characters '0' and '1'.You have to choose a contiguous substring of s and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?
Input: ['4011010101010111001100010001'] Output:['0', '5', '3', '0', '']
[ 2 ]
You are given an integer n. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by 7.If there are multiple ways to do it, print any of them. If the given number is already divisible by 7, leave it unchanged.
Input: ['34223377'] Output:['42', '28', '777', '']
[ 0 ]
This version of the problem differs from the previous one only in the constraint on n.A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.You are given a weighted tree with n vertices, each edge has a weight of 1. Denote d(v) as the distance between vertex 1 and vertex v.Let f(x) be the minimum possible value of \max\limits_{1 <=q v <=q n} \ {d(v)} if you can temporarily add an edge with weight x between any two vertices a and b (1 <= a, b <= n). Note that after this operation, the graph is no longer a tree.For each integer x from 1 to n, find f(x).
Input: ['341 22 31 421 271 21 33 43 53 65 7'] Output:['1 2 2 2 ', '1 1 ', '2 2 3 3 3 3 3 ', '']
[ 4 ]
This version of the problem differs from the next one only in the constraint on n.A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.You are given a weighted tree with n vertices, each edge has a weight of 1. Denote d(v) as the distance between vertex 1 and vertex v.Let f(x) be the minimum possible value of \max\limits_{1 <=q v <=q n} \ {d(v)} if you can temporarily add an edge with weight x between any two vertices a and b (1 <= a, b <= n). Note that after this operation, the graph is no longer a tree.For each integer x from 1 to n, find f(x).
Input: ['341 22 31 421 271 21 33 43 53 65 7'] Output:['1 2 2 2 ', '1 1 ', '2 2 3 3 3 3 3 ', '']
[ 4 ]
New Year is just around the corner, which means that in School 179, preparations for the concert are in full swing.There are n classes in the school, numbered from 1 to n, the i-th class has prepared a scene of length a_i minutes.As the main one responsible for holding the concert, Idnar knows that if a concert has k scenes of lengths b_1, b_2, ..., b_k minutes, then the audience will get bored if there exist two integers l and r such that 1 <= l <= r <= k and \gcd(b_l, b_{l + 1}, ..., b_{r - 1}, b_r) = r - l + 1, where \gcd(b_l, b_{l + 1}, ..., b_{r - 1}, b_r) is equal to the greatest common divisor (GCD) of the numbers b_l, b_{l + 1}, ..., b_{r - 1}, b_r.To avoid boring the audience, Idnar can ask any number of times (possibly zero) for the t-th class (1 <= t <= k) to make a new scene d minutes in length, where d can be any positive integer. Thus, after this operation, b_t is equal to d. Note that t and d can be different for each operation.For a sequence of scene lengths b_1, b_2, ..., b_{k}, let f(b) be the minimum number of classes Idnar has to ask to change their scene if he wants to avoid boring the audience.Idnar hasn't decided which scenes will be allowed for the concert, so he wants to know the value of f for each non-empty prefix of a. In other words, Idnar wants to know the values of f(a_1), f(a_1,a_2), ..., f(a_1,a_2,...,a_n).
Input: ['1', '1', ''] Output:['1 ']
[ 2, 3, 4 ]
Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers a and b (a < b). After that, the student can apply any of the following operations any number of times: a := a + 1 (increase a by 1), b := b + 1 (increase b by 1), a := a \ | \ b (replace a with the bitwise OR of a and b). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make a and b equal.Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make a equal to b.
Input: ['5', '1 3', '5 8', '2 5', '3 19', '56678 164422', ''] Output:['1', '3', '2', '1', '23329', '']
[ 0, 3, 4 ]
You are given an array a of n elements. You can apply the following operation to it any number of times: Select some subarray from a of even size 2k that begins at position l (1<= l <= l+2\cdot{k}-1<= n, k >= 1) and for each i between 0 and k-1 (inclusive), assign the value a_{l+k+i} to a_{l+i}. For example, if a = [2, 1, 3, 4, 5, 3], then choose l = 1 and k = 2, applying this operation the array will become a = [3, 4, 3, 4, 5, 3].Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.
Input: ['531 1 122 154 4 4 2 444 2 1 311'] Output:['0', '1', '1', '2', '0', '']
[ 2 ]
You are given two arrays a and b of n positive integers each. You can apply the following operation to them any number of times: Select an index i (1<=q i<=q n) and swap a_i with b_i (i. e. a_i becomes b_i and vice versa). Find the minimum possible value of \max(a_1, a_2, ..., a_n) \cdot \max(b_1, b_2, ..., b_n) you can get after applying such operation any number of times (possibly zero).
Input: ['361 2 6 5 1 23 4 3 2 2 533 3 33 3 321 22 1'] Output:['18', '9', '2', '']
[ 2 ]
Given a cyclic array a of size n, where a_i is the value of a in the i-th position, there may be repeated values. Let us define that a permutation of a is equal to another permutation of a if and only if their values are the same for each position i or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array b its number of components as the number of connected components in a graph, where the vertices are the positions of b and we add an edge between each pair of adjacent positions of b with equal values (note that in a cyclic array the first and last position are also adjacents).Find the expected value of components of a permutation of a if we select it equiprobably over the set of all the different permutations of a.
Input: ['541 1 1 141 1 2 141 2 1 254 3 2 5 1121 3 2 3 2 1 3 3 1 3 3 2'] Output:['1', '2', '3', '5', '358642921', '']
[ 3 ]
You are given an array a of n integers and a set B of m positive integers such that 1 <=q b_i <=q \lfloor \frac{n}{2} \rfloor for 1<= i<= m, where b_i is the i-th element of B. You can make the following operation on a: Select some x such that x appears in B. Select an interval from array a of size x and multiply by -1 every element in the interval. Formally, select l and r such that 1<=q l<=q r <=q n and r-l+1=x, then assign a_i:=-a_i for every i such that l<=q i<=q r. Consider the following example, let a=[0,6,-2,1,-4,5] and B=\{1,2\}: [0,6,-2,-1,4,5] is obtained after choosing size 2 and l=4, r=5. [0,6,2,-1,4,5] is obtained after choosing size 1 and l=3, r=3. Find the maximum \sum\limits_{i=1}^n {a_i} you can get after applying such operation any number of times (possibly zero).
Input: ['36 20 6 -2 1 -4 51 27 11 -1 1 -1 1 -1 125 1-1000000000 -1000000000 -1000000000 -1000000000 -10000000001'] Output:['18', '5', '5000000000', '']
[ 2 ]
You are given n elements numbered from 1 to n, the element i has value a_i and color c_i, initially, c_i = 0 for all i.The following operation can be applied: Select three elements i, j and k (1 <=q i < j < k <=q n), such that c_i, c_j and c_k are all equal to 0 and a_i = a_k, then set c_j = 1. Find the maximum value of \sum\limits_{i=1}^n{c_i} that can be obtained after applying the given operation any number of times.
Input: ['7', '1 2 1 2 7 4 7', ''] Output:['2', '']
[ 2 ]
Given an array a of n integers, find a range of values [x, y] (x <= y), and split a into exactly k (1 <= k <= n) subarrays in such a way that: Each subarray is formed by several continuous elements of a, that is, it is equal to a_l, a_{l+1}, ..., a_r for some l and r (1 <=q l <=q r <=q n). Each element from a belongs to exactly one subarray. In each subarray the number of elements inside the range [x, y] (inclusive) is strictly greater than the number of elements outside the range. An element with index i is inside the range [x, y] if and only if x <= a_i <= y. Print any solution that minimizes y - x.
Input: ['32 11 24 21 2 2 211 35 5 5 1 5 5 1 5 5 5 1'] Output:['1 2', '1 2', '2 2', '1 3', '4 4', '5 5', '1 1', '2 2', '3 11', '']
[ 2, 4 ]
Consider the array a composed of all the integers in the range [l, r]. For example, if l = 3 and r = 7, then a = [3, 4, 5, 6, 7].Given l, r, and k, is it possible for \gcd(a) to be greater than 1 after doing the following operation at most k times? Choose 2 numbers from a. Permanently remove one occurrence of each of them from the array. Insert their product back into a. \gcd(b) denotes the greatest common divisor (GCD) of the integers in b.
Input: ['91 1 03 5 113 13 04 4 03 7 44 10 32 4 01 7 31 5 3'] Output:['NO', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'YES', '']
[ 2, 3 ]
Did you know you can download more RAM? There is a shop with n different pieces of software that increase your RAM. The i-th RAM increasing software takes a_i GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional b_i GB of RAM (permanently). Each software can only be used once. Your PC currently has k GB of RAM.Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?
Input: ['43 1020 30 109 100 105 11 1 5 1 11 1 1 1 15 12 2 2 2 2100 100 100 100 1005 8128 64 32 16 8128 64 32 16 8'] Output:['29', '6', '1', '256', '']
[ 0, 2 ]
NASA (Norwegian Astronaut Stuff Association) is developing a new steering system for spaceships. But in its current state, it wouldn't be very safe if the spaceship would end up in a bunch of space junk. To make the steering system safe, they need to answer the following:Given the target position t = (0, 0), a set of n pieces of space junk l described by line segments l_i = ((a_{ix}, a_{iy}), (b_{ix}, b_{iy})), and a starting position s = (s_x, s_y), is there a direction such that floating in that direction from the starting position would lead to the target position?When the spaceship hits a piece of space junk, what happens depends on the absolute difference in angle between the floating direction and the line segment, \theta: If \theta < 45^{\circ}, the spaceship slides along the piece of space junk in the direction that minimizes the change in angle, and when the spaceship slides off the end of the space junk, it continues floating in the direction it came in (before hitting the space junk). If \theta >= 45^{\circ}, the spaceship stops, because there is too much friction to slide along the space junk. You are only given the set of pieces of space junk once, and the target position is always (0, 0), but there are q queries, each with a starting position s_j = (s_{jx}, s_{jy}).Answer the above question for each query.
Input: ['3', '0 1 2 4', '1 3 -1 6', '0 -1 1 -1', '14', '-2 10', '-1 10', '0 10', '1 10', '2 10', '3 10', '4 10', '5 10', '6 10', '-1 -2', '0 -2', '1 -2', '2 -2', '3 -2', ''] Output:['YES', 'YES', 'YES', 'YES', 'YES', 'NO', 'NO', 'NO', 'YES', 'YES', 'NO', 'NO', 'NO', 'YES', '']
[ 4 ]
Mihai lives in a town where meteor storms are a common problem. It's annoying, because Mihai has to buy groceries sometimes, and getting hit by meteors isn't fun. Therefore, we ask you to find the most dangerous way to buy groceries so that we can trick him to go there.The town has n buildings numbered from 1 to n. Some buildings have roads between them, and there is exactly 1 simple path from any building to any other building. Each road has a certain meteor danger level. The buildings all have grocery stores, but Mihai only cares about the open ones, of course. Initially, all the grocery stores are closed.You are given q queries of three types: Given the integers l and r, the buildings numbered from l to r open their grocery stores (nothing happens to buildings in the range that already have an open grocery store). Given the integers l and r, the buildings numbered from l to r close their grocery stores (nothing happens to buildings in the range that didn't have an open grocery store). Given the integer x, find the maximum meteor danger level on the simple path from x to any open grocery store, or -1 if there is no edge on any simple path to an open store.
Input: ['6 9', '1 3 1', '2 3 2', '4 5 3', '4 6 4', '3 4 5', '3 1', '1 1 1', '3 1', '2 1 1', '1 5 6', '3 4', '2 6 6', '3 4', '3 1', ''] Output:['-1', '-1', '4', '3', '5', '']
[ 4 ]
This is the hard version of the problem. The difference is the constraints on n, m and t. You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers n, m and k, and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially 0. The game consists of n turns. Each turn, Alice picks a real number from 0 to k (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least m out of the n turns.Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn).If Alice and Bob play optimally, what will the final score of the game be?
Input: ['73 3 22 1 106 3 106 4 10100 1 14 4 069 4 20'] Output:['6', '5', '375000012', '500000026', '958557139', '0', '49735962', '']
[ 3 ]
This is the easy version of the problem. The difference is the constraints on n, m and t. You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers n, m and k, and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially 0. The game consists of n turns. Each turn, Alice picks a real number from 0 to k (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least m out of the n turns.Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn).If Alice and Bob play optimally, what will the final score of the game be?
Input: ['73 3 22 1 106 3 106 4 10100 1 14 4 069 4 20'] Output:['6', '5', '375000012', '500000026', '958557139', '0', '49735962', '']
[ 3 ]
Note: The XOR-sum of set \{s_1,s_2,...,s_m\} is defined as s_1 \oplus s_2 \oplus ... \oplus s_m, where \oplus denotes the bitwise XOR operation.After almost winning IOI, Victor bought himself an n* n grid containing integers in each cell. n is an even integer. The integer in the cell in the i-th row and j-th column is a_{i,j}.Sadly, Mihai stole the grid from Victor and told him he would return it with only one condition: Victor has to tell Mihai the XOR-sum of all the integers in the whole grid.Victor doesn't remember all the elements of the grid, but he remembers some information about it: For each cell, Victor remembers the XOR-sum of all its neighboring cells.Two cells are considered neighbors if they share an edge β€” in other words, for some integers 1 <= i, j, k, l <= n, the cell in the i-th row and j-th column is a neighbor of the cell in the k-th row and l-th column if |i - k| = 1 and j = l, or if i = k and |j - l| = 1.To get his grid back, Victor is asking you for your help. Can you use the information Victor remembers to find the XOR-sum of the whole grid?It can be proven that the answer is unique.
Input: ['3', '2', '1 5', '5 1', '4', '1 14 8 9', '3 1 5 9', '4 13 11 1', '1 15 4 11', '4', '2 4 1 6', '3 7 3 10', '15 9 4 2', '12 7 15 1', ''] Output:['4', '9', '5', '']
[ 2, 3 ]
Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic.You are given a list s of n non-empty strings of length at most 3, representing the scenes of Mihai's movie.A subsequence of s is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome.Can you help Mihai check if there is at least one awesome subsequence of s?A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.A sequence a is a non-empty subsequence of a non-empty sequence b if a can be obtained from b by deletion of several (possibly zero, but not all) elements.
Input: ['65zxabcczxba2abbad4codeforces3abc3abcdcba2abab'] Output:['YES', 'NO', 'NO', 'YES', 'YES', 'NO', '']
[ 2 ]
Mihai has just learned about the MEX concept and since he liked it so much, he decided to use it right away.Given an array a of n non-negative integers, Mihai wants to create a new array b that is formed in the following way:While a is not empty: Choose an integer k (1 <=q k <=q |a|). Append the MEX of the first k numbers of the array a to the end of array b and erase them from the array a, shifting the positions of the remaining numbers in a. But, since Mihai loves big arrays as much as the MEX concept, he wants the new array b to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array b that can be created by constructing the array optimally is.An array x is lexicographically greater than an array y if in the first position where x and y differ x_i > y_i or if |x| > |y| and y is a prefix of x (where |x| denotes the size of the array x).The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({{1, 2, 3}}) = 0 and MEX({{0, 1, 2, 4, 5}}) = 3.
Input: ['651 0 2 0 382 2 3 4 0 1 2 01150 1 2 3 440 1 1 0100 0 2 1 1 1 0 0 1 1'] Output:['1', '4 ', '2', '5 1 ', '1', '0 ', '1', '5 ', '2', '2 2 ', '4', '3 2 2 0 ', '']
[ 2, 3, 4 ]
There is a k * k grid, where k is even. The square in row r and column c is denoted by (r,c). Two squares (r_1, c_1) and (r_2, c_2) are considered adjacent if \lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array a of n pairs of adjacent squares. Find the size of the largest strong subsequence of a. An array p is a subsequence of an array q if p can be obtained from q by deletion of several (possibly, zero or all) elements.
Input: ['38 41 2 1 32 2 2 33 2 3 34 2 4 31 4 2 42 1 3 12 2 3 24 1 4 27 21 1 1 22 1 2 21 1 1 21 1 2 11 2 2 21 1 2 11 2 2 21 63 3 3 4'] Output:['7', '4', '1', '']
[ 2 ]
You have an array a_1, a_2, ..., a_n consisting of n distinct integers. You are allowed to perform the following operation on it: Choose two elements from the array a_i and a_j (i!=j) such that \gcd(a_i, a_j) is not present in the array, and add \gcd(a_i, a_j) to the end of the array. Here \gcd(x, y) denotes greatest common divisor (GCD) of integers x and y. Note that the array changes after each operation, and the subsequent operations are performed on the new array.What is the maximum number of times you can perform the operation on the array?
Input: ['5', '4 20 1 25 30', ''] Output:['3']
[ 0, 3 ]
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a n * m grid. The seat in row r and column c is denoted by (r, c), and the distance between two seats (a,b) and (c,d) is |a-c| + |b-d|. As the class president, Tina has access to exactly k buckets of pink paint. The following process occurs. First, Tina chooses exactly k seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted k seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for k = 0, 1, ..., n \cdot m - 1, if Tina has k buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
Input: ['24 31 2'] Output:['3 3 4 4 4 4 4 4 5 5 5 5 ', '1 1 ', '']
[ 2 ]
You are given an integer array a_0, a_1, ..., a_{n - 1}, and an integer k. You perform the following code with it:long long ans = 0; // create a 64-bit signed variable which is initially equal to 0for(int i = 1; i <= k; i++){ int idx = rnd.next(0, n - 1); // generate a random integer between 0 and n - 1, both inclusive // each integer from 0 to n - 1 has the same probability of being chosen ans += a[idx]; a[idx] -= (a[idx] % i);}Your task is to calculate the expected value of the variable ans after performing this code.Note that the input is generated according to special rules (see the input format section).
Input: ['3 10 3 5 13 88', ''] Output:['382842030', '']
[ 3 ]
You are given a tree consisting of n vertices. Some of the vertices (at least two) are black, all the other vertices are white.You place a chip on one of the vertices of the tree, and then perform the following operations: let the current vertex where the chip is located is x. You choose a black vertex y, and then move the chip along the first edge on the simple path from x to y. You are not allowed to choose the same black vertex y in two operations in a row (i. e., for every two consecutive operations, the chosen black vertex should be different).You end your operations when the chip moves to the black vertex (if it is initially placed in a black vertex, you don't perform the operations at all), or when the number of performed operations exceeds 100^{500}.For every vertex i, you have to determine if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex, if the chip is initially placed on the vertex i.
Input: ['8', '0 1 0 0 0 0 1 0', '8 6', '2 5', '7 8', '6 5', '4 5', '6 1', '7 3', ''] Output:['0 1 1 1 1 0 1 1 ', '']
[ 2 ]
Monocarp is planning to host a martial arts tournament. There will be three divisions based on weight: lightweight, middleweight and heavyweight. The winner of each division will be determined by a single elimination system.In particular, that implies that the number of participants in each division should be a power of two. Additionally, each division should have a non-zero amount of participants.n participants have registered for the tournament so far, the i-th of them weighs a_i. To split participants into divisions, Monocarp is going to establish two integer weight boundaries x and y (x < y). All participants who weigh strictly less than x will be considered lightweight. All participants who weigh greater or equal to y will be considered heavyweight. The remaining participants will be considered middleweight.It's possible that the distribution doesn't make the number of participants in each division a power of two. It can also lead to empty divisions. To fix the issues, Monocarp can invite an arbitrary number of participants to each division.Note that Monocarp can't kick out any of the n participants who have already registered for the tournament.However, he wants to invite as little extra participants as possible. Help Monocarp to choose x and y in such a way that the total amount of extra participants required is as small as possible. Output that amount.
Input: ['443 1 2 11162 2 2 1 1 186 3 6 3 6 3 6 6'] Output:['0', '2', '3', '2', '']
[ 0, 2, 3, 4 ]
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.The level he's currently on contains n monsters. The i-th of them appears k_i seconds after the start of the level and has h_i health points. As an additional constraint, h_i <= k_i for all 1 <= i <= n. All k_i are different.Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: 1, 2, 3, ... The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is 1. Otherwise, let the damage at the previous second be x. Then he can choose the damage to be either x + 1 or 1. A spell uses mana: casting a spell with damage x uses x mana. Mana doesn't regenerate.To kill the i-th monster, Monocarp has to cast a spell with damage at least h_i at the exact moment the monster appears, which is k_i.Note that Monocarp can cast the spell even when there is no monster at the current second.The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.It can be shown that it's always possible to kill all monsters under the constraints of the problem.
Input: ['316424 52 235 7 92 1 2'] Output:['10', '6', '7', '']
[ 2, 3, 4 ]
You are given a decimal representation of an integer x without leading zeros.You have to perform the following reduction on it exactly once: take two neighboring digits in x and replace them with their sum without leading zeros (if the sum is 0, it's represented as a single 0).For example, if x = 10057, the possible reductions are: choose the first and the second digits 1 and 0, replace them with 1+0=1; the result is 1057; choose the second and the third digits 0 and 0, replace them with 0+0=0; the result is also 1057; choose the third and the fourth digits 0 and 5, replace them with 0+5=5; the result is still 1057; choose the fourth and the fifth digits 5 and 7, replace them with 5+7=12; the result is 10012. What's the largest number that can be obtained?
Input: ['21005790'] Output:['10012', '9', '']
[ 2 ]
This is the hard version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version."Interplanetary Software, Inc." together with "Robots of Cydonia, Ltd." has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways.The developers from "Interplanetary Software, Inc." have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below. First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters "(", ")" and ".". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single "." characters, or a continuous substring "()". For instance, the string "(()(.))" is an RBS, as it can be transformed to an empty string with the following sequence of removals: "(()(.))" \rightarrow "(()())" \rightarrow "(())" \rightarrow "()" \rightarrow "". We got an empty string, so the initial string was an RBS. At the same time, the string ")(" is not an RBS, as it is not possible to apply such removal operations to it.An RBS is simple if this RBS is not empty, doesn't start with ".", and doesn't end with ".".Denote the substring of the string s as its sequential subsegment. In particular, s[l... r] = s_ls_{l+1}... s_r, where s_i is the i-th character of the string s.Now, move on to the problem statement itself. You are given a string s, initially consisting of characters "(" and ")". You need to answer the following queries: Given two indices, l and r (1 <= l < r <= n). It's guaranteed that the l-th character is equal to "(", the r-th character is equal to ")", and the characters between them are equal to ".". Then the l-th and the r-th characters must be set to ".". Given two indices, l and r (1 <= l < r <= n), and it's guaranteed that the substring s[l... r] is a simple RBS. You need to find the number of substrings in s[l... r] such that they are simple RBS. In other words, find the number of index pairs i, j such that l <= i < j <= r and s[i... j] is a simple RBS. You are an employee in "Interplanetary Software, Inc." and you were given the task to teach the cats to solve the problem above, after the update.
Input: ['9 8', ')(()())()', '2 3 6', '2 2 7', '1 3 4', '2 2 7', '2 2 9', '1 5 6', '1 2 7', '2 8 9', ''] Output:['3', '4', '2', '4', '1', '']
[ 4 ]
This is the easy version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version."Interplanetary Software, Inc." together with "Robots of Cydonia, Ltd." has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways.The developers from "Interplanetary Software, Inc." have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below. First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters "(", ")" and ".". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single "." characters, or a continuous substring "()". For instance, the string "(()(.))" is an RBS, as it can be transformed to an empty string with the following sequence of removals: "(()(.))" \rightarrow "(()())" \rightarrow "(())" \rightarrow "()" \rightarrow "". We got an empty string, so the initial string was an RBS. At the same time, the string ")(" is not an RBS, as it is not possible to apply such removal operations to it.An RBS is simple if this RBS is not empty, doesn't start with ".", and doesn't end with ".".Denote the substring of the string s as its sequential subsegment. In particular, s[l... r] = s_ls_{l+1}... s_r, where s_i is the i-th character of the string s.Now, move on to the problem statement itself. You are given a string s, initially consisting of characters "(" and ")". You need to answer the queries of the following kind.Given two indices, l and r (1 <= l < r <= n), and it's guaranteed that the substring s[l... r] is a simple RBS. You need to find the number of substrings in s[l... r] such that they are simple RBS. In other words, find the number of index pairs i, j such that l <= i < j <= r and s[i... j] is a simple RBS.You are an employee in "Interplanetary Software, Inc." and you were given the task to teach the cats to solve the problem above, after the update.Note that the "." character cannot appear in the string in this version of the problem. It is only needed for the hard version.
Input: ['9 4', ')(()())()', '2 3 6', '2 2 7', '2 8 9', '2 2 9', ''] Output:['3', '4', '1', '6', '']
[ 0 ]
Binary Spiders are species of spiders that live on Mars. These spiders weave their webs to defend themselves from enemies.To weave a web, spiders join in pairs. If the first spider in pair has x legs, and the second spider has y legs, then they weave a web with durability x \oplus y. Here, \oplus means bitwise XOR.Binary Spiders live in large groups. You observe a group of n spiders, and the i-th spider has a_i legs.When the group is threatened, some of the spiders become defenders. Defenders are chosen in the following way. First, there must be at least two defenders. Second, any pair of defenders must be able to weave a web with durability at least k. Third, there must be as much defenders as possible.Scientists have researched the behaviour of Binary Spiders for a long time, and now they have a hypothesis that they can always choose the defenders in an optimal way, satisfying the conditions above. You need to verify this hypothesis on your group of spiders. So, you need to understand how many spiders must become defenders. You are not a Binary Spider, so you decided to use a computer to solve this problem.
Input: ['6 8', '2 8 4 16 10 14', ''] Output:['3', '1 5 4', '']
[ 3 ]
Martians are actively engaged in interplanetary trade. Olymp City, the Martian city known for its spaceport, has become a place where goods from all the corners of our Galaxy come. To deliver even more freight from faraway planets, Martians need fast spaceships.A group of scientists conducts experiments to build a fast engine for the new spaceship. In the current experiment, there are n elementary particles, the i-th of them has type a_i.Denote a subsegment of the particle sequence (a_1, a_2, ..., a_n) as a sequence (a_l, a_{l+1}, ..., a_r) for some left bound l and right bound r (1 <= l <= r <= n). For instance, the sequence (1\ 4\ 2\ 8\ 5\ 7) for l=2 and r=4 has the sequence (4\ 2\ 8) as a subsegment. Two subsegments are considered different if at least one bound of those subsegments differs.Note that the subsegments can be equal as sequences but still considered different. For example, consider the sequence (1\ 1\ 1\ 1\ 1) and two of its subsegments: one with l=1 and r=3 and another with l=2 and r=4. Both subsegments are equal to (1\ 1\ 1), but still considered different, as their left and right bounds differ.The scientists want to conduct a reaction to get two different subsegments of the same length. Denote this length k. The resulting pair of subsegments must be harmonious, i. e. for some i (1 <= i <= k) it must be true that the types of particles on the i-th position are the same for these two subsegments. For example, the pair (1\ 7\ 3) and (4\ 7\ 8) is harmonious, as both subsegments have 7 on the second position. The pair (1\ 2\ 3) and (3\ 1\ 2) is not harmonious.The longer are harmonious subsegments, the more chances for the scientists to design a fast engine. So, they asked you to calculate the maximal possible length of harmonious pair made of different subsegments.
Input: ['473 1 5 2 1 3 461 1 1 1 1 161 4 2 8 5 7215 15'] Output:['4', '5', '-1', '1', '']
[ 0, 2 ]
Martian scientists explore Ganymede, one of Jupiter's numerous moons. Recently, they have found ruins of an ancient civilization. The scientists brought to Mars some tablets with writings in a language unknown to science.They found out that the inhabitants of Ganymede used an alphabet consisting of two letters, and each word was exactly \ell letters long. So, the scientists decided to write each word of this language as an integer from 0 to 2^{\ell} - 1 inclusively. The first letter of the alphabet corresponds to zero bit in this integer, and the second letter corresponds to one bit.The same word may have various forms in this language. Then, you need to restore the initial form. The process of doing it is described below.Denote the distance between two words as the amount of positions, in which these words differ. For example, the distance between 1001_2 and 1100_2 (in binary) is equal to two, as these words have different letters in the second and the fourth positions, counting from left to right. Further, denote the distance between words x and y as d(x, y).Let the word have n forms, the i-th of which is described with an integer x_i. All the x_i are not necessarily different, as two various forms of the word can be written the same. Consider some word y. Then, closeness of the word y is equal to the sum of distances to each of the word forms, i. e. the sum d(x_i, y) over all 1 <= i <= n.The initial form is the word y with minimal possible nearness.You need to help the scientists and write the program which finds the initial form of the word given all its known forms. Note that the initial form is not necessarily equal to any of the n given forms.
Input: ['73 518 9 213 518 18 181 115 301 2 3 4 56 1099 35 85 46 78 552 10 18 85 16 42 15 83 65 78 42'] Output:['17', '18', '1', '1', '39', '0', '2', '']
[ 2, 3 ]
Recently, Vlad has been carried away by spanning trees, so his friends, without hesitation, gave him a connected weighted undirected graph of n vertices and m edges for his birthday.Vlad defined the ority of a spanning tree as the bitwise OR of all its weights, and now he is interested in what is the minimum possible ority that can be achieved by choosing a certain spanning tree. A spanning tree is a connected subgraph of a given graph that does not contain cycles.In other words, you want to keep n-1 edges so that the graph remains connected and the bitwise OR weights of the edges are as small as possible. You have to find the minimum bitwise OR itself.
Input: ['33 31 2 12 3 21 3 25 74 2 72 5 83 4 23 2 12 4 24 1 21 2 23 41 2 12 3 21 3 33 1 4'] Output:['2', '10', '3', '']
[ 2 ]
This problem is interactive.We decided to play a game with you and guess the number x (1 <= x < n), where you know the number n.You can make queries like this: + c: this command assigns x = x + c (1 <= c < n) and then returns you the value \lfloor\frac{x}{n}\rfloor (x divide by n and round down).You win if you guess the current number with no more than 10 queries.
Input: ['3', '', '1'] Output:['+ 1', '', '! 3']
[ 4 ]
Masha meets a new friend and learns his phone number β€” s. She wants to remember it as soon as possible. The phone number β€” is a string of length m that consists of digits from 0 to 9. The phone number may start with 0.Masha already knows n phone numbers (all numbers have the same length m). It will be easier for her to remember a new number if the s is represented as segments of numbers she already knows. Each such segment must be of length at least 2, otherwise there will be too many segments and Masha will get confused.For example, Masha needs to remember the number: s = '12345678' and she already knows n = 4 numbers: '12340219', '20215601', '56782022', '12300678'. You can represent s as a 3 segment: '1234' of number one, '56' of number two, and '78' of number three. There are other ways to represent s.Masha asks you for help, she asks you to break the string s into segments of length 2 or more of the numbers she already knows. If there are several possible answers, print any of them.
Input: ['54 812340219202156015678202212300678123456782 31341261231 4121012214 32510648599570544 779686369486033461422454541979482268'] Output:['3', '1 4 1', '5 6 2', '3 4 3', '-1', '2', '1 2 1', '2 3 1', '-1', '3', '1 3 2', '5 6 3', '3 4 1', '']
[ 0 ]
You have a string s consisting of lowercase Latin alphabet letters. You can color some letters in colors from 1 to k. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color.Then you can swap any two symbols painted in the same color as many times as you want. After that, k strings will be created, i-th of them will contain all the characters colored in the color i, written in the order of their sequence in the string s.Your task is to color the characters of the string so that all the resulting k strings are palindromes, and the length of the shortest of these k strings is as large as possible.Read the note for the first test case of the example if you need a clarification.Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaa β€” are not.
Input: ['108 2bxyaxzay6 3aaaaaa6 1abcdef6 6abcdef3 2dxd11 2abcabcabcac6 6sipkic7 2eatoohd3 1llw6 2bfvfbv'] Output:['3', '2', '1', '1', '1', '5', '1', '1', '3', '3', '']
[ 2, 4 ]
You are given an array a consisting of n positive integers. You can perform operations on it.In one operation you can replace any element of the array a_i with \lfloor \frac{a_i}{2} \rfloor, that is, by an integer part of dividing a_i by 2 (rounding down).See if you can apply the operation some number of times (possible 0) to make the array a become a permutation of numbers from 1 to n β€”that is, so that it contains all numbers from 1 to n, each exactly once.For example, if a = [1, 8, 25, 2], n = 4, then the answer is yes. You could do the following: Replace 8 with \lfloor \frac{8}{2} \rfloor = 4, then a = [1, 4, 25, 2]. Replace 25 with \lfloor \frac{25}{2} \rfloor = 12, then a = [1, 4, 12, 2]. Replace 12 with \lfloor \frac{12}{2} \rfloor = 6, then a = [1, 4, 6, 2]. Replace 6 with \lfloor \frac{6}{2} \rfloor = 3, then a = [1, 4, 3, 2].
Input: ['641 8 25 221 199 8 3 4 2 7 1 5 638 2 1424 7 16 7522 6 22 4 22'] Output:['YES', 'NO', 'YES', 'NO', 'NO', 'YES', '']
[ 2, 3 ]
Polycarp has 3 positive integers a, b and c. He can perform the following operation exactly once. Choose a positive integer m and multiply exactly one of the integers a, b or c by m. Can Polycarp make it so that after performing the operation, the sequence of three numbers a, b, c (in this order) forms an arithmetic progression? Note that you cannot change the order of a, b and c.Formally, a sequence x_1, x_2, ..., x_n is called an arithmetic progression (AP) if there exists a number d (called "common difference") such that x_{i+1}=x_i+d for all i from 1 to n-1. In this problem, n=3.For example, the following sequences are AP: [5, 10, 15], [3, 2, 1], [1, 1, 1], and [13, 10, 7]. The following sequences are not AP: [1, 2, 4], [0, 1, 0] and [1, 3, 2].You need to answer t independent test cases.
Input: ['1110 5 3030 5 101 2 31 6 32 6 31 1 11 1 21 1 31 100000000 12 1 11 2 2'] Output:['YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'NO', 'YES', 'YES', 'NO', 'YES', '']
[ 3 ]
Polycarp got an array of integers a[1 ... n] as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become a_1=a_2=...=a_n). In one operation, he can take some indices in the array and increase the elements of the array at those indices by 1.For example, let a=[4,2,1,6,2]. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by 1. As a result, in one operation, he can get a new state of the array a=[5,3,1,7,2].What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become a_1=a_2=...=a_n)?
Input: ['363 4 2 4 1 231000 1002 998212 11'] Output:['3', '4', '1', '']
[ 3 ]
A binary tree of n nodes is given. Nodes of the tree are numbered from 1 to n and the root is the node 1. Each node can have no child, only one left child, only one right child, or both children. For convenience, let's denote l_u and r_u as the left and the right child of the node u respectively, l_u = 0 if u does not have the left child, and r_u = 0 if the node u does not have the right child.Each node has a string label, initially is a single character c_u. Let's define the string representation of the binary tree as the concatenation of the labels of the nodes in the in-order. Formally, let f(u) be the string representation of the tree rooted at the node u. f(u) is defined as follows: f(u) = \begin{cases} \texttt{<empty string>}, & \text{if }u = 0; \\ f(l_u) + c_u + f(r_u) & \text{otherwise}, \end{cases} where + denotes the string concatenation operation.This way, the string representation of the tree is f(1).For each node, we can duplicate its label at most once, that is, assign c_u with c_u + c_u, but only if u is the root of the tree, or if its parent also has its label duplicated.You are given the tree and an integer k. What is the lexicographically smallest string representation of the tree, if we can duplicate labels of at most k nodes?A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a!=b; 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: ['4 3', 'abab', '2 3', '0 0', '0 4', '0 0', ''] Output:['baaaab']
[ 2 ]
The statement of this problem shares a lot with problem A. The differences are that in this problem, the probability is introduced, and the constraint is different.A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of n rows and m columns. The rows of the floor are numbered from 1 to n from top to bottom, and columns of the floor are numbered from 1 to m from left to right. The cell on the intersection of the r-th row and the c-th column is denoted as (r,c). The initial position of the robot is (r_b, c_b).In one second, the robot moves by dr rows and dc columns, that is, after one second, the robot moves from the cell (r, c) to (r + dr, c + dc). Initially dr = 1, dc = 1. If there is a vertical wall (the left or the right walls) in the movement direction, dc is reflected before the movement, so the new value of dc is -dc. And if there is a horizontal wall (the upper or lower walls), dr is reflected before the movement, so the new value of dr is -dr.Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at (r_d, c_d). The job of the robot is to clean that dirty cell. After a lot of testings in problem A, the robot is now broken. It cleans the floor as described above, but at each second the cleaning operation is performed with probability \frac p {100} only, and not performed with probability 1 - \frac p {100}. The cleaning or not cleaning outcomes are independent each second.Given the floor size n and m, the robot's initial position (r_b, c_b) and the dirty cell's position (r_d, c_d), find the expected time for the robot to do its job.It can be shown that the answer can be expressed as an irreducible fraction \frac x y, where x and y are integers and y \not \equiv 0 \pmod{10^9 + 7} . Output the integer equal to x \cdot y^{-1} \bmod (10^9 + 7). In other words, output such an integer a that 0 <= a < 10^9 + 7 and a \cdot y \equiv x \pmod {10^9 + 7}.
Input: ['6', '2 2 1 1 2 1 25', '3 3 1 2 2 2 25', '10 10 1 1 10 10 75', '10 10 10 10 1 1 75', '5 5 1 3 2 2 10', '97 98 3 5 41 43 50', ''] Output:['3', '3', '15', '15', '332103349', '99224487', '']
[ 3 ]
There are n heaps of stone. The i-th heap has h_i stones. You want to change the number of stones in the heap by performing the following process once: You go through the heaps from the 3-rd heap to the n-th heap, in this order. Let i be the number of the current heap. You can choose a number d (0 <= 3 \cdot d <= h_i), move d stones from the i-th heap to the (i - 1)-th heap, and 2 \cdot d stones from the i-th heap to the (i - 2)-th heap. So after that h_i is decreased by 3 \cdot d, h_{i - 1} is increased by d, and h_{i - 2} is increased by 2 \cdot d. You can choose different or same d for different operations. Some heaps may become empty, but they still count as heaps. What is the maximum number of stones in the smallest heap after the process?
Input: ['441 2 10 1004100 100 100 155 1 1 1 861 2 3 4 5 6'] Output:['7', '1', '1', '3', '']
[ 2, 4 ]
Alice and Bob play the following game. Alice has a set S of disjoint ranges of integers, initially containing only one range [1, n]. In one turn, Alice picks a range [l, r] from the set S and asks Bob to pick a number in the range. Bob chooses a number d (l <= d <= r). Then Alice removes [l, r] from S and puts into the set S the range [l, d - 1] (if l <= d - 1) and the range [d + 1, r] (if d + 1 <= r). The game ends when the set S is empty. We can show that the number of turns in each game is exactly n.After playing the game, Alice remembers all the ranges [l, r] she picked from the set S, but Bob does not remember any of the numbers that he picked. But Bob is smart, and he knows he can find out his numbers d from Alice's ranges, and so he asks you for help with your programming skill.Given the list of ranges that Alice has picked ([l, r]), for each range, help Bob find the number d that Bob has picked.We can show that there is always a unique way for Bob to choose his number for a list of valid ranges picked by Alice.
Input: ['4', '1', '1 1', '3', '1 3', '2 3', '2 2', '6', '1 1', '3 5', '4 4', '3 6', '4 5', '1 6', '5', '1 5', '1 2', '4 5', '2 2', '4 4', ''] Output:['1 1 1', '', '1 3 1', '2 2 2', '2 3 3', '', '1 1 1', '3 5 3', '4 4 4', '3 6 6', '4 5 5', '1 6 2', '', '1 5 3', '1 2 1', '4 5 5', '2 2 2', '4 4 4', '']
[ 0 ]
A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of n rows and m columns. The rows of the floor are numbered from 1 to n from top to bottom, and columns of the floor are numbered from 1 to m from left to right. The cell on the intersection of the r-th row and the c-th column is denoted as (r,c). The initial position of the robot is (r_b, c_b).In one second, the robot moves by dr rows and dc columns, that is, after one second, the robot moves from the cell (r, c) to (r + dr, c + dc). Initially dr = 1, dc = 1. If there is a vertical wall (the left or the right walls) in the movement direction, dc is reflected before the movement, so the new value of dc is -dc. And if there is a horizontal wall (the upper or lower walls), dr is reflected before the movement, so the new value of dr is -dr.Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at (r_d, c_d). The job of the robot is to clean that dirty cell. Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes. Given the floor size n and m, the robot's initial position (r_b, c_b) and the dirty cell's position (r_d, c_d), find the time for the robot to do its job.
Input: ['5', '10 10 6 1 2 8', '10 10 9 9 1 1', '9 8 5 6 2 1', '6 9 2 2 5 8', '2 2 1 1 2 1', ''] Output:['7', '10', '9', '3', '0', '']
[ 0, 3 ]
Let's call a set of positive integers a_1, a_2, ..., a_k quadratic if the product of the factorials of its elements is a square of an integer, i. e. \prod\limits_{i=1}^{k} a_i! = m^2, for some integer m.You are given a positive integer n.Your task is to find a quadratic subset of a set 1, 2, ..., n of maximum size. If there are multiple answers, print any of them.
Input: ['1', ''] Output:['1', '1 ', '']
[ 3 ]
Petya is a math teacher. n of his students has written a test consisting of m questions. For each student, it is known which questions he has answered correctly and which he has not.If the student answers the j-th question correctly, he gets p_j points (otherwise, he gets 0 points). Moreover, the points for the questions are distributed in such a way that the array p is a permutation of numbers from 1 to m.For the i-th student, Petya knows that he expects to get x_i points for the test. Petya wonders how unexpected the results could be. Petya believes that the surprise value of the results for students is equal to \sum\limits_{i=1}^{n} |x_i - r_i|, where r_i is the number of points that the i-th student has got for the test.Your task is to help Petya find such a permutation p for which the surprise value of the results is maximum possible. If there are multiple answers, print any of them.
Input: ['3', '4 3', '5 1 2 2', '110', '100', '101', '100', '4 4', '6 2 0 10', '1001', '0010', '0110', '0101', '3 6', '20 3 15', '010110', '000101', '111111', ''] Output:['3 1 2 ', '2 3 4 1 ', '3 1 4 5 2 6 ', '']
[ 0, 2 ]
You are given a binary string (i. e. a string consisting of characters 0 and/or 1) s of length n. You can perform the following operation with the string s at most once: choose a substring (a contiguous subsequence) of s having exactly k characters 1 in it, and shuffle it (reorder the characters in the substring as you wish).Calculate the number of different strings which can be obtained from s by performing this operation at most once.
Input: ['7 2', '1100110', ''] Output:['16', '']
[ 3 ]
You are given an integer array a_1, a_2, ..., a_n and integer k.In one step you can either choose some index i and decrease a_i by one (make a_i = a_i - 1); or choose two indices i and j and set a_i equal to a_j (make a_i = a_j). What is the minimum number of steps you need to make the sum of array \sum\limits_{i=1}^{n}{a_i} <= k? (You are allowed to make values of array negative).
Input: ['4', '1 10', '20', '2 69', '6 9', '7 8', '1 2 1 3 1 2 1', '10 1', '1 2 3 1 2 6 1 6 8 10', ''] Output:['10', '0', '2', '7', '']
[ 0, 2, 4 ]
Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.So imagine Monocarp got recommended n songs, numbered from 1 to n. The i-th song had its predicted rating equal to p_i, where 1 <= p_i <= n and every integer from 1 to n appears exactly once. In other words, p is a permutation.After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string s, such that s_i=0 means that he disliked the i-th song, and s_i=1 means that he liked it.Now the service has to re-evaluate the song ratings in such a way that: the new ratings q_1, q_2, ..., q_n still form a permutation (1 <= q_i <= n; each integer from 1 to n appears exactly once); every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all i, j such that s_i=1 and s_j=0, q_i>q_j should hold). Among all valid permutations q find the one that has the smallest value of \sum\limits_{i=1}^n |p_i-q_i|, where |x| is an absolute value of x.Print the permutation q_1, q_2, ..., q_n. If there are multiple answers, you can print any of them.
Input: ['3', '2', '1 2', '10', '3', '3 1 2', '111', '8', '2 3 1 8 5 4 7 6', '01110001', ''] Output:['2 1', '3 1 2', '1 6 5 8 3 2 4 7', '']
[ 2, 3 ]
There are three sticks with integer lengths l_1, l_2 and l_3.You are asked to break exactly one of them into two pieces in such a way that: both pieces have positive (strictly greater than 0) integer length; the total length of the pieces is equal to the original length of the stick; it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides. A square is also considered a rectangle.Determine if it's possible to do that.
Input: ['4', '6 1 5', '2 5 2', '2 4 2', '5 5 4', ''] Output:['YES', 'NO', 'YES', 'YES', '']
[ 3 ]
You are given the sequence of integers a_1, a_2, ..., a_n of length n. The sequence of indices i_1 < i_2 < ... < i_k of length k denotes the subsequence a_{i_1}, a_{i_2}, ..., a_{i_k} of length k of sequence a.The subsequence a_{i_1}, a_{i_2}, ..., a_{i_k} of length k of sequence a is called increasing subsequence if a_{i_j} < a_{i_{j+1}} for each 1 <=q j < k.The weight of the increasing subsequence a_{i_1}, a_{i_2}, ..., a_{i_k} of length k of sequence a is the number of 1 <=q j <=q k, such that exists index i_k < x <=q n and a_x > a_{i_j}.For example, if a = [6, 4, 8, 6, 5], then the sequence of indices i = [2, 4] denotes increasing subsequence [4, 6] of sequence a. The weight of this increasing subsequence is 1, because for j = 1 exists x = 5 and a_5 = 5 > a_{i_1} = 4, but for j = 2 such x doesn't exist.Find the sum of weights of all increasing subsequences of a modulo 10^9+7.
Input: ['456 4 8 6 541 2 3 433 2 244 5 6 5'] Output:['4', '12', '0', '6', '']
[ 3 ]
Dasha has 10^{100} coins. Recently, she found a binary string s of length n and some operations that allows to change this string (she can do each operation any number of times): Replace substring 00 of s by 0 and receive a coins. Replace substring 11 of s by 1 and receive b coins. Remove 0 from any position in s and pay c coins. It turned out that while doing this operations Dasha should follow the rule: It is forbidden to do two operations with the same parity in a row. Operations are numbered by integers 1-3 in the order they are given above. Please, calculate what is the maximum profit Dasha can get by doing these operations and following this rule.
Input: ['35 2 2 1011016 4 3 51100016 3 2 1011110'] Output:['3', '11', '4', '']
[ 2 ]
You have decided to open a new school. You have already found n teachers and m groups of students. The i-th group of students consists of k_i >=q 2 students. You know age of each teacher and each student. The ages of teachers are a_1, a_2, ..., a_n and the ages of students of the i-th group are b_{i, 1}, b_{i, 2}, ..., b_{i, k_i}.To start lessons you should assign the teacher to each group of students. Such assignment should satisfy the following requirements: To each group exactly one teacher assigned. To each teacher at most 1 group of students assigned. The average of students' ages in each group doesn't exceed the age of the teacher assigned to this group. The average of set x_1, x_2, ..., x_k of k integers is \frac{x_1 + x_2 + ... + x_k}{k}.Recently you have heard that one of the students will refuse to study in your school. After this, the size of one group will decrease by 1 while all other groups will remain unchanged.You don't know who will refuse to study. For each student determine if you can start lessons in case of his refusal.Note, that it is not guaranteed that it is possible to start lessons before any refusal.
Input: ['2', '1 1', '30', '3', '25 16 37', '4 2', '9 12 12 6', '2', '4 5', '3', '111 11 11', ''] Output:['101', '00100', '']
[ 2, 4 ]
Circular land is an 2n * 2n grid. Rows of this grid are numbered by integers from 1 to 2n from top to bottom and columns of this grid are numbered by integers from 1 to 2n from left to right. The cell (x, y) is the cell on the intersection of row x and column y for 1 <=q x <=q 2n and 1 <=q y <=q 2n.There are n^2 of your friends in the top left corner of the grid. That is, in each cell (x, y) with 1 <=q x, y <=q n there is exactly one friend. Some of the other cells are covered with snow.Your friends want to get to the bottom right corner of the grid. For this in each cell (x, y) with n+1 <=q x, y <=q 2n there should be exactly one friend. It doesn't matter in what cell each of friends will be.You have decided to help your friends to get to the bottom right corner of the grid.For this, you can give instructions of the following types: You select a row x. All friends in this row should move to the next cell in this row. That is, friend from the cell (x, y) with 1 <=q y < 2n will move to the cell (x, y + 1) and friend from the cell (x, 2n) will move to the cell (x, 1). You select a row x. All friends in this row should move to the previous cell in this row. That is, friend from the cell (x, y) with 1 < y <=q 2n will move to the cell (x, y - 1) and friend from the cell (x, 1) will move to the cell (x, 2n). You select a column y. All friends in this column should move to the next cell in this column. That is, friend from the cell (x, y) with 1 <=q x < 2n will move to the cell (x + 1, y) and friend from the cell (2n, y) will move to the cell (1, y). You select a column y. All friends in this column should move to the previous cell in this column. That is, friend from the cell (x, y) with 1 < x <=q 2n will move to the cell (x - 1, y) and friend from the cell (1, y) will move to the cell (2n, y). Note how friends on the grid border behave in these instructions. Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.In order to save your friends you can remove snow from some cells before giving the first instruction: You can select the cell (x, y) that is covered with snow now and remove snow from this cell for c_{x, y} coins. You can do this operation any number of times.You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.Please, find how many coins you will spend.
Input: ['410 81 9920 0 0 00 0 0 09 9 2 29 9 9 920 0 4 20 0 2 44 2 4 22 4 2 440 0 0 0 0 0 0 20 0 0 0 0 0 2 00 0 0 0 0 2 0 00 0 0 0 2 0 0 00 0 0 2 2 0 2 20 0 2 0 1 6 2 10 2 0 0 2 4 7 42 0 0 0 2 0 1 6'] Output:['100', '22', '14', '42', '']
[ 2, 3 ]
This is an interactive problem.The jury has a permutation p of length n and wants you to guess it. For this, the jury created another permutation q of length n. Initially, q is an identity permutation (q_i = i for all i).You can ask queries to get q_i for any i you want. After each query, the jury will change q in the following way: At first, the jury will create a new permutation q' of length n such that q'_i = q_{p_i} for all i. Then the jury will replace permutation q with pemutation q'. You can make no more than 2n queries in order to quess p.
Input: ['2', '4', '', '3', '', '2', '', '1', '', '4', '', '2', '', '4', '', '4'] Output:['? 3', '', '? 2', '', '? 4', '', '! 4 2 1 3', '', '? 2', '', '? 3', '', '? 2', '', '! 1 3 4 2']
[ 3 ]
The integers shop sells n segments. The i-th of them contains all integers from l_i to r_i and costs c_i coins.Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.After shopping, Vasya will get some more integers as a gift. He will get integer x as a gift if and only if all of the following conditions are satisfied: Vasya hasn't bought x. Vasya has bought integer l that is less than x. Vasya has bought integer r that is greater than x. Vasya can get integer x as a gift only once so he won't have the same integers after receiving a gift.For example, if Vasya buys segment [2, 4] for 20 coins and segment [7, 8] for 22 coins, he spends 42 coins and receives integers 2, 3, 4, 7, 8 from these segments. He also gets integers 5 and 6 as a gift.Due to the technical issues only the first s segments (that is, segments [l_1, r_1], [l_2, r_2], ..., [l_s, r_s]) will be available tomorrow in the shop.Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.For each s from 1 to n, find how many coins will Vasya spend if only the first s segments will be available.
Input: ['322 4 207 8 2225 11 425 11 4261 4 45 8 97 8 72 10 2521 11 2711 10 1'] Output:['20', '42', '42', '42', '4', '13', '11', '256', '271', '271', '']
[ 2 ]
You are given a permutation p consisting of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once).Let's call an array a bipartite if the following undirected graph is bipartite: the graph consists of n vertices; two vertices i and j are connected by an edge if i < j and a_i > a_j. Your task is to find a bipartite array of integers a of size n, such that a_i = p_i or a_i = -p_i, or report that no such array exists. If there are multiple answers, print any of them.
Input: ['4', '3', '1 2 3', '6', '1 3 2 6 5 4', '4', '4 1 3 2', '8', '3 2 1 6 7 8 5 4', ''] Output:['YES', '1 2 3', 'NO', 'YES', '-4 -1 -3 -2', 'YES', '-3 -2 1 6 7 -8 -5 -4', '']
[ 2 ]
One day, early in the morning, you decided to buy yourself a bag of chips in the nearby store. The store has chips of n different flavors. A bag of the i-th flavor costs a_i burles.The store may run out of some flavors, so you'll decide which one to buy after arriving there. But there are two major flaws in this plan: you have only coins of 1, 2 and 3 burles; since it's morning, the store will ask you to pay in exact change, i. e. if you choose the i-th flavor, you'll have to pay exactly a_i burles. Coins are heavy, so you'd like to take the least possible number of coins in total. That's why you are wondering: what is the minimum total number of coins you should take with you, so you can buy a bag of chips of any flavor in exact change?
Input: ['4', '1', '1337', '3', '10 8 10', '5', '1 2 3 4 5', '3', '7 77 777', ''] Output:['446', '4', '3', '260', '']
[ 0, 2 ]
You are given an integer k and a string s that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk).Each asterisk should be replaced with several (from 0 to k inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'.The result of the replacement is called a BA-string.Two strings a and b are different if they either have different lengths or there exists such a position i that a_i \neq b_i.A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a!=b; 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. Now consider all different BA-strings and find the x-th lexicographically smallest of them.
Input: ['3', '2 4 3', 'a*', '4 1 3', 'a**a', '6 3 20', '**a***', ''] Output:['abb', 'abba', 'babbbbbbbbb', '']
[ 0, 2, 3 ]
A rectangle with its opposite corners in (0, 0) and (w, h) and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choose three points in such a way that: exactly two of them belong to the same side of a rectangle; the area of a triangle formed by them is maximum possible. Print the doubled area of this triangle. It can be shown that the doubled area of any triangle formed by lattice points is always an integer.
Input: ['3', '5 8', '2 1 2', '3 2 3 4', '3 1 4 6', '2 4 5', '10 7', '2 3 9', '2 1 7', '3 1 3 4', '3 4 5 6', '11 5', '3 1 6 8', '3 3 6 8', '3 1 3 4', '2 2 4', ''] Output:['25', '42', '35', '']
[ 2, 3 ]
You are given a permutation p of n elements. A permutation of n elements is an array of length n containing each integer from 1 to n exactly once. For example, [1, 2, 3] and [4, 3, 5, 1, 2] are permutations, but [1, 2, 4] and [4, 3, 2, 1, 2] are not permutations. You should perform q queries.There are two types of queries: 1 x y β€” swap p_x and p_y. 2 i k β€” print the number that i will become if we assign i = p_i k times.
Input: ['5 4', '5 3 4 2 1', '2 3 1', '2 1 2', '1 1 3', '2 1 2', ''] Output:['4', '1', '2', '']
[ 0 ]
Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules.There are mines on the field, for each the coordinates of its location are known (x_i, y_i). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates all mines vertically and horizontally at a distance of k (two perpendicular lines). As a result, we get an explosion on the field in the form of a "plus" symbol ('+'). Thus, one explosion can cause new explosions, and so on.Also, Polycarp can detonate anyone mine every second, starting from zero seconds. After that, a chain reaction of explosions also takes place. Mines explode instantly and also instantly detonate other mines according to the rules described above.Polycarp wants to set a new record and asks you to help him calculate in what minimum number of seconds all mines can be detonated.
Input: ['3', '', '5 0', '0 0 1', '0 1 4', '1 0 2', '1 1 3', '2 2 9', '', '5 2', '0 0 1', '0 1 4', '1 0 2', '1 1 3', '2 2 9', '', '6 1', '1 -1 3', '0 -1 9', '0 1 7', '-1 0 1', '-1 1 9', '-1 -1 7', ''] Output:['2', '1', '0', '']
[ 2, 4 ]
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play).n people gathered in a room with m tables (n >= 2m). They want to play the Hat k times. Thus, k games will be played at each table. Each player will play in k games.To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables.Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either \lfloor\frac{n}{m}\rfloor people or \lceil\frac{n}{m}\rceil people (that is, either n/m rounded down, or n/m rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value b_i β€” the number of times the i-th player played at a table with \lceil\frac{n}{m}\rceil persons (n/m rounded up). Any two values of b_imust differ by no more than 1. In other words, for any two players i and j, it must be true |b_i - b_j| <= 1. For example, if n=5, m=2 and k=2, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: 1, 2, 3 are played at the first table, and 4, 5 at the second one. The second game: at the first table they play 5, 1, and at the second β€” 2, 3, 4. This schedule is not "fair" since b_2=2 (the second player played twice at a big table) and b_5=0 (the fifth player did not play at a big table). First game: 1, 2, 3 are played at the first table, and 4, 5 at the second one. The second game: at the first table they play 4, 5, 2, and at the second one β€” 1, 3. This schedule is "fair": b=[1,2,1,1,1] (any two values of b_i differ by no more than 1). Find any "fair" game schedule for n people if they play on the m tables of k games.
Input: ['3', '5 2 2', '8 3 1', '2 1 3', ''] Output:['3 1 2 3', '2 4 5', '3 4 5 2', '2 1 3', '', '2 6 2', '3 3 5 1', '3 4 7 8', '', '2 2 1', '2 2 1', '2 2 1', '']
[ 0, 2, 3 ]
Dmitry has an array of n non-negative integers a_1, a_2, ..., a_n.In one operation, Dmitry can choose any index j (1 <= j <= n) and increase the value of the element a_j by 1. He can choose the same index j multiple times.For each i from 0 to n, determine whether Dmitry can make the \mathrm{MEX} of the array equal to exactly i. If it is possible, then determine the minimum number of operations to do it.The \mathrm{MEX} of the array is equal to the minimum non-negative integer that is not in the array. For example, the \mathrm{MEX} of the array [3, 1, 0] is equal to 2, and the array [3, 3, 1, 4] is equal to 0.
Input: ['5', '3', '0 1 3', '7', '0 1 2 3 4 3 2', '4', '3 0 0 0', '7', '4 6 2 3 5 0 5', '5', '4 0 1 0 4', ''] Output:['1 1 0 -1 ', '1 1 2 2 1 0 2 6 ', '3 0 1 4 3 ', '1 0 -1 -1 -1 -1 -1 -1 ', '2 1 0 2 -1 -1 ', '']
[ 2, 3 ]