question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
You are given a tree (connected graph without cycles) consisting of n vertices. The tree is unrooted β it is just a connected undirected graph without cycles.In one move, you can choose exactly k leaves (leaf is such a vertex that is connected to only one another vertex) connected to the same vertex and remove them with edges incident to them. I.e. you choose such leaves u_1, u_2, ..., u_k that there are edges (u_1, v), (u_2, v), ..., (u_k, v) and remove these leaves and these edges.Your task is to find the maximum number of moves you can perform if you remove leaves optimally.You have to answer t independent test cases. | Input: ['4', '8 3', '1 2', '1 5', '7 6', '6 8', '3 1', '6 4', '6 1', '10 3', '1 2', '1 10', '2 3', '1 5', '1 6', '2 4', '7 10', '10 9', '8 10', '7 2', '3 1', '4 5', '3 6', '7 4', '1 2', '1 4', '5 1', '1 2', '2 3', '4 3', '5 3', ''] Output:['2', '3', '3', '4', ''] | [
2
] |
You are given a string s[1 ... n] consisting of lowercase Latin letters. It is guaranteed that n = 2^k for some integer k >= 0.The string s[1 ... n] is called c-good if at least one of the following three conditions is satisfied: The length of s is 1, and it consists of the character c (i.e. s_1=c); The length of s is greater than 1, the first half of the string consists of only the character c (i.e. s_1=s_2=...=s_{\frac{n}{2}}=c) and the second half of the string (i.e. the string s_{\frac{n}{2} + 1}s_{\frac{n}{2} + 2} ... s_n) is a (c+1)-good string; The length of s is greater than 1, the second half of the string consists of only the character c (i.e. s_{\frac{n}{2} + 1}=s_{\frac{n}{2} + 2}=...=s_n=c) and the first half of the string (i.e. the string s_1s_2 ... s_{\frac{n}{2}}) is a (c+1)-good string. For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good.In one move, you can choose one index i from 1 to n and replace s_i with any lowercase Latin letter (any character from 'a' to 'z').Your task is to find the minimum number of moves required to obtain an 'a'-good string from s (i.e. c-good string for c= 'a'). It is guaranteed that the answer always exists.You have to answer t independent test cases.Another example of an 'a'-good string is as follows. Consider the string s = "cdbbaaaa". It is an 'a'-good string, because: the second half of the string ("aaaa") consists of only the character 'a'; the first half of the string ("cdbb") is 'b'-good string, because: the second half of the string ("bb") consists of only the character 'b'; the first half of the string ("cd") is 'c'-good string, because: the first half of the string ("c") consists of only the character 'c'; the second half of the string ("d") is 'd'-good string. | Input: ['6', '8', 'bbdcaaaa', '8', 'asdfghjk', '8', 'ceaaaabb', '8', 'bbaaddcc', '1', 'z', '2', 'ac', ''] Output:['0', '7', '4', '5', '1', '1', ''] | [
0
] |
You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a=[a_1, a_2, ..., a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [a_1, a_2, ..., a_k] (0 <= k <= n).The array b of length m is called good, if you can obtain a non-decreasing array c (c_1 <= c_2 <= ... <= c_{m}) from it, repeating the following operation m times (initially, c is empty): select either the first or the last element of b, remove it from b, and append it to the end of the array c. For example, if we do 4 operations: take b_1, then b_{m}, then b_{m-1} and at last b_2, then b becomes [b_3, b_4, ..., b_{m-3}] and c =[b_1, b_{m}, b_{m-1}, b_2].Consider the following example: b = [1, 2, 3, 4, 4, 2, 1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations: take the first element of b, so b = [2, 3, 4, 4, 2, 1], c = [1]; take the last element of b, so b = [2, 3, 4, 4, 2], c = [1, 1]; take the last element of b, so b = [2, 3, 4, 4], c = [1, 1, 2]; take the first element of b, so b = [3, 4, 4], c = [1, 1, 2, 2]; take the first element of b, so b = [4, 4], c = [1, 1, 2, 2, 3]; take the last element of b, so b = [4], c = [1, 1, 2, 2, 3, 4]; take the only element of b, so b = [], c = [1, 1, 2, 2, 3, 4, 4] β c is non-decreasing. Note that the array consisting of one element is good.Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.You have to answer t independent test cases. | Input: ['5', '4', '1 2 3 4', '7', '4 3 3 8 4 5 2', '3', '1 1 1', '7', '1 3 1 4 5 3 2', '5', '5 4 3 2 3', ''] Output:['0', '4', '0', '2', '3', ''] | [
2
] |
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2], [3, 2, 1] are permutations, and [1, 1], [0, 1], [2, 2, 1, 4] are not.There was a permutation p[1 ... n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.For example, if p=[3, 1, 2] some possible results are: [3, 1, 2, 3, 1, 2], [3, 3, 1, 1, 2, 2], [3, 1, 3, 1, 2, 2]. The following sequences are not possible results of a merging: [1, 3, 2, 1, 2, 3], [3, 1, 2, 3, 2, 1], [3, 3, 1, 2, 2, 1].For example, if p=[2, 1] the possible results are: [2, 2, 1, 1], [2, 1, 2, 1]. The following sequences are not possible results of a merging: [1, 1, 2, 2], [2, 1, 1, 2], [1, 2, 2, 1].Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.You have to answer t independent test cases. | Input: ['5', '2', '1 1 2 2', '4', '1 3 1 4 3 4 2 2', '5', '1 2 1 2 3 4 3 5 4 5', '3', '1 2 3 1 2 3', '4', '2 3 2 4 1 3 4 1', ''] Output:['1 2 ', '1 3 4 2 ', '1 2 3 4 5 ', '1 2 3 ', '2 3 4 1 ', ''] | [
2
] |
You are given three positive (i.e. strictly greater than zero) integers x, y and z.Your task is to find positive integers a, b and c such that x = \max(a, b), y = \max(a, c) and z = \max(b, c), or determine that it is impossible to find such a, b and c.You have to answer t independent test cases. Print required a, b and c in any (arbitrary) order. | Input: ['5', '3 2 3', '100 100 100', '50 49 49', '10 30 20', '1 1000000000 1000000000', ''] Output:['YES', '3 2 1', 'YES', '100 100 100', 'NO', 'NO', 'YES', '1 1 1000000000', ''] | [
3
] |
The only difference between easy and hard versions is on constraints. In this version constraints are higher. You can make hacks only if all versions of the problem are solved.Koa the Koala is at the beach!The beach consists (from left to right) of a shore, n+1 meters of sea and an island at n+1 meters from the shore.She measured the depth of the sea at 1, 2, ..., n meters from the shore and saved them in array d. d_i denotes the depth of the sea at i meters from the shore for 1 <= i <= n.Like any beach this one has tide, the intensity of the tide is measured by parameter k and affects all depths from the beginning at time t=0 in the following way: For a total of k seconds, each second, tide increases all depths by 1. Then, for a total of k seconds, each second, tide decreases all depths by 1. This process repeats again and again (ie. depths increase for k seconds then decrease for k seconds and so on ...).Formally, let's define 0-indexed array p = [0, 1, 2, ..., k - 2, k - 1, k, k - 1, k - 2, ..., 2, 1] of length 2k. At time t (0 <= t) depth at i meters from the shore equals d_i + p[t \bmod 2k] (t \bmod 2k denotes the remainder of the division of t by 2k). Note that the changes occur instantaneously after each second, see the notes for better understanding. At time t=0 Koa is standing at the shore and wants to get to the island. Suppose that at some time t (0 <= t) she is at x (0 <= x <= n) meters from the shore: In one second Koa can swim 1 meter further from the shore (x changes to x+1) or not swim at all (x stays the same), in both cases t changes to t+1. As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed l at integer points of time (or she will drown). More formally, if Koa is at x (1 <= x <= n) meters from the shore at the moment t (for some integer t>= 0), the depth of the sea at this point β d_x + p[t \bmod 2k] β can't exceed l. In other words, d_x + p[t \bmod 2k] <= l must hold always. Once Koa reaches the island at n+1 meters from the shore, she stops and can rest.Note that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there). Koa wants to know whether she can go from the shore to the island. Help her! | Input: ['7', '2 1 1', '1 0', '5 2 3', '1 2 3 2 2', '4 3 4', '0 2 4 3', '2 3 5', '3 0', '7 2 3', '3 0 2 1 3 0 1', '7 1 4', '4 4 3 0 2 4 2', '5 2 3', '1 2 3 2 2', ''] Output:['Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'No', ''] | [
2
] |
The only difference between easy and hard versions is on constraints. In this version constraints are lower. You can make hacks only if all versions of the problem are solved.Koa the Koala is at the beach!The beach consists (from left to right) of a shore, n+1 meters of sea and an island at n+1 meters from the shore.She measured the depth of the sea at 1, 2, ..., n meters from the shore and saved them in array d. d_i denotes the depth of the sea at i meters from the shore for 1 <= i <= n.Like any beach this one has tide, the intensity of the tide is measured by parameter k and affects all depths from the beginning at time t=0 in the following way: For a total of k seconds, each second, tide increases all depths by 1. Then, for a total of k seconds, each second, tide decreases all depths by 1. This process repeats again and again (ie. depths increase for k seconds then decrease for k seconds and so on ...).Formally, let's define 0-indexed array p = [0, 1, 2, ..., k - 2, k - 1, k, k - 1, k - 2, ..., 2, 1] of length 2k. At time t (0 <= t) depth at i meters from the shore equals d_i + p[t \bmod 2k] (t \bmod 2k denotes the remainder of the division of t by 2k). Note that the changes occur instantaneously after each second, see the notes for better understanding. At time t=0 Koa is standing at the shore and wants to get to the island. Suppose that at some time t (0 <= t) she is at x (0 <= x <= n) meters from the shore: In one second Koa can swim 1 meter further from the shore (x changes to x+1) or not swim at all (x stays the same), in both cases t changes to t+1. As Koa is a bad swimmer, the depth of the sea at the point where she is can't exceed l at integer points of time (or she will drown). More formally, if Koa is at x (1 <= x <= n) meters from the shore at the moment t (for some integer t>= 0), the depth of the sea at this point β d_x + p[t \bmod 2k] β can't exceed l. In other words, d_x + p[t \bmod 2k] <= l must hold always. Once Koa reaches the island at n+1 meters from the shore, she stops and can rest.Note that while Koa swims tide doesn't have effect on her (ie. she can't drown while swimming). Note that Koa can choose to stay on the shore for as long as she needs and neither the shore or the island are affected by the tide (they are solid ground and she won't drown there). Koa wants to know whether she can go from the shore to the island. Help her! | Input: ['7', '2 1 1', '1 0', '5 2 3', '1 2 3 2 2', '4 3 4', '0 2 4 3', '2 3 5', '3 0', '7 2 3', '3 0 2 1 3 0 1', '7 1 4', '4 4 3 0 2 4 2', '5 2 3', '1 2 3 2 2', ''] Output:['Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'No', ''] | [
0,
2
] |
The length of the longest common prefix of two strings s = s_1 s_2 ... s_n and t = t_1 t_2 ... t_m is defined as the maximum integer k (0 <= k <= min(n,m)) such that s_1 s_2 ... s_k equals t_1 t_2 ... t_k.Koa the Koala initially has n+1 strings s_1, s_2, ..., s_{n+1}.For each i (1 <= i <= n) she calculated a_i β the length of the longest common prefix of s_i and s_{i+1}.Several days later Koa found these numbers, but she couldn't remember the strings.So Koa would like to find some strings s_1, s_2, ..., s_{n+1} which would have generated numbers a_1, a_2, ..., a_n. Can you help her?If there are many answers print any. We can show that answer always exists for the given constraints. | Input: ['4', '4', '1 2 4 2', '2', '5 3', '3', '1 3 1', '3', '0 0 0', ''] Output:['aeren', 'ari', 'arousal', 'around', 'ari', 'monogon', 'monogamy', 'monthly', 'kevinvu', 'kuroni', 'kurioni', 'korone', 'anton', 'loves', 'adhoc', 'problems'] | [
2
] |
Koa the Koala has a matrix A of n rows and m columns. Elements of this matrix are distinct integers from 1 to n \cdot m (each number from 1 to n \cdot m appears exactly once in the matrix).For any matrix M of n rows and m columns let's define the following: The i-th row of M is defined as R_i(M) = [ M_{i1}, M_{i2}, ..., M_{im} ] for all i (1 <= i <= n). The j-th column of M is defined as C_j(M) = [ M_{1j}, M_{2j}, ..., M_{nj} ] for all j (1 <= j <= m). Koa defines S(A) = (X, Y) as the spectrum of A, where X is the set of the maximum values in rows of A and Y is the set of the maximum values in columns of A.More formally: X = \{ \max(R_1(A)), \max(R_2(A)), ..., \max(R_n(A)) \} Y = \{ \max(C_1(A)), \max(C_2(A)), ..., \max(C_m(A)) \}Koa asks you to find some matrix A' of n rows and m columns, such that each number from 1 to n \cdot m appears exactly once in the matrix, and the following conditions hold: S(A') = S(A) R_i(A') is bitonic for all i (1 <= i <= n) C_j(A') is bitonic for all j (1 <= j <= m) An array t (t_1, t_2, ..., t_k) is called bitonic if it first increases and then decreases. More formally: t is bitonic if there exists some position p (1 <= p <= k) such that: t_1 < t_2 < ... < t_p > t_{p+1} > ... > t_k.Help Koa to find such matrix or to determine that it doesn't exist. | Input: ['3 3', '3 5 6', '1 7 9', '4 8 2', ''] Output:['9 5 1', '7 8 2', '3 6 4', ''] | [
0,
2
] |
Koa the Koala and her best friend want to play a game.The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.Let's describe a move in the game: During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player. More formally: if the current score of the player is x and the chosen element is y, his new score will be x \oplus y. Here \oplus denotes bitwise XOR operation. Note that after a move element y is removed from a. The game ends when the array is empty. At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.If both players play optimally find out whether Koa will win, lose or draw the game. | Input: ['3', '3', '1 2 2', '3', '2 2 3', '5', '0 0 0 2 2', ''] Output:['WIN', 'LOSE', 'DRAW', ''] | [
2,
3
] |
Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter y Koa selects must be strictly greater alphabetically than x (read statement for better understanding). You can make hacks in these problems independently.Koa the Koala has two strings A and B of the same length n (|A|=|B|=n) consisting of the first 20 lowercase English alphabet letters (ie. from a to t).In one move Koa: selects some subset of positions p_1, p_2, ..., p_k (k >= 1; 1 <= p_i <= n; p_i \neq p_j if i \neq j) of A such that A_{p_1} = A_{p_2} = ... = A_{p_k} = x (ie. all letters on this positions are equal to some letter x). selects a letter y (from the first 20 lowercase letters in English alphabet) such that y>x (ie. letter y is strictly greater alphabetically than x). sets each letter in positions p_1, p_2, ..., p_k to letter y. More formally: for each i (1 <= i <= k) Koa sets A_{p_i} = y. Note that you can only modify letters in string A.Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A = B) or to determine that there is no way to make them equal. Help her! | Input: ['5', '3', 'aab', 'bcc', '4', 'cabc', 'abcb', '3', 'abc', 'tsr', '4', 'aabd', 'cccd', '5', 'abcbd', 'bcdda', ''] Output:['2', '-1', '3', '2', '-1', ''] | [
2
] |
You are given two arrays of integers a_1,...,a_n and b_1,...,b_m.Your task is to find a non-empty array c_1,...,c_k that is a subsequence of a_1,...,a_n, and also a subsequence of b_1,...,b_m. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero) elements. For example, [3,1] is a subsequence of [3,2,1] and [4,3,1], but not a subsequence of [1,3,3,7] and [3,10,4]. | Input: ['5', '4 5', '10 8 6 4', '1 2 3 4 5', '1 1', '3', '3', '1 1', '3', '2', '5 3', '1000 2 2 2 3', '3 1 5', '5 5', '1 2 3 4 5', '1 2 3 4 5', ''] Output:['YES', '1 4', 'YES', '1 3', 'NO', 'YES', '1 3', 'YES', '1 2', ''] | [
0
] |
After being discouraged by 13 time-limit-exceeded verdicts on an ugly geometry problem, you decided to take a relaxing break for arts and crafts.There is a piece of paper in the shape of a simple polygon with n vertices. The polygon may be non-convex, but we all know that proper origami paper has the property that any horizontal line intersects the boundary of the polygon in at most two points.If you fold the paper along the vertical line x=f, what will be the area of the resulting shape? When you fold, the part of the paper to the left of the line is symmetrically reflected on the right side. Your task is to answer q independent queries for values f_1,...,f_q. | Input: ['4 7', '0 10', '10 0', '0 -10', '-10 0', '-9', '-5', '-1', '0', '1', '5', '9', ''] Output:['199.0000000000', '175.0000000000', '119.0000000000', '100.0000000000', '119.0000000000', '175.0000000000', '199.0000000000', ''] | [
3
] |
There is an undirected tree of n vertices, connected by n-1 bidirectional edges. There is also a snake stuck inside of this tree. Its head is at vertex a and its tail is at vertex b. The snake's body occupies all vertices on the unique simple path between a and b.The snake wants to know if it can reverse itself β that is, to move its head to where its tail started, and its tail to where its head started. Unfortunately, the snake's movements are restricted to the tree's structure.In an operation, the snake can move its head to an adjacent vertex not currently occupied by the snake. When it does this, the tail moves one vertex closer to the head, so that the length of the snake remains unchanged. Similarly, the snake can also move its tail to an adjacent vertex not currently occupied by the snake. When it does this, the head moves one unit closer to the tail. Let's denote a snake position by (h,t), where h is the index of the vertex with the snake's head, t is the index of the vertex with the snake's tail. This snake can reverse itself with the movements (4,7)\to (5,1)\to (4,2)\to (1, 3)\to (7,2)\to (8,1)\to (7,4). Determine if it is possible to reverse the snake with some sequence of operations. | Input: ['4', '8 4 7', '1 2', '2 3', '1 4', '4 5', '4 6', '1 7', '7 8', '4 3 2', '4 3', '1 2', '2 3', '9 3 5', '1 2', '2 3', '3 4', '1 5', '5 6', '6 7', '1 8', '8 9', '16 15 12', '1 2', '2 3', '1 4', '4 5', '5 6', '6 7', '4 8', '8 9', '8 10', '10 11', '11 12', '11 13', '13 14', '10 15', '15 16', ''] Output:['YES', 'NO', 'NO', 'YES', ''] | [
2
] |
In the game of Mastermind, there are two players β Alice and Bob. Alice has a secret code, which Bob tries to guess. Here, a code is defined as a sequence of n colors. There are exactly n+1 colors in the entire universe, numbered from 1 to n+1 inclusive.When Bob guesses a code, Alice tells him some information about how good of a guess it is, in the form of two integers x and y.The first integer x is the number of indices where Bob's guess correctly matches Alice's code. The second integer y is the size of the intersection of the two codes as multisets. That is, if Bob were to change the order of the colors in his guess, y is the maximum number of indices he could get correct.For example, suppose n=5, Alice's code is [3,1,6,1,2], and Bob's guess is [3,1,1,2,5]. At indices 1 and 2 colors are equal, while in the other indices they are not equal. So x=2. And the two codes have the four colors 1,1,2,3 in common, so y=4. Solid lines denote a matched color for the same index. Dashed lines denote a matched color at a different index. x is the number of solid lines, and y is the total number of lines. You are given Bob's guess and two values x and y. Can you find one possibility of Alice's code so that the values of x and y are correct? | Input: ['7', '5 2 4', '3 1 1 2 5', '5 3 4', '1 1 2 1 2', '4 0 4', '5 5 3 3', '4 1 4', '2 3 2 3', '6 1 2', '3 2 1 1 1 1', '6 2 4', '3 3 2 1 1 1', '6 2 6', '1 1 3 2 1 1', ''] Output:['YES', '3 1 6 1 2', 'YES', '3 1 1 1 2', 'YES', '3 3 5 5', 'NO', 'YES', '4 4 4 4 3 1', 'YES', '3 1 3 1 7 7', 'YES', '2 3 1 1 1 1', ''] | [
2
] |
You are creating a level for a video game. The level consists of n rooms placed in a circle. The rooms are numbered 1 through n. Each room contains exactly one exit: completing the j-th room allows you to go the (j+1)-th room (and completing the n-th room allows you to go the 1-st room).You are given the description of the multiset of n chests: the i-th chest has treasure value c_i.Each chest can be of one of two types: regular chest β when a player enters a room with this chest, he grabs the treasure and proceeds to the next room; mimic chest β when a player enters a room with this chest, the chest eats him alive, and he loses. The player starts in a random room with each room having an equal probability of being chosen. The players earnings is equal to the total value of treasure chests he'd collected before he lost.You are allowed to choose the order the chests go into the rooms. For each k from 1 to n place the chests into the rooms in such a way that: each room contains exactly one chest; exactly k chests are mimics; the expected value of players earnings is minimum possible. Please note that for each k the placement is chosen independently.It can be shown that it is in the form of \frac{P}{Q} where P and Q are non-negative integers and Q!=0. Report the values of P \cdot Q^{-1} \pmod {998244353}. | Input: ['2', '1 2', ''] Output:['499122177 0 ', ''] | [
2,
3
] |
There are n warriors in a row. The power of the i-th warrior is a_i. All powers are pairwise distinct.You have two types of spells which you may cast: Fireball: you spend x mana and destroy exactly k consecutive warriors; Berserk: you spend y mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be [2, 3, 7, 8, 11, 5, 4], and k = 3. If you cast Berserk on warriors with powers 8 and 11, the resulting sequence of powers becomes [2, 3, 7, 11, 5, 4]. Then, for example, if you cast Fireball on consecutive warriors with powers [7, 11, 5], the resulting sequence of powers becomes [2, 3, 4].You want to turn the current sequence of warriors powers a_1, a_2, ..., a_n into b_1, b_2, ..., b_m. Calculate the minimum amount of mana you need to spend on it. | Input: ['5 2', '5 2 3', '3 1 4 5 2', '3 5', ''] Output:['8', ''] | [
2,
3
] |
There are n programmers that you want to split into several non-empty teams. The skill of the i-th programmer is a_i. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least x.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble. | Input: ['3', '5 10', '7 11 2 9 5', '4 8', '2 4 2 3', '4 11', '1 3 3 7', ''] Output:['2', '1', '0', ''] | [
0,
2
] |
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string s = s_1 s_2 ... s_{n} of length n where each letter is either R, S or P.While initializing, the bot is choosing a starting index pos (1 <= pos <= n), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of s_{pos}: if s_{pos} is equal to R the bot chooses "Rock"; if s_{pos} is equal to S the bot chooses "Scissors"; if s_{pos} is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of s_{pos + 1}. In the third round β on s_{pos + 2} and so on. After s_n the bot returns to s_1 and continues his game.You plan to play n rounds and you've already figured out the string s but still don't know what is the starting index pos. But since the bot's tactic is so boring, you've decided to find n choices to each round to maximize the average number of wins.In other words, let's suggest your choices are c_1 c_2 ... c_n and if the bot starts from index pos then you'll win in win(pos) rounds. Find c_1 c_2 ... c_n such that \frac{win(1) + win(2) + ... + win(n)}{n} is maximum possible. | Input: ['3', 'RRRR', 'RSP', 'S', ''] Output:['PPPP', 'RSP', 'R'] | [
2
] |
You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.Find three indices i, j and k such that: 1 <= i < j < k <= n; p_i < p_j and p_j > p_k. Or say that there are no such indices. | Input: ['3', '4', '2 1 4 3', '6', '4 6 1 2 5 3', '5', '5 3 1 2 4', ''] Output:['YES', '2 3 4', 'YES', '3 5 6', 'NO', ''] | [
0
] |
Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved.Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n * 2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise.The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n * m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner.Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! | Input: ['1 3 3', '1 1', '1 5', '2 4', ''] Output:['YES', 'YES', 'NO', ''] | [
4
] |
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children).We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc.We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. | Input: ['3 0', ''] Output:['YES', '0 1 1', ''] | [
3
] |
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of h hours, and each hour consists of m minutes, where m is an even number. Currently, there are n freight trains, and they depart every day at the same time: i-th train departs at h_i hours and m_i minutes.The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at 0 hours and t minutes, where 0 <= t < {m \over 2}, the second tram departs m \over 2 minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.To allow passengers to board the tram safely, the tram must arrive k minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at 0 hours and t minutes, where t < k, then the freight trains can not depart during the last k - t minutes of the day. A schematic picture of the correct way to run passenger trams. Here h=2 (therefore, the number of passenger trams is 2h=4), the number of freight trains is n=6. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length k before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of t to minimize the number of canceled freight trains in case all passenger trams depart according to schedule. | Input: ['2 24 60 15', '16 0', '17 15', ''] Output:['0 0', '', ''] | [
0,
4
] |
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by a_i and after receiving each consecutive flower of this type her happiness increases by b_i. That is, if among the chosen flowers there are x_i > 0 flowers of type i, his wife gets a_i + (x_i - 1) \cdot b_i additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife. | Input: ['2', '4 3', '5 0', '1 4', '2 2', '', '5 3', '5 2', '4 2', '3 1', ''] Output:['14', '16', ''] | [
0,
2,
4
] |
Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer n, he encrypts it in the following way: he picks three integers a, b and c such that l <=q a,b,c <=q r, and then he computes the encrypted value m = n \cdot a + b - c.Unfortunately, an adversary intercepted the values l, r and m. Is it possible to recover the original values of a, b and c from this information? More formally, you are asked to find any values of a, b and c such that a, b and c are integers, l <=q a, b, c <=q r, there exists a strictly positive integer n, such that n \cdot a + b - c = m. | Input: ['2', '4 6 13', '2 3 1', ''] Output:['4 6 5', '2 2 3', ''] | [
0,
3,
4
] |
Acacius is studying strings theory. Today he came with the following problem.You are given a string s of length n consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".Occurrence of a string t of length m in the string s of length n as a substring is a index i (1 <=q i <=q n - m + 1) such that string s[i..i+m-1] consisting of m consecutive symbols of s starting from i-th equals to string t. For example string "ababa" has two occurrences of a string "aba" as a substring with i = 1 and i = 3, but there are no occurrences of a string "aba" in the string "acba" as a substring.Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once. | Input: ['6', '7', 'abacaba', '7', '???????', '11', 'aba?abacaba', '11', 'abacaba?aba', '15', 'asdf???f???qwer', '11', 'abacabacaba', ''] Output:['Yes', 'abacaba', 'Yes', 'abacaba', 'Yes', 'abadabacaba', 'Yes', 'abacabadaba', 'No', 'No', ''] | [
0
] |
A cubic lattice L in 3-dimensional euclidean space is a set of points defined in the following way: L=\{u \cdot \vec r_1 + v \cdot \vec r_2 + w \cdot \vec r_3\}_{u, v, w \in \mathbb Z} Where \vec r_1, \vec r_2, \vec r_3 \in \mathbb{Z}^3 are some integer vectors such that: \vec r_1, \vec r_2 and \vec r_3 are pairwise orthogonal: \vec r_1 \cdot \vec r_2 = \vec r_1 \cdot \vec r_3 = \vec r_2 \cdot \vec r_3 = 0 Where \vec a \cdot \vec b is a dot product of vectors \vec a and \vec b. \vec r_1, \vec r_2 and \vec r_3 all have the same length: |\vec r_1| = |\vec r_2| = |\vec r_3| = r You're given a set A=\{\vec a_1, \vec a_2, ..., \vec a_n\} of integer points, i-th point has coordinates a_i=(x_i;y_i;z_i). Let g_i=\gcd(x_i,y_i,z_i). It is guaranteed that \gcd(g_1,g_2,...,g_n)=1.You have to find a cubic lattice L such that A \subset L and r is the maximum possible. | Input: ['2', '1 2 3', '1 2 1', ''] Output:['1', '1 0 0', '0 1 0', '0 0 1', ''] | [
3
] |
You are given a tree with n vertices. You are allowed to modify the structure of the tree through the following multi-step operation: Choose three vertices a, b, and c such that b is adjacent to both a and c. For every vertex d other than b that is adjacent to a, remove the edge connecting d and a and add the edge connecting d and c. Delete the edge connecting a and b and add the edge connecting a and c. As an example, consider the following tree: The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices 2, 4, and 5: It can be proven that after each operation, the resulting graph is still a tree.Find the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree n - 1, called its center, and n - 1 vertices of degree 1. | Input: ['6', '4 5', '2 6', '3 2', '1 2', '2 4', ''] Output:['1', ''] | [
0
] |
This is an interactive problem.Anton and Harris are playing a game to decide which of them is the king of problemsetting.There are three piles of stones, initially containing a, b, and c stones, where a, b, and c are distinct positive integers. On each turn of the game, the following sequence of events takes place: The first player chooses a positive integer y and provides it to the second player. The second player adds y stones to one of the piles, with the condition that he cannot choose the same pile in two consecutive turns. The second player loses if, at any point, two of the piles contain the same number of stones. The first player loses if 1000 turns have passed without the second player losing.Feeling confident in his skills, Anton decided to let Harris choose whether he wants to go first or second. Help Harris defeat Anton and become the king of problemsetting! | Input: ['5 2 6', '', '', '3', '', '0'] Output:['', 'First', '2', '', '3', ''] | [
3
] |
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if: 1 <= u < v <= n. a_u > a_v. Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions: all the pairs in the list are distinct and form an inversion in a. all the pairs that form an inversion in a are in the list. Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. | Input: ['3', '3 1 2', ''] Output:['2', '1 3', '1 2', ''] | [
2
] |
You're given an array of n integers between 0 and n inclusive.In one operation, you can choose any element of the array and replace it by the MEX of the elements of the array (which may change after the operation).For example, if the current array is [0, 2, 2, 1, 4], you can choose the second element and replace it by the MEX of the present elements β 3. Array will become [0, 3, 2, 1, 4].You must make the array non-decreasing, using at most 2n operations.It can be proven that it is always possible. Please note that you do not have to minimize the number of operations. If there are many solutions, you can print any of them. βAn array b[1 ... n] is non-decreasing if and only if b_1 <= b_2 <= ... <= b_n.The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2, 2, 1] is 0, because 0 does not belong to the array. The MEX of [3, 1, 0, 1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0, 3, 1, 2] is 4 because 0, 1, 2 and 3 belong to the array, but 4 does not. It's worth mentioning that the MEX of an array of length n is always between 0 and n inclusive. | Input: ['5', '3', '2 2 3', '3', '2 1 0', '7', '0 7 3 1 3 7 7', '9', '2 0 1 1 2 4 4 2 0', '9', '8 4 7 6 1 2 3 0 5', ''] Output:['0', '', '2', '3 1', '4', '2 5 5 4', '11', '3 8 9 7 8 5 9 6 4 1 2', '10', '1 8 1 9 5 2 4 6 3 7', ''] | [
0
] |
You are given an array a of length n, which initially is a permutation of numbers from 1 to n. In one operation, you can choose an index i (1 <=q i < n) such that a_i < a_{i + 1}, and remove either a_i or a_{i + 1} from the array (after the removal, the remaining parts are concatenated). For example, if you have the array [1, 3, 2], you can choose i = 1 (since a_1 = 1 < a_2 = 3), then either remove a_1 which gives the new array [3, 2], or remove a_2 which gives the new array [1, 2].Is it possible to make the length of this array equal to 1 with these operations? | Input: ['4', '3', '1 2 3', '4', '3 1 2 4', '3', '2 3 1', '6', '2 4 6 1 3 5', ''] Output:['YES', 'YES', 'NO', 'YES', ''] | [
2
] |
You are given a grid with n rows and m columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number k > 0 written on it, then exactly k of its neighboring cells have a number greater than 0 written on them. Note that if the number in the cell is 0, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by 1. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge. | Input: ['5', '3 4', '0 0 0 0', '0 1 0 0', '0 0 0 0', '2 2', '3 0', '0 0', '2 2', '0 0', '0 0', '2 3', '0 0 0', '0 4 0', '4 4', '0 0 0 0', '0 2 0 1', '0 0 0 0', '0 0 0 0', ''] Output:['YES', '0 0 0 0', '0 1 1 0', '0 0 0 0', 'NO', 'YES', '0 0', '0 0', 'NO', 'YES', '0 1 0 0', '1 4 2 1', '0 2 0 0', '1 3 1 0', ''] | [
2
] |
You are given n integers a_1, a_2, ..., a_n, where n is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold: At least \frac{n - 1}{2} of the adjacent differences a_{i + 1} - a_i for i = 1, 2, ..., n - 1 are greater than or equal to 0. At least \frac{n - 1}{2} of the adjacent differences a_{i + 1} - a_i for i = 1, 2, ..., n - 1 are less than or equal to 0. Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them. | Input: ['5', '3', '-2 4 3', '5', '1 1 1 1 1', '5', '-2 4 7 -6 4', '9', '9 7 -4 -2 1 -3 9 -4 -5', '9', '-4 1 9 4 8 9 5 1 -9', ''] Output:['-2 -4 3', '1 1 1 1 1', '-2 -4 7 -6 4', '-9 -7 -4 2 1 -3 -9 -4 -5', '4 -1 -9 -4 -8 -9 -5 -1 9', ''] | [
3
] |
You are given an array a consisting of n integers.In one move, you can choose some index i (1 <= i <= n - 2) and shift the segment [a_i, a_{i + 1}, a_{i + 2}] cyclically to the right (i.e. replace the segment [a_i, a_{i + 1}, a_{i + 2}] with [a_{i + 2}, a_i, a_{i + 1}]). Your task is to sort the initial array by no more than n^2 such operations or say that it is impossible to do that.You have to answer t independent test cases. | Input: ['5', '5', '1 2 3 4 5', '5', '5 4 3 2 1', '8', '8 4 5 2 3 6 7 3', '7', '5 2 1 6 4 7 3', '6', '1 2 3 3 6 4', ''] Output:['0', '', '6', '3 1 3 2 2 3 ', '13', '2 1 1 6 4 2 4 3 3 4 4 6 6 ', '-1', '4', '3 3 4 4 ', ''] | [
0
] |
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly m books before all entertainments. Alice and Bob will read each book together to end this exercise faster.There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).So they need to choose exactly m books from the given n books in such a way that: Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; the total reading time of these m books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. | Input: ['6 3 16 0 011 1 09 0 121 1 110 1 08 0 1'] Output:['246 5 1 '] | [
2,
4
] |
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).So they need to choose some books from the given n books in such a way that: Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. | Input: ['8 47 1 12 1 14 0 18 1 11 0 11 1 11 0 13 0 0'] Output:['18'] | [
2
] |
You are given an array a consisting of n positive integers.Initially, you have an integer x = 0. During one move, you can do one of the following two operations: Choose exactly one i from 1 to n and increase a_i by x (a_i := a_i + x), then increase x by 1 (x := x + 1). Just increase x by 1 (x := x + 1). The first operation can be applied no more than once to each i from 1 to n.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by k (the value k is given).You have to answer t independent test cases. | Input: ['5', '4 3', '1 2 1 3', '10 6', '8 7 1 8 3 7 5 10 8 9', '5 10', '20 100 50 20 100500', '10 25', '24 24 24 24 24 24 24 24 24 24', '8 8', '1 2 3 4 5 6 7 8', ''] Output:['6', '18', '0', '227', '8', ''] | [
3
] |
You are given a bracket sequence s of length n, where n is even (divisible by two). The string s consists of \frac{n}{2} opening brackets '(' and \frac{n}{2} closing brackets ')'.In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index i, remove the i-th character of s and insert it before or after all remaining characters of s).Your task is to find the minimum number of moves required to obtain regular bracket sequence from s. It can be proved that the answer always exists under the given constraints.Recall what the regular bracket sequence is: "()" is regular bracket sequence; if s is regular bracket sequence then "(" + s + ")" is regular bracket sequence; if s and t are regular bracket sequences then s + t is regular bracket sequence. For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.You have to answer t independent test cases. | Input: ['4', '2', ')(', '4', '()()', '8', '())()()(', '10', ')))((((())', ''] Output:['1', '0', '1', '3', ''] | [
2
] |
You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder).Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it's impossible to do that.You have to answer t independent test cases. | Input: ['7', '1', '2', '3', '12', '12345', '15116544', '387420489', ''] Output:['0', '-1', '2', '-1', '-1', '12', '36', ''] | [
3
] |
You are given three integers x, y and n. Your task is to find the maximum integer k such that 0 <= k <= n that k \bmod x = y, where \bmod is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given x, y and n you need to find the maximum possible integer from 0 to n that has the remainder y modulo x.You have to answer t independent test cases. It is guaranteed that such k exists for each test case. | Input: ['7', '7 5 12345', '5 0 4', '10 5 15', '17 8 54321', '499999993 9 1000000000', '10 5 187', '2 0 999999999', ''] Output:['12339', '0', '15', '54306', '999999995', '185', '999999998', ''] | [
3
] |
You are given a chessboard consisting of n rows and n columns. Rows are numbered from bottom to top from 1 to n. Columns are numbered from left to right from 1 to n. The cell at the intersection of the x-th column and the y-th row is denoted as (x, y). Furthermore, the k-th column is a special column. Initially, the board is empty. There are m changes to the board. During the i-th change one pawn is added or removed from the board. The current board is good if we can move all pawns to the special column by the followings rules: Pawn in the cell (x, y) can be moved to the cell (x, y + 1), (x - 1, y + 1) or (x + 1, y + 1); You can make as many such moves as you like; Pawns can not be moved outside the chessboard; Each cell can not contain more than one pawn. The current board may not always be good. To fix it, you can add new rows to the board. New rows are added at the top, i.βe. they will have numbers n+1, n+2, n+3, ....After each of m changes, print one integer β the minimum number of rows which you have to add to make the board good. | Input: ['5 3 5', '4 4', '3 5', '2 4', '3 4', '3 5', ''] Output:['0', '1', '2', '2', '1', ''] | [
2
] |
The government of Berland decided to improve network coverage in his country. Berland has a unique structure: the capital in the center and n cities in a circle around the capital. The capital already has a good network coverage (so the government ignores it), but the i-th city contains a_i households that require a connection.The government designed a plan to build n network stations between all pairs of neighboring cities which will maintain connections only for these cities. In other words, the i-th network station will provide service only for the i-th and the (i + 1)-th city (the n-th station is connected to the n-th and the 1-st city).All network stations have capacities: the i-th station can provide the connection to at most b_i households.Now the government asks you to check can the designed stations meet the needs of all cities or not β that is, is it possible to assign each household a network station so that each network station i provides the connection to at most b_i households. | Input: ['5', '3', '2 3 4', '3 3 3', '3', '3 3 3', '2 3 4', '4', '2 3 4 5', '3 7 2 2', '4', '4 5 2 3', '2 3 2 7', '2', '1 1', '10 10', ''] Output:['YES', 'YES', 'NO', 'YES', 'YES', ''] | [
2,
4
] |
Let f(x) be the sum of digits of a decimal number x.Find the smallest non-negative integer x such that f(x) + f(x + 1) + ... + f(x + k) = n. | Input: ['7', '1 0', '1 1', '42 7', '13 7', '99 1', '99 0', '99 2', ''] Output:['1', '0', '4', '-1', '599998', '99999999999', '7997', ''] | [
0,
2
] |
You are given an array a consisting of n integers. Indices of the array start from zero (i.βe. the first element is a_0, the second one is a_1, and so on).You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of a with borders l and r is a[l; r] = a_l, a_{l + 1}, ..., a_{r}.Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i.βe. the sum of elements a_0, a_2, ..., a_{2k} for integer k = \lfloor\frac{n-1}{2}\rfloor should be maximum possible).You have to answer t independent test cases. | Input: ['4', '8', '1 7 3 4 7 6 2 9', '5', '1 2 1 2 1', '10', '7 8 4 5 7 6 8 9 7 3', '4', '3 1 2 1', ''] Output:['26', '5', '37', '5', ''] | [
2
] |
You are given a string s consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 else cur = cur - 1 if cur < 0 ok = false break if ok breakNote that the inf denotes infinity, and the characters of the string are numbered from 1 to |s|.You have to calculate the value of the res after the process ends. | Input: ['3', '--+-', '---', '++--+-', ''] Output:['7', '9', '6', ''] | [
3
] |
There are two rival donut shops.The first shop sells donuts at retail: each donut costs a dollars.The second shop sells donuts only in bulk: box of b donuts costs c dollars. So if you want to buy x donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to x.You want to determine two positive integer values: how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop? how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn't exist then that value should be equal to -1. If there are multiple possible answers, then print any of them.The printed values should be less or equal to 10^9. It can be shown that under the given constraints such values always exist if any values exist at all. | Input: ['4', '5 10 4', '4 5 20', '2 2 3', '1000000000 1000000000 1000000000', ''] Output:['-1 20', '8 -1', '1 2', '-1 1000000000', ''] | [
2,
3
] |
Ray lost his array and needs to find it by asking Omkar. Omkar is willing to disclose that the array has the following qualities: The array has n (1 <= n <= 2 \cdot 10^5) elements. Every element in the array a_i is an integer in the range 1 <= a_i <= 10^9. The array is sorted in nondecreasing order. Ray is allowed to send Omkar a series of queries. A query consists of two integers, l and r such that 1 <= l <= r <= n. Omkar will respond with two integers, x and f. x is the mode of the subarray from index l to index r inclusive. The mode of an array is defined by the number that appears the most frequently. If there are multiple numbers that appear the most number of times, the smallest such number is considered to be the mode. f is the amount of times that x appears in the queried subarray.The array has k (1 <= k <= \min(25000,n)) distinct elements. However, due to Ray's sins, Omkar will not tell Ray what k is. Ray is allowed to send at most 4k queries.Help Ray find his lost array. | Input: ['6', '', '2 2', '', '2 2', '', '3 2', '', '2 1', ''] Output:['? 1 6', '', '? 1 3', '', '? 4 6', '', '? 3 4', '', '! 1 2 2 3 3 4'] | [
4
] |
Omkar is building a house. He wants to decide how to make the floor plan for the last floor.Omkar's floor starts out as n rows of m zeros (1 <= n,m <= 100). Every row is divided into intervals such that every 0 in the row is in exactly 1 interval. For every interval for every row, Omkar can change exactly one of the 0s contained in that interval to a 1. Omkar defines the quality of a floor as the sum of the squares of the sums of the values in each column, i. e. if the sum of the values in the i-th column is q_i, then the quality of the floor is \sum_{i = 1}^m q_i^2.Help Omkar find the maximum quality that the floor can have. | Input: ['4 5', '2', '1 2', '3 5', '2', '1 3', '4 5', '3', '1 1', '2 4', '5 5', '3', '1 1', '2 2', '3 5', ''] Output:['36', ''] | [
2
] |
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given n nonnegative integers a_1, a_2, ..., a_n arranged in a circle, where n must be odd (ie. n-1 is divisible by 2). Formally, for all i such that 2 <=q i <=q n, the elements a_{i - 1} and a_i are considered to be adjacent, and a_n and a_1 are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | Input: ['3', '7 10 2', ''] Output:['17', ''] | [
0,
2
] |
Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}.An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. | Input: ['251 2 3 4 573 2 4 5 1 6 7'] Output:['0', '2', ''] | [
3
] |
In Omkar's last class of math, he learned about the least common multiple, or LCM. LCM(a, b) is the smallest positive integer x which is divisible by both a and b.Omkar, having a laudably curious mind, immediately thought of a problem involving the LCM operation: given an integer n, find positive integers a and b such that a + b = n and LCM(a, b) is the minimum value possible.Can you help Omkar solve his ludicrously challenging math problem? | Input: ['3', '4', '6', '9', ''] Output:['2 2', '3 3', '3 6', ''] | [
2,
3
] |
This is the hard version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved.First, Aoi came up with the following idea for the competitive programming problem:Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies.Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array).After that, she will do n duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist?This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea:Let's define f(x) as the number of valid permutations for the integer x.You are given n, a and a prime number p <= n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x.Your task is to solve this problem made by Akari. | Input: ['3 2', '3 4 5', ''] Output:['1', '3', ''] | [
3,
4
] |
This is the easy version of the problem. The difference between versions is the constraints on n and a_i. You can make hacks only if all versions of the problem are solved.First, Aoi came up with the following idea for the competitive programming problem:Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has a_i candies.Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, \{2,3,1,5,4\} is a permutation, but \{1,2,2\} is not a permutation (2 appears twice in the array) and \{1,3,4\} is also not a permutation (because n=3 but there is the number 4 in the array).After that, she will do n duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy P_i, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations P exist?This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea:Let's define f(x) as the number of valid permutations for the integer x.You are given n, a and a prime number p <= n. Let's call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x.Your task is to solve this problem made by Akari. | Input: ['3 2', '3 4 5', ''] Output:['1', '3', ''] | [
0,
3,
4
] |
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers n,k. Construct a grid A with size n * n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k.Let's define: A_{i,j} as the integer in the i-th row and the j-th column. R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 <= i <= n). C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 <= j <= n). In other words, R_i are row sums and C_j are column sums of the grid A. For the grid A let's define the value f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2 (here for an integer sequence X we define \max(X) as the maximum value in X and \min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. | Input: ['4', '2 2', '3 8', '1 0', '4 16', ''] Output:['0', '10', '01', '2', '111', '111', '101', '0', '0', '0', '1111', '1111', '1111', '1111', ''] | [
2
] |
Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has a vanilla cookies and b chocolate cookies for the party.She invited n guests of the first type and m guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:If there are v vanilla cookies and c chocolate cookies at the moment, when the guest comes, then if the guest of the first type: if v>c the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie. if the guest of the second type: if v>c the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. After that: If there is at least one cookie of the selected type, the guest eats one. Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question. | Input: ['6', '2 2 1 2', '0 100 0 1', '12 13 25 1', '27 83 14 25', '0 0 1 0', '1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000', ''] Output:['Yes', 'No', 'No', 'Yes', 'No', 'Yes', ''] | [
2,
3
] |
A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily 7 days!In detail, she can choose any integer k which satisfies 1 <=q k <=q r, and set k days as the number of days in a week.Alice is going to paint some n consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.For example, in the picture, a week has 4 days and Alice paints 5 consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive n days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side. | Input: ['5', '3 4', '3 2', '3 1', '13 7', '1010000 9999999', ''] Output:['4', '3', '1', '28', '510049495001', ''] | [
3
] |
A penguin Rocher has n sticks. He has exactly one stick with length i for all 1 <= i <= n.He can connect some sticks. If he connects two sticks that have lengths a and b, he gets one stick with length a + b. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create? | Input: ['4', '1', '2', '3', '4', ''] Output:['1', '1', '2', '2', ''] | [
3
] |
Note that the only difference between the easy and hard version is the constraint on the number of queries. You can make hacks only if all versions of the problem are solved.This is an interactive problem.You are given a tree consisting of n nodes numbered with integers from 1 to n. Ayush and Ashish chose two secret distinct nodes in the tree. You need to find out both the nodes. You can make the following query: Provide a list of nodes and you will receive a node from that list whose sum of distances to both the hidden nodes is minimal (if there are multiple such nodes in the list, you will receive any one of them). You will also get the sum of distances of that node to the hidden nodes. Recall that a tree is a connected graph without cycles. The distance between two nodes is defined as the number of edges in the simple path between them.More formally, let's define two hidden nodes as s and f. In one query you can provide the set of nodes \{a_1, a_2, ..., a_c\} of the tree. As a result, you will get two numbers a_i and dist(a_i, s) + dist(a_i, f). The node a_i is any node from the provided set, for which the number dist(a_i, s) + dist(a_i, f) is minimal.You can ask no more than 11 queries. | Input: ['1', '3', '1 2', '1 3', '', '1 1', '', '2 3', '', '3 1', '', '3 1', '', 'Correct'] Output:['? 1 1', '', '? 1 2', '', '? 1 3', '', '? 2 2 3', '', '! 1 3'] | [
4
] |
Note that the only difference between the easy and hard version is the constraint on the number of queries. You can make hacks only if all versions of the problem are solved.This is an interactive problem.You are given a tree consisting of n nodes numbered with integers from 1 to n. Ayush and Ashish chose two secret distinct nodes in the tree. You need to find out both the nodes. You can make the following query: Provide a list of nodes and you will receive a node from that list whose sum of distances to both the hidden nodes is minimal (if there are multiple such nodes in the list, you will receive any one of them). You will also get the sum of distances of that node to the hidden nodes. Recall that a tree is a connected graph without cycles. The distance between two nodes is defined as the number of edges in the simple path between them.More formally, let's define two hidden nodes as s and f. In one query you can provide the set of nodes \{a_1, a_2, ..., a_c\} of the tree. As a result, you will get two numbers a_i and dist(a_i, s) + dist(a_i, f). The node a_i is any node from the provided set, for which the number dist(a_i, s) + dist(a_i, f) is minimal.You can ask no more than 14 queries. | Input: ['1', '3', '1 2', '1 3', '', '1 1', '', '2 3', '', '3 1', '', '3 1', '', 'Correct'] Output:['? 1 1', '', '? 1 2', '', '? 1 3', '', '? 2 2 3', '', '! 1 3'] | [
4
] |
Naman has two binary strings s and t of length n (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert s into t using the following operation as few times as possible.In one operation, he can choose any subsequence of s and rotate it clockwise once.For example, if s = 1\textbf{1}101\textbf{00}, he can choose a subsequence corresponding to indices (1-based) \{2, 6, 7 \} and rotate them clockwise. The resulting string would then be s = 1\textbf{0}101\textbf{10}.A string a is said to be a subsequence of string b if a can be obtained from b by deleting some characters without changing the ordering of the remaining characters.To perform a clockwise rotation on a sequence c of size k is to perform an operation which sets c_1:=c_k, c_2:=c_1, c_3:=c_2, ..., c_k:=c_{k-1} simultaneously.Determine the minimum number of operations Naman has to perform to convert s into t or say that it is impossible. | Input: ['6', '010000', '000001', ''] Output:['1'] | [
2,
4
] |
Ashish has an array a of size n.A subsequence of a is defined as a sequence that can be obtained from a by deleting some elements (possibly none), without changing the order of the remaining elements.Consider a subsequence s of a. He defines the cost of s as the minimum between: The maximum among all elements at odd indices of s. The maximum among all elements at even indices of s. Note that the index of an element is its index in s, rather than its index in a. The positions are numbered from 1. So, the cost of s is equal to min(max(s_1, s_3, s_5, ...), max(s_2, s_4, s_6, ...)).For example, the cost of \{7, 5, 6\} is min( max(7, 6), max(5) ) = min(7, 5) = 5.Help him find the minimum cost of a subsequence of size k. | Input: ['4 2', '1 2 3 4', ''] Output:['1'] | [
2,
4
] |
Ashishgup and FastestFinger play a game. They start with a number n and play in turns. In each turn, a player can make any one of the following moves: Divide n by any of its odd divisors greater than 1. Subtract 1 from n if n is greater than 1. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally. | Input: ['7', '1', '2', '3', '4', '5', '6', '12', ''] Output:['FastestFinger', 'Ashishgup', 'Ashishgup', 'FastestFinger', 'Ashishgup', 'FastestFinger', 'Ashishgup', ''] | [
3
] |
Ashish has an array a of consisting of 2n positive integers. He wants to compress a into an array b of size n-1. To do this, he first discards exactly 2 (any two) elements from a. He then performs the following operation until there are no elements left in a: Remove any two elements from a and append their sum to b. The compressed array b has to have a special property. The greatest common divisor (\mathrm{gcd}) of all its elements should be greater than 1.Recall that the \mathrm{gcd} of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array a into an array b of size n-1 such that gcd(b_1, b_2..., b_{n-1}) > 1. Help Ashish find a way to do so. | Input: ['3', '3', '1 2 3 4 5 6', '2', '5 7 9 10', '5', '1 3 3 4 5 90 100 101 2 3', ''] Output:['3 6', '4 5', '3 4', '1 9', '2 3', '4 5', '6 10', ''] | [
3
] |
Let's consider all integers in the range from 1 to n (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of \mathrm{gcd}(a, b), where 1 <=q a < b <=q n.The greatest common divisor, \mathrm{gcd}(a, b), of two positive integers a and b is the biggest integer that is a divisor of both a and b. | Input: ['2', '3', '5', ''] Output:['1', '2', ''] | [
2,
3
] |
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020...There are n different types of food and m Lee's best friends. Lee has w_i plates of the i-th type of food and each friend has two different favorite types of food: the i-th friend's favorite types of food are x_i and y_i (x_i!=y_i).Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once.The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither x_i nor y_i), he will eat Lee instead *\_*.Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself. | Input: ['3 3', '1 2 1', '1 2', '2 3', '1 3', ''] Output:['ALIVE', '3 2 1 ', ''] | [
2
] |
Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...Let's define a Rooted Dead Bush (RDB) of level n as a rooted tree constructed as described below.A rooted dead bush of level 1 is a single vertex. To construct an RDB of level i we, at first, construct an RDB of level i-1, then for each vertex u: if u has no children then we will add a single child to it; if u has one child then we will add two children to it; if u has more than one child, then we will skip it. Rooted Dead Bushes of level 1, 2 and 3. Let's define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw: The center of the claw is the vertex with label 1. Lee has a Rooted Dead Bush of level n. Initially, all vertices of his RDB are green.In one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw's vertices in yellow.He'd like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo 10^9+7. | Input: ['7', '1', '2', '3', '4', '5', '100', '2000000', ''] Output:['0', '0', '4', '4', '12', '990998587', '804665184', ''] | [
2,
3
] |
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...Lee has n integers a_1, a_2, ..., a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness. | Input: ['3', '4 2', '1 13 7 17', '1 3', '6 2', '10 10 10 10 11 11', '3 3', '4 4', '1000000000 1000000000 1000000000 1000000000', '1 1 1 1', ''] Output:['48', '42', '8000000000', ''] | [
2,
3
] |
Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s).In one move he can choose two consecutive characters s_i and s_{i+1}, and if s_i is 1 and s_{i + 1} is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string s as clean as possible. He thinks for two different strings x and y, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.Now you should answer t test cases: for the i-th test case, print the cleanest possible string that Lee can get by doing some number of moves.Small reminder: if we have two strings x and y of the same length then x is lexicographically smaller than y if there is a position i such that x_1 = y_1, x_2 = y_2,..., x_{i - 1} = y_{i - 1} and x_i < y_i. | Input: ['5', '10', '0001111111', '4', '0101', '8', '11001101', '10', '1110000000', '1', '1', ''] Output:['0001111111', '001', '01', '0', '1', ''] | [
2
] |
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular n-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the OX-axis and at least one of its edges is parallel to the OY-axis at the same time.Recall that a regular n-sided polygon is a convex polygon with n vertices such that all the edges and angles are equal.Now he is shopping: the market has t regular polygons. For each of them print YES if it is beautiful and NO otherwise. | Input: ['4', '3', '4', '12', '1000000000', ''] Output:['NO', 'YES', 'YES', 'YES', ''] | [
3
] |
This is an easier version of the problem H without modification queries.Lester and Delbert work at an electronics company. They are currently working on a microchip component serving to connect two independent parts of a large supercomputer.The component is built on top of a breadboard β a grid-like base for a microchip. The breadboard has n rows and m columns, and each row-column intersection contains a node. Also, on each side of the breadboard there are ports that can be attached to adjacent nodes. Left and right side have n ports each, and top and bottom side have m ports each. Each of the ports is connected on the outside to one of the parts bridged by the breadboard, and is colored red or blue respectively. Ports can be connected by wires going inside the breadboard. However, there are a few rules to follow: Each wire should connect a red port with a blue port, and each port should be connected to at most one wire. Each part of the wire should be horizontal or vertical, and turns are only possible at one of the nodes. To avoid interference, wires can not have common parts of non-zero length (but may have common nodes). Also, a wire can not cover the same segment of non-zero length twice.The capacity of the breadboard is the largest number of red-blue wire connections that can be made subject to the rules above. For example, the breadboard above has capacity 7, and one way to make seven connections is pictured below. Up to this point statements of both versions are identical. Differences follow below.Given the current breadboard configuration, help Lester and Delbert find its capacity efficiently. | Input: ['4 5 0', 'BBRR', 'RBBR', 'BBBBB', 'RRRRR', ''] Output:['7', ''] | [
2
] |
This is an interactive problem.John and his imaginary friend play a game. There are n lamps arranged in a circle. Lamps are numbered 1 through n in clockwise order, that is, lamps i and i + 1 are adjacent for any i = 1, ..., n - 1, and also lamps n and 1 are adjacent. Initially all lamps are turned off.John and his friend take turns, with John moving first. On his turn John can choose to terminate the game, or to make a move. To make a move, John can choose any positive number k and turn any k lamps of his choosing on. In response to this move, John's friend will choose k consecutive lamps and turn all of them off (the lamps in the range that were off before this move stay off). Note that the value of k is the same as John's number on his last move. For example, if n = 5 and John have just turned three lamps on, John's friend may choose to turn off lamps 1, 2, 3, or 2, 3, 4, or 3, 4, 5, or 4, 5, 1, or 5, 1, 2.After this, John may choose to terminate or move again, and so on. However, John can not make more than 10^4 moves.John wants to maximize the number of lamps turned on at the end of the game, while his friend wants to minimize this number. Your task is to provide a strategy for John to achieve optimal result. Your program will play interactively for John against the jury's interactor program playing for John's friend.Suppose there are n lamps in the game. Let R(n) be the number of turned on lamps at the end of the game if both players act optimally. Your program has to terminate the game with at least R(n) turned on lamps within 10^4 moves. Refer to Interaction section below for interaction details.For technical reasons hacks for this problem are disabled. | Input: ['3', ''] Output:['', '0', ''] | [
3
] |
Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot.A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks.Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks.Arthur doesn't want to close too many spots. He will be happy to find any way to close at most \frac{4}{7}n spots so that the remaining part is safe. Help him find any suitable way to do so. | Input: ['2', '4 6', '1 2', '1 3', '2 3', '2 4', '3 4', '3 4', '7 6', '1 2', '1 3', '2 4', '2 5', '3 6', '3 7', ''] Output:['2', '3 4 ', '4', '4 5 6 7 ', ''] | [
2
] |
Gottfried learned about binary number representation. He then came up with this task and presented it to you.You are given a collection of n non-negative integers a_1, ..., a_n. You are allowed to perform the following operation: choose two distinct indices 1 <=q i, j <=q n. If before the operation a_i = x, a_j = y, then after the operation a_i = x~\mathsf{AND}~y, a_j = x~\mathsf{OR}~y, where \mathsf{AND} and \mathsf{OR} are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).After all operations are done, compute \sum_{i=1}^n a_i^2 β the sum of squares of all a_i. What is the largest sum of squares you can achieve? | Input: ['1', '123', ''] Output:['15129', ''] | [
2,
3
] |
Karl likes Codeforces and subsequences. He wants to find a string of lowercase English letters that contains at least k subsequences codeforces. Out of all possible strings, Karl wants to find a shortest one.Formally, a codeforces subsequence of a string s is a subset of ten characters of s that read codeforces from left to right. For example, codeforces contains codeforces a single time, while codeforcesisawesome contains codeforces four times: codeforcesisawesome, codeforcesisawesome, codeforcesisawesome, codeforcesisawesome.Help Karl find any shortest string that contains at least k codeforces subsequences. | Input: ['1', ''] Output:['codeforces', ''] | [
0,
2,
3
] |
Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = 2, b = 3 changes the value of a to 5 (the value of b does not change).In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value n. What is the smallest number of operations he has to perform? | Input: ['2', '1 2 3', '5 4 100', ''] Output:['2', '7', ''] | [
0,
2,
3
] |
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on n are greater than in the easy version of the problem.You are given an array a of n integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index i (1 <= i <= n) and move the element a[i] to the begin of the array; choose any index i (1 <= i <= n) and move the element a[i] to the end of the array. For example, if n = 5, a = [4, 7, 2, 2, 9], then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array a will become [7, 4, 2, 2, 9]; after performing the operation of the second type to the second element, the array a will become [7, 2, 2, 9, 4]. You can perform operations of any type any number of times in any order.Find the minimum total number of operations of the first and second type that will make the a array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities a[1] <= a[2] <= ... <= a[n]. | Input: ['9', '5', '4 7 2 2 9', '5', '3 5 8 1 7', '5', '1 2 2 4 5', '2', '0 1', '3', '0 1 0', '4', '0 1 0 0', '4', '0 1 0 1', '4', '0 1 0 2', '20', '16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9', ''] Output:['2', '2', '0', '0', '1', '1', '1', '1', '16', ''] | [
2,
4
] |
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on n are less than in the hard version of the problem.You are given an array a of n integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index i (1 <= i <= n) and move the element a[i] to the begin of the array; choose any index i (1 <= i <= n) and move the element a[i] to the end of the array. For example, if n = 5, a = [4, 7, 2, 3, 9], then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array a will become [7, 4, 2, 3, 9]; after performing the operation of the second type to the second element, the array a will become [7, 2, 3, 9, 4]. You can perform operations of any type any number of times in any order.Find the minimum total number of operations of the first and second type that will make the a array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities a[1] <= a[2] <= ... <= a[n]. | Input: ['4', '5', '4 7 2 3 9', '5', '3 5 8 1 7', '5', '1 4 5 7 12', '4', '0 2 1 3', ''] Output:['2', '2', '0', '2', ''] | [
2
] |
The store sells n beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"β"z"). You want to buy some beads to assemble a necklace from them.A necklace is a set of beads connected in a circle.For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): And the following necklaces cannot be assembled from beads sold in the store: The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace k-beautiful if, when it is turned clockwise by k beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. As you can see, this necklace is, for example, 3-beautiful, 6-beautiful, 9-beautiful, and so on, but it is not 1-beautiful or 2-beautiful.In particular, a necklace of length 1 is k-beautiful for any integer k. A necklace that consists of beads of the same color is also beautiful for any k.You are given the integers n and k, and also the string s containing n lowercase letters of the English alphabet β each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a k-beautiful necklace you can assemble. | Input: ['6', '6 3', 'abcbac', '3 6', 'aaa', '7 1000', 'abczgyo', '5 4', 'ababa', '20 10', 'aaebdbabdbbddaadaadc', '20 5', 'ecbedececacbcbccbdec', ''] Output:['6', '3', '5', '4', '15', '10', ''] | [
0,
2
] |
Polycarp wrote on the board a string s containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information.Suppose that the string t has length m and the characters are numbered from left to right from 1 to m. You are given a sequence of m integers: b_1, b_2, ..., b_m, where b_i is the sum of the distances |i-j| from the index i to all such indices j that t_j > t_i (consider that 'a'<'b'<...<'z'). In other words, to calculate b_i, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than t_i and sums all the values |i-j|.For example, if t = "abzb", then: since t_1='a', all other indices contain letters which are later in the alphabet, that is: b_1=|1-2|+|1-3|+|1-4|=1+2+3=6; since t_2='b', only the index j=3 contains the letter, which is later in the alphabet, that is: b_2=|2-3|=1; since t_3='z', then there are no indexes j such that t_j>t_i, thus b_3=0; since t_4='b', only the index j=3 contains the letter, which is later in the alphabet, that is: b_4=|4-3|=1. Thus, if t = "abzb", then b=[6,1,0,1].Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously: t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string t according to the rules above, equals to the array b specified in the input data. | Input: ['4', 'abac', '3', '2 1 0', 'abc', '1', '0', 'abba', '3', '1 0 1', 'ecoosdcefr', '10', '38 13 24 14 11 5 3 24 17 0', ''] Output:['aac', 'b', 'aba', 'codeforces', ''] | [
2
] |
Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty).Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k.For example, if n=8 and k=2, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s.Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. | Input: ['6', '6 1', '100010', '6 2', '000000', '5 1', '10101', '3 1', '001', '2 2', '00', '1 1', '0', ''] Output:['1', '2', '0', '1', '1', '1', ''] | [
2,
3
] |
You are given an array a[0 ... n-1] of length n which consists of non-negative integers. Note that array indices start from zero.An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 <= i <= n - 1) the equality i \bmod 2 = a[i] \bmod 2 holds, where x \bmod 2 is the remainder of dividing x by 2.For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i \bmod 2 = 1 \bmod 2 = 1, but a[i] \bmod 2 = 4 \bmod 2 = 0.In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).Find the minimum number of moves in which you can make the array a good, or say that this is not possible. | Input: ['4', '4', '3 2 7 6', '3', '3 2 6', '1', '7', '7', '4 9 2 1 18 3 0', ''] Output:['2', '1', '-1', '0', ''] | [
2,
3
] |
You are given a simple weighted connected undirected graph, consisting of n vertices and m edges.A path in the graph of length k is a sequence of k+1 vertices v_1, v_2, ..., v_{k+1} such that for each i (1 <= i <= k) the edge (v_i, v_{i+1}) is present in the graph. A path from some vertex v also has vertex v_1=v. Note that edges and vertices are allowed to be included in the path multiple times.The weight of the path is the total weight of edges in it.For each i from 1 to q consider a path from vertex 1 of length i of the maximum weight. What is the sum of weights of these q paths?Answer can be quite large, so print it modulo 10^9+7. | Input: ['7 8 25', '1 2 1', '2 3 10', '3 4 2', '1 5 2', '5 6 7', '6 4 15', '5 3 1', '1 7 3', ''] Output:['4361', ''] | [
4
] |
You are given two arrays a_1, a_2, ... , a_n and b_1, b_2, ... , b_m. Array b is sorted in ascending order (b_i < b_{i + 1} for each i from 1 to m - 1).You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to b_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on.For example, if a = [12, 10, 20, 20, 25, 30] and b = [10, 20, 30] then there are two good partitions of array a: [12, 10, 20], [20, 25], [30]; [12, 10], [20, 20, 25], [30]. You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353. | Input: ['6 3', '12 10 20 20 25 30', '10 20 30', ''] Output:['2', ''] | [
0,
4
] |
You are given n integers a_1, a_2, ..., a_n.For each a_i find its two divisors d_1 > 1 and d_2 > 1 such that \gcd(d_1 + d_2, a_i) = 1 (where \gcd(a, b) is the greatest common divisor of a and b) or say that there is no such pair. | Input: ['10', '2 3 4 5 6 7 8 9 10 24', ''] Output:['-1 -1 -1 -1 3 -1 -1 -1 2 2 ', '-1 -1 -1 -1 2 -1 -1 -1 5 3 ', ''] | [
3
] |
You are given a matrix with n rows (numbered from 1 to n) and m columns (numbered from 1 to m). A number a_{i, j} is written in the cell belonging to the i-th row and the j-th column, each number is either 0 or 1.A chip is initially in the cell (1, 1), and it will be moved to the cell (n, m). During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is (x, y), then after the move it can be either (x + 1, y) or (x, y + 1)). The chip cannot leave the matrix.Consider each path of the chip from (1, 1) to (n, m). A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.Your goal is to change the values in the minimum number of cells so that every path is palindromic. | Input: ['4', '2 2', '1 1', '0 1', '2 3', '1 1 0', '1 0 0', '3 7', '1 0 1 1 1 1 1', '0 0 0 0 0 0 0', '1 1 1 1 1 0 1', '3 5', '1 0 1 0 0', '1 1 1 1 0', '0 0 1 0 0', ''] Output:['0', '3', '4', '4', ''] | [
2,
3
] |
You are given an array consisting of n integers a_1, a_2, ..., a_n. Initially a_x = 1, all other elements are equal to 0.You have to perform m operations. During the i-th operation, you choose two indices c and d such that l_i <= c, d <= r_i, and swap a_c and a_d.Calculate the number of indices k such that it is possible to choose the operations so that a_k = 1 in the end. | Input: ['3', '6 4 3', '1 6', '2 3', '5 5', '4 1 2', '2 4', '1 2', '3 3 2', '2 3', '1 2', ''] Output:['6', '2', '3', ''] | [
3
] |
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types β shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has a sticks and b diamonds? | Input: ['4', '4 4', '1000000000 0', '7 15', '8 7', ''] Output:['2', '0', '7', '5', ''] | [
2,
3,
4
] |
This is an interactive problem.Ayush devised yet another scheme to set the password of his lock. The lock has n slots where each slot can hold any non-negative integer. The password P is a sequence of n integers, i-th element of which goes into the i-th slot of the lock.To set the password, Ayush comes up with an array A of n integers each in the range [0, 2^{63}-1]. He then sets the i-th element of P as the bitwise OR of all integers in the array except A_i.You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the bitwise OR all elements of the array with index in this subset. You can ask no more than 13 queries. | Input: ['3', '', '1', '', '2', '', '4', '', ''] Output:['? 1 1', '', '? 1 2', '', '? 1 3', '', '! 6 5 3', ''] | [
3
] |
Ridhiman challenged Ashish to find the maximum valued subsequence of an array a of size n consisting of positive integers. The value of a non-empty subsequence of k elements of a is defined as \sum 2^i over all integers i >= 0 such that at least \max(1, k - 2) elements of the subsequence have the i-th bit set in their binary representation (value x has the i-th bit set in its binary representation if \lfloor \frac{x}{2^i} \rfloor \mod 2 is equal to 1). Recall that b is a subsequence of a, if b can be obtained by deleting some(possibly zero) elements from a.Help Ashish find the maximum value he can get by choosing some subsequence of a. | Input: ['3', '2 1 3', ''] Output:['3'] | [
0
] |
Vivek has encountered a problem. He has a maze that can be represented as an n * m grid. Each of the grid cells may represent the following: Empty β '.' Wall β '#' Good person β 'G' Bad person β 'B' The only escape from the maze is at cell (n, m).A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains 'G' or 'B' cannot be blocked and can be travelled through.Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.It is guaranteed that the cell (n,m) is empty. Vivek can also block this cell. | Input: ['6', '1 1', '.', '1 2', 'G.', '2 2', '#B', 'G.', '2 3', 'G.#', 'B#.', '3 3', '#B.', '#..', 'GG.', '2 2', '#B', 'B.', ''] Output:['Yes', 'Yes', 'No', 'No', 'Yes', 'Yes', ''] | [
2
] |
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.Note that a permutation of n elements is a sequence of numbers a_1, a_2, ..., a_n, in which every number from 1 to n appears exactly once. The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if: i = j, that is, they are at the same index. a_i = b_j His two disciples are allowed to perform the following operation any number of times: choose a number k and cyclically shift one of the permutations to the left or right k times. A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, ..., c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, ..., c_n:=c_{n-1} simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times. | Input: ['5', '1 2 3 4 5', '2 3 4 5 1', ''] Output:['5'] | [
2
] |
Ashish and Vivek play a game on a matrix consisting of n rows and m columns, where they take turns claiming cells. Unclaimed cells are represented by 0, while claimed cells are represented by 1. The initial state of the matrix is given. There can be some claimed cells in the initial state.In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. | Input: ['4', '2 2', '0 0', '0 0', '2 2', '0 0', '0 1', '2 3', '1 0 1', '1 1 0', '3 3', '1 0 0', '0 0 0', '1 0 0', ''] Output:['Vivek', 'Ashish', 'Vivek', 'Ashish', ''] | [
2
] |
Given a connected undirected graph with n vertices and an integer k, you have to either: either find an independent set that has exactly \lceil\frac{k}{2}\rceil vertices. or find a simple cycle of length at most k. An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof that for any input you can always solve at least one of these problems, but it's left as an exercise for the reader. | Input: ['4 4 3', '1 2', '2 3', '3 4', '4 1', ''] Output:['1', '1 3 '] | [
2
] |
Given an array a of length n, find another array, b, of length n such that: for each i (1 <= i <= n) MEX(\{b_1, b_2, ..., b_i\})=a_i. The MEX of a set of integers is the smallest non-negative integer that doesn't belong to this set.If such array doesn't exist, determine this. | Input: ['3', '1 2 3', ''] Output:['0 1 2 '] | [
0,
2
] |
Given a permutation p of length n, find its subsequence s_1, s_2, ..., s_k of length at least 2 such that: |s_1-s_2|+|s_2-s_3|+...+|s_{k-1}-s_k| is as big as possible over all subsequences of p with length at least 2. Among all such subsequences, choose the one whose length, k, is as small as possible. If multiple subsequences satisfy these conditions, you are allowed to find any of them.A sequence a is a subsequence of an array b if a can be obtained from b by deleting some (possibly, zero or all) elements.A permutation of length n is an array of length n in which every element from 1 to n occurs exactly once. | Input: ['2', '3', '3 2 1', '4', '1 3 4 2', ''] Output:['2', '3 1 ', '3', '1 4 2 ', ''] | [
2
] |
Ehab loves number theory, but for some reason he hates the number x. Given an array a, find the length of its longest subarray such that the sum of its elements isn't divisible by x, or determine that such subarray doesn't exist.An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. | Input: ['3', '3 3', '1 2 3', '3 4', '1 2 3', '2 2', '0 6', ''] Output:['2', '3', '-1', ''] | [
0
] |
Ashish has a tree consisting of n nodes numbered 1 to n rooted at node 1. The i-th node in the tree has a cost a_i, and binary digit b_i is written in it. He wants to have binary digit c_i written in the i-th node in the end.To achieve this, he can perform the following operation any number of times: Select any k nodes from the subtree of any node u, and shuffle the digits in these nodes as he wishes, incurring a cost of k \cdot a_u. Here, he can choose k ranging from 1 to the size of the subtree of u. He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.Help him find the minimum total cost he needs to spend so that after all the operations, every node u has digit c_u written in it, or determine that it is impossible. | Input: ['5', '1 0 1', '20 1 0', '300 0 1', '4000 0 0', '50000 1 0', '1 2', '2 3', '2 4', '1 5', ''] Output:['4'] | [
2
] |
This is an interactive problem.Ayush devised a new scheme to set the password of his lock. The lock has k slots where each slot can hold integers from 1 to n. The password P is a sequence of k integers each in the range [1, n], i-th element of which goes into the i-th slot of the lock.To set the password of his lock, Ayush comes up with an array A of n integers each in the range [1, n] (not necessarily distinct). He then picks k non-empty mutually disjoint subsets of indices S_1, S_2, ..., S_k (S_i \underset{i \neq j} \cap S_j = \emptyset) and sets his password as P_i = \max\limits_{j \notin S_i} A[j]. In other words, the i-th integer in the password is equal to the maximum over all elements of A whose indices do not belong to S_i.You are given the subsets of indices chosen by Ayush. You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the maximum of all elements of the array with index in this subset. You can ask no more than 12 queries. | Input: ['1', '4 2', '2 1 3', '2 2 4', '', '1', '', '2', '', '3', '', '4', '', 'Correct'] Output:['? 1 1', '', '? 1 2', '', '? 1 3', '', '? 1 4', '', '! 4 3'] | [
3,
4
] |
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.Tell him whether he can do so. | Input: ['5', '1 1', '999', '1 1', '1000', '2 1', '51 50', '2 2', '51 50', '3 3', '101 102 103', ''] Output:['Yes', 'No', 'Yes', 'Yes', 'No', ''] | [
0,
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.