question_text
stringlengths
2
3.82k
input_outputs
stringlengths
23
941
algo_tags
sequence
Slavic has an array of length n consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to s after performing all the operations? In case the sum s can't be obtained after any amount of operations, you should output -1.
Input: ['73 11 0 03 11 1 09 30 1 0 1 1 1 0 0 16 41 1 1 1 1 15 10 0 1 1 016 21 1 0 0 1 0 0 1 1 0 0 0 0 0 1 16 31 0 1 0 0 0'] Output:['0', '1', '3', '2', '2', '7', '-1', '']
[ 4 ]
Victor has a 24-hour clock that shows the time in the format "HH:MM" (00 <= HH <= 23, 00 <= MM <= 59). He looks at the clock every x minutes, and the clock is currently showing time s. How many different palindromes will Victor see in total after looking at the clock every x minutes, the first time being at time s?For example, if the clock starts out as 03:12 and Victor looks at the clock every 360 minutes (i.e. every 6 hours), then he will see the times 03:12, 09:12, 15:12, 21:12, 03:12, and the times will continue to repeat. Here the time 21:12 is the only palindrome he will ever see, so the answer is 1.A palindrome is a string that reads the same backward as forward. For example, the times 12:21, 05:50, 11:11 are palindromes but 13:13, 22:10, 02:22 are not.
Input: ['603:12 36000:00 113:22 215:15 1011:11 144022:30 27'] Output:['1', '16', '10', '0', '1', '1', '']
[ 0 ]
Sho has an array a consisting of n integers. An operation consists of choosing two distinct indices i and j and removing a_i and a_j from the array.For example, for the array [2, 3, 4, 2, 5], Sho can choose to remove indices 1 and 3. After this operation, the array becomes [3, 2, 5]. Note that after any operation, the length of the array is reduced by two.After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible. More formally, the array after Sho has made his operations respects these criteria: No pairs such that (i < j) and a_i = a_j exist. The length of a is maximized. Output the length of the final array.
Input: ['462 2 2 3 3 359 1 9 9 1415 16 16 15410 100 1000 10000'] Output:['2', '1', '2', '4', '']
[ 2 ]
You are given a tree G with n vertices and an integer k. The vertices of the tree are numbered from 1 to n.For a vertex r and a subset S of vertices of G, such that |S| = k, we define f(r, S) as the size of the smallest rooted subtree containing all vertices in S when the tree is rooted at r. A set of vertices T is called a rooted subtree, if all the vertices in T are connected, and for each vertex in T, all its descendants belong to T.You need to calculate the sum of f(r, S) over all possible distinct combinations of vertices r and subsets S, where |S| = k. Formally, compute the following: \sum_{r \in V} \sum_{S \subseteq V, |S| = k} f(r, S), where V is the set of vertices in G.Output the answer modulo 10^9 + 7.
Input: ['3 2', '1 2', '1 3', ''] Output:['25', '']
[ 3 ]
You are given n colored segments on the number line. Each segment is either colored red or blue. The i-th segment can be represented by a tuple (c_i, l_i, r_i). The segment contains all the points in the range [l_i, r_i], inclusive, and its color denoted by c_i: if c_i = 0, it is a red segment; if c_i = 1, it is a blue segment. We say that two segments of different colors are connected, if they share at least one common point. Two segments belong to the same group, if they are either connected directly, or through a sequence of directly connected segments. Find the number of groups of segments.
Input: ['250 0 51 2 120 4 71 9 160 13 1931 0 11 1 20 3 4'] Output:['2', '3', '']
[ 2 ]
You are given an array a of n integers. You are asked to find out if the inequality \max(a_i, a_{i + 1}, ..., a_{j - 1}, a_{j}) >=q a_i + a_{i + 1} + ... + a_{j - 1} + a_{j} holds for all pairs of indices (i, j), where 1 <=q i <=q j <=q n.
Input: ['34-1 1 -1 25-1 2 -3 2 -132 3 -1'] Output:['YES', 'YES', 'NO', '']
[ 4 ]
You are given a binary string s of length n.Let's define d_i as the number whose decimal representation is s_i s_{i+1} (possibly, with a leading zero). We define f(s) to be the sum of all the valid d_i. In other words, f(s) = \sum\limits_{i=1}^{n-1} d_i.For example, for the string s = 1011: d_1 = 10 (ten); d_2 = 01 (one) d_3 = 11 (eleven); f(s) = 10 + 01 + 11 = 22. In one operation you can swap any two adjacent elements of the string. Find the minimum value of f(s) that can be achieved if at most k operations are allowed.
Input: ['34 010107 100101005 200110'] Output:['21', '22', '12', '']
[ 0, 2, 3 ]
A class of students got bored wearing the same pair of shoes every day, so they decided to shuffle their shoes among themselves. In this problem, a pair of shoes is inseparable and is considered as a single object.There are n students in the class, and you are given an array s in non-decreasing order, where s_i is the shoe size of the i-th student. A shuffling of shoes is valid only if no student gets their own shoes and if every student gets shoes of size greater than or equal to their size. You have to output a permutation p of \{1,2,...,n\} denoting a valid shuffling of shoes, where the i-th student gets the shoes of the p_i-th student (p_i!=i). And output -1 if a valid shuffling does not exist.A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Input: ['2', '5', '1 1 1 1 1', '6', '3 6 8 13 15 21', ''] Output:['5 1 2 3 4 ', '-1', '']
[ 2 ]
Given a sequence a_1, a_2, ..., a_n, find the minimum number of elements to remove from the sequence such that after the removal, the sum of every 2 consecutive elements is even.
Input: ['252 4 3 6 863 5 9 7 1 3'] Output:['1', '0', '']
[ 0, 2, 3 ]
There are n of independent carriages on the rails. The carriages are numbered from left to right from 1 to n. The carriages are not connected to each other. The carriages move to the left, so that the carriage with number 1 moves ahead of all of them.The i-th carriage has its own engine, which can accelerate the carriage to a_i km/h, but the carriage cannot go faster than the carriage in front of it. See example for explanation.All carriages start moving to the left at the same time, and they naturally form trains. We will call trains — consecutive moving carriages having the same speed.For example, we have n=5 carriages and array a = [10, 13, 5, 2, 6]. Then the final speeds of the carriages will be [10, 10, 5, 2, 2]. Respectively, 3 of the train will be formed.There are also messages saying that some engine has been corrupted: message "k d" means that the speed of the k-th carriage has decreased by d (that is, there has been a change in the maximum speed of the carriage a_k = a_k - d). Messages arrive sequentially, the processing of the next message takes into account the changes from all previous messages.After each message determine the number of formed trains.
Input: ['34 26 2 3 73 24 75 410 13 5 2 62 45 21 53 213 4769 514 336 173 181 373 519 338 985 709 729 702 16812 5816 2227 2335 117'] Output:['3 4 ', '4 4 2 3 ', '5 6 6 5 ', '']
[ 2, 4 ]
Polycarp found the string s and the permutation p. Their lengths turned out to be the same and equal to n.A permutation of n elements — is an array of length n, in which every integer from 1 to n occurs exactly once. For example, [1, 2, 3] and [4, 3, 5, 1, 2] are permutations, but [1, 2, 4], [4, 3, 2, 1, 2] and [0, 1, 2] are not.In one operation he can multiply s by p, so he replaces s with string new, in which for any i from 1 to n it is true that new_i = s_{p_i}. For example, with s=wmbe and p = [3, 1, 4, 2], after operation the string will turn to s=s_3 s_1 s_4 s_2=bwem.Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.
Input: ['35ababa3 4 5 2 15ababa2 1 4 5 310codeforces8 6 1 7 5 2 9 3 10 4'] Output:['1', '6', '12', '']
[ 3 ]
A batch of n goods (n — an even number) is brought to the store, i-th of which has weight a_i. Before selling the goods, they must be packed into packages. After packing, the following will be done: There will be \frac{n}{2} packages, each package contains exactly two goods; The weight of the package that contains goods with indices i and j (1 <= i, j <= n) is a_i + a_j. With this, the cost of a package of weight x is always <=ft \lfloor\frac{x}{k}\right\rfloor burles (rounded down), where k — a fixed and given value.Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such \frac{n}{2} pairs of given goods that the sum of the values <=ft \lfloor\frac{x_i}{k} \right \rfloor, where x_i is the weight of the package number i (1 <= i <= \frac{n}{2}), is maximal.For example, let n = 6, k = 3, weights of goods a = [3, 2, 7, 1, 4, 8]. Let's pack them into the following packages. In the first package we will put the third and sixth goods. Its weight will be a_3 + a_6 = 7 + 8 = 15. The cost of the package will be <=ft \lfloor\frac{15}{3}\right\rfloor = 5 burles. In the second package put the first and fifth goods, the weight is a_1 + a_5 = 3 + 4 = 7. The cost of the package is <=ft \lfloor\frac{7}{3}\right\rfloor = 2 burles. In the third package put the second and fourth goods, the weight is a_2 + a_4 = 2 + 1 = 3. The cost of the package is <=ft \lfloor\frac{3}{3}\right\rfloor = 1 burle. With this packing, the total cost of all packs would be 5 + 2 + 1 = 8 burles.
Input: ['66 33 2 7 1 4 84 32 1 5 64 120 0 0 02 11 16 102 0 0 5 9 46 55 3 8 6 3 2'] Output:['8', '4', '0', '2', '1', '5', '']
[ 2, 3, 4 ]
Recently, Polycarp completed n successive tasks.For each completed task, the time s_i is known when it was given, no two tasks were given at the same time. Also given is the time f_i when the task was completed. For each task, there is an unknown value d_i (d_i>0) — duration of task execution.It is known that the tasks were completed in the order in which they came. Polycarp performed the tasks as follows: As soon as the very first task came, Polycarp immediately began to carry it out. If a new task arrived before Polycarp finished the previous one, he put the new task at the end of the queue. When Polycarp finished executing the next task and the queue was not empty, he immediately took a new task from the head of the queue (if the queue is empty — he just waited for the next task). Find d_i (duration) of each task.
Input: ['430 3 72 10 11210 1511 16912 16 90 195 1456 1569 3001 5237 1927513 199 200 260 9100 10000 10914 91066 5735533101000000000'] Output:['2 7 1 ', '1 1 ', '1 183 1 60 7644 900 914 80152 5644467 ', '1000000000 ', '']
[ 2 ]
Kristina has two arrays a and b, each containing n non-negative integers. She can perform the following operation on array a any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element a_i such that a_i > 0 with the value a_i - 1 (1 <= i <= n). If a_i was 0, its value does not change. Determine whether Kristina can get an array b from an array a in some number of operations (probably zero). In other words, can she make a_i = b_i after some number of operations for each 1 <= i <= n?For example, let n = 4, a = [3, 5, 4, 1] and b = [1, 3, 2, 0]. In this case, she can apply the operation twice: after the first application of the operation she gets a = [2, 4, 3, 0]; after the second use of the operation she gets a = [1, 3, 2, 0]. Thus, in two operations, she can get an array b from an array a.
Input: ['643 5 4 11 3 2 031 2 10 1 045 3 7 21 1 1 151 2 3 4 51 2 3 4 6180146'] Output:['YES', 'YES', 'NO', 'NO', 'YES', 'NO', '']
[ 2 ]
Given the integer n — the number of available blocks. You must use all blocks to build a pedestal. The pedestal consists of 3 platforms for 2-nd, 1-st and 3-rd places respectively. The platform for the 1-st place must be strictly higher than for the 2-nd place, and the platform for the 2-nd place must be strictly higher than for the 3-rd place. Also, the height of each platform must be greater than zero (that is, each platform must contain at least one block). Example pedestal of n=11 blocks: second place height equals 4 blocks, first place height equals 5 blocks, third place height equals 2 blocks.Among all possible pedestals of n blocks, deduce one such that the platform height for the 1-st place minimum as possible. If there are several of them, output any of them.
Input: ['61161010000078'] Output:['4 5 2', '2 3 1', '4 5 1', '33334 33335 33331', '2 4 1', '3 4 1', '']
[ 2 ]
Bit Lightyear, to the ANDfinity and beyond!After graduating from computer sciences, Vlad has been awarded an array a_1,a_2,...,a_n of n non-negative integers. As it is natural, he wanted to construct a graph consisting of n vertices, numbered 1, 2,..., n. He decided to add an edge between i and j if and only if a_i \& a_j > 0, where \& denotes the bitwise AND operation.Vlad also wants the graph to be connected, which might not be the case initially. In order to satisfy that, he can do the following two types of operations on the array: Choose some element a_i and increment it by 1. Choose some element a_i and decrement it by 1 (possible only if a_i > 0). It can be proven that there exists a finite sequence of operations such that the graph will be connected. So, can you please help Vlad find the minimum possible number of operations to do that and also provide the way how to do that?
Input: ['4', '5', '1 2 3 4 5', '2', '0 2', '2', '3 12', '4', '3 0 0 0', ''] Output:['0', '1 2 3 4 5', '2', '2 2', '1', '3 11', '3', '3 1 1 1', '']
[ 0 ]
Monocarp is a little boy who lives in Byteland and he loves programming.Recently, he found a permutation of length n. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position.More formally, if the old permutation is p_1,p_2,...,p_n and the new one is q_1,q_2,...,q_n it must hold that p_1\neq q_1, p_2\neq q_2, ... ,p_n\neq q_n.Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation?
Input: ['431 2 352 3 4 5 142 3 1 411'] Output:['2 3 1', '1 2 3 4 5', '1 2 4 3', '-1', '']
[ 2 ]
Kuznecov likes art, poetry, and music. And strings consisting of lowercase English letters.Recently, Kuznecov has found two strings, a and b, of lengths n and m respectively. They consist of lowercase English letters and no character is contained in both strings. Let another string c be initially empty. Kuznecov can do the following two types of operations: Choose any character from the string a, remove it from a, and add it to the end of c. Choose any character from the string b, remove it from b, and add it to the end of c. But, he can not do more than k operations of the same type in a row. He must perform operations until either a or b becomes empty. What is the lexicographically smallest possible value of c after he finishes?A string x is lexicographically smaller than a string y if and only if one of the following holds: x is a prefix of y, but x \neq y; in the first position where x and y differ, the string x has a letter that appears earlier in the alphabet than the corresponding letter in y.
Input: ['36 4 2aaaaaabbbb5 9 3caacabedededeb7 7 1noskillwxhtzdy'] Output:['aabaabaa', 'aaabbcc', 'dihktlwlxnyoz', '']
[ 0, 2 ]
As a human, she can erase history of its entirety. As a Bai Ze (Hakutaku), she can create history out of nothingness.—Perfect Memento in Strict SenseKeine has the ability to manipulate history. The history of Gensokyo is a string s of length 1 initially. To fix the chaos caused by Yukari, she needs to do the following operations n times, for the i-th time: She chooses a non-empty substring t_{2i-1} of s. She replaces t_{2i-1} with a non-empty string, t_{2i}. Note that the lengths of strings t_{2i-1} and t_{2i} can be different.Note that if t_{2i-1} occurs more than once in s, exactly one of them will be replaced.For example, let s="marisa", t_{2i-1}="a", and t_{2i}="z". After the operation, s becomes "mzrisa" or "marisz".After n operations, Keine got the final string and an operation sequence t of length 2n. Just as Keine thinks she has finished, Yukari appears again and shuffles the order of t. Worse still, Keine forgets the initial history. Help Keine find the initial history of Gensokyo!Recall that a substring is a sequence of consecutive characters of the string. For example, for string "abc" its substrings are: "ab", "c", "bc" and some others. But the following strings are not its substring: "ac", "cba", "acb".HacksYou cannot make hacks in this problem.
Input: ['22aabbcdacd3zaaaayakumoranyakumoran'] Output:['a', 'z', '']
[ 2 ]
She is skilled in all kinds of magics, and is keen on inventing new one.—Perfect Memento in Strict SensePatchouli is making a magical talisman. She initially has n magical tokens. Their magical power can be represented with positive integers a_1, a_2, ..., a_n. Patchouli may perform the following two operations on the tokens. Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens. Reduction: Patchouli chooses a token with an even value of magical power x, removes it and creates a new token with magical power equal to \frac{x}{2}. Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values.
Input: ['421 931 1 232 4 831049600 33792 1280'] Output:['0', '1', '3', '10', '']
[ 2 ]
Even if it's a really easy question, she won't be able to answer it— Perfect Memento in Strict SenseCirno's perfect bitmasks classroom has just started!Cirno gave her students a positive integer x. As an assignment, her students need to find the minimum positive integer y, which satisfies the following two conditions:x\ \texttt{and}\ y > 0 x\ \texttt{xor}\ y > 0Where \texttt{and} is the bitwise AND operation, and \texttt{xor} is the bitwise XOR operation.Among the students was Mystia, who was truly baffled by all these new operators. Please help her!
Input: ['71259161145141000000'] Output:['3', '3', '1', '1', '17', '2', '64', '']
[ 0 ]
As she closed the Satori's eye that could read minds, Koishi gained the ability to live in unconsciousness. Even she herself does not know what she is up to.— Subterranean AnimismKoishi is unconsciously permuting n numbers: 1, 2, ..., n.She thinks the permutation p is beautiful if s=\sum\limits_{i=1}^{n-1} [p_i+1=p_{i+1}]. [x] equals to 1 if x holds, or 0 otherwise.For each k\in[0,n-1], she wants to know the number of beautiful permutations of length n satisfying k=\sum\limits_{i=1}^{n-1}[p_i<p_{i+1}].
Input: ['2 0', ''] Output:['1 0', '']
[ 3 ]
Come, let's build a world where even the weak are not forgotten!—Kijin Seija, Double Dealing CharactersShinmyoumaru has a mallet that can turn objects bigger or smaller. She is testing it out on a sequence a and a number v whose initial value is 1. She wants to make v = \gcd\limits_{i\ne j}\{a_i\cdot a_j\} by no more than 10^5 operations (\gcd\limits_{i\ne j}\{a_i\cdot a_j\} denotes the \gcd of all products of two distinct elements of the sequence a). In each operation, she picks a subsequence b of a, and does one of the followings: Enlarge: v = v \cdot \mathrm{lcm}(b) Reduce: v = \frac{v}{\mathrm{lcm}(b)} Note that she does not need to guarantee that v is an integer, that is, v does not need to be a multiple of \mathrm{lcm}(b) when performing Reduce.Moreover, she wants to guarantee that the total length of b chosen over the operations does not exceed 10^6. Fine a possible operation sequence for her. You don't need to minimize anything.
Input: ['3', '6 10 15', ''] Output:['1', '0 3 1 2 3', '']
[ 2, 3 ]
Ran is especially skilled in computation and mathematics. It is said that she can do unimaginable calculation work in an instant.—Perfect Memento in Strict SenseRan Yakumo is a cute girl who loves creating cute Maths problems.Let f(x) be the minimal square number strictly greater than x, and g(x) be the maximal square number less than or equal to x. For example, f(1)=f(2)=g(4)=g(8)=4.A positive integer x is cute if x-g(x)<f(x)-x. For example, 1,5,11 are cute integers, while 3,8,15 are not. Ran gives you an array a of length n. She wants you to find the smallest non-negative integer k such that a_i + k is a cute number for any element of a.
Input: ['4', '1 3 8 10', ''] Output:['1', '']
[ 0, 3, 4 ]
Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!— Kochiya Sanae Sanae made a giant robot — Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length n. Initially, the robot is at state a. She wishes to turn it into state b. As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from b and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of a does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment [l,r] and assign a_i = b_i (l<= i<= r) if \sum\limits_{i=1}^n a_i does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state a to the desired state b with any (possibly, zero) operations.
Input: ['25 21 5 4 2 33 2 5 4 11 32 55 21 5 4 2 33 2 4 5 11 22 4'] Output:['YES', 'NO', '']
[ 0, 2, 4 ]
As for the technology in the outside world, it is really too advanced for Gensokyo to even look up to.—Yasaka Kanako, Symposium of Post-mysticismThis is an interactive problem.Under the direct supervision of Kanako and the Moriya Shrine, the railway system of Gensokyo is finally finished. GSKR (Gensokyo Railways) consists of n stations with m bidirectional tracks connecting them. The i-th track has length l_i (1<= l_i<= 10^6). Due to budget limits, the railway system may not be connected, though there may be more than one track between two stations.The value of a railway system is defined as the total length of its all tracks. The maximum (or minimum) capacity of a railway system is defined as the maximum (or minimum) value among all of the currently functional system's full spanning forest.In brief, full spanning forest of a graph is a spanning forest with the same connectivity as the given graph.Kanako has a simulator only able to process no more than 2m queries. The input of the simulator is a string s of length m, consisting of characters 0 and/or 1. The simulator will assume the i-th track functional if s_i= 1. The device will then tell Kanako the maximum capacity of the system in the simulated state.Kanako wants to know the the minimum capacity of the system with all tracks functional with the help of the simulator.The structure of the railway system is fixed in advance. In other words, the interactor is not adaptive.
Input: ['5 4', '', '0', '', '5', '', '9', '', '7', ''] Output:['? 0000', '', '? 1110', '', '? 1111', '', '? 1101', '', '! 7', '']
[ 2 ]
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by n points on the X-axis numbered 1 through n. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by a_1,a_2,...,a_n.Marisa can start out at any point in the forest on minute 0. Each minute, the followings happen in order: She moves from point x to y (|x-y|<= 1, possibly y=x). She collects all mushrooms on point y. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute 0.Now, Marisa wants to know the maximum number of mushrooms she can pick after k minutes.
Input: ['45 25 6 1 2 35 75 6 1 2 31 29999995 700001000000000 1000000000 1000000000 1000000000 1000000000'] Output:['12', '37', '1000000', '5000349985', '']
[ 0, 2 ]
For an array [b_1, b_2, ..., b_m] define its number of inversions as the number of pairs (i, j) of integers such that 1 <= i < j <= m and b_i>b_j. Let's call array b odd if its number of inversions is odd. For example, array [4, 2, 7] is odd, as its number of inversions is 1, while array [2, 1, 4, 3] isn't, as its number of inversions is 2.You are given a permutation [p_1, p_2, ..., p_n] of integers from 1 to n (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?
Input: ['531 2 344 3 2 121 222 164 5 6 1 2 3'] Output:['0', '2', '0', '1', '1', '']
[ 2 ]
You are given an array of n integers a_1, a_2, ..., a_n. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation.In one operation, you choose n-1 elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array [1, 2, 3, 1] we can get the array [2, 2, 2, 1], if we choose the first three elements, or we can get the array [\frac{4}{3}, \frac{4}{3}, 3, \frac{4}{3}], if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?
Input: ['4342 42 4251 2 3 4 544 3 2 1324 2 22'] Output:['YES', 'YES', 'NO', 'NO', '']
[ 2 ]
It turns out that this is exactly the 100-th problem of mine that appears in some programming competition. So it has to be special! And what can be more special than another problem about LIS...You are given a permutation p_1, p_2, ..., p_{2n+1} of integers from 1 to 2n+1. You will have to process q updates, where the i-th update consists in swapping p_{u_i}, p_{v_i}.After each update, find any cyclic shift of p with LIS <= n, or determine that there is no such shift. (Refer to the output section for details).Here LIS(a) denotes the length of longest strictly increasing subsequence of a.Hacks are disabled in this problem. Don't ask why.
Input: ['2 6', '1 2 3 4 5', '1 5', '1 5', '4 5', '5 4', '1 4', '2 5', ''] Output:['-1', '-1', '2', '-1', '4', '0', '']
[ 2 ]
This is a hard version of the problem. The difference between the easy and hard versions is that in this version, you have to output the lexicographically smallest permutation with the smallest weight.You are given a permutation p_1, p_2, ..., p_n of integers from 1 to n.Let's define the weight of the permutation q_1, q_2, ..., q_n of integers from 1 to n as |q_1 - p_{q_{2}}| + |q_2 - p_{q_{3}}| + ... + |q_{n-1} - p_{q_{n}}| + |q_n - p_{q_{1}}|You want your permutation to be as lightweight as possible. Among the permutations q with the smallest possible weight, find the lexicographically smallest.Permutation a_1, a_2, ..., a_n is lexicographically smaller than permutation b_1, b_2, ..., b_n, if there exists some 1 <= i <= n such that a_j = b_j for all 1 <= j < i and a_i<b_i.
Input: ['322 142 3 1 455 4 3 2 1'] Output:['1 2 ', '1 3 4 2 ', '1 3 4 2 5 ', '']
[ 2 ]
Alina has a bracket sequence s of length 2n, consisting of n opening brackets '(' and n closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence.In one operation, she can reverse any substring of s.What's the smallest number of operations that she needs to turn s into a balanced bracket sequence? It can be shown that it's always possible in at most n operations.As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.
Input: ['32(())5())((()))(6())((()))(()'] Output:['0', '2', '3 4', '9 10', '1', '2 11']
[ 0, 2 ]
Alina has discovered a weird language, which contains only 4 words: \texttt{A}, \texttt{B}, \texttt{AB}, \texttt{BA}. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.Alina has found one such sentence s and she is curious: is it possible that it consists of precisely a words \texttt{A}, b words \texttt{B}, c words \texttt{AB}, and d words \texttt{BA}?In other words, determine, if it's possible to concatenate these a+b+c+d words in some order so that the resulting string is s. Each of the a+b+c+d words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
Input: ['8', '1 0 0 0', 'B', '0 0 1 0', 'AB', '1 1 0 1', 'ABAB', '1 0 1 1', 'ABAAB', '1 1 2 2', 'BAABBABBAA', '1 1 2 3', 'ABABABBAABAB', '2 3 5 4', 'AABAABBABAAABABBABBBABB', '1 3 3 10', 'BBABABABABBBABABABABABABAABABA', ''] Output:['NO', 'YES', 'YES', 'YES', 'YES', 'YES', 'NO', 'YES', '']
[ 2 ]
You are given n integers a_1, a_2, ..., a_n. Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors?In other words, check if there exists a rearrangement b_1, b_2, ..., b_n of the integers a_1, a_2, ..., a_n such that for each i from 1 to n at least one of the following conditions holds: b_{i-1} < b_i > b_{i+1} b_{i-1} > b_i < b_{i+1}To make sense of the previous formulas for i=1 and i=n, one shall define b_0=b_n and b_{n+1}=b_1.
Input: ['431 1 241 9 8 442 0 2 261 1 1 11 111 1111'] Output:['NO', 'YES', '1 8 4 9 ', 'NO', 'YES', '1 11 1 111 1 1111 ', '']
[ 2 ]
You are given a binary string s. You have to cut it into any number of non-intersecting substrings, so that the sum of binary integers denoted by these substrings is a power of 2. Each element of s should be in exactly one substring.
Input: ['4', '00000', '01101', '0111011001011', '000111100111110', ''] Output:['-1', '', '3', '1 3', '4 4', '5 5', '', '8', '1 2', '3 3', '4 4', '5 6', '7 7', '8 10', '11 12', '13 13', '', '5', '1 5', '6 7', '8 11', '12 14', '15 15', '']
[ 3 ]
Let's consider Euclid's algorithm for finding the greatest common divisor, where t is a list:function Euclid(a, b): if a < b: swap(a, b) if b == 0: return a r = reminder from dividing a by b if r > 0: append r to the back of t return Euclid(b, r)There is an array p of pairs of positive integers that are not greater than m. Initially, the list t is empty. Then the function is run on each pair in p. After that the list t is shuffled and given to you.You have to find an array p of any size not greater than 2 \cdot 10^4 that produces the given list t, or tell that no such array exists.
Input: ['7 20', '1 8 1 6 3 2 3', ''] Output:['3', '19 11', '15 9', '3 7', '']
[ 3 ]
You are given an array a of n non-negative integers. In one operation you can change any number in the array to any other non-negative integer.Let's define the cost of the array as \operatorname{DIFF}(a) - \operatorname{MEX}(a), where \operatorname{MEX} of a set of non-negative integers is the smallest non-negative integer not present in the set, and \operatorname{DIFF} is the number of different numbers in the array.For example, \operatorname{MEX}(\{1, 2, 3\}) = 0, \operatorname{MEX}(\{0, 1, 2, 4, 5\}) = 3.You should find the minimal cost of the array a if you are allowed to make at most k operations.
Input: ['44 13 0 1 24 10 2 4 57 24 13 0 0 13 1337 10000000006 21 2 8 0 0 0'] Output:['0', '1', '2', '0', '']
[ 0, 2, 4 ]
There are n traps numbered from 1 to n. You will go through them one by one in order. The i-th trap deals a_i base damage to you.Instead of going through a trap, you can jump it over. You can jump over no more than k traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by 1 (this is a bonus damage).Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap i with base damage a_i, and you have already jumped over 3 traps, you get (a_i + 3) damage.You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than k traps.
Input: ['54 48 7 1 44 15 10 11 57 58 2 5 15 11 2 86 31 2 3 4 5 61 17'] Output:['0', '21', '9', '6', '0', '']
[ 2 ]
You are given a grid with n rows and m columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each 1 <= i <= n and 2 <= j <= m the following holds: a_{i,j} >= a_{i, j-1}.You have to to do the following operation exactly once: choose two columns with indexes i and j (not necessarily different), 1 <= i, j <= m, and swap them.You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped.
Input: ['52 31 2 31 1 12 24 12 32 22 11 12 36 2 15 4 32 112'] Output:['1 1', '-1', '1 2', '1 3', '1 1', '']
[ 0, 2 ]
You are given three positive integers a, b, c (a < b < c). You have to find three positive integers x, y, z such that:x \bmod y = a, y \bmod z = b, z \bmod x = c.Here p \bmod q denotes the remainder from dividing p by q. It is possible to show that for such constraints the answer always exists.
Input: ['4', '1 3 4', '127 234 421', '2 7 8', '59 94 388', ''] Output:['12 11 4', '1063 234 1484', '25 23 8', '2221 94 2609', '']
[ 3 ]
There is an integer n without zeros in its decimal representation. Alice and Bob are playing a game with this integer. Alice starts first. They play the game in turns.On her turn, Alice must swap any two digits of the integer that are on different positions. Bob on his turn always removes the last digit of the integer. The game ends when there is only one digit left.You have to find the smallest integer Alice can get in the end, if she plays optimally.
Input: ['312132487456398'] Output:['2', '1', '3', '']
[ 3 ]
You are given two integer arrays a and b (b_i \neq 0 and |b_i| <=q 10^9). Array a is sorted in non-decreasing order.The cost of a subarray a[l:r] is defined as follows:If \sum\limits_{j = l}^{r} b_j \neq 0, then the cost is not defined.Otherwise: Construct a bipartite flow graph with r-l+1 vertices, labeled from l to r, with all vertices having b_i \lt 0 on the left and those with b_i \gt 0 on right. For each i, j such that l <= i, j <= r, b_i<0 and b_j>0, draw an edge from i to j with infinite capacity and cost of unit flow as |a_i-a_j|. Add two more vertices: source S and sink T. For each i such that l <= i <= r and b_i<0, add an edge from S to i with cost 0 and capacity |b_i|. For each i such that l <= i <= r and b_i>0, add an edge from i to T with cost 0 and capacity |b_i|. The cost of the subarray is then defined as the minimum cost of maximum flow from S to T.You are given q queries in the form of two integers l and r. You have to compute the cost of subarray a[l:r] for each query, modulo 10^9 + 7.If you don't know what the minimum cost of maximum flow means, read here.
Input: ['8 4', '1 2 4 5 9 10 10 13', '6 -1 1 -3 2 1 -1 1', '2 3', '6 7', '3 5', '2 6', ''] Output:['2', '0', '9', '15', '']
[ 2 ]
Alice had a permutation p of numbers from 1 to n. Alice can swap a pair (x, y) which means swapping elements at positions x and y in p (i.e. swap p_x and p_y). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them to sort the permutation on a piece of paper. For example, [(2, 3), (1, 3)] is a valid swap sequence by Alice for permutation p = [3, 1, 2] whereas [(1, 3), (2, 3)] is not because it doesn't sort the permutation. Note that we cannot sort the permutation in less than 2 swaps. [(1, 2), (2, 3), (2, 4), (2, 3)] cannot be a sequence of swaps by Alice for p = [2, 1, 4, 3] even if it sorts the permutation because p can be sorted in 2 swaps, for example using the sequence [(4, 3), (1, 2)]. Unfortunately, Bob shuffled the sequence of swaps written by Alice.You are given Alice's permutation p and the swaps performed by Alice in arbitrary order. Can you restore the correct sequence of swaps that sorts the permutation p? Since Alice wrote correct swaps before Bob shuffled them up, it is guaranteed that there exists some order of swaps that sorts the permutation.
Input: ['4 3', '2 3 4 1', '1 4', '2 1', '1 3', ''] Output:['2 3 1 ', '']
[ 2, 3 ]
You are given an array a of n positive integers. Let \text{LIS}(a) denote the length of longest strictly increasing subsequence of a. For example, \text{LIS}([2, \underline{1}, 1, \underline{3}]) = 2. \text{LIS}([\underline{3}, \underline{5}, \underline{10}, \underline{20}]) = 4. \text{LIS}([3, \underline{1}, \underline{2}, \underline{4}]) = 3. We define array a' as the array obtained after reversing the array a i.e. a' = [a_n, a_{n-1}, ... , a_1].The beauty of array a is defined as min(\text{LIS}(a),\text{LIS}(a')).Your task is to determine the maximum possible beauty of the array a if you can rearrange the array a arbitrarily.
Input: ['336 6 662 5 4 5 2 441 3 2 2'] Output:['1', '3', '2', '']
[ 2, 3 ]
You are given a palindromic string s of length n.You have to count the number of indices i (1 <= i <= n) such that the string after removing s_i from s still remains a palindrome. For example, consider s = "aba" If we remove s_1 from s, the string becomes "ba" which is not a palindrome. If we remove s_2 from s, the string becomes "aa" which is a palindrome. If we remove s_3 from s, the string becomes "ab" which is not a palindrome. A palindrome is a string that reads the same backward as forward. For example, "abba", "a", "fef" are palindromes whereas "codeforces", "acd", "xy" are not.
Input: ['33aba8acaaaaca2dd'] Output:['1', '4', '2', '']
[ 2 ]
You are given two integer numbers, n and x. You may perform several operations with the integer x.Each operation you perform is the following one: choose any digit y that occurs in the decimal representation of x at least once, and replace x by x \cdot y.You want to make the length of decimal representation of x (without leading zeroes) equal to n. What is the minimum number of operations required to do that?
Input: ['2 1', ''] Output:['-1', '']
[ 0 ]
Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of n cards. You see that the values of cards from the topmost to the bottommost are integers a_1, a_2, ..., a_n, and all values are different.Then he asks you to shuffle the deck m times. With the j-th shuffle, you should take b_j topmost cards and move them under the remaining (n - b_j) cards without changing the order.And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it?
Input: ['321 231 1 143 1 4 223 152 1 5 4 353 2 1 2 1'] Output:['2', '3', '3', '']
[ 3 ]
Alice and Bob play a game. Alice has n cards, the i-th of them has the integer a_i written on it. Bob has m cards, the j-th of them has the integer b_j written on it.On the first turn of the game, the first player chooses one of his/her cards and puts it on the table (plays it). On the second turn, the second player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the first turn, and plays it. On the third turn, the first player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the second turn, and plays it, and so on — the players take turns, and each player has to choose one of his/her cards with greater integer than the card played by the other player on the last turn.If some player cannot make a turn, he/she loses.For example, if Alice has 4 cards with numbers [10, 5, 3, 8], and Bob has 3 cards with numbers [6, 11, 6], the game may go as follows: Alice can choose any of her cards. She chooses the card with integer 5 and plays it. Bob can choose any of his cards with number greater than 5. He chooses a card with integer 6 and plays it. Alice can choose any of her cards with number greater than 6. She chooses the card with integer 10 and plays it. Bob can choose any of his cards with number greater than 10. He chooses a card with integer 11 and plays it. Alice can choose any of her cards with number greater than 11, but she has no such cards, so she loses. Both Alice and Bob play optimally (if a player is able to win the game no matter how the other player plays, the former player will definitely win the game).You have to answer two questions: who wins if Alice is the first player? who wins if Bob is the first player?
Input: ['41626 841 3 3 724 2150225 50101 2 3 4 5 6 7 8 9 1025 15'] Output:['Bob', 'Bob', 'Alice', 'Alice', 'Alice', 'Bob', 'Bob', 'Bob', '']
[ 2 ]
You are given a board of size 2 * n (2 rows, n columns). Some cells of the board contain chips. The chip is represented as '*', and an empty space is represented as '.'. It is guaranteed that there is at least one chip on the board.In one move, you can choose any chip and move it to any adjacent (by side) cell of the board (if this cell is inside the board). It means that if the chip is in the first row, you can move it left, right or down (but it shouldn't leave the board). Same, if the chip is in the second row, you can move it left, right or up.If the chip moves to the cell with another chip, the chip in the destination cell disappears (i. e. our chip captures it).Your task is to calculate the minimum number of moves required to leave exactly one chip on the board.You have to answer t independent test cases.
Input: ['51*.2.***3*.*.*.4**.***..5**......**'] Output:['0', '2', '3', '5', '5', '']
[ 2 ]
You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point 0 with your dog. You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have some writings about how she ran. During the i-th minute, the dog position changed from her previous position by the value a_i (it means, that the dog ran for a_i meters during the i-th minute). If a_i is positive, the dog ran a_i meters to the right, otherwise (if a_i is negative) she ran a_i meters to the left.During some minutes, you were chatting with your friend, so you don't have writings about your dog movement during these minutes. These values a_i equal zero.You want your dog to return to you after the end of the walk, so the destination point of the dog after n minutes should be 0.Now you are wondering: what is the maximum possible number of different integer points of the line your dog could visit on her way, if you replace every 0 with some integer from -k to k (and your dog should return to 0 after the walk)? The dog visits an integer point if she runs through that point or reaches in it at the end of any minute. Point 0 is always visited by the dog, since she is initially there.If the dog cannot return to the point 0 after n minutes regardless of the integers you place, print -1.
Input: ['3 2', '5 0 -4', ''] Output:['6', '']
[ 0, 2, 3 ]
You are given a string s consisting of characters 0 and/or 1.You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values: the number of characters 0 left in the string; the number of characters 1 removed from the string. What is the minimum cost of removal you can achieve?
Input: ['510111011010010010010010000111111000001111'] Output:['1', '3', '0', '0', '0', '']
[ 2, 4 ]
An array is beautiful if both of the following two conditions meet: there are at least l_1 and at most r_1 elements in the array equal to its minimum; there are at least l_2 and at most r_2 elements in the array equal to its maximum. For example, the array [2, 3, 2, 4, 4, 3, 2] has 3 elements equal to its minimum (1-st, 3-rd and 7-th) and 2 elements equal to its maximum (4-th and 5-th).Another example: the array [42, 42, 42] has 3 elements equal to its minimum and 3 elements equal to its maximum.Your task is to calculate the minimum possible number of elements in a beautiful array.
Input: ['73 5 4 65 8 5 53 3 10 121 5 3 31 1 2 22 2 1 16 6 6 6'] Output:['4', '5', '13', '3', '3', '3', '6', '']
[ 0, 3 ]
Yura is a mathematician, and his cognition of the world is so absolute as if he have been solving formal problems a hundred of trillions of billions of years. This problem is just that!Consider all non-negative integers from the interval [0, 10^{n}). For convenience we complement all numbers with leading zeros in such way that each number from the given interval consists of exactly n decimal digits.You are given a set of pairs (u_i, v_i), where u_i and v_i are distinct decimal digits from 0 to 9.Consider a number x consisting of n digits. We will enumerate all digits from left to right and denote them as d_1, d_2, ..., d_n. In one operation you can swap digits d_i and d_{i + 1} if and only if there is a pair (u_j, v_j) in the set such that at least one of the following conditions is satisfied: d_i = u_j and d_{i + 1} = v_j, d_i = v_j and d_{i + 1} = u_j. We will call the numbers x and y, consisting of n digits, equivalent if the number x can be transformed into the number y using some number of operations described above. In particular, every number is considered equivalent to itself.You are given an integer n and a set of m pairs of digits (u_i, v_i). You have to find the maximum integer k such that there exists a set of integers x_1, x_2, ..., x_k (0 <= x_i < 10^{n}) such that for each 1 <= i < j <= k the number x_i is not equivalent to the number x_j.
Input: ['1', '0', ''] Output:['10', '']
[ 3 ]
One day Masha was walking in the park and found a graph under a tree... Surprised? Did you think that this problem would have some logical and reasoned story? No way! So, the problem...Masha has an oriented graph which i-th vertex contains some positive integer a_i. Initially Masha can put a coin at some vertex. In one operation she can move a coin placed in some vertex u to any other vertex v such that there is an oriented edge u \to v in the graph. Each time when the coin is placed in some vertex i, Masha write down an integer a_i in her notebook (in particular, when Masha initially puts a coin at some vertex, she writes an integer written at this vertex in her notebook). Masha wants to make exactly k - 1 operations in such way that the maximum number written in her notebook is as small as possible.
Input: ['6 7 4', '1 10 2 3 4 5', '1 2', '1 3', '3 4', '4 5', '5 6', '6 2', '2 5', ''] Output:['4', '']
[ 4 ]
Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses.You own a small bus service business and you have just received an order to replace n tires. You know that the bus fleet owns two types of buses: with two axles (these buses have 4 wheels) and with three axles (these buses have 6 wheels).You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is n.
Input: ['44724998244353998244352'] Output:['1 1', '-1', '4 6', '166374058999707392 249561088499561088', '']
[ 0, 2, 3 ]
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string s of length n, consisting only of zeros and ones, n is even.Now Tokitsukaze divides s into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, s is considered good if the lengths of all subsegments are even.For example, if s is "11001111", it will be divided into "11", "00" and "1111". Their lengths are 2, 2, 4 respectively, which are all even numbers, so "11001111" is good. Another example, if s is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are 3, 2, 2, 3. Obviously, "1110011000" is not good.Tokitsukaze wants to make s good by changing the values of some positions in s. Specifically, she can perform the operation any number of times: change the value of s_i to '0' or '1' (1 <=q i <=q n). Can you tell her the minimum number of operations to make s good? Meanwhile, she also wants to know the minimum number of subsegments that s can be divided into among all solutions with the minimum number of operations.
Input: ['51011100110008110011112002116100110'] Output:['3 2', '0 3', '0 1', '0 1', '3 1', '']
[ 2 ]
Tokitsukaze has a sequence with length of n. She likes gems very much. There are n kinds of gems. The gems of the i-th kind are on the i-th position, and there are a_i gems of the same kind on that position. Define G(l,r) as the multiset containing all gems on the segment [l,r] (inclusive).A multiset of gems can be represented as S=[s_1,s_2,...,s_n], which is a non-negative integer sequence of length n and means that S contains s_i gems of the i-th kind in the multiset. A multiset T=[t_1,t_2,...,t_n] is a multisubset of S=[s_1,s_2,...,s_n] if and only if t_i<= s_i for any i satisfying 1<= i<= n.Now, given two positive integers k and p, you need to calculate the result of\sum_{l=1}^n \sum_{r=l}^n\sum\limits_{[t_1,t_2,\cdots,t_n] \subseteq G(l,r)}<=ft(<=ft(\sum_{i=1}^n p^{t_i}t_i^k\right)<=ft(\sum_{i=1}^n[t_i>0]\right)\right),where [t_i>0]=1 if t_i>0 and [t_i>0]=0 if t_i=0.Since the answer can be quite large, print it modulo 998\,244\,353.
Input: ['5 2 2', '1 1 1 2 2', ''] Output:['6428', '']
[ 3 ]
Tokitsukaze has a permutation p. She performed the following operation to p exactly k times: in one operation, for each i from 1 to n - 1 in order, if p_i > p_{i+1}, swap p_i, p_{i+1}. After exactly k times of operations, Tokitsukaze got a new sequence a, obviously the sequence a is also a permutation.After that, Tokitsukaze wrote down the value sequence v of a on paper. Denote the value sequence v of the permutation a of length n as v_i=\sum_{j=1}^{i-1}[a_i < a_j], where the value of [a_i < a_j] define as if a_i < a_j, the value is 1, otherwise is 0 (in other words, v_i is equal to the number of elements greater than a_i that are to the left of position i). Then Tokitsukaze went out to work.There are three naughty cats in Tokitsukaze's house. When she came home, she found the paper with the value sequence v to be bitten out by the cats, leaving several holes, so that the value of some positions could not be seen clearly. She forgot what the original permutation p was. She wants to know how many different permutations p there are, so that the value sequence v of the new permutation a after exactly k operations is the same as the v written on the paper (not taking into account the unclear positions).Since the answer may be too large, print it modulo 998\,244\,353.
Input: ['35 00 1 2 3 45 2-1 1 2 0 05 20 1 1 0 0'] Output:['1', '6', '6', '']
[ 3 ]
Tokitsukaze has two colorful tapes. There are n distinct colors, numbered 1 through n, and each color appears exactly once on each of the two tapes. Denote the color of the i-th position of the first tape as ca_i, and the color of the i-th position of the second tape as cb_i.Now Tokitsukaze wants to select each color an integer value from 1 to n, distinct for all the colors. After that she will put down the color values in each colored position on the tapes. Denote the number of the i-th position of the first tape as numa_i, and the number of the i-th position of the second tape as numb_i. For example, for the above picture, assuming that the color red has value x (1 <=q x <=q n), it appears at the 1-st position of the first tape and the 3-rd position of the second tape, so numa_1=numb_3=x.Note that each color i from 1 to n should have a distinct value, and the same color which appears in both tapes has the same value. After labeling each color, the beauty of the two tapes is calculated as \sum_{i=1}^{n}|numa_i-numb_i|.Please help Tokitsukaze to find the highest possible beauty.
Input: ['361 5 4 3 2 65 3 1 4 6 263 5 4 6 2 13 6 4 5 2 1111'] Output:['18', '10', '0', '']
[ 2 ]
Tokitsukaze is arranging a meeting. There are n rows and m columns of seats in the meeting hall.There are exactly n \cdot m students attending the meeting, including several naughty students and several serious students. The students are numerated from 1 to n\cdot m. The students will enter the meeting hall in order. When the i-th student enters the meeting hall, he will sit in the 1-st column of the 1-st row, and the students who are already seated will move back one seat. Specifically, the student sitting in the j-th (1<=q j <=q m-1) column of the i-th row will move to the (j+1)-th column of the i-th row, and the student sitting in m-th column of the i-th row will move to the 1-st column of the (i+1)-th row.For example, there is a meeting hall with 2 rows and 2 columns of seats shown as below: There will be 4 students entering the meeting hall in order, represented as a binary string "1100", of which '0' represents naughty students and '1' represents serious students. The changes of seats in the meeting hall are as follows: Denote a row or a column good if and only if there is at least one serious student in this row or column. Please predict the number of good rows and columns just after the i-th student enters the meeting hall, for all i.
Input: ['32 211004 2110011012 411001101'] Output:['2 3 4 3', '2 3 4 3 5 4 6 5', '2 3 3 3 4 4 4 5', '']
[ 3 ]
Tokitsukaze has a permutation p of length n. Recall that a permutation p of length n is a sequence p_1, p_2, ..., p_n consisting of n distinct integers, each of which from 1 to n (1 <=q p_i <=q n).She wants to know how many different indices tuples [a,b,c,d] (1 <=q a < b < c < d <=q n) in this permutation satisfy the following two inequalities: p_a < p_c and p_b > p_d. Note that two tuples [a_1,b_1,c_1,d_1] and [a_2,b_2,c_2,d_2] are considered to be different if a_1!=a_2 or b_1!=b_2 or c_1!=c_2 or d_1!=d_2.
Input: ['365 3 6 1 4 241 2 3 4105 1 6 2 8 3 4 10 9 7'] Output:['3', '0', '28', '']
[ 0 ]
The only difference between the two versions is that in this version n <=q 1000 and the sum of n over all test cases does not exceed 1000.A terminal is a row of n equal segments numbered 1 to n in order. There are two terminals, one above the other. You are given an array a of length n. For all i = 1, 2, ..., n, there should be a straight wire from some point on segment i of the top terminal to some point on segment a_i of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if n=7 and a=[4,1,4,6,7,7,5]. A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red.What is the maximum number of crossings there can be if you place the wires optimally?
Input: ['474 1 4 6 7 7 522 11132 2 2'] Output:['6', '1', '0', '3', '']
[ 0 ]
Given an array a of length n and an integer k, you are tasked to find any two numbers l and r (l <=q r) such that: For each x (l <=q x <=q r), x appears in a at least k times (i.e. k or more array elements are equal to x). The value r-l is maximized. If no numbers satisfy the conditions, output -1.For example, if a=[11, 11, 12, 13, 13, 14, 14] and k=2, then: for l=12, r=14 the first condition fails because 12 does not appear at least k=2 times. for l=13, r=14 the first condition holds, because 13 occurs at least k=2 times in a and 14 occurs at least k=2 times in a. for l=11, r=11 the first condition holds, because 11 occurs at least k=2 times in a. A pair of l and r for which the first condition holds and r-l is maximal is l = 13, r = 14.
Input: ['47 211 11 12 13 13 14 145 16 3 5 2 16 44 3 4 3 3 414 21 1 2 2 2 3 3 3 3 4 4 4 4 4'] Output:['13 14', '1 3', '-1', '1 4', '']
[ 2 ]
Timur has n candies. The i-th candy has a quantity of sugar equal to a_i. So, by eating the i-th candy, Timur consumes a quantity of sugar equal to a_i.Timur will ask you q queries regarding his candies. For the j-th query you have to answer what is the minimum number of candies he needs to eat in order to reach a quantity of sugar greater than or equal to x_j or print -1 if it's not possible to obtain such a quantity. In other words, you should print the minimum possible k such that after eating k candies, Timur consumes a quantity of sugar of at least x_j or say that no possible k exists.Note that he can't eat the same candy twice and queries are independent of each other (Timur can use the same candy in different queries).
Input: ['38 74 3 3 1 1 4 5 911050141522304 11 2 3 431 2546'] Output:['1', '2', '-1', '2', '3', '4', '8', '1', '1', '-1', '']
[ 2, 4 ]
Timur's grandfather gifted him a chessboard to practice his chess skills. This chessboard is a grid a with n rows and m columns with each cell having a non-negative integer written on it. Timur's challenge is to place a bishop on the board such that the sum of all cells attacked by the bishop is maximal. The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. Help him find the maximal sum he can get.
Input: ['44 41 2 2 12 4 2 42 2 3 12 4 2 42 1103 31 1 11 1 11 1 13 30 1 11 0 11 1 0'] Output:['20', '1', '5', '3', '']
[ 0, 2 ]
You are given n words of equal length m, consisting of lowercase Latin alphabet letters. The i-th word is denoted s_i.In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example: you can change 'e' to 'd' or to 'f'; 'a' can only be changed to 'b'; 'z' can only be changed to 'y'. The difference between two words is the minimum number of moves required to make them equal. For example, the difference between "best" and "cost" is 1 + 10 + 0 + 0 = 11.Find the minimum difference of s_i and s_j such that (i < j). In other words, find the minimum difference over all possible pairs of the n words.
Input: ['62 4bestcost6 3abbzbabefcduooozzz2 7aaabbbcbbaezfe3 2ababab2 8aaaaaaaazzzzzzzz3 1auy'] Output:['11', '8', '35', '0', '200', '4', '']
[ 0, 2, 3 ]
There are n boxes with different quantities of candies in each of them. The i-th box has a_i candies inside.You also have n friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don't want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.What's the minimum total number of candies you have to eat to satisfy the requirements?
Input: ['551 2 3 4 561000 1000 5 1000 1000 1000101 2 3 5 1 2 7 9 13 538 8 8110000000'] Output:['10', '4975', '38', '0', '0', '']
[ 2, 3 ]
Vlad and Nastya live in a city consisting of n houses and n-1 road. From each house, you can get to the other by moving only along the roads. That is, the city is a tree.Vlad lives in a house with index x, and Nastya lives in a house with index y. Vlad decided to visit Nastya. However, he remembered that he had postponed for later k things that he has to do before coming to Nastya. To do the i-th thing, he needs to come to the a_i-th house, things can be done in any order. In 1 minute, he can walk from one house to another if they are connected by a road.Vlad does not really like walking, so he is interested what is the minimum number of minutes he has to spend on the road to do all things and then come to Nastya. Houses a_1, a_2, ..., a_k he can visit in any order. He can visit any house multiple times (if he wants).
Input: ['33 11 321 31 26 43 51 6 2 11 33 43 55 65 26 23 25 31 33 43 55 65 2'] Output:['3', '7', '2', '']
[ 2 ]
You are given a string s of lowercase Latin letters. The following operation can be used: select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or replace all 'a' with 'z'. And you are given the integer k — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than k operations.The string a=a_1a_2 ... a_n is lexicographically smaller than the string b = b_1b_2 ... b_n if there exists an index k (1 <= k <= n) such that a_1=b_1, a_2=b_2, ..., a_{k-1}=b_{k-1}, but a_k < b_k.
Input: ['43 2cba4 5fgde7 5gndcafb4 19ekyv'] Output:['aaa', 'agaa', 'bnbbabb', 'aapp', '']
[ 2 ]
Given n integers a_1, a_2, ..., a_n. You can perform the following operation on them: select any element a_i (1 <= i <= n) and divide it by 2 (round down). In other words, you can replace any selected element a_i with the value <=ft \lfloor \frac{a_i}{2}\right\rfloor (where <=ft \lfloor x \right\rfloor is – round down the real number x). Output the minimum number of operations that must be done for a sequence of integers to become strictly increasing (that is, for the condition a_1 \lt a_2 \lt ... \lt a_n to be satisfied). Or determine that it is impossible to obtain such a sequence. Note that elements cannot be swapped. The only possible operation is described above.For example, let n = 3 and a sequence of numbers [3, 6, 5] be given. Then it is enough to perform two operations on it: Write the number <=ft \lfloor \frac{6}{2}\right\rfloor = 3 instead of the number a_2=6 and get the sequence [3, 3, 5]; Then replace a_1=3 with <=ft \lfloor \frac{3}{2}\right\rfloor = 1 and get the sequence [1, 3, 5]. The resulting sequence is strictly increasing because 1 \lt 3 \lt 5.
Input: ['733 6 545 3 2 151 2 3 4 51100000000042 8 7 558 26 5 21 1025 14'] Output:['2', '-1', '0', '0', '4', '11', '0', '']
[ 2 ]
In the pet store on sale there are: a packs of dog food; b packs of cat food; c packs of universal food (such food is suitable for both dogs and cats). Polycarp has x dogs and y cats. Is it possible that he will be able to buy food for all his animals in the store? Each of his dogs and each of his cats should receive one pack of suitable food for it.
Input: ['71 1 4 2 30 0 0 0 05 5 0 4 61 1 1 1 150000000 50000000 100000000 100000000 1000000000 0 0 100000000 1000000001 3 2 2 5'] Output:['YES', 'YES', 'NO', 'YES', 'YES', 'NO', 'NO', '']
[ 2, 3 ]
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size n * m consisting of characters '.' (empty cell of the desktop) and '*' (an icon).The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.In one move, you can take one icon and move it to any empty cell in the desktop.Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer q queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?Note that queries are permanent and change the state of the desktop.
Input: ['4 4 8', '..**', '.*..', '*...', '...*', '1 3', '2 3', '3 1', '2 3', '3 4', '4 3', '2 3', '2 2', ''] Output:['3', '4', '4', '3', '4', '5', '5', '5', '']
[ 2 ]
Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.The wall consists of n sections, aligned in a row. The i-th section initially has durability a_i. If durability of some section becomes 0 or less, this section is considered broken.To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals 2 damage to the target section and 1 damage to adjacent sections. In other words, if the onager shoots at the section x, then the durability of the section x decreases by 2, and the durability of the sections x - 1 and x + 1 (if they exist) decreases by 1 each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!
Input: ['5', '20 10 30 10 20', ''] Output:['10', '']
[ 0, 2, 3, 4 ]
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word a comes earlier than word b in the dictionary if one of the following conditions hold: the first letter of a is less than the first letter of b; the first letters of a and b are the same, and the second letter of a is less than the second letter of b. So, the dictionary looks like that: Word 1: ab Word 2: ac ... Word 25: az Word 26: ba Word 27: bc ... Word 649: zx Word 650: zy You are given a word s from the Berland language. Your task is to find its index in the dictionary.
Input: ['7abacazbabczxzy'] Output:['1', '2', '25', '26', '27', '649', '650', '']
[ 3 ]
You are given two integers x and y. You want to choose two strictly positive (greater than zero) integers a and b, and then apply the following operation to x exactly a times: replace x with b \cdot x.You want to find two positive integers a and b such that x becomes equal to y after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.For example: if x = 3 and y = 75, you may choose a = 2 and b = 5, so that x becomes equal to 3 \cdot 5 \cdot 5 = 75; if x = 100 and y = 100, you may choose a = 3 and b = 1, so that x becomes equal to 100 \cdot 1 \cdot 1 \cdot 1 = 100; if x = 42 and y = 13, there is no answer since you cannot decrease x with the given operations.
Input: ['33 75100 10042 13'] Output:['2 5', '3 1', '0 0', '']
[ 3 ]
This is an interactive problem.A city has n^2 buildings divided into a grid of n rows and n columns. You need to build a road of some length D(A,B) of your choice between each pair of adjacent by side buildings A and B. Due to budget limitations and legal restrictions, the length of each road must be a positive integer and the total length of all roads should not exceed 48\,000.There is a thief in the city who will start from the topmost, leftmost building (in the first row and the first column) and roam around the city, occasionally stealing artifacts from some of the buildings. He can move from one building to another adjacent building by travelling through the road which connects them.You are unable to track down what buildings he visits and what path he follows to reach them. But there is one tracking mechanism in the city. The tracker is capable of storing a single integer x which is initially 0. Each time the thief travels from a building A to another adjacent building B through a road of length D(A,B), the tracker changes x to x\oplus D(A,B). Each time the thief steals from a building, the tracker reports the value x stored in it and resets it back to 0.It is known beforehand that the thief will steal in exactly k buildings but you will know the values returned by the tracker only after the thefts actually happen. Your task is to choose the lengths of roads in such a way that no matter what strategy or routes the thief follows, you will be able to exactly tell the location of all the buildings where the thefts occurred from the values returned by the tracker.
Input: ['2 4', '', '', '', '14', '', '1', '', '14', '', '3', '', ''] Output:['', '1', '8', '2 4', '', '1 2', '', '1 1', '', '1 2', '', '2 1', '']
[ 2, 3 ]
The symbol \wedge is quite ambiguous, especially when used without context. Sometimes it is used to denote a power (a\wedge b = a^b) and sometimes it is used to denote the XOR operation (a\wedge b=a\oplus b). You have an ambiguous expression E=A_1\wedge A_2\wedge A_3\wedge...\wedge A_n. You can replace each \wedge symbol with either a \texttt{Power} operation or a \texttt{XOR} operation to get an unambiguous expression E'.The value of this expression E' is determined according to the following rules: All \texttt{Power} operations are performed before any \texttt{XOR} operation. In other words, the \texttt{Power} operation takes precedence over \texttt{XOR} operation. For example, 4\;\texttt{XOR}\;6\;\texttt{Power}\;2=4\oplus (6^2)=4\oplus 36=32. Consecutive powers are calculated from left to right. For example, 2\;\texttt{Power}\;3 \;\texttt{Power}\;4 = (2^3)^4 = 8^4 = 4096. You are given an array B of length n and an integer k. The array A is given by A_i=2^{B_i} and the expression E is given by E=A_1\wedge A_2\wedge A_3\wedge...\wedge A_n. You need to find the XOR of the values of all possible unambiguous expressions E' which can be obtained from E and has at least k \wedge symbols used as \texttt{XOR} operation. Since the answer can be very large, you need to find it modulo 2^{2^{20}}. Since this number can also be very large, you need to print its binary representation without leading zeroes. If the answer is equal to 0, print 0.
Input: ['3 2', '3 1 2', ''] Output:['1110', '']
[ 3 ]
Long ago, you thought of two finite arithmetic progressions A and B. Then you found out another sequence C containing all elements common to both A and B. It is not hard to see that C is also a finite arithmetic progression. After many years, you forgot what A was but remember B and C. You are, for some reason, determined to find this lost arithmetic progression. Before you begin this eternal search, you want to know how many different finite arithmetic progressions exist which can be your lost progression A. Two arithmetic progressions are considered different if they differ in their first term, common difference or number of terms.It may be possible that there are infinitely many such progressions, in which case you won't even try to look for them! Print -1 in all such cases. Even if there are finite number of them, the answer might be very large. So, you are only interested to find the answer modulo 10^9+7.
Input: ['8-3 1 7-1 2 4-9 3 110 6 32 5 57 5 42 2 1110 5 30 2 92 4 3-11 4 121 12 2-27 4 7-17 8 2-8400 420 10000000000 4620 10'] Output:['0', '10', '-1', '0', '-1', '21', '0', '273000', '']
[ 3 ]
You are given a positive integer n. Let's call some positive integer a without leading zeroes palindromic if it remains the same after reversing the order of its digits. Find the number of distinct ways to express n as a sum of positive palindromic integers. Two ways are considered different if the frequency of at least one palindromic integer is different in them. For example, 5=4+1 and 5=3+1+1 are considered different but 5=3+1+1 and 5=1+3+1 are considered the same. Formally, you need to find the number of distinct multisets of positive palindromic integers the sum of which is equal to n.Since the answer can be quite large, print it modulo 10^9+7.
Input: ['2512'] Output:['7', '74', '']
[ 0, 3 ]
Let's call a string s perfectly balanced if for all possible triplets (t,u,v) such that t is a non-empty substring of s and u and v are characters present in s, the difference between the frequencies of u and v in t is not more than 1.For example, the strings "aba" and "abc" are perfectly balanced but "abb" is not because for the triplet ("bb",'a','b'), the condition is not satisfied.You are given a string s consisting of lowercase English letters only. Your task is to determine whether s is perfectly balanced or not.A string b is called a substring of another string a if b can be obtained by deleting some characters (possibly 0) from the start and some characters (possibly 0) from the end of a.
Input: ['5abaabbabcaaaaaabcba'] Output:['YES', 'NO', 'YES', 'YES', 'NO', '']
[ 0, 2 ]
Alice and Bob are playing a game with strings. There will be t rounds in the game. In each round, there will be a string s consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is allowed to remove any substring of odd length from s.More formally, if there was a string s = s_1s_2 ... s_k the player can choose a substring s_ls_{l+1} ... s_{r-1}s_r with length of corresponding parity and remove it. After that the string will become s = s_1 ... s_{l-1}s_{r+1} ... s_k.After the string becomes empty, the round ends and each player calculates his/her score for this round. The score of a player is the sum of values of all characters removed by him/her. The value of \texttt{a} is 1, the value of \texttt{b} is 2, the value of \texttt{c} is 3, ..., and the value of \texttt{z} is 26. The player with higher score wins the round. For each round, determine the winner and the difference between winner's and loser's scores. Assume that both players play optimally to maximize their score. It can be proved that a draw is impossible.
Input: ['5abaabccbancodeforces'] Output:['Alice 2', 'Alice 4', 'Alice 4', 'Bob 14', 'Alice 93', '']
[ 2 ]
You have a permutation p of integers from 1 to n.You have a strength of s and will perform the following operation some times: Choose an index i such that 1 <=q i <=q |p| and |i-p_i| <=q s. For all j such that 1 <=q j <=q |p| and p_i<p_j, update p_j to p_j-1. Delete the i-th element from p. Formally, update p to [p_1,...,p_{i-1},p_{i+1},...,p_n]. It can be shown that no matter what i you have chosen, p will be a permutation of integers from 1 to |p| after all operations.You want to be able to transform p into the empty permutation. Find the minimum strength s that will allow you to do so.
Input: ['3', '3 2 1', ''] Output:['1', '']
[ 2 ]
You have a binary string a of length n consisting only of digits 0 and 1. You are given q queries. In the i-th query, you are given two indices l and r such that 1 <= l <= r <= n. Let s=a[l,r]. You are allowed to do the following operation on s: Choose two indices x and y such that 1 <= x <= y <= |s|. Let t be the substring t = s[x, y]. Then for all 1 <= i <= |t| - 1, the condition t_i \neq t_{i+1} has to hold. Note that x = y is always a valid substring. Delete the substring s[x, y] from s. For each of the q queries, find the minimum number of operations needed to make s an empty string.Note that for a string s, s[l,r] denotes the subsegment s_l,s_{l+1},...,s_r.
Input: ['5 3', '11011', '2 4', '1 5', '3 5', ''] Output:['1', '3', '2', '']
[ 2 ]
There is a grid with r rows and c columns, where the square on the i-th row and j-th column has an integer a_{i, j} written on it. Initially, all elements are set to 0. We are allowed to do the following operation: Choose indices 1 <= i <= r and 1 <= j <= c, then replace all values on the same row or column as (i, j) with the value xor 1. In other words, for all a_{x, y} where x=i or y=j or both, replace a_{x, y} with a_{x, y} xor 1. You want to form grid b by doing the above operations a finite number of times. However, some elements of b are missing and are replaced with '?' instead.Let k be the number of '?' characters. Among all the 2^k ways of filling up the grid b by replacing each '?' with '0' or '1', count the number of grids, that can be formed by doing the above operation a finite number of times, starting from the grid filled with 0. As this number can be large, output it modulo 998244353.
Input: ['3 3', '?10', '1??', '010', ''] Output:['1', '']
[ 3 ]
oolimry has an array a of length n which he really likes. Today, you have changed his array to b, a permutation of a, to make him sad.Because oolimry is only a duck, he can only perform the following operation to restore his array: Choose two integers i,j such that 1 <=q i,j <=q n. Swap b_i and b_j. The sadness of the array b is the minimum number of operations needed to transform b into a.Given the array a, find any array b which is a permutation of a that has the maximum sadness over all permutations of the array a.
Input: ['222 141 2 3 3'] Output:['1 2', '3 3 2 1', '']
[ 2 ]
This is an interactive problem.There are n words in a text editor. The i-th word has length l_i (1 <=q l_i <=q 2000). The array l is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized.More formally, suppose that the text editor has width w. Let a be an array of length k+1 where 1=a_1 < a_2 < ... < a_{k+1}=n+1. a is a valid array if for all 1 <=q i <=q k, l_{a_i}+1+l_{a_i+1}+1+...+1+l_{a_{i+1}-1} <=q w. Then the height of the text editor is the minimum k over all valid arrays.Note that if w < \max(l_i), the text editor cannot display all the words properly and will crash, and the height of the text editor will be 0 instead.You can ask n+30 queries. In one query, you provide a width w. Then, the grader will return the height h_w of the text editor when its width is w.Find the minimum area of the text editor, which is the minimum value of w \cdot h_w over all w for which h_w \neq 0.The lengths are fixed in advance. In other words, the interactor is not adaptive.
Input: ['6', '', '0', '', '4', '', '2', '', ''] Output:['', '? 1', '', '? 9', '', '? 16', '', '! 32', '']
[ 2, 4 ]
There is an array a of length n. You may perform the following operation any number of times: Choose two indices l and r where 1 <= l < r <= n and a_l = a_r. Then, set a[l ... r] = [a_{l+1}, a_{l+2}, ..., a_r, a_l]. You are also given another array b of length n which is a permutation of a. Determine whether it is possible to transform array a into an array b using the above operation some number of times.
Input: ['551 2 3 3 21 3 3 2 251 2 4 2 14 2 2 1 152 4 5 5 22 2 4 5 531 2 31 2 331 1 22 1 1'] Output:['YES', 'YES', 'NO', 'YES', 'NO', '']
[ 2 ]
You are given an array a of length n. We define the equality of the array as the number of indices 1 <= i <= n - 1 such that a_i = a_{i + 1}. We are allowed to do the following operation: Select two integers i and x such that 1 <= i <= n - 1 and 1 <= x <= 10^9. Then, set a_i and a_{i + 1} to be equal to x. Find the minimum number of operations needed such that the equality of the array is less than or equal to 1.
Input: ['451 1 1 1 152 1 1 1 261 1 2 3 3 461 2 1 4 5 4'] Output:['2', '1', '2', '0', '']
[ 2 ]
There are n logs, the i-th log has a length of a_i meters. Since chopping logs is tiring work, errorgorn and maomao90 have decided to play a game.errorgorn and maomao90 will take turns chopping the logs with errorgorn chopping first. On his turn, the player will pick a log and chop it into 2 pieces. If the length of the chosen log is x, and the lengths of the resulting pieces are y and z, then y and z have to be positive integers, and x=y+z must hold. For example, you can chop a log of length 3 into logs of lengths 2 and 1, but not into logs of lengths 3 and 0, 2 and 2, or 1.5 and 1.5.The player who is unable to make a chop will be the loser. Assuming that both errorgorn and maomao90 play optimally, who will be the winner?
Input: ['242 4 2 111'] Output:['errorgorn', 'maomao90', '']
[ 3 ]
Calculate the number of permutations p of size n with exactly k inversions (pairs of indices (i, j) such that i < j and p_i > p_j) and exactly x indices i such that p_i > p_{i+1}.Yep, that's the whole problem. Good luck!
Input: ['510 6 47 3 1163316 11 7136373 11 1325902 11 11'] Output:['465', '12', '986128624', '7636394', '57118194', '']
[ 0, 3 ]
You are given a sequence of n integers a_1, a_2, ..., a_n. You are also given x integers 1, 2, ..., x.You are asked to insert each of the extra integers into the sequence a. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence.The score of the resulting sequence a' is the sum of absolute differences of adjacent elements in it <=ft(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right).What is the smallest possible score of the resulting sequence a'?
Input: ['41 5103 87 2 1010 26 1 5 7 3 3 9 10 10 14 101 3 1 2'] Output:['9', '15', '31', '13', '']
[ 0, 2 ]
Turbulent times are coming, so you decided to buy sugar in advance. There are n shops around that sell sugar: the i-th shop sells one pack of sugar for a_i coins, but only one pack to one customer each day. So in order to buy several packs, you need to visit several shops.Another problem is that prices are increasing each day: during the first day the cost is a_i, during the second day cost is a_i + 1, during the third day — a_i + 2 and so on for each shop i.On the contrary, your everyday budget is only x coins. In other words, each day you go and buy as many packs as possible with total cost not exceeding x. Note that if you don't spend some amount of coins during a day, you can't use these coins during the next days.Eventually, the cost for each pack will exceed x, and you won't be able to buy even a single pack. So, how many packs will you be able to buy till that moment in total?
Input: ['43 72 1 25 910 20 30 40 501 112 10001 1'] Output:['11', '0', '1', '1500', '']
[ 0, 2, 3, 4 ]
You are given n points with integer coordinates on a coordinate axis OX. The coordinate of the i-th point is x_i. All points' coordinates are distinct and given in strictly increasing order.For each point i, you can do the following operation no more than once: take this point and move it by 1 to the left or to the right (i..e., you can change its coordinate x_i to x_i - 1 or to x_i + 1). In other words, for each point, you choose (separately) its new coordinate. For the i-th point, it can be either x_i - 1, x_i or x_i + 1.Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer l the coordinates of points should be equal to l, l + 1, ..., l + n - 1.Note that the resulting points should have distinct coordinates.You have to answer t independent test cases.
Input: ['521 431 2 341 2 3 71100000032 5 6'] Output:['YES', 'YES', 'NO', 'YES', 'YES', '']
[ 0, 3 ]
Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam.They love equilateral triangles and want to create n equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexagon edge should pass through any of the triangles).You are allowed to add straight lines parallel to the edges of the hexagons. Given n, what is the minimum number of lines you need to add to create at least n equilateral triangles as described? Adding two red lines results in two new yellow equilateral triangles.
Input: ['41234567'] Output:['2', '2', '3', '83', '']
[ 0, 2, 3, 4 ]
While searching for the pizza, baby Hosssam came across two permutations a and b of length n.Recall that a permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).Baby Hosssam forgot about the pizza and started playing around with the two permutations. While he was playing with them, some elements of the first permutation got mixed up with some elements of the second permutation, and to his surprise those elements also formed a permutation of size n.Specifically, he mixed up the permutations to form a new array c in the following way. For each i (1<= i<= n), he either made c_i=a_i or c_i=b_i. The array c is a permutation. You know permutations a, b, and values at some positions in c. Please count the number different permutations c that are consistent with the described process and the given values. Since the answer can be large, print it modulo 10^9+7.It is guaranteed that there exists at least one permutation c that satisfies all the requirements.
Input: ['971 2 3 4 5 6 72 3 1 7 6 5 42 0 1 0 0 0 0111061 5 2 4 6 36 5 3 1 4 26 0 0 0 0 081 6 4 7 2 3 8 53 2 8 1 4 5 6 71 0 0 7 0 3 0 5101 8 6 2 4 7 9 3 10 51 9 2 3 4 10 8 6 7 51 9 2 3 4 10 8 6 7 571 2 3 4 5 6 72 3 1 7 6 5 40 0 0 0 0 0 051 2 3 4 51 2 3 4 50 0 0 0 051 2 3 4 51 2 3 5 40 0 0 0 031 2 33 1 20 0 0'] Output:['4', '1', '2', '2', '1', '8', '1', '2', '2', '']
[ 3 ]
Hosssam decided to sneak into Hemose's room while he is sleeping and change his laptop's password. He already knows the password, which is a string s of length n. He also knows that there are k special letters of the alphabet: c_1,c_2,..., c_k.Hosssam made a program that can do the following. The program considers the current password s of some length m. Then it finds all positions i (1<= i<m) such that s_{i+1} is one of the k special letters. Then it deletes all of those positions from the password s even if s_{i} is a special character. If there are no positions to delete, then the program displays an error message which has a very loud sound. For example, suppose the string s is "abcdef" and the special characters are 'b' and 'd'. If he runs the program once, the positions 1 and 3 will be deleted as they come before special characters, so the password becomes "bdef". If he runs the program again, it deletes position 1, and the password becomes "def". If he is wise, he won't run it a third time.Hosssam wants to know how many times he can run the program on Hemose's laptop without waking him up from the sound of the error message. Can you help him?
Input: ['109iloveslim1 s7joobeel2 o e7basiozi2 s i6khater1 r7abobeih6 a b e h i o5zondl5 a b c e f6shoman2 a h7shetwey2 h y5samez1 m6mouraz1 m'] Output:['5', '2', '3', '5', '1', '0', '3', '5', '2', '0', '']
[ 0 ]
One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.Given an array of n integers a_1,a_2,...,a_n, where a_{i} \neq 0, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order. select two indices i and j (1 <= i,j <= n) such that a_i and a_j have different signs. In other words, one must be positive and one must be negative. swap the signs of a_{i} and a_{j}. For example if you select a_i=3 and a_j=-2, then they will change to a_i=-3 and a_j=2. Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.
Input: ['477 3 2 -11 -13 -17 -2364 10 25 47 71 96671 -35 7 -4 -11 -256-45 9 -48 -67 -55 7'] Output:['NO', 'YES', 'YES', 'NO', '']
[ 2 ]
Let \mathsf{AND} denote the bitwise AND operation, and \mathsf{OR} denote the bitwise OR operation.You are given an array a of length n and a non-negative integer k. You can perform at most k operations on the array of the following type: Select an index i (1 <=q i <=q n) and replace a_i with a_i \mathsf{OR} 2^j where j is any integer between 0 and 30 inclusive. In other words, in an operation you can choose an index i (1 <=q i <=q n) and set the j-th bit of a_i to 1 (0 <=q j <=q 30). Output the maximum possible value of a_1 \mathsf{AND} a_2 \mathsf{AND} ... \mathsf{AND} a_n after performing at most k operations.
Input: ['43 22 1 17 04 6 6 28 6 6 121 3004 43 1 3 1'] Output:['2', '4', '2147483646', '1073741825', '']
[ 2, 3 ]
There are n candies put from left to right on a table. The candies are numbered from left to right. The i-th candy has weight w_i. Alice and Bob eat candies. Alice can eat any number of candies from the left (she can't skip candies, she eats them in a row). Bob can eat any number of candies from the right (he can't skip candies, he eats them in a row). Of course, if Alice ate a candy, Bob can't eat it (and vice versa).They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?
Input: ['4310 20 1062 1 4 2 4 151 2 4 8 1697 3 20 5 15 1 11 8 10'] Output:['2', '6', '0', '7', '']
[ 2, 4 ]
Given n strings, each of length 2, consisting of lowercase Latin alphabet letters from 'a' to 'k', output the number of pairs of indices (i, j) such that i < j and the i-th string and the j-th string differ in exactly one position.In other words, count the number of pairs (i, j) (i < j) such that the i-th string and the j-th string have exactly one position p (1 <=q p <=q 2) such that {s_{i}}_{p} \neq {s_{j}}_{p}.The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow.
Input: ['46abcbdbaaccef7aabbccaccabbaa4kkkkabab5jfjfjkjkjk'] Output:['5', '6', '0', '6', '']
[ 3 ]