question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
A new delivery of clothing has arrived today to the clothing store. This delivery consists of a ties, b scarves, c vests and d jackets.The store does not sell single clothing items β instead, it sells suits of two types: a suit of the first type consists of one tie and one jacket; a suit of the second type consists of one scarf, one vest and one jacket. Each suit of the first type costs e coins, and each suit of the second type costs f coins.Calculate the maximum possible cost of a set of suits that can be composed from the delivered clothing items. Note that one item cannot be used in more than one suit (though some items may be left unused). | Input: ['4', '5', '6', '3', '1', '2', ''] Output:['6', ''] | [
0,
2,
3
] |
There is given an integer k and a grid 2^k * 2^k with some numbers written in its cells, cell (i, j) initially contains number a_{ij}. Grid is considered to be a torus, that is, the cell to the right of (i, 2^k) is (i, 1), the cell below the (2^k, i) is (1, i) There is also given a lattice figure F, consisting of t cells, where t is odd. F doesn't have to be connected.We can perform the following operation: place F at some position on the grid. (Only translations are allowed, rotations and reflections are prohibited). Now choose any nonnegative integer p. After that, for each cell (i, j), covered by F, replace a_{ij} by a_{ij}\oplus p, where \oplus denotes the bitwise XOR operation.More formally, let F be given by cells (x_1, y_1), (x_2, y_2), ..., (x_t, y_t). Then you can do the following operation: choose any x, y with 1<= x, y <= 2^k, any nonnegative integer p, and for every i from 1 to n replace number in the cell (((x + x_i - 1)\bmod 2^k) + 1, ((y + y_i - 1)\bmod 2^k) + 1) with a_{((x + x_i - 1)\bmod 2^k) + 1, ((y + y_i - 1)\bmod 2^k) + 1}\oplus p.Our goal is to make all the numbers equal to 0. Can we achieve it? If we can, find the smallest number of operations in which it is possible to do this. | Input: ['2', '5 5 5 5', '2 6 2 3', '0 0 2 0', '0 0 0 0', '5', '1 1', '1 2', '1 3', '1 4', '2 4', ''] Output:['3'] | [
3
] |
You are given n integers a_1, a_2, ..., a_n, such that for each 1<= i <= n holds i-n<= a_i<= i-1.Find some nonempty subset of these integers, whose sum is equal to 0. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them. | Input: ['2', '5', '0 1 2 3 4', '4', '-3 1 1 1', ''] Output:['1', '1 ', '4', '1 4 3 2 ', ''] | [
3
] |
Let's call a binary string s awesome, if it has at least 1 symbol 1 and length of the string is divisible by the number of 1 in it. In particular, 1, 1010, 111 are awesome, but 0, 110, 01010 aren't.You are given a binary string s. Count the number of its awesome substrings.A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. | Input: ['111', ''] Output:['6', ''] | [
3
] |
You are given a set of n>= 2 pairwise different points with integer coordinates. Your task is to partition these points into two nonempty groups A and B, such that the following condition holds:For every two points P and Q, write the Euclidean distance between them on the blackboard: if they belong to the same group β with a yellow pen, and if they belong to different groups β with a blue pen. Then no yellow number is equal to any blue number.It is guaranteed that such a partition exists for any possible input. If there exist multiple partitions, you are allowed to output any of them. | Input: ['3', '0 0', '0 1', '1 0', ''] Output:['1', '1 '] | [
3
] |
This problem is interactive.We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order.Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don't remember m. Your task is to find m using queries to this device. You can ask not more than n queries.Note that the array a and number m are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive.Note that you don't have to minimize the number of queries, and you don't need to guess array a. You just have to guess m. | Input: ['4 3', '4 9', '4 9', '4 9', '1 2'] Output:['? 2 3 4', '? 1 3 4', '? 1 2 4', '? 1 2 3', '! 3', ''] | [
3
] |
Let's call an array a_1, a_2, ..., a_m of nonnegative integer numbers good if a_1 + a_2 + ... + a_m = 2\cdot(a_1 \oplus a_2 \oplus ... \oplus a_m), where \oplus denotes the bitwise XOR operation.For example, array [1, 2, 3, 6] is good, as 1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6). At the same time, array [1, 2, 1, 3] isn't good, as 1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3).You are given an array of length n: a_1, a_2, ..., a_n. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements. | Input: ['3', '4', '1 2 3 6', '1', '8', '2', '1 1', ''] Output:['0', '', '2', '4 4', '3', '2 6 2', ''] | [
3
] |
For an array a of integers let's denote its maximal element as \max(a), and minimal as \min(a). We will call an array a of k integers interesting if \max(a) - \min(a) >= k. For example, array [1, 3, 4, 3] isn't interesting as \max(a) - \min(a) = 4 - 1 = 3 < 4 while array [7, 3, 0, 4, 3] is as \max(a) - \min(a) = 7 - 0 = 7 >= 5.You are given an array a of n integers. Find some interesting nonempty subarray of a, or tell that it doesn't exist.An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself. | Input: ['3', '5', '1 2 3 4 5', '4', '2 0 1 9', '2', '2019 2020', ''] Output:['NO', 'YES', '1 4', 'NO', ''] | [
2,
3
] |
Two players decided to play one interesting card game.There is a deck of n cards, with values from 1 to n. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that n = 5, the first player has cards with values 2 and 3, and the second player has cards with values 1, 4, 5. Then one possible flow of the game is:The first player chooses the card 3. The second player chooses the card 1. As 3>1, the first player gets both cards. Now the first player has cards 1, 2, 3, the second player has cards 4, 5.The first player chooses the card 3. The second player chooses the card 4. As 3<4, the second player gets both cards. Now the first player has cards 1, 2. The second player has cards 3, 4, 5.The first player chooses the card 1. The second player chooses the card 3. As 1<3, the second player gets both cards. Now the first player has only the card 2. The second player has cards 1, 3, 4, 5.The first player chooses the card 2. The second player chooses the card 4. As 2<4, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | Input: ['2', '2 1 1', '2', '1', '5 2 3', '2 3', '1 4 5', ''] Output:['YES', 'NO', ''] | [
2,
3
] |
You are given a positive integer m and two integer sequence: a=[a_1, a_2, ..., a_n] and b=[b_1, b_2, ..., b_n]. Both of these sequence have a length n.Permutation is a sequence of n different positive integers from 1 to n. For example, these sequences are permutations: [1], [1,2], [2,1], [6,7,3,4,1,2,5]. These are not: [0], [1,1], [2,3].You need to find the non-negative integer x, and increase all elements of a_i by x, modulo m (i.e. you want to change a_i to (a_i + x) \bmod m), so it would be possible to rearrange elements of a to make it equal b, among them you need to find the smallest possible x.In other words, you need to find the smallest non-negative integer x, for which it is possible to find some permutation p=[p_1, p_2, ..., p_n], such that for all 1 <=q i <=q n, (a_i + x) \bmod m = b_{p_i}, where y \bmod m β remainder of division of y by m.For example, if m=3, a = [0, 0, 2, 1], b = [2, 0, 1, 1], you can choose x=1, and a will be equal to [1, 1, 0, 2] and you can rearrange it to make it equal [2, 0, 1, 1], which is equal to b. | Input: ['4 3', '0 0 2 1', '2 0 1 1', ''] Output:['1', ''] | [
0
] |
Let's call a positive integer composite if it has at least one divisor other than 1 and itself. For example: the following numbers are composite: 1024, 4, 6, 9; the following numbers are not composite: 13, 1, 2, 3, 37. You are given a positive integer n. Find two composite integers a,b such that a-b=n.It can be proven that solution always exists. | Input: ['1', ''] Output:['9 8', ''] | [
0,
3
] |
You are given a tournament β complete directed graph.In one operation you can pick any vertex v and change the direction of all edges with v on one of the ends (i.e all edges u \to v change their orientation to v \to u and vice versa).You want to make the tournament strongly connected with the smallest possible number of such operations if it is possible. Also, if it is possible, you need to find the number of ways to make this number of operations to make graph strongly connected (two ways are different if for some i vertex that we chose on i-th operation in one way is different from vertex that we chose on i-th operation in another way). You only need to find this value modulo 998\,244\,353. | Input: ['3', '010', '001', '100', ''] Output:['0 1', ''] | [
0,
3
] |
You are given a permutation p_1, p_2, ..., p_n.In one move you can swap two adjacent values.You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,..., k, in other words in the end there should be an integer i, 1 <=q i <=q n-k+1 such that p_i = 1, p_{i+1} = 2, ..., p_{i+k-1}=k.Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,...,k appear in the permutation.You need to find f(1), f(2), ..., f(n). | Input: ['5', '5 4 3 2 1', ''] Output:['0 1 3 6 10 ', ''] | [
4
] |
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, ..., a_n (a_1 >=q a_2 >=q ... >=q a_n >=q 1). Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 * 2 or 2 * 1 rectangle. | Input: ['5', '3 2 2 2 1', ''] Output:['4', ''] | [
2,
3
] |
You are given an integer x of n digits a_1, a_2, ..., a_n, which make up its decimal notation in order from left to right.Also, you are given a positive integer k < n.Let's call integer b_1, b_2, ..., b_m beautiful if b_i = b_{i+k} for each i, such that 1 <=q i <=q m - k.You need to find the smallest beautiful integer y, such that y >=q x. | Input: ['3 2', '353', ''] Output:['3', '353', ''] | [
2
] |
Karl is developing a key storage service. Each user has a positive integer key.Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions. For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her. | Input: ['3', '1', '11', '123456', ''] Output:['0', '1', '127', ''] | [
3
] |
BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between 1 and n, inclusive; the i-th application has category c_i. You can choose m β the number of screens and s β the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements: On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category); Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s-1). Your task is to find the minimal possible number of screens m. | Input: ['3', '11', '1 5 1 5 1 5 1 1 1 1 5', '6', '1 2 2 2 2 1', '5', '4 3 3 1 2', ''] Output:['3', '3', '4', ''] | [
2,
3
] |
This is an interactive problem.You are the head coach of a chess club. The club has 2n players, each player has some strength which can be represented by a number, and all those numbers are distinct. The strengths of the players are not known to you.You need to select n players who would represent your club in the upcoming championship. Naturally, you want to select n players with the highest strengths.You can organize matches between the players to do that. In every match, you pick two players, they play some games, and you learn which one of the two has higher strength. You can wait for the outcome of a match before deciding who will participate in the next one.However, you do not want to know exactly how those n players compare between themselves, as that would make the championship itself less intriguing. More formally, you must reach a state where there is exactly one way to choose n players with the highest strengths that is consistent with the outcomes of the matches you organized, but there must be at least two possible orderings of those n players by strength that are consistent with the outcomes of the matches you organized. | Input: ['2', '3', '', '>', '', '<', '', '>', '', '<', '', '>', '', '>', '', '3', '', '<', '', '<', '', '<', '', '>', '', '>', ''] Output:['', '', '? 1 3', '', '? 4 2', '', '? 4 5', '', '? 6 5', '', '? 3 4', '', '? 5 6', '', '!', '', '? 3 4', '', '? 4 2', '', '? 5 3', '', '? 6 4', '', '? 3 1', '', '!', ''] | [
0
] |
Esports is a form of competitive sports using video games. Dota 2 is one of the most popular competitive video games in Esports. Recently, a new video game Dota 3 was released. In Dota 3 a player can buy some relics for their hero. Relics are counters that track hero's actions and statistics in a game.Gloria likes to play Dota 3, so she wants to buy all n available relics for her favorite hero.Relics can be bought using an in-game currency called shards. Each relic has its own price β c_i shards for the i-th relic. A player can buy a relic using one of the following options: Pay c_i shards to buy the i-th relic; Pay x shards and randomly get one of all n relics. The probability of getting a relic is the same for all n relics. If a duplicate relic is received, then the relic is recycled and \frac{x}{2} shards are given back to the player. Gloria wants to buy all n relics. Help her minimize the expected number of shards she spends to buy all the relics. | Input: ['2 20', '25 100', ''] Output:['47.50000000000000000', ''] | [
3
] |
Byteburg Senate elections are coming. Usually "United Byteland", the ruling Byteland party, takes all the seats in the Senate to ensure stability and sustainable development. But this year there is one opposition candidate in one of the constituencies. Even one opposition member can disturb the stability in the Senate, so the head of the Party asks you to ensure that the opposition candidate will not be elected.There are n candidates, numbered from 1 to n. Candidate n is the opposition candidate. There are m polling stations in the constituency, numbered from 1 to m. You know the number of votes cast for each candidate at each polling station. The only thing you can do to prevent the election of the opposition candidate is to cancel the election results at some polling stations. The opposition candidate will be elected if the sum of the votes cast in their favor at all non-canceled stations will be strictly greater than the analogous sum for every other candidate. Your task is to prevent the election of the opposition candidate by canceling the election results at the minimal possible number of polling stations. Notice that solution always exists, because if you cancel the elections at all polling stations, the number of votes for each candidate will be 0, and the opposition candidate will not be elected. | Input: ['5 3', '6 3 4 2 8', '3 7 5 6 7', '5 2 4 7 9', ''] Output:['2', '3 1 ', ''] | [
2
] |
There is a directed graph on n vertices numbered 1 through n where each vertex (except n) has two outgoing arcs, red and blue. At any point in time, exactly one of the arcs is active for each vertex. Initially, all blue arcs are active and there is a token located at vertex 1. In one second, the vertex with token first switches its active arcs β the inactive arc becomes active and vice versa. Then, the token is moved along the active arc. When the token reaches the vertex n, it stops. It is guaranteed that n is reachable via arcs from every vertex.You are given q queries. Each query contains a state of the graph β a pair (v, s) of the following form: v is the vertex where the token is currently located; s is a string consisting of n - 1 characters. The i-th character corresponds to the color of the active edge leading from the i-th vertex (the character is 'R' if red arc is active, otherwise the character is 'B'). For each query, determine whether the given state is reachable from the initial state and the first time this configuration appears. Note that the two operations (change active arc and traverse it) are atomic β a state is not considered reached if it appears after changing the active arc but before traversing it. | Input: ['6', '2 1', '5 5', '2 1', '6 3', '4 3', '21', '1 BBBBB', '1 RBBBB', '2 BBBBB', '5 BRBBB', '3 BRBBR', '1 BRRBR', '1 RRRBR', '2 BRRBR', '5 BBRBR', '4 BBRBB', '3 BBRRB', '2 BBBRB', '5 BRBRB', '3 BRBRR', '1 BRRRR', '1 RRRRR', '2 BRRRR', '5 BBRRR', '4 BBRRB', '2 BRBBB', '4 BRBBR', ''] Output:['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '-1', '-1', ''] | [
3
] |
Bob is playing a game of Spaceship Solitaire. The goal of this game is to build a spaceship. In order to do this, he first needs to accumulate enough resources for the construction. There are n types of resources, numbered 1 through n. Bob needs at least a_i pieces of the i-th resource to build the spaceship. The number a_i is called the goal for resource i.Each resource takes 1 turn to produce and in each turn only one resource can be produced. However, there are certain milestones that speed up production. Every milestone is a triple (s_j, t_j, u_j), meaning that as soon as Bob has t_j units of the resource s_j, he receives one unit of the resource u_j for free, without him needing to spend a turn. It is possible that getting this free resource allows Bob to claim reward for another milestone. This way, he can obtain a large number of resources in a single turn.The game is constructed in such a way that there are never two milestones that have the same s_j and t_j, that is, the award for reaching t_j units of resource s_j is at most one additional resource.A bonus is never awarded for 0 of any resource, neither for reaching the goal a_i nor for going past the goal β formally, for every milestone 0 < t_j < a_{s_j}.A bonus for reaching certain amount of a resource can be the resource itself, that is, s_j = u_j.Initially there are no milestones. You are to process q updates, each of which adds, removes or modifies a milestone. After every update, output the minimum number of turns needed to finish the game, that is, to accumulate at least a_i of i-th resource for each i \in [1, n]. | Input: ['2', '2 3', '5', '2 1 1', '2 2 1', '1 1 1', '2 1 2', '2 2 0', ''] Output:['4', '3', '3', '2', '3', ''] | [
2
] |
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let d(a,b) denote the debt of a towards b, or 0 if there is no such debt.Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let d(a,b) > 0 and d(c,d) > 0 such that a \neq c or b \neq d. We can decrease the d(a,b) and d(c,d) by z and increase d(c,b) and d(a,d) by z, where 0 < z <=q \min(d(a,b),d(c,d)). Let d(a,a) > 0. We can set d(a,a) to 0. The total debt is defined as the sum of all debts:\Sigma_d = \sum_{a,b} d(a,b)Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. | Input: ['3 21 2 102 3 5'] Output:['21 2 51 3 5'] | [
2,
3
] |
Let a be a matrix of size r * c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 1 to r, columns are numbered from 1 to c. We can construct an array b consisting of r + c integers as follows: for each i \in [1, r], let b_i be the greatest common divisor of integers in the i-th row, and for each j \in [1, c] let b_{r+j} be the greatest common divisor of integers in the j-th column. We call the matrix diverse if all r + c numbers b_k (k \in [1, r + c]) are pairwise distinct. The magnitude of a matrix equals to the maximum of b_k.For example, suppose we have the following matrix: \begin{pmatrix} 2 & 9 & 7\\ 4 & 144 & 84 \end{pmatrix} We construct the array b: b_1 is the greatest common divisor of 2, 9, and 7, that is 1; b_2 is the greatest common divisor of 4, 144, and 84, that is 4; b_3 is the greatest common divisor of 2 and 4, that is 2; b_4 is the greatest common divisor of 9 and 144, that is 9; b_5 is the greatest common divisor of 7 and 84, that is 7. So b = [1, 4, 2, 9, 7]. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to 9.For a given r and c, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer 0. | Input: ['2 2', ''] Output:['4 12', '2 9'] | [
2,
3
] |
Bob is playing with 6-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is 29 β the number visible on the top is 1, from the south 5 and 3, from the west 4 and 2, from the north 2 and 4 and from the east 3 and 5.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has t favourite integers x_i, and for every such integer his goal is to build such a tower that the number of visible pips is exactly x_i. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips. | Input: ['4', '29 34 19 38', ''] Output:['YES', 'YES', 'YES', 'NO', ''] | [
3
] |
Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked n of them how much effort they needed to reach red."Oh, I just spent x_i hours solving problems", said the i-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes (60 \cdot x_i), thanked the grandmasters and went home. Bob could write numbers with leading zeroes β for example, if some grandmaster answered that he had spent 2 hours, Bob could write 000120 instead of 120.Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: rearranged its digits, or wrote a random number. This way, Alice generated n numbers, denoted y_1, ..., y_n.For each of the numbers, help Bob determine whether y_i can be a permutation of a number divisible by 60 (possibly with leading zeroes). | Input: ['6', '603', '006', '205', '228', '1053', '0000000000000000000000000000000000000000000000', ''] Output:['red', 'red', 'cyan', 'cyan', 'cyan', 'red', ''] | [
3
] |
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability \frac{p_i}{100} for all 1 <= i <= n.Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities: The i-th mirror tells Creatnx that he is beautiful. In this case, if i = n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy.This number should be found by modulo 998244353. Formally, let M = 998244353. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output such an integer x that 0 <= x < M and x \cdot q \equiv p \pmod{M}. | Input: ['1', '50', ''] Output:['2', ''] | [
3
] |
You are given a permutation p=[p_1, p_2, ..., p_n] of integers from 1 to n. Let's call the number m (1 <= m <= n) beautiful, if there exists two indices l, r (1 <= l <= r <= n), such that the numbers [p_l, p_{l+1}, ..., p_r] is a permutation of numbers 1, 2, ..., m.For example, let p = [4, 5, 1, 3, 2, 6]. In this case, the numbers 1, 3, 5, 6 are beautiful and 2, 4 are not. It is because: if l = 3 and r = 3 we will have a permutation [1] for m = 1; if l = 3 and r = 5 we will have a permutation [1, 3, 2] for m = 3; if l = 1 and r = 5 we will have a permutation [4, 5, 1, 3, 2] for m = 5; if l = 1 and r = 6 we will have a permutation [4, 5, 1, 3, 2, 6] for m = 6; it is impossible to take some l and r, such that [p_l, p_{l+1}, ..., p_r] is a permutation of numbers 1, 2, ..., m for m = 2 and for m = 4. You are given a permutation p=[p_1, p_2, ..., p_n]. For all m (1 <= m <= n) determine if it is a beautiful number or not. | Input: ['3', '6', '4 5 1 3 2 6', '5', '5 3 1 2 4', '4', '1 4 3 2', ''] Output:['101011', '11111', '1001', ''] | [
3
] |
A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa" and "bb" are not.Ahcl wants to construct a beautiful string. He has a string s, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!More formally, after replacing all characters '?', the condition s_i \neq s_{i+1} should be satisfied for all 1 <=q i <=q |s| - 1, where |s| is the length of the string s. | Input: ['3', 'a???cb', 'a??bbc', 'a?b?c', ''] Output:['ababcb', '-1', 'acbac', ''] | [
2
] |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s_1, s_2, ..., s_{n} is beautiful if |s_i - s_{i+1}| = 1 for all 1 <=q i <=q n - 1.Trans has a numbers 0, b numbers 1, c numbers 2 and d numbers 3. He wants to construct a beautiful sequence using all of these a + b + c + d numbers.However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans? | Input: ['2 2 2 1', ''] Output:['YES', '0 1 0 1 2 3 2', ''] | [
0,
2
] |
So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i problems. Since the participants are primarily sorted by the number of solved problems, then p_1 >= p_2 >= ... >= p_n.Help the jury distribute the gold, silver and bronze medals. Let their numbers be g, s and b, respectively. Here is a list of requirements from the rules, which all must be satisfied: for each of the three types of medals, at least one medal must be awarded (that is, g>0, s>0 and b>0); the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, g<s and g<b, but there are no requirements between s and b); each gold medalist must solve strictly more problems than any awarded with a silver medal; each silver medalist must solve strictly more problems than any awarded a bronze medal; each bronze medalist must solve strictly more problems than any participant not awarded a medal; the total number of medalists g+s+b should not exceed half of all participants (for example, if n=21, then you can award a maximum of 10 participants, and if n=26, then you can award a maximum of 13 participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize g+s+b) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals. | Input: ['5', '12', '5 4 4 3 2 2 1 1 1 1 1 1', '4', '4 3 2 1', '1', '1000000', '20', '20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1', '32', '64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11', ''] Output:['1 2 3', '0 0 0', '0 0 0', '2 5 3', '2 6 6', ''] | [
2
] |
On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, then the n rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain β it is not given to any of the participants.For example, if n = 5 and k = 3, then each participant will recieve an 1 rating unit, and also 2 rating units will remain unused. If n = 5, and k = 6, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if n=5, then the answer is equal to the sequence 0, 1, 2, 5. Each of the sequence values (and only them) can be obtained as \lfloor n/k \rfloor for some positive integer k (where \lfloor x \rfloor is the value of x rounded down): 0 = \lfloor 5/7 \rfloor, 1 = \lfloor 5/5 \rfloor, 2 = \lfloor 5/2 \rfloor, 5 = \lfloor 5/1 \rfloor.Write a program that, for a given n, finds a sequence of all possible rating increments. | Input: ['4', '5', '11', '1', '3', ''] Output:['4', '0 1 2 5 ', '6', '0 1 2 3 5 11 ', '2', '0 1 ', '3', '0 1 3 ', ''] | [
3,
4
] |
A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.Polycarp has n (2 <= n <= 10) bank cards, the PIN code of the i-th card is p_i.Polycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all n codes would become different.Formally, in one step, Polycarp picks i-th card (1 <= i <= n), then in its PIN code p_i selects one position (from 1 to 4), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.Polycarp quickly solved this problem. Can you solve it? | Input: ['3', '2', '1234', '0600', '2', '1337', '1337', '4', '3139', '3139', '3139', '3139', ''] Output:['0', '1234', '0600', '1', '1337', '1237', '3', '3139', '3138', '3939', '6139', ''] | [
2
] |
You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are r candies in it, the second pile contains only green candies and there are g candies in it, the third pile contains only blue candies and there are b candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies. | Input: ['6', '1 1 1', '1 2 1', '4 1 1', '7 4 10', '8 1 4', '8 2 8', ''] Output:['1', '2', '2', '10', '5', '9', ''] | [
3
] |
You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \{x | x = a \oplus b, a \in A, b \in B\} modulo 998244353, where \oplus denotes the bitwise XOR operation. Each number should be counted only once.For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 \oplus 2 and as 2 \oplus 3. So the answer for this case is equal to 1 + 0 = 1.Let's call a segment [l; r] a set of integers \{l, l+1, ..., r\}.The set A is given as a union of n_A segments, the set B is given as a union of n_B segments. | Input: ['2', '3 5', '5 8', '3', '1 2', '1 9', '2 9', ''] Output:['112', ''] | [
3
] |
You are organizing a boxing tournament, where n boxers will participate (n is a power of 2), and your friend is one of them. All boxers have different strength from 1 to n, and boxer i wins in the match against boxer j if and only if i is stronger than j.The tournament will be organized as follows: n boxers will be divided into pairs; the loser in each pair leaves the tournament, and \frac{n}{2} winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.Furthermore, during each stage you distribute the boxers into pairs as you wish.The boxer with strength i can be bribed if you pay him a_i dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish? | Input: ['4', '3 9 1 -1', ''] Output:['0'] | [
0,
2
] |
You are playing a computer game, where you lead a party of m soldiers. Each soldier is characterised by his agility a_i.The level you are trying to get through can be represented as a straight line segment from point 0 (where you and your squad is initially located) to point n + 1 (where the boss is located).The level is filled with k traps. Each trap is represented by three numbers l_i, r_i and d_i. l_i is the location of the trap, and d_i is the danger level of the trap: whenever a soldier with agility lower than d_i steps on a trap (that is, moves to the point l_i), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point r_i, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.You have t seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is x, you may move to x + 1 or x - 1. This action consumes one second; if your location is x and the location of your squad is x, you may move to x + 1 or to x - 1 with your squad in one second. You may not perform this action if it puts some soldier in danger (i.βe. the point your squad is moving into contains a non-disarmed trap with d_i greater than agility of some soldier from the squad). This action consumes one second; if your location is x and there is a trap i with r_i = x, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers.You have to choose the maximum number of soldiers such that they all can be brought from the point 0 to the point n + 1 (where the boss waits) in no more than t seconds. | Input: ['5 6 4 14', '1 2 3 4 5', '1 5 2', '1 2 5', '2 3 5', '3 5 3', ''] Output:['3', ''] | [
2,
4
] |
You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.You must paint a fence which consists of 10^{100} planks in two colors in the following way (suppose planks are numbered from left to right from 0): if the index of the plank is divisible by r (such planks have indices 0, r, 2r and so on) then you must paint it red; if the index of the plank is divisible by b (such planks have indices 0, b, 2b and so on) then you must paint it blue; if the index is divisible both by r and b you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). Furthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are k consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed.The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs. | Input: ['4', '1 1 2', '2 10 4', '5 2 3', '3 2 2', ''] Output:['OBEY', 'REBEL', 'OBEY', 'OBEY', ''] | [
2,
3
] |
You are given two integers a and b. You may perform any number of operations on them (possibly zero).During each operation you should choose any positive integer x and set a := a - x, b := b - 2x or a := a - 2x, b := b - x. Note that you may choose different values of x in different operations.Is it possible to make a and b equal to 0 simultaneously?Your program should answer t independent test cases. | Input: ['3', '6 9', '1 1', '1 2', ''] Output:['YES', 'NO', 'YES', ''] | [
3,
4
] |
Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.Your house has n rooms. In the i-th room you can install at most c_i heating radiators. Each radiator can have several sections, but the cost of the radiator with k sections is equal to k^2 burles.Since rooms can have different sizes, you calculated that you need at least sum_i sections in total in the i-th room. For each room calculate the minimum cost to install at most c_i radiators with total number of sections not less than sum_i. | Input: ['4', '1 10000', '10000 1', '2 6', '4 6', ''] Output:['100000000', '1', '18', '10', ''] | [
3
] |
You are given an integer x represented as a product of n its prime divisors p_1 \cdot p_2, \cdot ... \cdot p_n. Let S be the set of all positive integer divisors of x (including 1 and x itself).We call a set of integers D good if (and only if) there is no pair a \in D, b \in D such that a!=b and a divides b.Find a good subset of S with maximum possible size. Since the answer can be large, print the size of the subset modulo 998244353. | Input: ['3', '2999999 43 2999957', ''] Output:['3', ''] | [
2,
3
] |
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: 2 and 4 are similar (binary representations are 10 and 100); 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); 3 and 2 are not similar (binary representations are 11 and 10); 42 and 13 are similar (binary representations are 101010 and 1101). You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i \oplus x (\oplus denotes bitwise XOR).Is it possible to obtain an array b where all numbers are similar to each other? | Input: ['2', '7 2', ''] Output:['1', ''] | [
0
] |
A team of three programmers is going to play a contest. The contest consists of n problems, numbered from 1 to n. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong β the statements were printed in the wrong order, so the contestants have received the problems in some random order.The first contestant has received problems a_{1, 1}, a_{1, 2}, ..., a_{1, k_1}. The second one has received problems a_{2, 1}, a_{2, 2}, ..., a_{2, k_2}. The third one has received all remaining problems (a_{3, 1}, a_{3, 2}, ..., a_{3, k_3}).The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems.During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems?It is possible that after redistribution some participant (or even two of them) will not have any problems. | Input: ['2 1 2', '3 1', '4', '2 5', ''] Output:['1', ''] | [
2
] |
You play a computer game. In this game, you lead a party of m heroes, and you have to clear a dungeon with n monsters. Each monster is characterized by its power a_i. Each hero is characterized by his power p_i and endurance s_i.The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated k monsters, the hero fights with the monster k + 1). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the i-th hero cannot defeat more than s_i monsters during each day), or if all monsters are defeated β otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. | Input: ['2', '6', '2 3 11 14 1 8', '2', '3 2', '100 1', '5', '3 5 100 2 3', '2', '30 5', '90 1', ''] Output:['5', '-1', ''] | [
2,
4
] |
Let's call an array t dominated by value v in the next situation.At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1, 2, 3, 4, 5, 2], [11, 11] and [3, 2, 3, 2, 3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1, 2] and [3, 3, 2, 2, 1] are not.Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.You are given array a_1, a_2, ..., a_n. Calculate its shortest dominated subarray or say that there are no such subarrays.The subarray of a is a contiguous part of the array a, i.βe. the array a_i, a_{i + 1}, ..., a_j for some 1 <= i <= j <= n. | Input: ['4', '1', '1', '6', '1 2 3 4 5 1', '9', '4 1 2 4 5 4 3 2 1', '4', '3 3 3 3', ''] Output:['-1', '6', '3', '2', ''] | [
2
] |
Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number a is even, then the spell will turn it into \frac{3a}{2}; If the chosen number a is greater than one, then the spell will turn it into a-1. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number x. He wants to know if his favorite number y can be obtained from x using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave x as it is. | Input: ['7', '2 3', '1 1', '3 6', '6 8', '1 2', '4 1', '31235 6578234', ''] Output:['YES', 'YES', 'NO', 'YES', 'NO', 'YES', 'YES', ''] | [
3
] |
You are the gym teacher in the school.There are n students in the row. And there are two rivalling students among them. The first one is in position a, the second in position b. Positions are numbered from 1 to n from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions p and s respectively, then distance between them is |p - s|. You can do the following operation at most x times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most x swaps. | Input: ['3', '5 1 3 2', '100 33 100 1', '6 0 2 3', ''] Output:['2', '99', '1', ''] | [
2,
3
] |
There are n students at your university. The programming skill of the i-th student is a_i. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has 2 \cdot 10^5 students ready for the finals!Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of k students with programming skills a[i_1], a[i_2], ..., a[i_k], then the diversity of this team is \max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]).The total diversity is the sum of diversities of all teams formed.Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students. | Input: ['5', '1 1 3 4 2', ''] Output:['3 1', '1 1 1 1 1 ', ''] | [
2
] |
You are given a binary string of length n (i.βe. a string consisting of n characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than k moves? It is possible that you do not perform any moves at all.Note that you can swap the same pair of adjacent characters with indices i and i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.You have to answer q independent test cases. | Input: ['3', '8 5', '11011010', '7 9', '1111100', '7 11', '1111100', ''] Output:['01011110', '0101111', '0011111', ''] | [
2
] |
There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platform takes c_i consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.You are standing at 0 and want to reach n+1 somehow. If you are standing at the position x, you can jump to any position in the range [x + 1; x + d]. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 0 and n+1 belong to wooden platforms.You want to know if it is possible to reach n+1 from 0 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).For example, if n=7, m=3, d=2 and c = [1, 2, 1], then one of the ways to reach 8 from 0 is follow: The first example: n=7. | Input: ['7 3 2', '1 2 1', ''] Output:['YES', '0 1 0 2 2 0 3 ', ''] | [
2
] |
You are given a permutation of length n. Recall that the 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).You can perform at most n-1 operations with the given permutation (it is possible that you don't perform any operations at all). The i-th operation allows you to swap elements of the given permutation on positions i and i+1. Each operation can be performed at most once. The operations can be performed in arbitrary order.Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.You can see the definition of the lexicographical order in the notes section.You have to answer q independent test cases.For example, let's consider the permutation [5, 4, 1, 3, 2]. The minimum possible permutation we can obtain is [1, 5, 2, 4, 3] and we can do it in the following way: perform the second operation (swap the second and the third elements) and obtain the permutation [5, 1, 4, 3, 2]; perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation [5, 1, 4, 2, 3]; perform the third operation (swap the third and the fourth elements) and obtain the permutation [5, 1, 2, 4, 3]. perform the first operation (swap the first and the second elements) and obtain the permutation [1, 5, 2, 4, 3]; Another example is [1, 2, 4, 3]. The minimum possible permutation we can obtain is [1, 2, 3, 4] by performing the third operation (swap the third and the fourth elements). | Input: ['4', '5', '5 4 1 3 2', '4', '1 2 4 3', '1', '1', '4', '4 3 2 1', ''] Output:['1 5 2 4 3 ', '1 2 3 4 ', '1 ', '1 4 3 2 ', ''] | [
2
] |
You have a coins of value n and b coins of value 1. You always pay in exact change, so you want to know if there exist such x and y that if you take x (0 <= x <= a) coins of value n and y (0 <= y <= b) coins of value 1, then the total value of taken coins will be S.You have to answer q independent test cases. | Input: ['4', '1 2 3 4', '1 2 3 6', '5 2 6 27', '3 3 5 18', ''] Output:['YES', 'NO', 'NO', 'YES', ''] | [
3
] |
Bob watches TV every day. He always sets the volume of his TV to b. However, today he is angry to find out someone has changed the volume to a. Of course, Bob has a remote control that can change the volume.There are six buttons (-5, -2, -1, +1, +2, +5) on the control, which in one press can either increase or decrease the current volume by 1, 2, or 5. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than 0.As Bob is so angry, he wants to change the volume to b using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given a and b, finds the minimum number of presses to change the TV volume from a to b. | Input: ['3', '4 0', '5 14', '3 9', ''] Output:['2', '3', '2', ''] | [
3
] |
This is an interactive problem.Khanh has n points on the Cartesian plane, denoted by a_1, a_2, ..., a_n. All points' coordinates are integers between -10^9 and 10^9, inclusive. No three points are collinear. He says that these points are vertices of a convex polygon; in other words, there exists a permutation p_1, p_2, ..., p_n of integers from 1 to n such that the polygon a_{p_1} a_{p_2} ... a_{p_n} is convex and vertices are listed in counter-clockwise order.Khanh gives you the number n, but hides the coordinates of his points. Your task is to guess the above permutation by asking multiple queries. In each query, you give Khanh 4 integers t, i, j, k; where either t = 1 or t = 2; and i, j, k are three distinct indices from 1 to n, inclusive. In response, Khanh tells you: if t = 1, the area of the triangle a_ia_ja_k multiplied by 2. if t = 2, the sign of the cross product of two vectors \overrightarrow{a_ia_j} and \overrightarrow{a_ia_k}. Recall that the cross product of vector \overrightarrow{a} = (x_a, y_a) and vector \overrightarrow{b} = (x_b, y_b) is the integer x_a \cdot y_b - x_b \cdot y_a. The sign of a number is 1 it it is positive, and -1 otherwise. It can be proven that the cross product obtained in the above queries can not be 0.You can ask at most 3 \cdot n queries.Please note that Khanh fixes the coordinates of his points and does not change it while answering your queries. You do not need to guess the coordinates. In your permutation a_{p_1}a_{p_2}... a_{p_n}, p_1 should be equal to 1 and the indices of vertices should be listed in counter-clockwise order. | Input: ['6', '', '15', '', '-1', '', '1'] Output:['1 1 4 6', '', '2 1 5 6', '', '2 2 1 4', '', '0 1 3 4 2 6 5'] | [
3
] |
This is the harder version of the problem. In this version, 1 <= n <= 10^6 and 0 <=q a_i <=q 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problemsChristmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, ..., a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. | Input: ['3', '4 8 5', ''] Output:['9', ''] | [
2,
3,
4
] |
This is the easier version of the problem. In this version, 1 <= n <= 10^5 and 0 <= a_i <= 1. You can hack this problem only if you solve and lock both problems.Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains a_i chocolate pieces.Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a_1, a_2, ..., a_n is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k > 1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box i-1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. | Input: ['3', '1 0 1', ''] Output:['2', ''] | [
2,
3,
4
] |
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.His farm is presented by a rectangle grid with r rows and c columns. Some of these cells contain rice, others are empty. k chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements: Each cell of the farm is assigned to exactly one chicken. Each chicken is assigned at least one cell. The set of cells assigned to every chicken forms a connected area. More precisely, if two cells (x, y) and (u, v) are assigned to the same chicken, this chicken is able to walk from (x, y) to (u, v) by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him. | Input: ['4', '3 5 3', '..R..', '...R.', '....R', '6 4 6', 'R..R', 'R..R', 'RRRR', 'RRRR', 'R..R', 'R..R', '5 5 4', 'RRR..', 'R.R..', 'RRR..', 'R..R.', 'R...R', '2 31 62', 'RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR', 'RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR', ''] Output:['11122', '22223', '33333', 'aacc', 'aBBc', 'aBBc', 'CbbA', 'CbbA', 'CCAA', '11114', '22244', '32444', '33344', '33334', 'abcdefghijklmnopqrstuvwxyzABCDE', 'FGHIJKLMNOPQRSTUVWXYZ0123456789'] | [
2
] |
You're given a simple, undirected, connected, weighted graph with n nodes and m edges.Nodes are numbered from 1 to n. There are exactly k centrals (recharge points), which are nodes 1, 2, ..., k.We consider a robot moving into this graph, with a battery of capacity c, not fixed by the constructor yet. At any time, the battery contains an integer amount x of energy between 0 and c inclusive.Traversing an edge of weight w_i is possible only if x >= w_i, and costs w_i energy points (x := x - w_i).Moreover, when the robot reaches a central, its battery is entirely recharged (x := c).You're given q independent missions, the i-th mission requires to move the robot from central a_i to central b_i.For each mission, you should tell the minimum capacity required to acheive it. | Input: ['10 9 3 1', '10 9 11', '9 2 37', '2 4 4', '4 1 8', '1 5 2', '5 7 3', '7 3 2', '3 8 4', '8 6 13', '2 3', ''] Output:['12', ''] | [
4
] |
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the (Ox) axis.On this street, there are n antennas, numbered from 1 to n. The i-th antenna lies on the position x_i and has an initial scope of s_i: it covers all integer positions inside the interval [x_i - s_i; x_i + s_i].It is possible to increment the scope of any antenna by 1, this operation costs 1 coin. We can do this operation as much as we want (multiple times on the same antenna if we want).To modernize the street, we need to make all integer positions from 1 to m inclusive covered by at least one antenna. Note that it is authorized to cover positions outside [1; m], even if it's not required.What is the minimum amount of coins needed to achieve this modernization? | Input: ['3 595', '43 2', '300 4', '554 10', ''] Output:['281', ''] | [
2
] |
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n.The graph is considered harmonious if and only if the following property holds: For every triple of integers (l, m, r) such that 1 <= l < m < r <= n, if there exists a path going from node l to node r, then there exists a path going from node l to node m. In other words, in a harmonious graph, if from a node l we can reach a node r through edges (l < r), then we should able to reach nodes (l+1), (l+2), ..., (r-1) too.What is the minimum number of edges we need to add to make the graph harmonious? | Input: ['14 8', '1 2', '2 7', '3 4', '6 3', '5 7', '3 8', '6 8', '11 12', ''] Output:['1', ''] | [
2
] |
Tsumugi brought n delicious sweets to the Light Music Club. They are numbered from 1 to n, where the i-th sweet has a sugar concentration described by an integer a_i.Yui loves sweets, but she can eat at most m sweets each day for health reasons.Days are 1-indexed (numbered 1, 2, 3, ...). Eating the sweet i at the d-th day will cause a sugar penalty of (d \cdot a_i), as sweets become more sugary with time. A sweet can be eaten at most once.The total sugar penalty will be the sum of the individual penalties of each sweet eaten.Suppose that Yui chooses exactly k sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get?Since Yui is an undecided girl, she wants you to answer this question for every value of k between 1 and n. | Input: ['9 2', '6 19 3 4 4 2 6 7 8', ''] Output:['2 5 11 18 30 43 62 83 121', ''] | [
2,
3
] |
The Central Company has an office with a sophisticated security system. There are 10^6 employees, numbered from 1 to 10^6.The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer -i.The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day.Some examples of valid or invalid days: [1, 7, -7, 3, -1, -3] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves). [2, -2, 3, -3] is also a valid day. [2, 5, -5, 5, -5, -2] is not a valid day, because 5 entered the office twice during the same day. [-4, 4] is not a valid day, because 4 left the office without being in it. [4] is not a valid day, because 4 entered the office and didn't leave it before the end of the day. There are n events a_1, a_2, ..., a_n, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.You must partition (to cut) the array a of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.For example, if n=8 and a=[1, -1, 1, 2, -1, -2, 3, -3] then he can partition it into two contiguous subarrays which are valid days: a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3].Help the administrator to partition the given array a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. | Input: ['6', '1 7 -7 3 -1 -3', ''] Output:['1', '6', ''] | [
2
] |
Adding two numbers several times is a time-consuming task, so you want to build a robot. The robot should have a string S = S_1 S_2 ... S_N of N characters on its memory that represents addition instructions. Each character of the string, S_i, is either 'A' or 'B'.You want to be able to give Q commands to the robot, each command is either of the following types: 1 L R. The robot should toggle all the characters of S_i where L <= i <= R. Toggling a character means changing it to 'A' if it was previously 'B', or changing it to 'B' if it was previously 'A'. 2 L R A B. The robot should call f(L, R, A, B) and return two integers as defined in the following pseudocode: function f(L, R, A, B): FOR i from L to R if S[i] = 'A' A = A + B else B = A + B return (A, B) You want to implement the robot's expected behavior. | Input: ['5 3', 'ABAAA', '2 1 5 1 1', '1 3 5', '2 2 5 0 1000000000', ''] Output:['11 3', '0 1000000000', ''] | [
3
] |
Talia has just bought an abandoned house in the outskirt of Jakarta. The house has a nice and long yard which can be represented as a one-dimensional grid containing 1 * N cells. To beautify the house, Talia is going to build a terrace on the yard by tiling the cells. Each cell on the yard contains either soil (represented by the character '.') or rock (represented by the character '#'), and there are at most 50 cells containing rocks.Being a superstitious person, Talia wants to tile the terrace with mystical tiles that have the power to repel ghosts. There are three types of mystical tiles: Type-1: Covers 1 * 1 cell and can only be placed on a soil cell ("."). Type-2: Covers 1 * 2 cells and can only be placed on two consecutive soil cells (".."). Type-3: Covers 1 * 3 cells and can only be placed on consecutive soil-rock-soil cells (".#."). Each tile of Type-1, Type-2, and Type-3 has the power to repel G_1, G_2, and G_3 ghosts per day, respectively. There are also some mystical rules which must be followed for the power to be effective: There should be no overlapping tiles, i.e. each cell is covered by at most one tile. There should be at most K tiles of Type-1, while there are no limitations for tiles of Type-2 and Type-3. Talia is scared of ghosts, thus, the terrace (which is tiled by mystical tiles) should be able to repel as many ghosts as possible. Help Talia to find the maximum number of ghosts that can be repelled per day by the terrace. Note that Talia does not need to tile all the cells on the yard as long as the number of ghosts that can be repelled by the terrace is maximum. | Input: ['6 4 10 25 40', '..#...', ''] Output:['75', ''] | [
0
] |
As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find land to build the buildings.There are N lands available for sale. The i^{th} land has a rectangular shape of size L_i * W_i. For a good feng shui, the building's side should be parallel to the land's sides.One way is to build the two buildings on two different lands, one on each land (not necessarily with the same orientation). A building of size A * B can be build on the i^{th} land if and only if at least one of the following is satisfied: A <= L_i and B <= W_i, or A <= W_i and B <= L_i. Alternatively, it is also possible to build two buildings of A * B on the i^{th} land with the same orientation. Formally, it is possible to build two buildings of A * B on the i^{th} land if and only if at least one of the following is satisfied: A * 2 <= L_i and B <= W_i, or A * 2 <= W_i and B <= L_i, or A <= L_i and B * 2 <= W_i, or A <= W_i and B * 2 <= L_i. Your task in this problem is to help ICPC Jakarta to figure out the largest possible buildings they can build given N available lands. Note that ICPC Jakarta has to build two buildings of A * B; output the largest possible for A * B. | Input: ['2', '5 5', '3 4', ''] Output:['12.5', ''] | [
2
] |
Andi is a mathematician, a computer scientist, and a songwriter. After spending so much time writing songs, he finally writes a catchy melody that he thought as his best creation. However, the singer who will sing the song/melody has a unique vocal range, thus, an adjustment may be needed.A melody is defined as a sequence of N notes which are represented by integers. Let A be the original melody written by Andi. Andi needs to adjust A into a new melody B such that for every i where 1 <= i < N: If A_i < A_{i+1}, then B_i < B_{i+1}. If A_i = A_{i+1}, then B_i = B_{i+1}. If A_i > A_{i+1}, then B_i > B_{i+1}. |B_i - B_{i+1}| <= K, i.e. the difference between two successive notes is no larger than K. Moreover, the singer also requires that all notes are within her vocal range, i.e. L <= B_i <= R for all 1 <= i <= N.Help Andi to determine whether such B exists, and find the lexicographically smallest B if it exists. A melody X is lexicographically smaller than melody Y if and only if there exists j (1 <= j <= N) such that X_i = Y_i for all i < j and X_{j} < Y_{j}.For example, consider a melody A = \{1,3,5,6,7,8,9,10,3,7,8,9,10,11,12,12\} as shown in the following figure. The diagonal arrow up in the figure implies that A_i < A_{i+1}, the straight right arrow implies that A_i = A_{i+1}, and the diagonal arrow down implies that A_i > A_{i+1}. Supposed we want to make a new melody with L = 1, R = 8, and K = 6. The new melody B = \{1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,8\} as shown in the figure satisfies all the requirements, and it is the lexicographically smallest possible. | Input: ['16 1 8 6', '1 3 5 6 7 8 9 10 3 7 8 9 10 11 12 12', ''] Output:['1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 8', ''] | [
2
] |
The only difference between easy and hard versions is constraints.Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free.Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}.Calculate the minimum number of coins you have to spend so that everyone votes for you. | Input: ['3', '3', '1 5', '2 10', '2 8', '7', '0 1', '3 1', '1 1', '6 1', '1 1', '4 1', '4 1', '6', '2 6', '2 3', '2 8', '2 7', '4 4', '5 5', ''] Output:['8', '0', '7', ''] | [
2,
4
] |
The only difference between easy and hard versions is constraints.Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free.Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}.Calculate the minimum number of coins you have to spend so that everyone votes for you. | Input: ['3', '3', '1 5', '2 10', '2 8', '7', '0 1', '3 1', '1 1', '6 1', '1 1', '4 1', '4 1', '6', '2 6', '2 3', '2 8', '2 7', '4 4', '5 5', ''] Output:['8', '0', '7', ''] | [
2
] |
You are the head of a large enterprise. n people work at you, and n is odd (i.βe. n is not divisible by 2).You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from l_i to r_i dollars. You have to distribute salaries in such a way that the median salary is maximum possible.To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence [5, 1, 10, 17, 6] is 6, the median of the sequence [1, 2, 1] is 1. It is guaranteed that you have enough money to pay the minimum salary, i.e l_1 + l_2 + ... + l_n <= s.Note that you don't have to spend all your s dollars on salaries.You have to answer t test cases. | Input: ['3', '3 26', '10 12', '1 4', '10 11', '1 1337', '1 1000000000', '5 26', '4 4', '2 4', '6 8', '5 6', '2 7', ''] Output:['11', '1337', '6', ''] | [
2,
4
] |
You are given a huge integer a consisting of n digits (n is between 1 and 3 \cdot 10^5, inclusive). It may contain leading zeros.You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: 302867235 if you swap the first and the second digits; 023867235 if you swap the second and the third digits; 032876235 if you swap the fifth and the sixth digits; 032862735 if you swap the sixth and the seventh digits; 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity.You can perform any number (possibly, zero) of such operations.Find the minimum integer you can obtain.Note that the resulting integer also may contain leading zeros. | Input: ['3', '0709', '1337', '246432', ''] Output:['0079', '1337', '234642', ''] | [
2
] |
A palindrome is a string t which reads the same backward as forward (formally, t[i] = t[|t| + 1 - i] for all i \in [1, |t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.You have n binary strings s_1, s_2, ..., s_n (each s_i consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings β there are no restrictions.Formally, in one move you: choose four integer numbers x, a, y, b such that 1 <= x, y <= n and 1 <= a <= |s_x| and 1 <= b <= |s_y| (where x and y are string indices and a and b are positions in strings s_x and s_y respectively), swap (exchange) the characters s_x[a] and s_y[b]. What is the maximum number of strings you can make palindromic simultaneously? | Input: ['4', '1', '0', '3', '1110', '100110', '010101', '2', '11111', '000001', '2', '001', '11100111', ''] Output:['1', '2', '2', '2', ''] | [
2
] |
Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains 26 buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string s appeared on the screen. When Polycarp presses a button with character c, one of the following events happened: if the button was working correctly, a character c appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters c appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a \rightarrow abb \rightarrow abba \rightarrow abbac \rightarrow abbaca \rightarrow abbacabb \rightarrow abbacabba.You are given a string s which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process. | Input: ['4', 'a', 'zzaaz', 'ccff', 'cbddbb', ''] Output:['a', 'z', '', 'bc', ''] | [
0
] |
Polycarpus has a complex electronic device. The core of this device is a circuit board. The board has 10^9 contact points which are numbered from 1 to 10^9. Also there are n wires numbered from 1 to n, each connecting two distinct contact points on the board. An electric signal can pass between wires A and B if: either both wires share the same contact point; or there is a sequence of wires starting with A and ending with B, and each pair of adjacent wires in the sequence share a contact point. The picture shows a circuit board with 5 wires. Contact points with numbers 2, 5, 7, 8, 10, 13 are used. Here an electrical signal can pass from wire 2 to wire 3, but not to wire 1. Currently the circuit board is broken. Polycarpus thinks that the board could be fixed if the wires were re-soldered so that a signal could pass between any pair of wires.It takes 1 minute for Polycarpus to re-solder an end of a wire. I.e. it takes one minute to change one of the two contact points for a wire. Any contact point from range [1, 10^9] can be used as a new contact point. A wire's ends must always be soldered to distinct contact points. Both wire's ends can be re-solded, but that will require two actions and will take 2 minutes in total.Find the minimum amount of time Polycarpus needs to re-solder wires so that a signal can pass between any pair of wires. Also output an optimal sequence of wire re-soldering. | Input: ['2', '1', '4 7', '4', '1 2', '2 3', '4 5', '5 6', ''] Output:['0', '1', '2 3 5', ''] | [
2
] |
A group of students has recently been admitted to the Faculty of Computer Sciences at the Berland State University. Now the programming teacher wants to divide them into three subgroups for practice sessions.The teacher knows that a lot of programmers argue which language is the best. The teacher doesn't want to hear any arguments in the subgroups, so she wants to divide the students into three subgroups so that no pair of students belonging to the same subgroup want to argue.To perform this division, the teacher asked each student which programming language he likes. There are a students who answered that they enjoy Assembler, b students stated that their favourite language is Basic, and c remaining students claimed that C++ is the best programming language β and there was a large argument between Assembler fans and C++ fans.Now, knowing that Assembler programmers and C++ programmers can start an argument every minute, the teacher wants to divide the students into three subgroups so that every student belongs to exactly one subgroup, and there is no subgroup that contains at least one Assembler fan and at least one C++ fan. Since teaching a lot of students can be difficult, the teacher wants the size of the largest subgroup to be minimum possible.Please help the teacher to calculate the minimum possible size of the largest subgroup! | Input: ['5', '3 5 7', '4 8 4', '13 10 13', '1000 1000 1000', '13 22 7', ''] Output:['5', '6', '13', '1000', '14', ''] | [
2,
3,
4
] |
The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into k rows, and all rows will contain the same number of soldiers.Of course, not every arrangement of soldiers into k rows is suitable. Heights of all soldiers in the same row should not differ by more than 1. The height of each soldier is an integer between 1 and n.For each possible height, you know the number of soldiers having this height. To conduct a parade, you have to choose the soldiers participating in it, and then arrange all of the chosen soldiers into k rows so that both of the following conditions are met: each row has the same number of soldiers, no row contains a pair of soldiers such that their heights differ by 2 or more. Calculate the maximum number of soldiers who can participate in the parade. | Input: ['5', '3 4', '7 1 13', '1 1', '100', '1 3', '100', '2 1', '1000000000000 1000000000000', '4 1', '10 2 11 1', ''] Output:['16', '100', '99', '2000000000000', '13', ''] | [
2,
4
] |
The director of the famous dance show plans a tour. It is already decided that the tour will consist of up to m concerts.There are n dancers in the troupe. Each dancer is characterized by her awkwardness: the awkwardness of the i-th dancer is equal to a_i.The director likes diversity. For this reason, each concert will be performed by a different set of dancers. A dancer may perform in multiple concerts. For example, it is possible that a set of dancers performs in one concert and a subset of this set of dancers performs in another concert. The only constraint is that the same set of dancers cannot perform twice.The director prefers the set with larger number of dancers over the set with smaller number of dancers. If two sets consist of the same number of dancers, then the director prefers the one which has smaller sum of awkwardness of dancers. If two sets of dancers are equal in size and total awkwardness, then the director does not have a preference which one is better.A marketing study shows that viewers are not ready to come to a concert if the total awkwardness of all the dancers performing in the concert is greater than k.The director wants to find the best plan for m concerts. He thinks to write down all possible sets of dancers; then get rid of the sets with total awkwardness greater than k. The remaining sets of dancers will be sorted according to his preference. The most preferred set of dancers will give the first concert, the second preferred set β the second concert and so on until the m-th concert. If it turns out that the total number of valid sets is less than m, then the total number of concerts will be equal to the number of valid sets.It turns out that the director delegated finding the plan to you! Please, notice that there might be several acceptable plans due to the fact that the director does not have a preference over sets of dancers with the same size and total awkwardness. In this case any of these plans is good enough. For each concert find the number of dancers and the total awkwardness of the set performing. Also, for the last concert find its set of dancers. | Input: ['3', '7 13 10', '3 1 5 1 8 2 13', '2 10 1', '12 12', '3 32 100000', '2 1 5', ''] Output:['10', '5 12', '4 7', '4 9', '4 10', '4 11', '4 11', '4 12', '4 13', '3 4', '3 5', '2 4 1 ', '0', '7', '3 8', '2 3', '2 6', '2 7', '1 1', '1 2', '1 5', '3 ', ''] | [
0,
2,
4
] |
You have a set of birthday cake candles. Each of such candles represents a digit between 0 and 9, inclusive. Example of birthday cake candles. Let's denote the candle representing the digit d as d-candle.Your set contains c_0 instances of 0-candles, c_1 instances of 1-candles and so on. So, the total number of candles is c_0+c_1+...+c_9.These digits are needed to wish your cat a happy birthday. For each birthday, starting with the first, you want to compose the age of the cat using the digits from the set.Since you light candles for a very short time, candles don't have time to burn out. For this reason you can reuse candles an arbitrary number of times (therefore your set of candles never changes).For example, if you have one instance of each digit (i.e. c_0=c_1=...=c_9=1), you can compose any number from 1 to 10 using this set, but you cannot compose 11.You have to determine the first birthday, on which you cannot compose the age of the cat using the candles from your set. In other words, find the minimum number y such that all numbers from 1 to y-1 can be composed by digits from your set, but y cannot be composed. | Input: ['4', '1 1 1 1 1 1 1 1 1 1', '0 0 1 1 2 2 3 3 4 4', '1 2 1 2 1 3 1 0 0 0', '0 1 2 1 4 3 1 1 2 1', ''] Output:['11', '1', '7', '10', ''] | [
3
] |
Eulampius has created a game with the following rules: there are two players in the game: a human and a computer; the game lasts for no more than n rounds. Initially both players have 0 points. In the j-th round the human gains a_j points, and the computer gains b_j points. The points are gained simultaneously; the game ends when one of the players gets k or more points. This player loses the game. If both players get k or more points simultaneously, both lose; if both players have less than k points after n rounds, the game ends in a tie; after each round the human can push the "Reset" button. If the human had x points, and the computer had y points before the button is pushed (of course, x < k and y < k), then after the button is pushed the human will have x' = max(0, \, x - y) points, and the computer will have y' = max(0, \, y - x) points. E.βg. the push of "Reset" button transforms the state (x=3, \, y=5) into the state (x'=0, \, y'=2), and the state (x=8, \, y=2) into the state (x'=6, \, y'=0).Eulampius asked his friend Polycarpus to test the game. Polycarpus has quickly revealed that amounts of points gained by the human and the computer in each of n rounds are generated before the game and stored in a file. In other words, the pushes of the "Reset" button do not influence the values a_j and b_j, so sequences a and b are fixed and known in advance.Polycarpus wants to make a plan for the game. He would like to win the game pushing the "Reset" button as few times as possible. Your task is to determine this minimal number of pushes or determine that Polycarpus cannot win. | Input: ['3', '4 17', '1 3 5 7', '3 5 7 9', '11 17', '5 2 8 2 4 6 1 2 7 2 5', '4 6 3 3 5 1 7 4 2 5 3', '6 17', '6 1 2 7 2 5', '1 7 4 2 5 3', ''] Output:['0', '', '2', '2 4', '-1'] | [
2
] |
You are developing a project to build a new data center. The data center will be a rectangle with an area of exactly n square meters. Each side of the data center must be an integer.Your goal is to minimize the impact of the external environment on the data center. For this reason, you want to minimize the length of the perimeter of the data center (that is, the sum of the lengths of its four sides).What is the minimum perimeter of a rectangular data center with an area of exactly n square meters, if the lengths of all its sides must be integers? | Input: ['36', ''] Output:['24', ''] | [
0
] |
Employees of JebTrains are on their way to celebrate the 256-th day of the year! There are n employees and k teams in JebTrains. Each employee is a member of some (exactly one) team. All teams are numbered from 1 to k. You are given an array of numbers t_1, t_2, ..., t_n where t_i is the i-th employee's team number.JebTrains is going to rent a single bus to get employees to the feast. The bus will take one or more rides. A bus can pick up an entire team or two entire teams. If three or more teams take a ride together they may start a new project which is considered unacceptable. It's prohibited to split a team, so all members of a team should take the same ride.It is possible to rent a bus of any capacity s. Such a bus can take up to s people on a single ride. The total cost of the rent is equal to s \cdot r burles where r is the number of rides. Note that it's impossible to rent two or more buses.Help JebTrains to calculate the minimum cost of the rent, required to get all employees to the feast, fulfilling all the conditions above. | Input: ['6 3', '3 1 2 3 2 3', ''] Output:['6', ''] | [
0,
2,
3
] |
The only difference between easy and hard versions is constraints.You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The i-th segment is [l_i; r_i] (l_i <= r_i) and it covers all integer points j such that l_i <= j <= r_i.The integer point is called bad if it is covered by strictly more than k segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | Input: ['7 2', '11 11', '9 11', '7 8', '8 9', '7 8', '9 11', '7 9', ''] Output:['3', '4 6 7 ', ''] | [
2
] |
The only difference between easy and hard versions is constraints.You are given n segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The i-th segment is [l_i; r_i] (l_i <= r_i) and it covers all integer points j such that l_i <= j <= r_i.The integer point is called bad if it is covered by strictly more than k segments.Your task is to remove the minimum number of segments so that there are no bad points at all. | Input: ['7 2', '11 11', '9 11', '7 8', '8 9', '7 8', '9 11', '7 9', ''] Output:['3', '1 4 7 ', ''] | [
2
] |
The only difference between easy and hard versions is the maximum value of n.You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).For example: 30 is a good number: 30 = 3^3 + 3^1, 1 is a good number: 1 = 3^0, 12 is a good number: 12 = 3^2 + 3^1, but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0), 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid), 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid). Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.For the given positive integer n find such smallest m (n <= m) that m is a good number.You have to answer q independent queries. | Input: ['8', '1', '2', '6', '13', '14', '3620', '10000', '1000000000000000000', ''] Output:['1', '3', '9', '13', '27', '6561', '19683', '1350851717672992089', ''] | [
2,
3,
4
] |
The only difference between easy and hard versions is the maximum value of n.You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).For example: 30 is a good number: 30 = 3^3 + 3^1, 1 is a good number: 1 = 3^0, 12 is a good number: 12 = 3^2 + 3^1, but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0), 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid), 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid). Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.For the given positive integer n find such smallest m (n <= m) that m is a good number.You have to answer q independent queries. | Input: ['7', '1', '2', '6', '13', '14', '3620', '10000', ''] Output:['1', '3', '9', '13', '27', '6561', '19683', ''] | [
0,
2
] |
The only difference between easy and hard versions is constraints.There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed.For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on.Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n.Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: after the 1-st day it will belong to the 5-th kid, after the 2-nd day it will belong to the 3-rd kid, after the 3-rd day it will belong to the 2-nd kid, after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer q independent queries. | Input: ['6', '5', '1 2 3 4 5', '3', '2 3 1', '6', '4 6 2 1 5 3', '1', '1', '4', '3 4 1 2', '5', '5 1 2 4 3', ''] Output:['1 1 1 1 1 ', '3 3 3 ', '2 3 3 2 1 3 ', '1 ', '2 2 2 2 ', '4 4 4 1 4 ', ''] | [
3
] |
The only difference between easy and hard versions is constraints.There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed.For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on.Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n.Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids: after the 1-st day it will belong to the 5-th kid, after the 2-nd day it will belong to the 3-rd kid, after the 3-rd day it will belong to the 2-nd kid, after the 4-th day it will belong to the 1-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer q independent queries. | Input: ['6', '5', '1 2 3 4 5', '3', '2 3 1', '6', '4 6 2 1 5 3', '1', '1', '4', '3 4 1 2', '5', '5 1 2 4 3', ''] Output:['1 1 1 1 1 ', '3 3 3 ', '2 3 3 2 1 3 ', '1 ', '2 2 2 2 ', '4 4 4 1 4 ', ''] | [
3
] |
You are a coach of a group consisting of n students. The i-th student has programming skill a_i. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students i and j such that |a_i - a_j| = 1 belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than 1); the number of teams is the minimum possible. You have to answer q independent queries. | Input: ['4', '4', '2 10 1 20', '2', '3 6', '5', '2 3 4 99 100', '1', '42', ''] Output:['2', '1', '2', '1', ''] | [
3
] |
This is an easier version of the problem. In this version, n <= 500.Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.We remind that bracket sequence s is called correct if: s is empty; s is equal to "(t)", where t is correct bracket sequence; s is equal to t_1 t_2, i.e. concatenation of t_1 and t_2, where t_1 and t_2 are correct bracket sequences. For example, "(()())", "()" are correct, while ")(" and "())" are not.The cyclical shift of the string s of length n by k (0 <=q k < n) is a string formed by a concatenation of the last k symbols of the string s with the first n - k symbols of string s. For example, the cyclical shift of string "(())()" by 2 equals "()(())".Cyclical shifts i and j are considered different, if i!=j. | Input: ['10', '()()())(()', ''] Output:['5', '8 7', ''] | [
0,
2
] |
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0, 0). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to OX or OY axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.Alexey wants to make a polyline in such a way that its end is as far as possible from (0, 0). Please help him to grow the tree this way.Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches. | Input: ['3', '1 2 3', ''] Output:['26'] | [
2,
3
] |
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, ..., p_n.Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, ..., q_m.DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help. | Input: ['3', '3', '1 3 2', '2', '0 3', '1', '1', '1', '1', '1', '2', '1', '1', ''] Output:['3', '1', '0', ''] | [
3
] |
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute 1 + 3 using the calculator, he gets 2 instead of 4. But when he tries computing 1 + 4, he gets the correct answer, 5. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders! So, when he tries to compute the sum a + b using the calculator, he instead gets the xorsum a \oplus b (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers l and r, how many pairs of integers (a, b) satisfy the following conditions: a + b = a \oplus b l <=q a <=q r l <=q b <=q rHowever, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked. | Input: ['3', '1 4', '323 323', '1 1000000', ''] Output:['8', '0', '3439863766', ''] | [
0
] |
Shichikuji is the new resident deity of the South Black Snail Temple. Her first job is as follows:There are n new cities located in Prefecture X. Cities are numbered from 1 to n. City i is located x_i km North of the shrine and y_i km East of the shrine. It is possible that (x_i, y_i) = (x_j, y_j) even when i!=j.Shichikuji must provide electricity to each city either by building a power station in that city, or by making a connection between that city and another one that already has electricity. So the City has electricity if it has a power station in it or it is connected to a City which has electricity by a direct connection or via a chain of connections. Building a power station in City i will cost c_i yen; Making a connection between City i and City j will cost k_i + k_j yen per km of wire used for the connection. However, wires can only go the cardinal directions (North, South, East, West). Wires can cross each other. Each wire must have both of its endpoints in some cities. If City i and City j are connected by a wire, the wire will go through any shortest path from City i to City j. Thus, the length of the wire if City i and City j are connected is |x_i - x_j| + |y_i - y_j| km. Shichikuji wants to do this job spending as little money as possible, since according to her, there isn't really anything else in the world other than money. However, she died when she was only in fifth grade so she is not smart enough for this. And thus, the new resident deity asks for your help.And so, you have to provide Shichikuji with the following information: minimum amount of yen needed to provide electricity to all cities, the cities in which power stations will be built, and the connections to be made.If there are multiple ways to choose the cities and the connections to obtain the construction of minimum price, then print any of them. | Input: ['3', '2 3', '1 1', '3 2', '3 2 3', '3 2 3', ''] Output:['8', '3', '1 2 3 ', '0', ''] | [
2
] |
Let n be a positive integer. Let a, b, c be nonnegative integers such that a + b + c = n.Alice and Bob are gonna play rock-paper-scissors n times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock a times, paper b times, and scissors c times.Alice wins if she beats Bob in at least \lceil \frac{n}{2} \rceil (\frac{n}{2} rounded up to the nearest integer) hands, otherwise Alice loses.Note that in rock-paper-scissors: rock beats scissors; paper beats rock; scissors beat paper. The task is, given the sequence of hands that Bob will play, and the numbers a, b, c, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.If there are multiple answers, print any of them. | Input: ['2', '3', '1 1 1', 'RPS', '3', '3 0 0', 'RPS', ''] Output:['YES', 'PSR', 'NO', ''] | [
2
] |
Consider the set of all nonnegative integers: {0, 1, 2, ...}. Given two integers a and b (1 <= a, b <= 10^4). We paint all the numbers in increasing number first we paint 0, then we paint 1, then 2 and so on.Each number is painted white or black. We paint a number i according to the following rules: if i = 0, it is colored white; if i >= a and i - a is colored white, i is also colored white; if i >= b and i - b is colored white, i is also colored white; if i is still not colored white, it is colored black. In this way, each nonnegative integer gets one of two colors.For example, if a=3, b=5, then the colors of the numbers (in the order from 0) are: white (0), black (1), black (2), white (3), black (4), white (5), white (6), black (7), white (8), white (9), ...Note that: It is possible that there are infinitely many nonnegative integers colored black. For example, if a = 10 and b = 10, then only 0, 10, 20, 30 and any other nonnegative integers that end in 0 when written in base 10 are white. The other integers are colored black. It is also possible that there are only finitely many nonnegative integers colored black. For example, when a = 1 and b = 10, then there is no nonnegative integer colored black at all. Your task is to determine whether or not the number of nonnegative integers colored black is infinite.If there are infinitely many nonnegative integers colored black, simply print a line containing "Infinite" (without the quotes). Otherwise, print "Finite" (without the quotes). | Input: ['4', '10 10', '1 10', '6 9', '7 3', ''] Output:['Infinite', 'Finite', 'Infinite', 'Finite', ''] | [
3
] |
Demonstrative competitions will be held in the run-up to the 20NN Berlatov Olympic Games. Today is the day for the running competition!Berlatov team consists of 2n runners which are placed on two running tracks; n runners are placed on each track. The runners are numbered from 1 to n on each track. The runner with number i runs through the entire track in i seconds.The competition is held as follows: first runners on both tracks start running at the same time; when the slower of them arrives at the end of the track, second runners on both tracks start running, and everyone waits until the slower of them finishes running, and so on, until all n pairs run through the track.The organizers want the run to be as long as possible, but if it lasts for more than k seconds, the crowd will get bored. As the coach of the team, you may choose any order in which the runners are arranged on each track (but you can't change the number of runners on each track or swap runners between different tracks).You have to choose the order of runners on each track so that the duration of the competition is as long as possible, but does not exceed k seconds.Formally, you want to find two permutations p and q (both consisting of n elements) such that sum = \sum\limits_{i=1}^{n} max(p_i, q_i) is maximum possible, but does not exceed k. If there is no such pair, report about it. | Input: ['5 20', ''] Output:['20', '1 2 3 4 5 ', '5 2 4 3 1 ', ''] | [
2,
3
] |
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times. | Input: ['4 5', '3 1 7 5', ''] Output:['2', ''] | [
2,
4,
4
] |
You are given a tree consisting of n vertices. A tree is an undirected connected acyclic graph. Example of a tree. You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples (x, y, z) such that x \neq y, y \neq z, x \neq z, x is connected by an edge with y, and y is connected by an edge with z. The colours of x, y and z should be pairwise distinct. Let's call a painting which meets this condition good.You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it. | Input: ['3', '3 2 3', '4 3 2', '3 1 3', '1 2', '2 3', ''] Output:['6', '1 3 2 ', ''] | [
0
] |
The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets w points, and the opposing team gets 0 points. If the game results in a draw, both teams get d points.The manager of the Berland capital team wants to summarize the results of the season, but, unfortunately, all information about the results of each match is lost. The manager only knows that the team has played n games and got p points for them.You have to determine three integers x, y and z β the number of wins, draws and loses of the team. If there are multiple answers, print any of them. If there is no suitable triple (x, y, z), report about it. | Input: ['30 60 3 1', ''] Output:['17 9 4', ''] | [
0,
3
] |
Nikolay lives in a two-storied house. There are n rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between 1 and n). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers i and i+1 on each floor are neighbouring, for all 1 <=q i <=q n - 1. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room x on the first floor and the room x on the second floor, then Nikolay can use it to move from one room to another. The picture illustrates a house with n = 4. There is a staircase between the room 2 on the first floor and the room 2 on the second floor, and another staircase between the room 4 on the first floor and the room 4 on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string "0101" in the input. Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if: he can start in any room on any floor of his choice, and he won't visit the same room twice. | Input: ['4', '5', '00100', '8', '00000000', '5', '11111', '3', '110', ''] Output:['6', '8', '10', '6', ''] | [
0
] |
Tomorrow is a difficult day for Polycarp: he has to attend a lectures and b practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them.While preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down c lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during d practical classes, after which it is unusable.Polycarp's pencilcase can hold no more than k writing implements, so if Polycarp wants to take x pens and y pencils, they will fit in the pencilcase if and only if x + y <= k.Now Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow!Note that you don't have to minimize the number of writing implements (though their total number must not exceed k). | Input: ['3', '7 5 4 5 8', '7 5 4 5 2', '20 53 45 26 4', ''] Output:['7 1', '-1', '1 3', ''] | [
3
] |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.