question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
Vlad has n friends, for each of whom he wants to buy one gift for the New Year.There are m shops in the city, in each of which he can buy a gift for any of his friends. If the j-th friend (1 <= j <= n) receives a gift bought in the shop with the number i (1 <= i <= m), then the friend receives p_{ij} units of joy. The rectangular table p_{ij} is given in the input.Vlad has time to visit at most n-1 shops (where n is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them.Let the j-th friend receive a_j units of joy from Vlad's gift. Let's find the value \alpha=\min\{a_1, a_2, ..., a_n\}. Vlad's goal is to buy gifts so that the value of \alpha is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends.For example, let m = 2, n = 2. Let the joy from the gifts that we can buy in the first shop: p_{11} = 1, p_{12}=2, in the second shop: p_{21} = 3, p_{22}=4.Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy 3, and for the second — bringing joy 4. In this case, the value \alpha will be equal to \min\{3, 4\} = 3Help Vlad choose gifts for his friends so that the value of \alpha is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most n-1 shops (where n is the number of friends). In the shop, he can buy any number of gifts. | Input: ['5', '', '2 2', '1 2', '3 4', '', '4 3', '1 3 1', '3 1 1', '1 2 2', '1 1 3', '', '2 3', '5 3 4', '2 5 1', '', '4 2', '7 9', '8 1', '9 6', '10 8', '', '2 4', '6 5 2 1', '7 9 7 2', ''] Output:['3', '2', '4', '8', '2', ''] | [
2,
4
] |
Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: 1, 4, 8, 9, ....For a given number n, count the number of integers from 1 to n that Polycarp likes. In other words, find the number of such x that x is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously). | Input: ['6', '10', '1', '25', '1000000000', '999999999', '500000000', ''] Output:['4', '1', '6', '32591', '32590', '23125', ''] | [
3
] |
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics.To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price x, then he can trade it for any item (exactly one item) with price not greater than x+k.Monocarp initially has n items, the price of the i-th item he has is a_i. The character Monocarp is trading with has m items, the price of the i-th item they have is b_i. Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it.You have to answer q queries. Each query consists of one integer, which is the value of k, and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost x for an item of cost not greater than x+k during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get. | Input: ['3 4 5', '10 30 15', '12 31 14 18', '0 1 2 3 4', ''] Output:['55', '56', '60', '64', '64', ''] | [
2
] |
You are given two positive integers x and y. You can perform the following operation with x: write it in its binary form without leading zeros, add 0 or 1 to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of x.For example: 34 can be turned into 81 via one operation: the binary form of 34 is 100010, if you add 1, reverse it and remove leading zeros, you will get 1010001, which is the binary form of 81. 34 can be turned into 17 via one operation: the binary form of 34 is 100010, if you add 0, reverse it and remove leading zeros, you will get 10001, which is the binary form of 17. 81 can be turned into 69 via one operation: the binary form of 81 is 1010001, if you add 0, reverse it and remove leading zeros, you will get 1000101, which is the binary form of 69. 34 can be turned into 69 via two operations: first you turn 34 into 81 and then 81 into 69. Your task is to find out whether x can be turned into y after a certain number of operations (possibly zero). | Input: ['3 3', ''] Output:['YES', ''] | [
3
] |
n towns are arranged in a circle sequentially. The towns are numbered from 1 to n in clockwise order. In the i-th town, there lives a singer with a repertoire of a_i minutes for each i \in [1, n].Each singer visited all n towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the i-th singer got inspired and came up with a song that lasts a_i minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.Hence, for the i-th singer, the concert in the i-th town will last a_i minutes, in the (i + 1)-th town the concert will last 2 \cdot a_i minutes, ..., in the ((i + k) \bmod n + 1)-th town the duration of the concert will be (k + 2) \cdot a_i, ..., in the town ((i + n - 2) \bmod n + 1) — n \cdot a_i minutes.You are given an array of b integer numbers, where b_i is the total duration of concerts in the i-th town. Reconstruct any correct sequence of positive integers a or say that it is impossible. | Input: ['4', '3', '12 16 14', '1', '1', '3', '1 2 3', '6', '81 75 75 93 93 87', ''] Output:['YES', '3 1 3 ', 'YES', '1 ', 'NO', 'YES', '5 5 4 1 4 5 ', ''] | [
3
] |
You are given an array a of n integers, and another integer k such that 2k <= n.You have to perform exactly k operations with this array. In one operation, you have to choose two elements of the array (let them be a_i and a_j; they can be equal or different, but their positions in the array must not be the same), remove them from the array, and add \lfloor \frac{a_i}{a_j} \rfloor to your score, where \lfloor \frac{x}{y} \rfloor is the maximum integer not exceeding \frac{x}{y}.Initially, your score is 0. After you perform exactly k operations, you add all the remaining elements of the array to the score.Calculate the minimum possible score you can get. | Input: ['5', '7 3', '1 1 1 2 1 3 1', '5 1', '5 5 5 5 5', '4 2', '1 3 3 7', '2 0', '4 2', '9 2', '1 10 10 1 10 2 7 10 3', ''] Output:['2', '16', '0', '6', '16', ''] | [
2,
3
] |
You are given an array a consisting of n positive integers. You have to choose a positive integer d and paint all elements into two colors. All elements which are divisible by d will be painted red, and all other elements will be painted blue.The coloring is called beautiful if there are no pairs of adjacent elements with the same color in the array. Your task is to find any value of d which yields a beautiful coloring, or report that it is impossible. | Input: ['5', '5', '1 2 3 4 5', '3', '10 5 15', '3', '100 10 200', '10', '9 8 2 6 6 2 8 6 5 4', '2', '1 3', ''] Output:['2', '0', '100', '0', '3', ''] | [
3
] |
Polycarp had an array a of 3 positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array b of 7 integers.For example, if a = \{1, 4, 3\}, then Polycarp wrote out 1, 4, 3, 1 + 4 = 5, 1 + 3 = 4, 4 + 3 = 7, 1 + 4 + 3 = 8. After sorting, he got an array b = \{1, 3, 4, 4, 5, 7, 8\}.Unfortunately, Polycarp lost the array a. He only has the array b left. Help him to restore the array a. | Input: ['5', '1 3 4 4 5 7 8', '1 2 3 4 5 6 7', '300000000 300000000 300000000 600000000 600000000 600000000 900000000', '1 1 2 999999998 999999999 999999999 1000000000', '1 2 2 3 3 4 5', ''] Output:['1 4 3', '4 1 2', '300000000 300000000 300000000', '999999998 1 1', '1 2 2', ''] | [
3
] |
Christmas is coming, Icy has just received a box of chocolates from her grandparents! The box contains n chocolates. The i-th chocolate has a non-negative integer type a_i.Icy believes that good things come in pairs. Unfortunately, all types of chocolates are distinct (all a_i are distinct). Icy wants to make at least one pair of chocolates the same type. As a result, she asks her grandparents to perform some chocolate exchanges. Before performing any chocolate exchanges, Icy chooses two chocolates with indices x and y (1 <= x, y <= n, x!=y).In a chocolate exchange, Icy's grandparents choose a non-negative integer k, such that 2^k >= a_x, and change the type of the chocolate x from a_x to 2^k - a_x (that is, perform a_x := 2^k - a_x).The chocolate exchanges will be stopped only when a_x = a_y. Note that other pairs of equal chocolate types do not stop the procedure.Icy's grandparents are smart, so they would choose the sequence of chocolate exchanges that minimizes the number of exchanges needed. Since Icy likes causing trouble, she wants to maximize the minimum number of exchanges needed by choosing x and y appropriately. She wonders what is the optimal pair (x, y) such that the minimum number of exchanges needed is maximized across all possible choices of (x, y).Since Icy is not good at math, she hopes that you can help her solve the problem. | Input: ['5', '5 6 7 8 9', ''] Output:['2 5 5', ''] | [
3
] |
This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.There are n players labelled from 1 to n. It is guaranteed that n is a multiple of 3.Among them, there are k impostors and n-k crewmates. The number of impostors, k, is not given to you. It is guaranteed that \frac{n}{3} < k < \frac{2n}{3}.In each question, you can choose three distinct integers a, b, c (1 <= a, b, c <= n) and ask: "Among the players labelled a, b and c, are there more impostors or more crewmates?" You will be given the integer 0 if there are more impostors than crewmates, and 1 otherwise.Find the number of impostors k and the indices of players that are impostors after asking at most n+6 questions.The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time. | Input: ['2', '6', '', '0', '', '1', '', '9', '', '1'] Output:['? 1 2 3', '', '? 3 4 5', '', '! 3 4 1 2', '', '? 7 1 9', '', '! 4 2 3 6 8'] | [
3
] |
Paprika loves permutations. She has an array a_1, a_2, ..., a_n. She wants to make the array a permutation of integers 1 to n.In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers i (1 <= i <= n) and x (x > 0), then perform a_i := a_i \bmod x (that is, replace a_i by the remainder of a_i divided by x). In different operations, the chosen i and x can be different.Determine the minimum number of operations needed to make the array a permutation of integers 1 to n. If it is impossible, output -1.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: ['4', '2', '1 7', '3', '1 5 4', '4', '12345678 87654321 20211218 23571113', '9', '1 2 3 4 18 19 5 6 7', ''] Output:['1', '-1', '4', '2', ''] | [
2,
3,
4
] |
Given a positive integer n. Find three distinct positive integers a, b, c such that a + b + c = n and \operatorname{gcd}(a, b) = c, where \operatorname{gcd}(x, y) denotes the greatest common divisor (GCD) of integers x and y. | Input: ['6', '18', '63', '73', '91', '438', '122690412', ''] Output:['6 9 3', '21 39 3', '29 43 1', '49 35 7', '146 219 73', '28622 122661788 2'] | [
0,
3
] |
You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc. Find string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'.String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.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: ['7', 'abacaba', 'abc', 'cccba', 'acb', 'dbsic', 'bac', 'abracadabra', 'abc', 'dddddddddddd', 'cba', 'bbc', 'abc', 'ac', 'abc', ''] Output:['aaaacbb', 'abccc', 'bcdis', 'aaaaacbbdrr', 'dddddddddddd', 'bbc', 'ac', ''] | [
2
] |
You are given an array a_1, a_2, ..., a_n and an integer x.Find the number of non-empty subsets of indices of this array 1 <=q b_1 < b_2 < ... < b_k <=q n, such that for all pairs (i, j) where 1 <=q i < j <=q k, the inequality a_{b_i} \oplus a_{b_j} <=q x is held. Here, \oplus denotes the bitwise XOR operation. As the answer may be very large, output it modulo 998\,244\,353. | Input: ['4 2', '0 1 2 3', ''] Output:['8', ''] | [
3
] |
You are given a simple undirected graph with n vertices and m edges. Edge i is colored in the color c_i, which is either 1, 2, or 3, or left uncolored (in this case, c_i = -1).You need to color all of the uncolored edges in such a way that for any three pairwise adjacent vertices 1 <=q a < b < c <=q n, the colors of the edges a <=ftrightarrow b, b <=ftrightarrow c, and a <=ftrightarrow c are either pairwise different, or all equal. In case no such coloring exists, you need to determine that. | Input: ['4', '3 3', '1 2 1', '2 3 2', '3 1 -1', '3 3', '1 2 1', '2 3 1', '3 1 -1', '4 4', '1 2 -1', '2 3 -1', '3 4 -1', '4 1 -1', '3 3', '1 2 1', '2 3 1', '3 1 2', ''] Output:['1 2 3 ', '1 1 1 ', '1 2 2 3 ', '-1', ''] | [
0,
3
] |
You are given two strings s and t of equal length n. In one move, you can swap any two adjacent characters of the string s.You need to find the minimal number of operations you need to make string s lexicographically smaller than string t.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', '1', 'a', 'a', '3', 'rll', 'rrr', '3', 'caa', 'aca', '5', 'ababa', 'aabba', ''] Output:['-1', '0', '2', '2', ''] | [
0,
2
] |
You are given an array of integers a_1, a_2, ..., a_n and an integer x.You need to select the maximum number of elements in the array, such that for every subsegment a_l, a_{l + 1}, ..., a_r containing strictly more than one element (l < r), either: At least one element on this subsegment is not selected, or a_l + a_{l+1} + ... + a_r >=q x \cdot (r - l + 1). | Input: ['4', '5', '1 2 3 4 5', '2', '10', '2 4 2 4 2 4 2 4 2 4', '3', '3', '-10 -5 -10', '-8', '3', '9 9 -3', '5', ''] Output:['4', '8', '2', '2', ''] | [
2,
3
] |
An array a_1, a_2, ..., a_n is good if and only if for every subsegment 1 <=q l <=q r <=q n, the following holds: a_l + a_{l + 1} + ... + a_r = \frac{1}{2}(a_l + a_r) \cdot (r - l + 1). You are given an array of integers a_1, a_2, ..., a_n. In one operation, you can replace any one element of this array with any real number. Find the minimum number of operations you need to make this array good. | Input: ['5', '4', '1 2 3 4', '4', '1 1 2 2', '2', '0 -1', '6', '3 -2 4 -1 -4 0', '1', '-100', ''] Output:['0', '2', '0', '3', '0', ''] | [
0,
3
] |
You have a string s_1 s_2 ... s_n and you stand on the left of the string looking right. You want to choose an index k (1 <= k <= n) and place a mirror after the k-th letter, so that what you see is s_1 s_2 ... s_k s_k s_{k - 1} ... s_1. What is the lexicographically smallest string you can see?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', '10', 'codeforces', '9', 'cbacbacba', '3', 'aaa', '4', 'bbaa', ''] Output:['cc', 'cbaabc', 'aa', 'bb', ''] | [
2
] |
There are n reindeer at the North Pole, all battling for the highest spot on the "Top Reindeer" leaderboard on the front page of CodeNorses (a popular competitive reindeer gaming website). Interestingly, the "Top Reindeer" title is just a measure of upvotes and has nothing to do with their skill level in the reindeer games, but they still give it the utmost importance.Currently, the i-th reindeer has a score of a_i. You would like to influence the leaderboard with some operations. In an operation, you can choose a reindeer, and either increase or decrease his score by 1 unit. Negative scores are allowed.You have m requirements for the resulting scores. Each requirement is given by an ordered pair (u, v), meaning that after all operations, the score of reindeer u must be less than or equal to the score of reindeer v.Your task is to perform the minimum number of operations so that all requirements will be satisfied. | Input: ['7 6', '3 1 4 9 2 5 6', '1 2', '2 3', '3 4', '4 5', '5 6', '6 7', ''] Output:['1 1 4 4 4 5 6 ', ''] | [
4
] |
After getting bored by playing with crayons, you decided to switch to Legos! Today, you're working with a long strip, with height 1 and length n, some positions of which are occupied by 1 by 1 Lego pieces.In one second, you can either remove two adjacent Lego pieces from the strip (if both are present), or add two Lego pieces to adjacent positions (if both are absent). You can only add or remove Lego's at two adjacent positions at the same time, as otherwise your chubby fingers run into precision issues.You want to know exactly how much time you'll spend playing with Legos. You value efficiency, so given some starting state and some ending state, you'll always spend the least number of seconds to transform the starting state into the ending state. If it's impossible to transform the starting state into the ending state, you just skip it (so you spend 0 seconds).The issue is that, for some positions, you don't remember whether there were Legos there or not (in either the starting state, the ending state, or both). Over all pairs of (starting state, ending state) that are consistent with your memory, find the total amount of time it will take to transform the starting state to the ending state. Print this value modulo 1\,000\,000\,007 (10^9 + 7). | Input: ['6', '2', '00', '11', '3', '???', '???', '3', '??1', '0?0', '4', '??0?', '??11', '5', '?????', '0??1?', '10', '?01??01?1?', '??100?1???', ''] Output:['1', '16', '1', '14', '101', '1674', ''] | [
3
] |
Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game.The game works as follows: there is a tree of size n, rooted at node 1, where each node is initially white. Red and Blue get one turn each. Red goes first. In Red's turn, he can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree red. However, to make the game fair, Red is only allowed to color k nodes of the tree. In other words, after Red's turn, at most k of the nodes can be colored red.Then, it's Blue's turn. Blue can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree blue. However, he's not allowed to choose a subtree that contains a node already colored red, as that would make the node purple and no one likes purple crayon. Note: there's no restriction on the number of nodes Blue can color, as long as he doesn't color a node that Red has already colored.After the two turns, the score of the game is determined as follows: let w be the number of white nodes, r be the number of red nodes, and b be the number of blue nodes. The score of the game is w \cdot (r - b).Red wants to maximize this score, and Blue wants to minimize it. If both players play optimally, what will the final score of the game be? | Input: ['4 2', '1 2', '1 3', '1 4', ''] Output:['1', ''] | [
2,
3
] |
There are n candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string s, where the i-th candle is lit if and only if s_i=1. Initially, the candle lights are described by a string a. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit).You would like to make the candles look the same as string b. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. | Input: ['5', '5', '11010', '11010', '2', '01', '11', '3', '000', '101', '9', '100010111', '101101100', '9', '001011011', '011010101', ''] Output:['0', '1', '-1', '3', '4', ''] | [
0,
2,
3
] |
You are given an array consisting of all integers from [l, r] inclusive. For example, if l = 2 and r = 5, the array would be [2, 3, 4, 5]. What's the minimum number of elements you can delete to make the bitwise AND of the array non-zero?A bitwise AND is a binary operation that takes two equal-length binary representations and performs the AND operation on each pair of the corresponding bits. | Input: ['5', '1 2', '2 8', '4 5', '1 5', '100000 200000', ''] Output:['1', '3', '0', '2', '31072', ''] | [
2,
3
] |
There are n block towers in a row, where tower i has a height of a_i. You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation: Choose two indices i and j (1 <=q i, j <=q n; i \neq j), and move a block from tower i to tower j. This essentially decreases a_i by 1 and increases a_j by 1. You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as \max(a)-\min(a). What's the minimum possible ugliness you can achieve, after any number of days? | Input: ['3', '3', '10 10 10', '4', '3 2 1 2', '5', '1 2 3 1 5', ''] Output:['0', '0', '1', ''] | [
2,
3
] |
Divan's new cottage is finally complete! However, after a thorough inspection, it turned out that the workers had installed the insulation incorrectly, and now the temperature in the house directly depends on the temperature outside. More precisely, if the temperature in the house is P in the morning, and the street temperature is T, then by the next morning the temperature in the house changes according to the following rule: P_{new} = P + 1, if P < T, P_{new} = P - 1, if P > T, P_{new} = P, if P = T. Here P_{new} is the temperature in the house next morning.Divan is a very busy businessman, so sometimes he is not at home for long periods and does not know what the temperature is there now, so he hired you to find it. You will work for n days. In the beginning of the i-th day, the temperature outside T_i is first given to you. After that, on the i-th day, you will receive k_i queries. Each query asks the following: "if the temperature in the house was x_i at the morning of the first day, what would be the temperature in the house next morning (after day i)?"Please answer all the businessman's queries. | Input: ['3', '50', '3', '1 2 3', '50', '3', '4 5 6', '0', '3', '7 8 9', ''] Output:['2', '5', '9', '15', '22', '30', '38', '47', '53', ''] | [
4
] |
Once Divan analyzed a sequence a_1, a_2, ..., a_n consisting of n non-negative integers as follows. He considered each non-empty subsequence of the sequence a, computed the bitwise XOR of its elements and added up all the XORs, obtaining the coziness of the sequence a.A sequence c is a subsequence of a sequence d if c can be obtained from d by deletion of several (possibly, zero or all) elements. For example, [1, \, 2, \, 3, \, 4], [2, \, 4], and [2] are subsequences of [1, \, 2, \, 3, \, 4], but [4, \, 3] and [0] are not.Divan was very proud of his analysis, but now he lost the sequence a, and also the coziness value! However, Divan remembers the value of bitwise OR on m contiguous subsegments of the sequence a. It turns out that each element of the original sequence is contained in at least one of these m segments.Divan asks you to help find the coziness of the sequence a using the information he remembers. If several coziness values are possible, print any.As the result can be very large, print the value modulo 10^9 + 7. | Input: ['3', '2 1', '1 2 2', '3 2', '1 3 5', '2 3 5', '5 4', '1 2 7', '3 3 7', '4 4 0', '4 5 2', ''] Output:['4', '20', '112', ''] | [
3
] |
Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well.The store he came to has n different chocolate bars, and the price of the i-th chocolate bar is a_i dollars. Divan considers a chocolate bar too expensive if it costs strictly more than r dollars. Similarly, he considers a bar of chocolate to be too cheap if it costs strictly less than l dollars. Divan will not buy too cheap or too expensive bars.Divan is not going to spend all his money on chocolate bars, so he will spend at most k dollars on chocolates.Please determine the maximum number of chocolate bars Divan can buy. | Input: ['8', '3 1 100 100', '50 100 50', '6 3 5 10', '1 2 3 4 5 6', '6 3 5 21', '1 2 3 4 5 6', '10 50 69 100', '20 30 40 77 1 1 12 4 70 10000', '3 50 80 30', '20 60 70', '10 2 7 100', '2 2 2 2 2 7 7 7 7 7', '4 1000000000 1000000000 1000000000', '1000000000 1000000000 1000000000 1000000000', '1 1 1 1', '1', ''] Output:['2', '2', '3', '0', '0', '10', '1', '1', ''] | [
0,
2
] |
Let's call a sequence of integers x_1, x_2, ..., x_k MEX-correct if for all i (1 <= i <= k) |x_i - \operatorname{MEX}(x_1, x_2, ..., x_i)| <= 1 holds. Where \operatorname{MEX}(x_1, ..., x_k) is the minimum non-negative integer that doesn't belong to the set x_1, ..., x_k. For example, \operatorname{MEX}(1, 0, 1, 3) = 2 and \operatorname{MEX}(2, 1, 5) = 0.You are given an array a consisting of n non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo 998244353. Note: a subsequence of an array a is a sequence [a_{i_1}, a_{i_2}, ..., a_{i_m}] meeting the constraints 1 <= i_1 < i_2 < ... < i_m <= n. If two different ways to choose the sequence of indices [i_1, i_2, ..., i_m] yield the same subsequence, the resulting subsequence should be counted twice (i. e. two subsequences are different if their sequences of indices [i_1, i_2, ..., i_m] are not the same). | Input: ['4', '3', '0 2 1', '2', '1 0', '5', '0 0 0 0 0', '4', '0 1 2 3', ''] Output:['4', '2', '31', '7', ''] | [
3
] |
Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts 100^{500} seconds, during which Monocarp attacks the dragon with a poisoned dagger. The i-th attack is performed at the beginning of the a_i-th second from the battle start. The dagger itself does not deal damage, but it applies a poison effect on the dragon, which deals 1 damage during each of the next k seconds (starting with the same second when the dragon was stabbed by the dagger). However, if the dragon has already been poisoned, then the dagger updates the poison effect (i.e. cancels the current poison effect and applies a new one).For example, suppose k = 4, and Monocarp stabs the dragon during the seconds 2, 4 and 10. Then the poison effect is applied at the start of the 2-nd second and deals 1 damage during the 2-nd and 3-rd seconds; then, at the beginning of the 4-th second, the poison effect is reapplied, so it deals exactly 1 damage during the seconds 4, 5, 6 and 7; then, during the 10-th second, the poison effect is applied again, and it deals 1 damage during the seconds 10, 11, 12 and 13. In total, the dragon receives 10 damage.Monocarp knows that the dragon has h hit points, and if he deals at least h damage to the dragon during the battle — he slays the dragon. Monocarp has not decided on the strength of the poison he will use during the battle, so he wants to find the minimum possible value of k (the number of seconds the poison effect lasts) that is enough to deal at least h damage to the dragon. | Input: ['4', '2 5', '1 5', '3 10', '2 4 10', '5 3', '1 2 4 5 7', '4 1000', '3 25 64 1337', ''] Output:['3', '4', '1', '470', ''] | [
4
] |
You are given a sequence a_1, a_2, ..., a_n consisting of n pairwise distinct positive integers.Find <=ft\lfloor \frac n 2 \right\rfloor different pairs of integers x and y such that: x \neq y; x and y appear in a; x~mod~y doesn't appear in a. Note that some x or y can belong to multiple pairs.\lfloor x \rfloor denotes the floor function — the largest integer less than or equal to x. x~mod~y denotes the remainder from dividing x by y.If there are multiple solutions, print any of them. It can be shown that at least one solution always exists. | Input: ['4', '2', '1 4', '4', '2 8 3 4', '5', '3 8 5 9 7', '6', '2 7 5 3 4 8', ''] Output:['4 1', '8 2', '8 4', '9 5', '7 5', '8 7', '4 3', '5 2'] | [
2
] |
Monocarp wrote down two numbers on a whiteboard. Both numbers follow a specific format: a positive integer x with p zeros appended to its end.Now Monocarp asks you to compare these two numbers. Can you help him? | Input: ['5', '2 1', '19 0', '10 2', '100 1', '1999 0', '2 3', '1 0', '1 0', '99 0', '1 2', ''] Output:['>', '=', '<', '=', '<', ''] | [
3
] |
You are given an array c = [c_1, c_2, ..., c_m]. An array a = [a_1, a_2, ..., a_n] is constructed in such a way that it consists of integers 1, 2, ..., m, and for each i \in [1,m], there are exactly c_i occurrences of integer i in a. So, the number of elements in a is exactly \sum\limits_{i=1}^{m} c_i.Let's define for such array a the value f(a) as f(a) = \sum_{\substack{1 <= i < j <= n\\ a_i = a_j}}{j - i}.In other words, f(a) is the total sum of distances between all pairs of equal elements.Your task is to calculate the maximum possible value of f(a) and the number of arrays yielding the maximum possible value of f(a). Two arrays are considered different, if elements at some position differ. | Input: ['6', '1 1 1 1 1 1', ''] Output:['0 720', ''] | [
2
] |
Monocarp plays a computer game. There are n different sets of armor and m different weapons in this game. If a character equips the i-th set of armor and wields the j-th weapon, their power is usually equal to i + j; but some combinations of armor and weapons synergize well. Formally, there is a list of q ordered pairs, and if the pair (i, j) belongs to this list, the power of the character equipped with the i-th set of armor and wielding the j-th weapon is not i + j, but i + j + 1.Initially, Monocarp's character has got only the 1-st armor set and the 1-st weapon. Monocarp can obtain a new weapon or a new set of armor in one hour. If he wants to obtain the k-th armor set or the k-th weapon, he must possess a combination of an armor set and a weapon that gets his power to k or greater. Of course, after Monocarp obtains a weapon or an armor set, he can use it to obtain new armor sets or weapons, but he can go with any of the older armor sets and/or weapons as well.Monocarp wants to obtain the n-th armor set and the m-th weapon. What is the minimum number of hours he has to spend on it? | Input: ['3 4', '0', ''] Output:['3', ''] | [
0,
2
] |
Monocarp is a tutor of a group of n students. He communicates with them using a conference in a popular messenger.Today was a busy day for Monocarp — he was asked to forward a lot of posts and announcements to his group, that's why he had to write a very large number of messages in the conference. Monocarp knows the students in the group he is tutoring quite well, so he understands which message should each student read: Monocarp wants the student i to read the message m_i.Of course, no one's going to read all the messages in the conference. That's why Monocarp decided to pin some of them. Monocarp can pin any number of messages, and if he wants anyone to read some message, he should pin it — otherwise it will definitely be skipped by everyone.Unfortunately, even if a message is pinned, some students may skip it anyway. For each student i, Monocarp knows that they will read at most k_i messages. Suppose Monocarp pins t messages; if t <= k_i, then the i-th student will read all the pinned messages; but if t > k_i, the i-th student will choose exactly k_i random pinned messages (all possible subsets of pinned messages of size k_i are equiprobable) and read only the chosen messages.Monocarp wants to maximize the expected number of students that read their respective messages (i.e. the number of such indices i that student i reads the message m_i). Help him to choose how many (and which) messages should he pin! | Input: ['3', '10 1', '10 2', '5 2', ''] Output:['2', '5 10 ', ''] | [
0,
2
] |
You are given a pair of integers (a, b) and an integer x.You can change the pair in two different ways: set (assign) a := |a - b|; set (assign) b := |a - b|, where |a - b| is the absolute difference between a and b.The pair (a, b) is called x-magic if x is obtainable either as a or as b using only the given operations (i.e. the pair (a, b) is x-magic if a = x or b = x after some number of operations applied). You can apply the operations any number of times (even zero).Your task is to find out if the pair (a, b) is x-magic or not.You have to answer t independent test cases. | Input: ['8', '6 9 3', '15 38 7', '18 8 8', '30 30 30', '40 50 90', '24 28 20', '365 216 52', '537037812705867558 338887693834423551 3199921013340', ''] Output:['YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', ''] | [
3
] |
You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size k. It consists of 2k-1 messages. The first message consists of one emote, the second one — of two emotes, ..., the k-th one — of k emotes, the k+1-th one — of k-1 emotes, ..., and the last one — of one emote.For example, the emote triangle for k=3 consists of 5 messages: Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least x emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all 2k-1 messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer t independent test cases. | Input: ['7', '4 6', '4 7', '1 2', '3 7', '2 5', '100 1', '1000000000 923456789987654321', ''] Output:['3', '4', '1', '4', '3', '1', '1608737403', ''] | [
3,
4
] |
A permutation of length n is an array p=[p_1,p_2,..., p_n] which contains every integer from 1 to n (inclusive) exactly once. For example, p=[4, 2, 6, 5, 3, 1] is a permutation of length 6.You are given three integers n, a and b, where n is an even number. Print any permutation of length n that the minimum among all its elements of the left half equals a and the maximum among all its elements of the right half equals b. Print -1 if no such permutation exists. | Input: ['7', '6 2 5', '6 1 3', '6 4 3', '4 2 4', '10 5 3', '2 1 2', '2 2 1', ''] Output:['4 2 6 5 3 1', '-1', '6 4 5 1 3 2 ', '3 2 4 1 ', '-1', '1 2 ', '2 1 ', ''] | [
2
] |
Let's denote the Manhattan distance between two points p_1 (with coordinates (x_1, y_1)) and p_2 (with coordinates (x_2, y_2)) as d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|. For example, the distance between two points with coordinates (1, 3) and (4, 2) is |1 - 4| + |3 - 2| = 4.You are given two points, A and B. The point A has coordinates (0, 0), the point B has coordinates (x, y).Your goal is to find a point C such that: both coordinates of C are non-negative integers; d(A, C) = \dfrac{d(A, B)}{2} (without any rounding); d(B, C) = \dfrac{d(A, B)}{2} (without any rounding). Find any point C that meets these constraints, or report that no such point exists. | Input: ['10', '49 3', '2 50', '13 0', '0 41', '42 0', '0 36', '13 37', '42 16', '42 13', '0 0', ''] Output:['23 3', '1 25', '-1 -1', '-1 -1', '21 0', '0 18', '13 12', '25 4', '-1 -1', '0 0', ''] | [
0
] |
Polycarp has a rectangular field of n * m cells (the size of the n \cdot m field does not exceed 10^6 cells, m >= 2), in each cell of which there can be candy. There are n rows and m columns in the field.Let's denote a cell with coordinates x vertically and y horizontally by (x, y). Then the top-left cell will be denoted as (1, 1), and the bottom-right cell will be denoted as (n, m).If there is candy in the cell, then the cell is marked with the symbol '1', otherwise — with the symbol '0'.Polycarp made a Robot that can collect candy. The Robot can move from (x, y) either to (x+1, y+1), or to (x+1, y-1). If the Robot is in a cell that contains candy, it takes it.While there is at least one candy on the field, the following procedure is executed: Polycarp puts the Robot in an arbitrary cell on the topmost row of the field. He himself chooses in which cell to place the Robot. It is allowed to put the Robot in the same cell multiple times. The Robot moves across the field and collects candies. He controls the Robot. When the Robot leaves the field, Polycarp takes it. If there are still candies left, Polycarp repeats the procedure. Find the minimum number of times Polycarp needs to put the Robot on the topmost row of the field in order to collect all the candies. It is guaranteed that Polycarp can always collect all the candies. | Input: ['4', '', '2 2', '00', '00', '', '3 3', '100', '000', '101', '', '4 5', '01000', '00001', '00010', '10000', '', '3 3', '111', '111', '111', ''] Output:['0', '2', '2', '4', ''] | [
2
] |
Polycarp started working at a bank. He was assigned to monitor the ATM. The ATM initially contains s rubles.A queue of n students lined up to him. Each student wants to either withdraw a certain amount of money or deposit it into an account. If a_i is positive, then the student credits that amount of money via ATM. Otherwise, the student withdraws |a_i| rubles.In the beginning, the ATM is turned off and an arbitrary number of students are not served. At some point, Polycarp turns on the ATM, which has an initial amount of s rubles. Then, the remaining students start queueing at the ATM. If at some point in time there is less money in the ATM than the student wants to withdraw, then the student is not served and Polycarp turns off the ATM and does not turn it on anymore.More formally, the students that are served are forming a contiguous subsequence.Polycarp wants the ATM to serve the maximum number of students. Help him in this matter. Print the numbers of the first and last student, or determine that he will not be able to serve anyone.In other words, find such a longest continuous segment of students that, starting with the sum of s at the ATM, all these students will be served. ATM serves students consistently (i.e. one after another in the order of the queue). | Input: ['3', '4 10', '-16 2 -6 8', '3 1000', '-100000 -100000 -100000', '6 0', '2 6 -164 1 -1 -6543', ''] Output:['2 4', '-1', '1 2', ''] | [
4
] |
The only difference with E1 is the question of the problem.Vlad built a maze out of n rooms and n-1 bidirectional corridors. From any room u any other room v can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited k friends to play a game with them.Vlad starts the game in the room 1 and wins if he reaches a room other than 1, into which exactly one corridor leads. Friends are placed in the maze: the friend with number i is in the room x_i, and no two friends are in the same room (that is, x_i \neq x_j for all i \neq j). Friends win if one of them meets Vlad in any room or corridor before he wins.For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad.In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist. | Input: ['4', '', '8 2', '5 3', '4 7', '2 5', '1 6', '3 6', '7 2', '1 7', '6 8', '', '8 4', '6 5 7 3', '4 7', '2 5', '1 6', '3 6', '7 2', '1 7', '6 8', '', '3 1', '2', '1 2', '2 3', '', '3 2', '2 3', '3 1', '1 2', ''] Output:['-1', '2', '1', '2', ''] | [
2
] |
The only difference with E2 is the question of the problem..Vlad built a maze out of n rooms and n-1 bidirectional corridors. From any room u any other room v can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited k friends to play a game with them.Vlad starts the game in the room 1 and wins if he reaches a room other than 1, into which exactly one corridor leads.Friends are placed in the maze: the friend with number i is in the room x_i, and no two friends are in the same room (that is, x_i \neq x_j for all i \neq j). Friends win if one of them meets Vlad in any room or corridor before he wins.For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time. Friends know the plan of a maze and intend to win. Vlad is a bit afraid of their ardor. Determine if he can guarantee victory (i.e. can he win in any way friends play).In other words, determine if there is such a sequence of Vlad's moves that lets Vlad win in any way friends play. | Input: ['4', '', '8 2', '5 3', '4 7', '2 5', '1 6', '3 6', '7 2', '1 7', '6 8', '', '3 1', '2', '1 2', '2 3', '', '3 1', '2', '1 2', '1 3', '', '3 2', '2 3', '3 1', '1 2', ''] Output:['YES', 'NO', 'YES', 'NO', ''] | [
2
] |
The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.There are a programmers and b mathematicians at Berland State University. How many maximum teams can be made if: each team must consist of exactly 4 students, teams of 4 mathematicians or 4 programmers are unlikely to perform well, so the decision was made not to compose such teams. Thus, each team must have at least one programmer and at least one mathematician.Print the required maximum number of teams. Each person can be a member of no more than one team. | Input: ['6', '5 5', '10 1', '2 3', '0 0', '17 2', '1000000000 1000000000', ''] Output:['2', '1', '1', '0', '2', '500000000', ''] | [
3,
4
] |
Polycarp has an integer n that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length l (in other words, l leftmost digits) of n. So, the leftmost digit is swapped with the l-th digit from the left, the second digit from the left swapped with (l-1)-th left, etc. For example, if n=123456789 and l=5, then the new value of n will be 543216789.Note that for different operations, the values of l can be different. The number l can be equal to the length of the number n — in this case, the whole number n is reversed.Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.Help Polycarp. Determine the minimum number of operations he needs to perform with the number n to make it even or determine that this is impossible.You need to answer t independent test cases. | Input: ['4', '3876', '387', '4489', '3', ''] Output:['0', '2', '1', '-1', ''] | [
3
] |
After watching the new over-rated series Squid Game, Mashtali and Soroush decided to hold their own Squid Games! Soroush agreed to be the host and will provide money for the winner's prize, and Mashtali became the Front Man!m players registered to play in the games to win the great prize, but when Mashtali found out how huge the winner's prize is going to be, he decided to kill eliminate all the players so he could take the money for himself!Here is how evil Mashtali is going to eliminate players:There is an unrooted tree with n vertices. Every player has 2 special vertices x_i and y_i.In one operation, Mashtali can choose any vertex v of the tree. Then, for each remaining player i he finds a vertex w on the simple path from x_i to y_i, which is the closest to v. If w\ne x_i and w\ne y_i, player i will be eliminated.Now Mashtali wondered: "What is the minimum number of operations I should perform so that I can remove every player from the game and take the money for myself?"Since he was only thinking about the money, he couldn't solve the problem by himself and asked for your help! | Input: ['6 3', '1 1 1 4 4', '1 5', '3 4', '2 6', ''] Output:['2', ''] | [
2
] |
Right before the UEFA Euro 2020, AmShZ and Safar placed bets on who'd be the champion, AmShZ betting on Italy, and Safar betting on France.Of course, AmShZ won. Hence, Safar gave him a bracket sequence S. Note that a bracket sequence is a string made of '(' and ')' characters.AmShZ can perform the following operation any number of times: First, he cuts his string S into three (possibly empty) contiguous substrings A, B and C. Then, he glues them back by using a '(' and a ')' characters, resulting in a new string S = A + "(" + B + ")" + C.For example, if S = "))((" and AmShZ cuts it into A = "", B = "))", and C = "((", He will obtain S = "()))((" as a new string. After performing some (possibly none) operations, AmShZ gives his string to Keshi and asks him to find the initial string. Of course, Keshi might be able to come up with more than one possible initial string. Keshi is interested in finding the lexicographically smallest possible initial string.Your task is to help Keshi in achieving his goal.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: [')(()(())))', ''] Output:[')((())))', ''] | [
2
] |
Let's call an array of k integers c_1, c_2, ..., c_k terrible, if the following condition holds:Let AVG be the \frac{c_1 + c_2 + ... + c_k}{k}(the average of all the elements of the array, it doesn't have to be integer). Then the number of elements of the array which are bigger than AVG should be strictly larger than the number of elements of the array which are smaller than AVG. Note that elements equal to AVG don't count.For example c = \{1, 4, 4, 5, 6\} is terrible because AVG = 4.0 and 5-th and 4-th elements are greater than AVG and 1-st element is smaller than AVG.Let's call an array of m integers b_1, b_2, ..., b_m bad, if at least one of its non-empty subsequences is terrible, and good otherwise.You are given an array of n integers a_1, a_2, ..., a_n. Find the minimum number of elements that you have to delete from it to obtain a good array.An array is a subsequence of another array if it can be obtained from it by deletion of several (possibly, zero or all) elements. | Input: ['4', '3', '1 2 3', '5', '1 4 4 5 6', '6', '7 8 197860736 212611869 360417095 837913434', '8', '6 10 56026534 405137099 550504063 784959015 802926648 967281024', ''] Output:['0', '1', '2', '3', ''] | [
0,
2,
3,
4
] |
Lee couldn't sleep lately, because he had nightmares. In one of his nightmares (which was about an unbalanced global round), he decided to fight back and propose a problem below (which you should solve) to balance the round, hopefully setting him free from the nightmares.A non-empty array b_1, b_2, ..., b_m is called good, if there exist m integer sequences which satisfy the following properties: The i-th sequence consists of b_i consecutive integers (for example if b_i = 3 then the i-th sequence can be (-1, 0, 1) or (-5, -4, -3) but not (0, -1, 1) or (1, 2, 3, 4)). Assuming the sum of integers in the i-th sequence is sum_i, we want sum_1 + sum_2 + ... + sum_m to be equal to 0. You are given an array a_1, a_2, ..., a_n. It has 2^n - 1 nonempty subsequences. Find how many of them are good.As this number can be very large, output it modulo 10^9 + 7.An array c is a subsequence of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements. | Input: ['4', '2 2 4 7', ''] Output:['10', ''] | [
3
] |
Keshi is throwing a party and he wants everybody in the party to be happy.He has n friends. His i-th friend has i dollars. If you invite the i-th friend to the party, he will be happy only if at most a_i people in the party are strictly richer than him and at most b_i people are strictly poorer than him.Keshi wants to invite as many people as possible. Find the maximum number of people he can invite to the party so that every invited person would be happy. | Input: ['3', '3', '1 2', '2 1', '1 1', '2', '0 0', '0 1', '2', '1 0', '0 1', ''] Output:['2', '1', '2', ''] | [
2,
4
] |
An array [b_1, b_2, ..., b_m] is a palindrome, if b_i = b_{m+1-i} for each i from 1 to m. Empty array is also a palindrome.An array is called kalindrome, if the following condition holds: It's possible to select some integer x and delete some of the elements of the array equal to x, so that the remaining array (after gluing together the remaining parts) is a palindrome. Note that you don't have to delete all elements equal to x, and you don't have to delete at least one element equal to x.For example : [1, 2, 1] is kalindrome because you can simply not delete a single element. [3, 1, 2, 3, 1] is kalindrome because you can choose x = 3 and delete both elements equal to 3, obtaining array [1, 2, 1], which is a palindrome. [1, 2, 3] is not kalindrome. You are given an array [a_1, a_2, ..., a_n]. Determine if a is kalindrome or not. | Input: ['4', '1', '1', '2', '1 2', '3', '1 2 3', '5', '1 4 4 1 4', ''] Output:['YES', 'YES', 'NO', 'YES', ''] | [
2
] |
You are playing a game on a n * m grid, in which the computer has selected some cell (x, y) of the grid, and you have to determine which one. To do so, you will choose some k and some k cells (x_1, y_1),\, (x_2, y_2), ..., (x_k, y_k), and give them to the computer. In response, you will get k numbers b_1,\, b_2, ... b_k, where b_i is the manhattan distance from (x_i, y_i) to the hidden cell (x, y) (so you know which distance corresponds to which of k input cells). After receiving these b_1,\, b_2, ..., b_k, you have to be able to determine the hidden cell. What is the smallest k for which is it possible to always guess the hidden cell correctly, no matter what cell computer chooses?As a reminder, the manhattan distance between cells (a_1, b_1) and (a_2, b_2) is equal to |a_1-a_2|+|b_1-b_2|. | Input: ['2', '2 3', '3 1', ''] Output:['2', '1', ''] | [
3
] |
William has two arrays of numbers a_1, a_2, ..., a_n and b_1, b_2, ..., b_m. The arrays satisfy the conditions of being convex. Formally an array c of length k is considered convex if c_i - c_{i - 1} < c_{i + 1} - c_i for all i from 2 to k - 1 and c_1 < c_2.Throughout William's life he observed q changes of two types happening to the arrays: Add the arithmetic progression d, d \cdot 2, d \cdot 3, ..., d \cdot k to the suffix of the array a of length k. The array after the change looks like this: [a_1, a_2, ..., a_{n - k}, a_{n - k + 1} + d, a_{n - k + 2} + d \cdot 2, ..., a_n + d \cdot k]. The same operation, but for array b. After each change a matrix d is created from arrays a and b, of size n * m, where d_{i, j}=a_i + b_j. William wants to get from cell (1, 1) to cell (n, m) of this matrix. From cell (x, y) he can only move to cells (x + 1, y) and (x, y + 1). The length of a path is calculated as the sum of numbers in cells visited by William, including the first and the last cells.After each change William wants you to help find out the minimal length of the path he could take. | Input: ['5 3 4', '1 2 4 7 11', '5 7 10', '1 3 2', '2 2 5', '1 5 4', '2 1 7', ''] Output:['98', '128', '219', '229', ''] | [
2,
3
] |
William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies.The conference has n participants, who are initially unfamiliar with each other. William can introduce any two people, a and b, who were not familiar before, to each other. William has d conditions, i'th of which requires person x_i to have a connection to person y_i. Formally, two people x and y have a connection if there is such a chain p_1=x, p_2, p_3, ..., p_k=y for which for all i from 1 to k - 1 it's true that two people with numbers p_i and p_{i + 1} know each other.For every i (1 <= i <= d) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from 1 and up to and including i and performed exactly i introductions. The conditions are being checked after William performed i introductions. The answer for each i must be calculated independently. It means that when you compute an answer for i, you should assume that no two people have been introduced to each other yet. | Input: ['7 6', '1 2', '3 4', '2 4', '7 6', '6 5', '1 7', ''] Output:['1', '1', '3', '3', '3', '6', ''] | [
2
] |
While performing complex market analysis William encountered the following problem:For a given array a of size n and a natural number e, calculate the number of pairs of natural numbers (i, k) which satisfy the following conditions: 1 <= i, k i + e \cdot k <= n. Product a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot ... \cdot a_{i + k \cdot e} is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | Input: ['6', '7 3', '10 2 1 3 1 19 3', '3 2', '1 13 1', '9 3', '2 4 2 1 1 1 1 4 2', '3 1', '1 1 1', '4 1', '1 2 1 1', '2 2', '1 2', ''] Output:['2', '0', '4', '0', '5', '0', ''] | [
4
] |
William has array of n numbers a_1, a_2, ..., a_n. He can perform the following sequence of operations any number of times: Pick any two items from array a_i and a_j, where a_i must be a multiple of 2 a_i = \frac{a_i}{2} a_j = a_j \cdot 2 Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. | Input: ['5', '3', '6 4 2', '5', '1 2 3 4 5', '1', '10', '3', '2 3 4', '15', '8 8 8 8 8 8 8 8 8 8 8 8 8 8 8', ''] Output:['50', '46', '10', '26', '35184372088846', ''] | [
2,
3
] |
You are given m strings and a tree on n nodes. Each edge has some letter written on it.You have to answer q queries. Each query is described by 4 integers u, v, l and r. The answer to the query is the total number of occurrences of str(u,v) in strings with indices from l to r. str(u,v) is defined as the string that is made by concatenating letters written on the edges on the shortest path from u to v (in order that they are traversed). | Input: ['2 5 3', '1 2 a', 'aab', 'abab', 'aaa', 'b', 'a', '2 1 1 5', '1 2 1 3', '2 1 3 5', ''] Output:['8', '7', '4', ''] | [
4
] |
On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly \frac{n}{3} marked cells of each of three colors! Find the largest such k that it's possible to choose \frac{k}{3} cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold: No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be 0. The i-th rectangle contains all the chosen cells of the i-th color and no chosen cells of other colors, for i = 1, 2, 3. | Input: ['9', '2 3 1', '4 1 2', '2 1 3', '3 4 1', '5 3 2', '4 4 3', '2 4 1', '5 2 2', '3 5 3', ''] Output:['6', ''] | [
4
] |
You are given n dominoes. Each domino has a left and a right cell. Each cell can be colored either black or white. Some cells are already colored, while some aren't yet.The coloring is said to be valid if and only if it is possible to rearrange the dominoes in some order such that for each 1 <= i <= n the color of the right cell of the i-th domino is different from the color of the left cell of the ((i \bmod n)+1)-st domino. Note that you can't rotate the dominoes, so the left cell always remains the left cell, and the right cell always remains the right cell.Count the number of valid ways to color the yet uncolored cells of dominoes. Two ways are considered different if there is a cell that is colored white in one way and black in the other. In particular, colorings BW WB and WB BW different (and both invalid).As this number can be very big, output it modulo 998\,244\,353. | Input: ['1', '?W', ''] Output:['1', ''] | [
3
] |
n players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of n-1 battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. | Input: ['3', '4', '1 2 3 4', '1 2 3 4', '4', '11 12 20 21', '44 22 11 30', '1', '1000000000', '1000000000', ''] Output:['0001', '1111', '1', ''] | [
2
] |
You are given three integers n, a, b. Determine if there exists a permutation p_1, p_2, ..., p_n of integers from 1 to n, such that:There are exactly a integers i with 2 <= i <= n-1 such that p_{i-1} < p_i > p_{i+1} (in other words, there are exactly a local maximums).There are exactly b integers i with 2 <= i <= n-1 such that p_{i-1} > p_i < p_{i+1} (in other words, there are exactly b local minimums).If such permutations exist, find any such permutation. | Input: ['3', '4 1 1', '6 1 2', '6 4 0', ''] Output:['1 3 2 4', '4 2 3 1 5 6', '-1', ''] | [
2
] |
Given n, find any array a_1, a_2, ..., a_n of integers such that all of the following conditions hold: 1 <= a_i <= 10^9 for every i from 1 to n.a_1 < a_2 < ... <a_nFor every i from 2 to n, a_i isn't divisible by a_{i-1}It can be shown that such an array always exists under the constraints of the problem. | Input: ['3', '1', '2', '7', ''] Output:['1', '2 3', '111 1111 11111 111111 1111111 11111111 111111111'] | [
3
] |
The chef has cooked n dishes yet again: the i-th dish consists of a_i grams of fish and b_i grams of meat. Banquet organizers consider two dishes i and j equal if a_i=a_j and b_i=b_j at the same time.The banquet organizers estimate the variety of n dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly m_i grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly m_i grams of the i-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | Input: ['5', '', '3', '10 10 2', '9 9 0', '10 9 1', '', '2', '3 4 1', '5 1 2', '', '3', '7 2 5', '6 5 4', '5 5 6', '', '1', '13 42 50', '', '5', '5 7 12', '3 1 4', '7 3 7', '0 0 0', '4 1 5', ''] Output:['1', '1 1', '0 0', '1 0', '2', '0 1', '1 1', '2', '3 2', '0 4', '1 5', '1', '8 42', '2', '5 7', '3 1', '4 3', '0 0', '4 1', ''] | [
2
] |
A known chef has prepared n dishes: the i-th dish consists of a_i grams of fish and b_i grams of meat. The banquet organizers estimate the balance of n dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to <=ft|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly m grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly m grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | Input: ['8', '', '1 5', '3 4', '', '1 6', '3 4', '', '2 2', '1 3', '4 2', '', '2 4', '1 3', '1 7', '', '3 6', '1 7', '1 8', '1 9', '', '3 6', '1 8', '1 9', '30 10', '', '3 4', '3 1', '3 2', '4 1', '', '5 4', '0 7', '6 4', '0 8', '4 1', '5 3', ''] Output:['0', '2 3', '1', '3 3', '0', '1 1', '1 1', '2', '1 3', '0 4', '3', '0 6', '0 6', '0 6', '7', '1 5', '1 5', '6 0', '0', '3 1', '3 1', '3 1', '0', '0 4', '2 2', '0 4', '3 1', '1 3', ''] | [
2
] |
The robot is located on a checkered rectangular board of size n * m (n rows, m columns). The rows in the board are numbered from 1 to n from top to bottom, and the columns — from 1 to m from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | Input: ['7', '', '1 1', 'R', '', '1 3', 'RRL', '', '2 2', 'DL', 'RU', '', '2 2', 'UD', 'RU', '', '3 2', 'DL', 'UL', 'RU', '', '4 4', 'RRRD', 'RUUD', 'URUD', 'ULLR', '', '4 4', 'DDLU', 'RDDU', 'UUUU', 'RDLD', ''] Output:['1 1 1', '1 1 3', '1 1 4', '2 1 3', '3 1 5', '4 3 12', '1 1 4', ''] | [
0
] |
You are given an array of integers a of length n. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by 1; or you can select any red element and increase its value by 1. Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make 0 or more steps such that the resulting array is a permutation of numbers from 1 to n?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array a contains in some order all numbers from 1 to n (inclusive), each exactly once. | Input: ['8', '4', '1 2 5 2', 'BRBR', '2', '1 1', 'BB', '5', '3 1 4 2 5', 'RBRRB', '5', '3 1 3 1 3', 'RBRRB', '5', '5 1 5 1 5', 'RBRRB', '4', '2 2 2 2', 'BRBR', '2', '1 -2', 'BR', '4', '-2 -1 4 0', 'RRRR', ''] Output:['YES', 'NO', 'YES', 'YES', 'NO', 'YES', 'YES', 'YES', ''] | [
2,
3
] |
Yelisey has an array a of n integers.If a has length strictly greater than 1, then Yelisei can apply an operation called minimum extraction to it: First, Yelisei finds the minimal number m in the array. If there are several identical minima, Yelisey can choose any of them. Then the selected minimal element is removed from the array. After that, m is subtracted from each remaining element. Thus, after each operation, the length of the array is reduced by 1.For example, if a = [1, 6, -4, -2, -4], then the minimum element in it is a_3 = -4, which means that after this operation the array will be equal to a=[1 {- (-4)}, 6 {- (-4)}, -2 {- (-4)}, -4 {- (-4)}] = [5, 10, 2, 0].Since Yelisey likes big numbers, he wants the numbers in the array a to be as big as possible.Formally speaking, he wants to make the minimum of the numbers in array a to be maximal possible (i.e. he want to maximize a minimum). To do this, Yelisey can apply the minimum extraction operation to the array as many times as he wants (possibly, zero). Note that the operation cannot be applied to an array of length 1.Help him find what maximal value can the minimal element of the array have after applying several (possibly, zero) minimum extraction operations to the array. | Input: ['8', '1', '10', '2', '0 0', '3', '-1 2 0', '4', '2 10 1 7', '2', '2 3', '5', '3 2 -4 -2 0', '2', '-1 1', '1', '-2', ''] Output:['10', '0', '2', '5', '2', '2', '2', '-2', ''] | [
0
] |
The grasshopper is located on the numeric axis at the point with coordinate x_0.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate x with a distance d to the left moves the grasshopper to a point with a coordinate x - d, while jumping to the right moves him to a point with a coordinate x + d.The grasshopper is very fond of positive integers, so for each integer i starting with 1 the following holds: exactly i minutes after the start he makes a jump with a distance of exactly i. So, in the first minutes he jumps by 1, then by 2, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after 18 consecutive jumps he arrives at the point with a coordinate 7, he will jump by a distance of 19 to the right, since 7 is an odd number, and will end up at a point 7 + 19 = 26. Since 26 is an even number, the next jump the grasshopper will make to the left by a distance of 20, and it will move him to the point 26 - 20 = 6.Find exactly which point the grasshopper will be at after exactly n jumps. | Input: ['9', '0 1', '0 2', '10 10', '10 99', '177 13', '10000000000 987654321', '-433494437 87178291199', '1 0', '-1 1', ''] Output:['-1', '1', '11', '110', '190', '9012345679', '-87611785637', '1', '0', ''] | [
3
] |
You are given a tree consisting of n vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 1.You have to process q queries. In each query, you are given a vertex of the tree v and an integer k.To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex v. When a vertex is deleted, its children become the children of its parent. You have to process a query in such a way that maximizes the value of c(v) - m \cdot k (where c(v) is the resulting number of children of the vertex v, and m is the number of vertices you have deleted). Print the maximum possible value you can obtain.The queries are independent: the changes you make to the tree while processing a query don't affect the tree in other queries. | Input: ['8', '6 7', '3 2', '8 3', '5 7', '7 4', '7 1', '7 3', '6', '1 0', '1 2', '1 3', '7 1', '5 0', '7 200000', ''] Output:['5', '2', '1', '4', '0', '4', ''] | [
0
] |
There are n heroes fighting in the arena. Initially, the i-th hero has a_i health points.The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals 1 damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than 1 at the end of the round are considered killed.If exactly 1 hero remains alive after a certain round, then he is declared the winner. Otherwise, there is no winner.Your task is to calculate the number of ways to choose the initial health points for each hero a_i, where 1 <= a_i <= x, so that there is no winner of the fight. The number of ways can be very large, so print it modulo 998244353. Two ways are considered different if at least one hero has a different amount of health. For example, [1, 2, 1] and [2, 1, 1] are different. | Input: ['2 5', ''] Output:['5', ''] | [
3
] |
You are given a matrix, consisting of n rows and m columns. The j-th cell of the i-th row contains an integer a_{ij}.First, you have to color each row of the matrix either red or blue in such a way that at least one row is colored red and at least one row is colored blue.Then, you have to choose an integer k (1 <= k < m) and cut the colored matrix in such a way that the first k columns become a separate matrix (the left matrix) and the last m-k columns become a separate matrix (the right matrix).The coloring and the cut are called perfect if two properties hold: every red cell in the left matrix contains an integer greater than every blue cell in the left matrix; every blue cell in the right matrix contains an integer greater than every red cell in the right matrix. Find any perfect coloring and cut, or report that there are none. | Input: ['3', '5 5', '1 5 8 8 7', '5 2 1 4 3', '1 6 9 7 5', '9 3 3 3 2', '1 7 9 9 8', '3 3', '8 9 8', '1 5 3', '7 5 7', '2 6', '3 3 3 2 2 2', '1 1 1 4 4 4', ''] Output:['YES', 'BRBRB 1', 'NO', 'YES', 'RB 3', ''] | [
0
] |
In Berland, n different types of banknotes are used. Banknotes of the i-th type have denomination 10^{a_i} burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly 1.Let's denote f(s) as the minimum number of banknotes required to represent exactly s burles. For example, if the denominations of banknotes used in Berland are 1, 10 and 100, then f(59) = 14: 9 banknotes with denomination of 1 burle and 5 banknotes with denomination of 10 burles can be used to represent exactly 9 \cdot 1 + 5 \cdot 10 = 59 burles, and there's no way to do it with fewer banknotes.For a given integer k, find the minimum positive number of burles s that cannot be represented with k or fewer banknotes (that is, f(s) > k). | Input: ['4', '3 13', '0 1 2', '2 777', '0 4', '3 255', '0 1 3', '10 1000000000', '0 1 2 3 4 5 6 7 8 9', ''] Output:['59', '778', '148999', '999999920999999999', ''] | [
2
] |
Berland State University has received a new update for the operating system. Initially it is installed only on the 1-st computer.Update files should be copied to all n computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.Your task is to find the minimum number of hours required to copy the update files to all n computers if there are only k patch cables in Berland State University. | Input: ['4', '8 3', '6 6', '7 1', '1 1', ''] Output:['4', '3', '6', '0', ''] | [
2,
3
] |
Jeevan has two arrays a and b of size n. He is fond of performing weird operations on arrays. This time, he comes up with two types of operations: Choose any i (1 <= i <= n) and increment a_j by 1 for every j which is a multiple of i and 1 <= j <= n. Choose any i (1 <= i <= n) and decrement a_j by 1 for every j which is a multiple of i and 1 <= j <= n. He wants to convert array a into an array b using the minimum total number of operations. However, Jeevan seems to have forgotten the value of b_1. So he makes some guesses. He will ask you q questions corresponding to his q guesses, the i-th of which is of the form: If b_1 = x_i, what is the minimum number of operations required to convert a to b? Help him by answering each question. | Input: ['2', '3 7', '-1 5', '3', '1', '4', '3', ''] Output:['2', '4', '2', ''] | [
2,
3,
4
] |
Eikooc and Sushi play a game.The game is played on a tree having n nodes numbered 1 to n. Recall that a tree having n nodes is an undirected, connected graph with n-1 edges.They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node u such that u is adjacent to the node v the token is currently on u has not been visited before u \oplus v <=q min(u, v) Here x \oplus y denotes the bitwise XOR operation on integers x and y.Both the players play optimally. The player who is unable to make a move loses.The following are examples which demonstrate the rules of the game. Suppose Eikooc starts the game by placing the token at node 4. Sushi then moves the token to node 6, which is unvisited and adjacent to 4. It also holds that 6 \oplus 4 = 2 <=q min(6, 4). In the next turn, Eikooc moves the token to node 5, which is unvisited and adjacent to 6. It holds that 5 \oplus 6 = 3 <=q min(5, 6). Sushi has no more moves to play, so she loses. Suppose Eikooc starts the game by placing the token at node 3. Sushi moves the token to node 2, which is unvisited and adjacent to 3. It also holds that 3 \oplus 2 = 1 <=q min(3, 2). Eikooc cannot move the token to node 6 since 6 \oplus 2 = 4 \nleq min(6, 2). Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation p of length n (sequence of n integers wherein each integer from 1 to n occurs exactly once) where p_i denotes the new numbering of node i.She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so. | Input: ['3', '1', '2', '1 2', '3', '1 2', '1 3', ''] Output:['1', '2 1', '1 2 3', ''] | [
2
] |
Ashish has a string s of length n containing only characters 'a', 'b' and 'c'.He wants to find the length of the smallest substring, which satisfies the following conditions: Length of the substring is at least 2 'a' occurs strictly more times in this substring than 'b' 'a' occurs strictly more times in this substring than 'c' Ashish is busy planning his next Codeforces round. Help him solve the problem.A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | Input: ['3', '2', 'aa', '5', 'cbabb', '8', 'cacabccc', ''] Output:['2', '-1', '3', ''] | [
0,
2
] |
Ashish has a binary string s of length n that he wants to sort in non-decreasing order.He can perform the following operation: Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any k such that 1 <=q k <=q n and any sequence of k indices 1 <= i_1 \lt i_2 \lt ... \lt i_k <= n such that s_{i_1} >= s_{i_2} >= ... >= s_{i_k}. Reverse this subsequence in-place. Formally, swap s_{i_1} with s_{i_k}, swap s_{i_2} with s_{i_{k-1}}, ... and swap s_{i_{\lfloor k/2 \rfloor}} with s_{i_{\lceil k/2 \rceil + 1}} (Here \lfloor x \rfloor denotes the largest integer not exceeding x, and \lceil x \rceil denotes the smallest integer not less than x) Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most n operations. | Input: ['3', '7', '0011111', '5', '10100', '6', '001000', ''] Output:['0', '1', '4 1 3 4 5 ', '1', '3 3 5 6 ', ''] | [
2
] |
A number a_2 is said to be the arithmetic mean of two numbers a_1 and a_3, if the following condition holds: a_1 + a_3 = 2\cdot a_2. We define an arithmetic mean deviation of three numbers a_1, a_2 and a_3 as follows: d(a_1, a_2, a_3) = |a_1 + a_3 - 2 \cdot a_2|.Arithmetic means a lot to Jeevan. He has three numbers a_1, a_2 and a_3 and he wants to minimize the arithmetic mean deviation d(a_1, a_2, a_3). To do so, he can perform the following operation any number of times (possibly zero): Choose i, j from \{1, 2, 3\} such that i!=j and increment a_i by 1 and decrement a_j by 1 Help Jeevan find out the minimum value of d(a_1, a_2, a_3) that can be obtained after applying the operation any number of times. | Input: ['3', '3 4 5', '2 2 6', '1 6 5', ''] Output:['0', '1', '0', ''] | [
3
] |
Shohag has an integer sequence a_1, a_2, ..., a_n. He can perform the following operation any number of times (possibly, zero): Select any positive integer k (it can be different in different operations). Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert k into the sequence at this position. This way, the sequence a changes, and the next operation is performed on this changed sequence. For example, if a=[3,3,4] and he selects k = 2, then after the operation he can obtain one of the sequences [\underline{2},3,3,4], [3,\underline{2},3,4], [3,3,\underline{2},4], or [3,3,4,\underline{2}].Shohag wants this sequence to satisfy the following condition: for each 1 <= i <= |a|, a_i <= i. Here, |a| denotes the size of a.Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it's always possible to achieve this goal in a finite number of operations. | Input: ['4', '3', '1 3 4', '5', '1 2 5 7 4', '1', '1', '3', '69 6969 696969', ''] Output:['1', '3', '0', '696966', ''] | [
2
] |
It was October 18, 2017. Shohag, a melancholic soul, made a strong determination that he will pursue Competitive Programming seriously, by heart, because he found it fascinating. Fast forward to 4 years, he is happy that he took this road. He is now creating a contest on Codeforces. He found an astounding problem but has no idea how to solve this. Help him to solve the final problem of the round.You are given three integers n, k and x. Find the number, modulo 998\,244\,353, of integer sequences a_1, a_2, ..., a_n such that the following conditions are satisfied: 0 <= a_i \lt 2^k for each integer i from 1 to n. There is no non-empty subsequence in a such that the bitwise XOR of the elements of the subsequence is x. A sequence b is a subsequence of a sequence c if b can be obtained from c by deletion of several (possibly, zero or all) elements. | Input: ['6', '2 2 0', '2 1 1', '3 2 3', '69 69 69', '2017 10 18', '5 7 0', ''] Output:['6', '1', '15', '699496932', '892852568', '713939942', ''] | [
3
] |
A sequence of integers b_1, b_2, ..., b_m is called good if max(b_1, b_2, ..., b_m) \cdot min(b_1, b_2, ..., b_m) >= b_1 + b_2 + ... + b_m.A sequence of integers a_1, a_2, ..., a_n is called perfect if every non-empty subsequence of a is good.YouKn0wWho has two integers n and M, M is prime. Help him find the number, modulo M, of perfect sequences a_1, a_2, ..., a_n such that 1 <= a_i <= n + 1 for each integer i from 1 to n.A sequence d is a subsequence of a sequence c if d can be obtained from c by deletion of several (possibly, zero or all) elements. | Input: ['2 998244353', ''] Output:['4', ''] | [
3
] |
For an array b of n integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make b non-decreasing: Select an index i such that 1 <= i <= |b|, where |b| is the current length of b. Replace b_i with two elements x and y such that x and y both are positive integers and x + y = b_i. This way, the array b changes and the next operation is performed on this modified array. For example, if b = [2, 4, 3] and index 2 gets selected, then the possible arrays after this operation are [2, \underline{1}, \underline{3}, 3], [2, \underline{2}, \underline{2}, 3], or [2, \underline{3}, \underline{1}, 3]. And consequently, for this array, this single operation is enough to make it non-decreasing: [2, 4, 3] \rightarrow [2, \underline{2}, \underline{2}, 3].It's easy to see that every array of positive integers can be made non-decreasing this way.YouKn0wWho has an array a of n integers. Help him find the sum of extreme values of all nonempty subarrays of a modulo 998\,244\,353. If a subarray appears in a multiple times, its extreme value should be counted the number of times it appears.An array d is a subarray of an array c if d can be obtained from c by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. | Input: ['4', '3', '5 4 3', '4', '3 2 1 4', '1', '69', '8', '7264 40515 28226 92776 35285 21709 75124 48163', ''] Output:['5', '9', '0', '117', ''] | [
2,
3
] |
YouKn0wWho has two even integers x and y. Help him to find an integer n such that 1 <= n <= 2 \cdot 10^{18} and n \bmod x = y \bmod n. Here, a \bmod b denotes the remainder of a after division by b. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints. | Input: ['4', '4 8', '4 2', '420 420', '69420 42068', ''] Output:['4', '10', '420', '9969128', ''] | [
3
] |
YouKn0wWho has an integer sequence a_1, a_2, ..., a_n. He will perform the following operation until the sequence becomes empty: select an index i such that 1 <= i <= |a| and a_i is not divisible by (i + 1), and erase this element from the sequence. Here |a| is the length of sequence a at the moment of operation. Note that the sequence a changes and the next operation is performed on this changed sequence.For example, if a=[3,5,4,5], then he can select i = 2, because a_2 = 5 is not divisible by i+1 = 3. After this operation the sequence is [3,4,5].Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation. | Input: ['5', '3', '1 2 3', '1', '2', '2', '7 7', '10', '384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328', '8', '6 69 696 69696 696969 6969696 69696969 696969696', ''] Output:['YES', 'NO', 'YES', 'YES', 'NO', ''] | [
3
] |
Integers from 1 to n (inclusive) were sorted lexicographically (considering integers as strings). As a result, array a_1, a_2, ..., a_n was obtained.Calculate value of (\sum_{i = 1}^n ((i - a_i) \mod 998244353)) \mod 10^9 + 7.x \mod y here means the remainder after division x by y. This remainder is always non-negative and doesn't exceed y - 1. For example, 5 \mod 3 = 2, (-1) \mod 6 = 5. | Input: ['3', ''] Output:['0', ''] | [
3,
4
] |
Students of one unknown college don't have PE courses. That's why q of them decided to visit a gym nearby by themselves. The gym is open for n days and has a ticket system. At the i-th day, the cost of one ticket is equal to a_i. You are free to buy more than one ticket per day.You can activate a ticket purchased at day i either at day i or any day later. Each activated ticket is valid only for k days. In other words, if you activate ticket at day t, it will be valid only at days t, t + 1, ..., t + k - 1. You know that the j-th student wants to visit the gym at each day from l_j to r_j inclusive. Each student will use the following strategy of visiting the gym at any day i (l_j <= i <= r_j): person comes to a desk selling tickets placed near the entrance and buy several tickets with cost a_i apiece (possibly, zero tickets); if the person has at least one activated and still valid ticket, they just go in. Otherwise, they activate one of tickets purchased today or earlier and go in. Note that each student will visit gym only starting l_j, so each student has to buy at least one ticket at day l_j.Help students to calculate the minimum amount of money they have to spend in order to go to the gym. | Input: ['7 5 2', '2 15 6 3 7 5 6', '1 2', '3 7', '5 5', '7 7', '3 5', ''] Output:['2', '12', '7', '6', '9', ''] | [
2
] |
A group of n alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer d.Each alpinist can be described by two integers s and a, where s is his skill of climbing mountains and a is his neatness.An alpinist of skill level s is able to climb a mountain of difficulty p only if p <=q s. As an alpinist climbs a mountain, they affect the path and thus may change mountain difficulty. Specifically, if an alpinist of neatness a climbs a mountain of difficulty p the difficulty of this mountain becomes \max(p, a). Alpinists will climb the mountain one by one. And before the start, they wonder, what is the maximum number of alpinists who will be able to climb the mountain if they choose the right order. As you are the only person in the group who does programming, you are to answer the question.Note that after the order is chosen, each alpinist who can climb the mountain, must climb the mountain at that time. | Input: ['3 2', '2 6', '3 5', '5 7', ''] Output:['2', ''] | [
2
] |
You are given two arrays of integers a_1, a_2, ..., a_n and b_1, b_2, ..., b_m.You need to insert all elements of b into a in an arbitrary way. As a result you will get an array c_1, c_2, ..., c_{n+m} of size n + m.Note that you are not allowed to change the order of elements in a, while you can insert elements of b at arbitrary positions. They can be inserted at the beginning, between any elements of a, or at the end. Moreover, elements of b can appear in the resulting array in any order.What is the minimum possible number of inversions in the resulting array c? Recall that an inversion is a pair of indices (i, j) such that i < j and c_i > c_j. | Input: ['3', '3 4', '1 2 3', '4 3 2 1', '3 3', '3 2 1', '1 2 3', '5 4', '1 3 5 3 1', '4 3 6 1', ''] Output:['0', '4', '6', ''] | [
2
] |
You are given array a_1, a_2, ..., a_n, consisting of non-negative integers.Let's define operation of "elimination" with integer parameter k (1 <=q k <=q n) as follows: Choose k distinct array indices 1 <=q i_1 < i_2 < ... < i_k <= n. Calculate x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ ... ~ \& ~ a_{i_k}, where \& denotes the bitwise AND operation (notes section contains formal definition). Subtract x from each of a_{i_1}, a_{i_2}, ..., a_{i_k}; all other elements remain untouched. Find all possible values of k, such that it's possible to make all elements of array a equal to 0 using a finite number of elimination operations with parameter k. It can be proven that exists at least one possible k for any array a.Note that you firstly choose k and only after that perform elimination operations with value k you've chosen initially. | Input: ['5', '4', '4 4 4 4', '4', '13 7 25 19', '6', '3 5 3 1 7 1', '1', '1', '5', '0 0 0 0 0', ''] Output:['1 2 4', '1 2', '1', '1', '1 2 3 4 5', ''] | [
2,
3
] |
On the great island of Baltia, there live N people, numbered from 1 to N. There are exactly M pairs of people that are friends with each other. The people of Baltia want to organize a successful party, but they have very strict rules on what a party is and when the party is successful. On the island of Baltia, a party is a gathering of exactly 5 people. The party is considered to be successful if either all the people at the party are friends with each other (so that they can all talk to each other without having to worry about talking to someone they are not friends with) or no two people at the party are friends with each other (so that everyone can just be on their phones without anyone else bothering them). Please help the people of Baltia organize a successful party or tell them that it's impossible to do so. | Input: ['6 3', '1 4', '4 2', '5 4', ''] Output:['1 2 3 5 6', ''] | [
0,
3
] |
Alice and Bob are playing a game. They are given an array A of length N. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? | Input: ['1', '5', ''] Output:['Alice', ''] | [
2
] |
Bob really likes playing with arrays of numbers. That's why for his birthday, his friends bought him a really interesting machine – an array beautifier. The array beautifier takes an array A consisting of N integers, and it outputs a new array B of length N that it constructed based on the array given to it. The array beautifier constructs the new array in the following way: it takes two numbers at different indices from the original array and writes their sum to the end of the new array. It does this step N times - resulting in an output array of length N. During this process, the machine can take the same index multiple times in different steps. Bob was very excited about the gift that his friends gave him, so he put his favorite array in the machine. However, when the machine finished, Bob was not happy with the resulting array. He misses his favorite array very much, and hopes to get it back. Given the array that the machine outputted, help Bob find an array that could be the original array that he put in the machine. Sometimes the machine makes mistakes, so it is possible that no appropriate input array exists for the array it has outputted. In such case, let Bob know that his array is forever lost. | Input: ['2', '5 5', ''] Output:['YES', '2 3', ''] | [
0,
2
] |
This is an interactive problem!As part of your contribution in the Great Bubble War, you have been tasked with finding the newly built enemy fortress. The world you live in is a giant 10^9 * 10^9 grid, with squares having both coordinates between 1 and 10^9. You know that the enemy base has the shape of a rectangle, with the sides parallel to the sides of the grid. The people of your world are extremely scared of being at the edge of the world, so you know that the base doesn't contain any of the squares on the edges of the grid (the x or y coordinate being 1 or 10^9). To help you locate the base, you have been given a device that you can place in any square of the grid, and it will tell you the manhattan distance to the closest square of the base. The manhattan distance from square (a, b) to square (p, q) is calculated as |a−p|+|b−q|. If you try to place the device inside the enemy base, you will be captured by the enemy. Because of this, you need to make sure to never place the device inside the enemy base. Unfortunately, the device is powered by a battery and you can't recharge it. This means that you can use the device at most 40 times. | Input: ['1', '1', '2', '1', ''] Output:['? 2 2', '? 5 5', '? 4 7', '? 1 5', '! 2 3 4 5', ''] | [
3
] |
You are given N points on an infinite plane with the Cartesian coordinate system on it. N-1 points lay on one line, and one point isn't on that line. You are on point K at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. What is the minimum length of the path? | Input: ['5 2', '0 0', '-1 1', '2 -2', '0 1', '-2 2', ''] Output:['7.478709'] | [
0,
3
] |
Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players.Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game will be played. The game system randomly selects three maps and shows them to the players. Each player must pick one of those three maps to be discarded. The game system then randomly selects one of the maps that were not picked by any of the players and starts the game.Johnny is deeply enthusiastic about the game and wants to spend some time studying maps, thus increasing chances to win games played on those maps. However, he also needs to do his homework, so he does not have time to study all the maps. That is why he asked himself the following question: "What is the minimum number of maps I have to study, so that the probability to play one of those maps is at least P"?Can you help Johnny find the answer for this question? You can assume Johnny's opponents do not know him, and they will randomly pick maps. | Input: ['7 1.0000', ''] Output:['6', ''] | [
3,
4
] |
You are given an array A of length N weights of masses A_1, A_2...A_N. No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order A_1,...,A_N. There is also a string S consisting of characters "L" and "R", meaning that after putting the i-th weight (not A_i, but i-th weight of your choice) left or right side of the balance should be heavier. Find the order of putting the weights on the balance such that rules of string S are satisfied. | Input: ['5', '3 8 2 13 7', 'LLRLL', ''] Output:['3 L', '2 R', '8 R', '13 L', '7 L', ''] | [
2
] |
Let's call a positive integer good if there is no digit 0 in its decimal representation.For an array of a good numbers a, one found out that the sum of some two neighboring elements is equal to x (i.e. x = a_i + a_{i + 1} for some i). x had turned out to be a good number as well.Then the elements of the array a were written out one after another without separators into one string s. For example, if a = [12, 5, 6, 133], then s = 1256133.You are given a string s and a number x. Your task is to determine the positions in the string that correspond to the adjacent elements of the array that have sum x. If there are several possible answers, you can print any of them. | Input: ['1256133', '17', ''] Output:['1 2', '3 3', ''] | [
3
] |
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. Let's denote the concatenation of two strings x and y as x+y. For example, "()()" + ")(" = "()())(".You are given n bracket sequences s_1, s_2, ..., s_n. You can rearrange them in any order (you can rearrange only the strings themselves, but not the characters in them).Your task is to rearrange the strings in such a way that the string s_1 + s_2 + ... + s_n has as many non-empty prefixes that are RBS as possible. | Input: ['2', '(', ')', ''] Output:['1', ''] | [
0,
4
] |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.Each cell of the matrix can be either free or locked.Let's call a path in the matrix a staircase if it: starts and ends in the free cell; visits only free cells; has one of the two following structures: the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on; the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on. In particular, a path, consisting of a single cell, is considered to be a staircase.Here are some examples of staircases: Initially all the cells of the matrix are free.You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path. | Input: ['2 2 8', '1 1', '1 1', '1 1', '2 2', '1 1', '1 2', '2 1', '1 1', ''] Output:['5', '10', '5', '2', '5', '3', '1', '0', ''] | [
0,
3
] |
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has n problems that none of his students have seen yet. The i-th problem has a topic a_i (an integer from 1 to n) and a difficulty b_i (an integer from 1 to n). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly 3 problems from n problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): the topics of all three selected problems are different; the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset. | Input: ['2', '4', '2 4', '3 4', '2 1', '1 3', '5', '1 5', '2 4', '3 3', '4 2', '5 1', ''] Output:['3', '10', ''] | [
3
] |