task_type
stringclasses
1 value
problem
stringlengths
209
3.39k
answer
stringlengths
35
6.15k
problem_tokens
int64
60
774
answer_tokens
int64
12
2.04k
coding
Solve the programming task below in a Python markdown code block. Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order. A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, `atcoder`, `zscoder` and `agc` are diverse words while `gotou` and `connect` aren't diverse words. Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist. Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}. Constraints * 1 \leq |S| \leq 26 * S is a diverse word. Input Input is given from Standard Input in the following format: S Output Print the next word that appears after S in the dictionary, or `-1` if it doesn't exist. Examples Input atcoder Output atcoderb Input abc Output abcd Input zyxwvutsrqponmlkjihgfedcba Output -1 Input abcdefghijklmnopqrstuvwzyx Output abcdefghijklmnopqrstuvx
{"inputs": ["cba", "bca", "bac", "abd", "bad", "acb", "cab", "dba"], "outputs": ["cbad\n", "bcad\n", "bacd\n", "abdc\n", "badc\n", "acbd\n", "cabd\n", "dbac\n"]}
368
71
coding
Solve the programming task below in a Python markdown code block. A binary gap within a positive number ```num``` is any sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of ```num```. For example: ```9``` has binary representation ```1001``` and contains a binary gap of length ```2```. ```529``` has binary representation ```1000010001``` and contains two binary gaps: one of length ```4``` and one of length ```3```. ```20``` has binary representation ```10100``` and contains one binary gap of length ```1```. ```15``` has binary representation ```1111``` and has ```0``` binary gaps. Write ```function gap(num)``` that,  given a positive ```num```,  returns the length of its longest binary gap. The function should return ```0``` if ```num``` doesn't contain a binary gap. Also feel free to reuse/extend the following starter code: ```python def gap(num): ```
{"functional": "_inputs = [[9]]\n_outputs = [[2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(gap(*i), o[0])"}
233
154
coding
Solve the programming task below in a Python markdown code block. Once N boys and M girls attended a party. You are given a matrix A of N rows and M columns where Aij is 1 if the i-th boy likes the j-th girl, otherwise it will be 0. Note that it is not necessary that if a boy x likes girl y, then girl y should like boy x. You know that if there are two different boys x and y, who both like girl z, then there will be a collision. Can you calculate the number of different collisions at this party? Note that order of boys in the collision doesn't matter. -----Input----- The first line contains a single integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains two space separated integers N, M denoting the number of boys and girls, respectively. Each of the following N lines contain M characters, each of them is either '0' or '1'. -----Output----- For each test case output a single line containing an integer corresponding to the number of collisions at the party. -----Constraints----- - 1 ≤ T ≤ 100 - 1 ≤ N, M ≤ 10 -----Example----- Input: 2 4 3 111 100 110 000 2 2 10 01 Output: 4 0 -----Explanation----- Example Case 1. All three boys like the first girl, so there are (1, 2, 1), (1, 3, 1), (2, 3, 1) collisions with her. Boys 1 and 3 both like the second girl so this is one more collision. Only one boy likes the third girl, so there are no collisions with her and thus we have 4 collisions total. Example Case 2. For each girl there is only one boy who likes her, so there are no collisions at all.
{"inputs": ["2\n4 3\n111\n100\n011\n100\n0 1\n10\n1", "2\n4 3\n101\n100\n011\n100\n0 1\n10\n1", "2\n4 3\n101\n100\n001\n100\n0 1\n10\n1", "2\n4 3\n101\n100\n110\n000\n1 2\n10\n2", "2\n4 3\n101\n100\n110\n000\n0 1\n10\n0", "2\n4 3\n101\n100\n010\n100\n2 1\n10\n0", "2\n4 3\n101\n101\n100\n000\n2 1\n10\n0", "2\n4 3\n101\n100\n011\n100\n0 1\n11\n1"], "outputs": ["5\n0\n", "4\n0\n", "4\n0\n", "3\n0\n", "3\n0\n", "3\n0\n", "4\n0\n", "4\n0\n"]}
415
310
coding
Solve the programming task below in a Python markdown code block. Given a string, you progressively need to concatenate the first letter from the left and the first letter to the right and "1", then the second letter from the left and the second letter to the right and "2", and so on. If the string's length is odd drop the central element. For example: ```python char_concat("abcdef") == 'af1be2cd3' char_concat("abc!def") == 'af1be2cd3' # same result ``` Also feel free to reuse/extend the following starter code: ```python def char_concat(word): ```
{"functional": "_inputs = [['abc def'], ['CodeWars'], ['CodeWars Rocks'], ['1234567890'], [\"$'D8KB)%PO@s\"]]\n_outputs = [['af1be2cd3'], ['Cs1or2da3eW4'], ['Cs1ok2dc3eo4WR5a 6rs7'], ['101292383474565'], [\"$s1'@2DO38P4K%5B)6\"]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(char_concat(*i), o[0])"}
138
257
coding
Solve the programming task below in a Python markdown code block. YouKn0wWho has an integer sequence a_1, a_2, …, a_n. He will perform the following operation until the sequence becomes empty: select an index i such that 1 ≤ i ≤ |a| and a_i is not divisible by (i + 1), and erase this element from the sequence. Here |a| is the length of sequence a at the moment of operation. Note that the sequence a changes and the next operation is performed on this changed sequence. For example, if a=[3,5,4,5], then he can select i = 2, because a_2 = 5 is not divisible by i+1 = 3. After this operation the sequence is [3,4,5]. Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation. Input The first line contains a single integer t (1 ≤ t ≤ 10 000) — the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n over all test cases doesn't exceed 3 ⋅ 10^5. Output For each test case, print "YES" (without quotes) if it is possible to erase the whole sequence using the aforementioned operation, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower). Example Input 5 3 1 2 3 1 2 2 7 7 10 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 8 6 69 696 69696 696969 6969696 69696969 696969696 Output YES NO YES YES NO Note In the first test case, YouKn0wWho can perform the following operations (the erased elements are underlined): [1, \underline{2}, 3] → [\underline{1}, 3] → [\underline{3}] → [ ]. In the second test case, it is impossible to erase the sequence as i can only be 1, and when i=1, a_1 = 2 is divisible by i + 1 = 2.
{"inputs": ["5\n3\n1 8 3\n1\n2\n2\n7 7\n10\n384836991 191890310 15674376 782177068 172478243 550654647 954291757 278929746 405393865 840594328\n8\n6 69 696 69696 687972 87299 69696969 1602686280\n", "5\n3\n1 8 3\n1\n2\n2\n7 7\n10\n384836991 191890310 15674376 892143751 172478243 550654647 954291757 278929746 405393865 840594328\n8\n6 69 696 69696 687972 87299 69696969 1602686280\n", "5\n3\n1 2 3\n1\n2\n2\n9 7\n10\n384836991 191890310 576823355 782177068 140331224 818008580 954291757 160449218 155374934 840594328\n8\n6 69 696 2324 696969 6969696 69696969 696969696\n", "5\n3\n1 4 3\n1\n2\n2\n7 7\n10\n384836991 191890310 576823355 782177068 172478243 818008580 954291757 278929746 229223092 840594328\n8\n6 69 696 69696 598267 32356 69696969 1602686280\n", "5\n3\n1 8 3\n1\n2\n2\n7 7\n10\n384836991 191890310 576823355 782177068 172478243 818008580 954291757 278929746 229223092 840594328\n8\n6 69 696 69696 598267 32356 69696969 1602686280\n", "5\n3\n1 8 3\n1\n2\n2\n7 7\n10\n384836991 191890310 576823355 782177068 172478243 818008580 954291757 278929746 405393865 840594328\n8\n6 69 696 69696 598267 32356 69696969 1602686280\n", "5\n3\n1 2 3\n1\n2\n2\n9 7\n10\n384836991 191890310 576823355 782177068 140331224 818008580 954291757 102467897 155374934 840594328\n8\n6 69 696 2324 696969 6969696 69696969 696969696\n", "5\n3\n1 4 3\n1\n2\n2\n7 7\n10\n384836991 191890310 576823355 782177068 172478243 818008580 954291757 278929746 229223092 840594328\n8\n6 69 696 69696 145083 32356 69696969 1602686280\n"], "outputs": ["YES\nNO\nYES\nNO\nNO\n", "YES\nNO\nYES\nNO\nNO\n", "YES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nYES\nNO\n", "YES\nNO\nYES\nYES\nNO\n"]}
645
1,500
coding
Solve the programming task below in a Python markdown code block. There is a task in Among Us in which $N$ temperature scale with unique readings are given and you have to make all of them equal. In one second you can choose an odd number and add or subtract that number in any one temperature value. Find minimum time (in seconds) required to complete the task. $Note$: Equal value may or may not belong to array. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - First line of each testcase contains single integer $N$, the number of temperature scales - Next line contain $A_i$ for $1 \leq i \leq N$, reading of temperature scales -----Output:----- For each testcase, output a single line the time (in seconds) required to complete the task. -----Constraints----- - $1 \leq T \leq 10^3$ - $1 \leq N \leq 10^3$ - $0 \leq A_i \leq 10^9$ -----Sample Input:----- 3 5 1 2 3 4 5 4 5 2 3 8 2 50 53 -----Sample Output:----- 5 4 1 -----EXPLANATION:----- - In the $2^{nd}$ test case we need $2$ seconds to change $2$ to $8$ , $1$ second to change $3$ and $5$ to $8$ . So total time required is $2+1+1=4$ seconds.
{"inputs": ["3\n5\n1 2 3 4 5\n4\n5 2 3 8\n2\n50 53"], "outputs": ["5\n4\n1"]}
346
46
coding
Solve the programming task below in a Python markdown code block. Finally, the pandemic is over in ChefLand, and the chef is visiting the school again. Chef likes to climb the stairs of his school's floor by skipping one step, sometimes chef climbs the stairs one by one. Simply, the chef can take one or 2 steps in one upward movement. There are N stairs between ground and next floor. The chef is on the ground floor and he wants to go to the next floor with Cheffina but, Cheffina asks chef in how many ways, the chef can reach the next floor normally or any combination of skipping one step, where order doesn't matter. -----Input:----- - First-line will contain $T$, the number of test cases. Then the test cases follow. - Each test case contains a single line of input, two integers $N$. -----Output:----- For each test case, output in a single line answer as the number of ways. -----Constraints----- - $1 \leq T \leq 1000$ - $1 \leq N \leq 10^5$ -----Sample Input:----- 1 3 -----Sample Output:----- 2 -----EXPLANATION:----- ways: [1,1,1], here chef climb to the next floor, one by one stair. [1,2], here chef climb to the next floor, one step first and after that 2 stairs at once. Note, [2,1] consider the same as that of [1,2] hence ignored.
{"inputs": ["1\n3"], "outputs": ["2"]}
326
14
coding
Solve the programming task below in a Python markdown code block. The mayor of your city has decided to throw a party to gather the favour of his people in different regions of the city. There are 3 distinct regions in the city namely A, B, C comprising of P_{A}, P_{B} and P_{C} number of people respectively. However, the mayor knows that people of the region B are in conflict with people of regions A and C. So, there will be a conflict if people from region B are present at the party along with people from region A or C. The mayor wants to invite as many people as possible and also avoid any conflicts. Help him invite maximum number of people to the party. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains three integers P_{A}, P_{B} and P_{C} - number of people living in regions A, B and C respectively. ------ Output Format ------ For each test case, output the maximum number of people that can be invited to the party. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ P_{A}, P_{B}, P_{C} ≤ 1000$ ----- Sample Input 1 ------ 3 2 3 4 1 5 2 8 8 8 ----- Sample Output 1 ------ 6 5 16 ----- explanation 1 ------ Test case-1: Mayor can invite all the people from region $A$ and $C$. So the maximum number of people invited is $6$. Test case-2: Mayor can invite all the people from region $B$. So the maximum number of people invited is $5$.
{"inputs": ["3\n2 3 4\n1 5 2\n8 8 8\n"], "outputs": ["6\n5\n16\n"]}
384
37
coding
Solve the programming task below in a Python markdown code block. # Making Change Complete the method that will determine the minimum number of coins needed to make change for a given amount in American currency. Coins used will be half-dollars, quarters, dimes, nickels, and pennies, worth 50¢, 25¢, 10¢, 5¢ and 1¢, respectively. They'll be represented by the symbols `H`, `Q`, `D`, `N` and `P` (symbols in Ruby, strings in in other languages) The argument passed in will be an integer representing the value in cents. The return value should be a hash/dictionary/object with the symbols as keys, and the numbers of coins as values. Coins that are not used should not be included in the hash. If the argument passed in is 0, then the method should return an empty hash. ## Examples ```python make_change(0) #--> {} make_change(1) #--> {"P":1} make_change(43) #--> {"Q":1, "D":1, "N":1, "P":3} make_change(91) #--> {"H":1, "Q":1, "D":1, "N":1, "P":1} ``` #### **If you liked this kata, check out [Part 2](https://www.codewars.com/kata/making-change-part-2/ruby).** Also feel free to reuse/extend the following starter code: ```python def make_change(amount): ```
{"functional": "_inputs = [[0], [1], [5], [43], [91], [101], [239]]\n_outputs = [[{}], [{'P': 1}], [{'N': 1}], [{'Q': 1, 'D': 1, 'N': 1, 'P': 3}], [{'H': 1, 'Q': 1, 'D': 1, 'N': 1, 'P': 1}], [{'H': 2, 'P': 1}], [{'H': 4, 'Q': 1, 'D': 1, 'P': 4}]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(make_change(*i), o[0])"}
342
281
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.   Please complete the following python code precisely: ```python class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[2,1,1],[1,1,0],[0,1,1]]) == 4\n assert candidate(grid = [[2,1,1],[0,1,1],[1,0,1]]) == -1\n assert candidate(grid = [[0,2]]) == 0\n\n\ncheck(Solution().orangesRotting)"}
135
91
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that: The first ten characters consist of the phone number of passengers. The next character denotes the gender of the person. The following two characters are used to indicate the age of the person. The last two characters determine the seat allotted to that person. Return the number of passengers who are strictly more than 60 years old.   Please complete the following python code precisely: ```python class Solution: def countSeniors(self, details: List[str]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(details = [\"7868190130M7522\",\"5303914400F9211\",\"9273338290F4010\"]) == 2\n assert candidate(details = [\"1313579440F2036\",\"2921522980M5644\"]) == 0\n\n\ncheck(Solution().countSeniors)"}
152
124
coding
Solve the programming task below in a Python markdown code block. Given a complete binary tree with the height of H, we index the nodes respectively top-down and left-right from 1. The i-th node stores a positive integer V_{i}. Define P_{i} as follows: P_{i}=V_{i} if the i-th node is a leaf, otherwise P_{i}=max(V_{i}*P_{L}, V_{i}*P_{R}), where L and R are the indices of the left and right children of i, respectively. Your task is to caculate the value of P_{1}. ------ Input ------ There are several test cases (fifteen at most), each formed as follows: The first line contains a positive integer H (H ≤ 15). The second line contains 2^{H}-1 positive integers (each having a value of 10^{9} at most), the i-th integer shows the value of V_{i}. The input is ended with H = 0. ------ Output ------ For each test case, output on a line an integer which is the respective value of P_{1} found, by modulo of 1,000,000,007. ----- Sample Input 1 ------ 2 1 2 3 3 3 1 5 2 6 4 7 0 ----- Sample Output 1 ------ 3 105 ----- explanation 1 ------ The second test case is constructed as follows: 3 / \ / \ 1 5 / \ / \ 2 6 4 7
{"inputs": ["2\n1 2 3\n3\n3 1 5 2 6 4 7\n0", "2\n1 2 3\n3\n3 1 1 2 6 4 7\n0", "2\n1 2 3\n3\n3 1 1 2 6 1 8\n0", "2\n1 2 1\n3\n3 1 1 2 3 1 8\n0", "2\n1 2 3\n3\n3 2 1 2 6 1 7\n0", "2\n1 2 1\n3\n4 1 1 2 3 1 8\n0", "2\n1 2 3\n3\n1 2 1 2 6 1 7\n0", "2\n2 2 3\n3\n3 1 1 2 6 4 7\n0"], "outputs": ["3\n105", "3\n21\n", "3\n24\n", "2\n24\n", "3\n36\n", "2\n32\n", "3\n12\n", "6\n21\n"]}
347
278
coding
Solve the programming task below in a Python markdown code block. A Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7. The Little Elephant has K favorite lucky strings A_{1}, A_{2}, ..., A_{K}. He thinks that the lucky string S is good if either |S| ≥ 47 or for some j from 1 to K we have that A_{j} is a substring of S. The Little Elephant has found N lucky strings B_{1}, B_{2}, ..., B_{N} under the pillow. Now he wants to know which of them are good. Help him and find for each i from 1 to N whether the string B_{i} is good or not. Notes. Let S be some lucky string. Then |S| denotes the length of the string S; S[i] (1 ≤ i ≤ |S|) denotes the i^{th} character of S (the numeration of characters starts from 1); The string T of the length M is called a substring of S if for some k from 0 to |S| - M we have T[1] = S[k + 1], T[2] = S[k + 2], ..., T[M] = S[k + M]. ------ Input ------ The first line of the input file contains two integers K and N, the number of favorite lucky strings of the Little Elephant and the number of strings he has found under the pillow. Each of the following K lines contains one favorite lucky string. Namely, j^{th} line among these K lines contains the string A_{j}. Each of the following N lines contains one lucky string that was found under the pillow. Namely, i^{th} line among these N lines contains the string B_{i}. The input file does not contain any whitespaces. ------ Output ------ For each of the N strings that were found under the pillow print Good if it is good, and Bad otherwise. ------ Constraints ------ 1 ≤ K, N ≤ 50 For each string S in the input file we have 1 ≤ |S| ≤ 50. Each string in the input file consists only of the lucky digits 4 and 7. ----- Sample Input 1 ------ 2 4 47 744 7444 447 7774 77777777777777777777777777777777777777777777774 ----- Sample Output 1 ------ Good Good Bad Good ----- explanation 1 ------ The string S = 7444 is good since the favorite string 744 is its substring. The string S = 447 is good since the favorite string 47 is its substring. The string S = 7774 is bad since none of the favorite strings 47 and 744 is a substring of S. The string S = 77777777777777777777777777777777777777777777774 is good since its length is 47. Note, however, that S does not have favorite substrings at all.
{"inputs": ["1 5\n52\n13\n486\n7\n1488\n894750131415894825036585194360843535399018392", "1 5\n52\n26\n2866\n7\n9112\n36086970008929625185698756226496234693333611129", "1 3\n7\n953\n744\n7\n10930\n141638385126219138109036667514165414693490507346", "2 4\n0\n548\n1250\n823\n8088\n12453409780445685649202911903565425620133167440", "1 1\n11\n1311\n413\n3\n8257\n206229052434988221866364720153767058416028935873", "2 4\n47\n744\n1534\n447\n7774\n3773507314264381108524987157663829981350194170", "2 1\n51\n744\n2650\n528\n500\n95142413585696281004906476796906063037090967865", "2 1\n51\n744\n2650\n528\n396\n95142413585696281004906476796906063037090967865"], "outputs": ["Bad\nBad\nBad\nBad\nBad\n", "Bad\nBad\nBad\nBad\nGood\n", "Bad\nGood\nGood\n", "Good\nBad\nGood\nGood\n", "Good\n", "Bad\nGood\nBad\nBad\n", "Bad\n", "Bad\n"]}
731
646
coding
Solve the programming task below in a Python markdown code block. Pankhuri hates Algebra. Doesn't matter how hard she tries, she always fails to get good marks in her assessments. One of her marked assessments has been recently returned. She noticed that this time, the professor has only given per question marks and filled the total marks section with an expression consisting of +,- operations rather than a calculated number. For example - if there were 3 questions and she was awarded 10 marks in first question while 6 and 2 marks were deducted in second and third questions, then the professor would have written 10-6-2 in the total marks section. Pankhuri might not be good at algebra but she is very smart. She came up with a brilliant idea. She plans to insert parentheses $($ or $)$ at some places in the expression to create a valid bracket expression which evaluates to the maximum possible value. For example in the above scenario, she could create the expression 10-(6-2) which evaluates to 6 unlike the original expression which evaluates to 4. But the expression given to her is very long and she is not able to figure out the maximum marks that she can get. Given an expression of $\text{N}$ numbers and N-1 operators (either + or -) between them. We can enclose parts of the expression in parentheses to form any valid expression. What is the maximum value of the resulting expression? ------ Input ------ First line contains a single integer $\text{T}$ - the number of test cases. For each testcase, First line contains a single integer $\text{N}$. The second line contains the expression. The input format is of the following form. $\text{N}$ a$_1$ o$_1$ a$_2$ o$_2$ a$_3$ o$_3$ .. o$_{n-1}$ a$_n$ ------ Output ------ For each testcase, print a line containing a single integer representing the maximum value of the expression. ------ Constraints ------ $1 ≤ \text{T} ≤ 10$ $1≤ \text{N} ≤ 200$ $1 ≤ \mathbf{a_{i}} ≤ 10^{9}$ o$_i$ is $+$ or $-$ ------ Sample Input ------ 3 3 8 - 1 - 25 2 38 + 20 4 40 - 8 - 1 + 25 ------ Sample Output ------ 32 58 58 ------ Explanation ------ Test 1: 8 - ( 1 - 25 ) = 32 Test 2: 38 + 20 = 58 Test 3: 40 - ( 8 - ( 1 + 25 ) ) = 58
{"inputs": ["3 \n3 \n8 - 1 - 25\n2\n38 + 20\n4\n40 - 8 - 1 + 25"], "outputs": ["32\n58\n58"]}
616
56
coding
Solve the programming task below in a Python markdown code block. Chef is a private detective. He was asked to investigate a case of murder in the city of Frangton. Chef arrived in Frangton to find out that the mafia was involved in the case. Chef spent some time watching for people that belong to the clan and was able to build a map of relationships between them. He knows that a mafia's organizational structure consists of a single Don, heading a hierarchical criminal organization. Each member reports exactly to one other member of the clan. It's obvious that there are no cycles in the reporting system of the mafia. There are N people in the clan, for simplicity indexed from 1 to N, and Chef knows who each of them report to. Member i reports to member Ri. Now, Chef needs to identfy all potential killers to continue his investigation. Having considerable knowledge about the mafia's activities, Chef knows that the killer must be a minor criminal, that is, one of the members who nobody reports to. Please find the list of potential killers for Chef. Since Don reports to nobody, his Ri will be equal to 0. -----Input----- The first line of input contains one integer N. Next line has N space-separated integers, the ith integer denotes Ri — the person whom the ith member reports to. -----Output----- Output a list of space-separated integers in ascending order — the indices of potential killers. -----Constraints----- - 1 ≤ N ≤ 105 - 1 ≤ Ri ≤ N except for Don, whose Ri equals to 0. - It is guaranteed that there are no cycles in the reporting structure. -----Subtasks----- - Subtask #1 [50 points]: N ≤ 10000 - Subtask #2 [50 points]: No additional constraints -----Example----- Input: 6 0 1 1 2 2 3 Output: 4 5 6 -----Explanation----- The reporting structure:
{"inputs": ["6\n0 1 1 2 2 3"], "outputs": ["4 5 6"]}
405
28
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.) Return the minimum number of operations to reduce the sum of nums by at least half.   Please complete the following python code precisely: ```python class Solution: def halveArray(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [5,19,8,1]) == 3\n assert candidate(nums = [3,8,20]) == 3\n\n\ncheck(Solution().halveArray)"}
109
56
coding
Solve the programming task below in a Python markdown code block. Lauren has a chart of distinct projected prices for a house over the next several years. She must buy the house in one year and sell it in another, and she must do so at a loss. She wants to minimize her financial loss. Example $price=[20,15,8,2,12]$ Her minimum loss is incurred by purchasing in year $2$ at $price[1]=15$ and reselling in year $5$ at $price[4]=12$. Return $15-12=3$. Function Description Complete the minimumLoss function in the editor below. minimumLoss has the following parameter(s): int price[n]: home prices at each year Returns int: the minimum loss possible Input Format The first line contains an integer $n$, the number of years of house data. The second line contains $n$ space-separated long integers that describe each $\textit{price[i]}$. Constraints $2\leq n\leq2\times10^5$ $1\leq price[i]\leq10^{16}$ All the prices are distinct. A valid answer exists. Subtasks $2\leq n\leq1000$ for $50\%$ of the maximum score. Sample Input 0 3 5 10 3 Sample Output 0 2 Explanation 0 Lauren buys the house in year $1$ at $price[0]=5$ and sells it in year $3$ at $price[2]=3$ for a minimal loss of $5-3=2$. Sample Input 1 5 20 7 8 2 5 Sample Output 1 2 Explanation 1 Lauren buys the house in year $2$ at $price[1]=7$ and sells it in year $5$ at $price[4]=5$ for a minimal loss of $7-5=2$.
{"inputs": ["3\n5 10 3\n", "5\n20 7 8 2 5\n"], "outputs": ["2\n", "2\n"]}
445
40
coding
Solve the programming task below in a Python markdown code block. Regex Failure - Bug Fixing #2 Oh no, Timmy's received some hate mail recently but he knows better. Help Timmy fix his regex filter so he can be awesome again! Also feel free to reuse/extend the following starter code: ```python def filter_words(phrase): ```
{"functional": "_inputs = [[\"You're Bad! timmy!\"], [\"You're MEAN! timmy!\"], [\"You're UGLY!! timmy!\"], [\"You're horrible! timmy!\"], [\"You're HiDeOuS!! timmy!\"]]\n_outputs = [[\"You're awesome! timmy!\"], [\"You're awesome! timmy!\"], [\"You're awesome!! timmy!\"], [\"You're awesome! timmy!\"], [\"You're awesome!! timmy!\"]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(filter_words(*i), o[0])"}
74
263
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k. To decrypt the code, you must replace every number. All the numbers are replaced simultaneously. If k > 0, replace the ith number with the sum of the next k numbers. If k < 0, replace the ith number with the sum of the previous k numbers. If k == 0, replace the ith number with 0. As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1]. Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!   Please complete the following python code precisely: ```python class Solution: def decrypt(self, code: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(code = [5,7,1,4], k = 3) == [12,10,16,13]\n assert candidate(code = [1,2,3,4], k = 0) == [0,0,0,0]\n assert candidate(code = [2,4,9,3], k = -2) == [12,5,6,13]\n\n\ncheck(Solution().decrypt)"}
214
114
coding
Solve the programming task below in a Python markdown code block. 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, \dots, 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----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. The first line of each testcase contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of cards in the deck. The second line contains $n$ pairwise distinct integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the values of the cards. The third line contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of shuffles. The fourth line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_j \le n - 1$) — the amount of cards that are moved on the $j$-th shuffle. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print a single integer — the value of the card on the top of the deck after the deck is shuffled $m$ times. -----Examples----- Input 3 2 1 2 3 1 1 1 4 3 1 4 2 2 3 1 5 2 1 5 4 3 5 3 2 1 2 1 Output 2 3 3 -----Note----- In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be $[2, 1]$. In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial one — the topmost card has value $3$.
{"inputs": ["3\n2\n1 2\n3\n1 1 1\n4\n3 1 4 2\n2\n3 1\n5\n2 1 5 4 3\n5\n3 2 1 2 1\n"], "outputs": ["2\n3\n3\n"]}
638
72
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key. Return a list of all k-distant indices sorted in increasing order.   Please complete the following python code precisely: ```python class Solution: def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [3,4,9,1,3,9,5], key = 9, k = 1) == [1,2,3,4,5,6]\n assert candidate(nums = [2,2,2,2,2], key = 2, k = 2) == [0,1,2,3,4]\n\n\ncheck(Solution().findKDistantIndices)"}
127
105
coding
Solve the programming task below in a Python markdown code block. n evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number k. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by k or decrease all numbers at points in the set by k. You would like to eventually end up with all numbers equal to 0. Is it possible? A set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon. -----Input----- The first line of input contains an integer n (3 ≤ n ≤ 100000), the number of points along the circle. The following line contains a string s with exactly n digits, indicating the numbers initially present at each of the points, in clockwise order. -----Output----- Print "YES" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise "NO" (without quotes). You can print each letter in any case (upper or lower). -----Examples----- Input 30 000100000100000110000000001100 Output YES Input 6 314159 Output NO -----Note----- If we label the points from 1 to n, then for the first test case we can set k = 1. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1.
{"inputs": ["3\n000\n", "3\n007\n", "3\n007\n", "3\n000\n", "3\n111\n", "3\n100\n", "3\n001\n", "3\n101\n"], "outputs": ["YES\n", "NO\n", "NO\n", "YES\n", "YES\n", "NO\n", "NO\n", "NO\n"]}
404
102
coding
Solve the programming task below in a Python markdown code block. Given two numbers and an arithmetic operator (the name of it, as a string), return the result of the two numbers having that operator used on them. ```a``` and ```b``` will both be positive integers, and ```a``` will always be the first number in the operation, and ```b``` always the second. The four operators are "add", "subtract", "divide", "multiply". A few examples: ``` javascript ArithmeticFunction.arithmetic(5, 2, "add") => returns 7 ArithmeticFunction.arithmetic(5, 2, "subtract") => returns 3 ArithmeticFunction.arithmetic(5, 2, "multiply") => returns 10 ArithmeticFunction.arithmetic(5, 2, "divide") => returns 2 ``` Try to do it without using if statements! Also feel free to reuse/extend the following starter code: ```python def arithmetic(a, b, operator): ```
{"functional": "_inputs = [[1, 2, 'add'], [8, 2, 'subtract'], [5, 2, 'multiply'], [8, 2, 'divide']]\n_outputs = [[3], [6], [10], [4]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(arithmetic(*i), o[0])"}
219
197
coding
Solve the programming task below in a Python markdown code block. You are given a number of sticks of varying lengths. You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left. At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length. When all the remaining sticks are the same length, they cannot be shortened so discard them. Given the lengths of $n$ sticks, print the number of sticks that are left before each iteration until there are none left. Example $arr=[1,2,3]$ The shortest stick length is $1$, so cut that length from the longer two and discard the pieces of length $1$. Now the lengths are $arr=[1,2]$. Again, the shortest stick is of length $1$, so cut that amount from the longer stick and discard those pieces. There is only one stick left, $arr=[1]$, so discard that stick. The number of sticks at each iteration are $answer=[3,2,1]$. Function Description Complete the cutTheSticks function in the editor below. It should return an array of integers representing the number of sticks before each cut operation is performed. cutTheSticks has the following parameter(s): int arr[n]: the lengths of each stick Returns int[]: the number of sticks after each iteration Input Format The first line contains a single integer $n$, the size of $\textbf{arr}$. The next line contains $n$ space-separated integers, each an $arr\left[i\right]$, where each value represents the length of the $i^{\mbox{th}}$ stick. Constraints $1\leq n\leq1000$ $1\leq arr[i]\leq1000$ Sample Input 0 STDIN Function ----- -------- 6 arr[] size n = 6 5 4 4 2 2 8 arr = [5, 4, 4, 2, 2, 8] Sample Output 0 6 4 2 1 Explanation 0 sticks-length length-of-cut sticks-cut 5 4 4 2 2 8 2 6 3 2 2 _ _ 6 2 4 1 _ _ _ _ 4 1 2 _ _ _ _ _ 3 3 1 _ _ _ _ _ _ DONE DONE Sample Input 1 8 1 2 3 4 3 3 2 1 Sample Output 1 8 6 4 1 Explanation 1 sticks-length length-of-cut sticks-cut 1 2 3 4 3 3 2 1 1 8 _ 1 2 3 2 2 1 _ 1 6 _ _ 1 2 1 1 _ _ 1 4 _ _ _ 1 _ _ _ _ 1 1 _ _ _ _ _ _ _ _ DONE DONE
{"inputs": ["6\n5 4 4 2 2 8\n", "8\n1 2 3 4 3 3 2 1\n"], "outputs": ["6\n4\n2\n1\n", "8\n6\n4\n1\n"]}
704
62
coding
Solve the programming task below in a Python markdown code block. You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above. The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner? -----Constraints----- - -40 \leq X \leq 40 - X is an integer. -----Input----- Input is given from Standard Input in the following format: X -----Output----- Print Yes if you will turn on the air conditioner; print No otherwise. -----Sample Input----- 25 -----Sample Output----- No
{"inputs": ["3", "1", "5", "0", "6", "2", "4", "8"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
129
62
coding
Solve the programming task below in a Python markdown code block. You are given an array of integers. Your task is to sort odd numbers within the array in ascending order, and even numbers in descending order. Note that zero is an even number. If you have an empty array, you need to return it. For example: ``` [5, 3, 2, 8, 1, 4] --> [1, 3, 8, 4, 5, 2] odd numbers ascending: [1, 3, 5 ] even numbers descending: [ 8, 4, 2] ``` Also feel free to reuse/extend the following starter code: ```python def sort_array(a): ```
{"functional": "_inputs = [[[5, 3, 2, 8, 1, 4, 11]], [[2, 22, 37, 11, 4, 1, 5, 0]], [[1, 111, 11, 11, 2, 1, 5, 0]], [[]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]], [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3, 4, 9, 8, 7, 6, 5]]]\n_outputs = [[[1, 3, 8, 4, 5, 2, 11]], [[22, 4, 1, 5, 2, 11, 37, 0]], [[1, 1, 5, 11, 2, 11, 111, 0]], [[]], [[1, 8, 3, 6, 5, 4, 7, 2, 9, 0]], [[8, 1, 6, 3, 4, 5, 2, 7, 0, 9]], [[8, 1, 6, 3, 4, 5, 2, 7, 0, 9]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sort_array(*i), o[0])"}
162
486
coding
Solve the programming task below in a Python markdown code block. You are visiting a large electronics store to buy a refrigerator and a microwave. The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen. You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time. You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required. Constraints * All values in input are integers. * 1 \le A \le 10^5 * 1 \le B \le 10^5 * 1 \le M \le 10^5 * 1 \le a_i , b_i , c_i \le 10^5 * 1 \le x_i \le A * 1 \le y_i \le B * c_i \le a_{x_i} + b_{y_i} Input Input is given from Standard Input in the following format: A B M a_1 a_2 ... a_A b_1 b_2 ... b_B x_1 y_1 c_1 \vdots x_M y_M c_M Output Print the answer. Examples Input 2 3 1 3 3 3 3 3 1 2 1 Output 5 Input 1 1 2 10 10 1 1 5 1 1 10 Output 10 Input 2 2 1 3 5 3 5 2 2 2 Output 6
{"inputs": ["2 2 1\n3 1\n3 5\n2 2 2", "2 2 1\n3 2\n3 8\n2 2 2", "2 4 0\n2 1\n1 5\n1 2 2", "2 4 0\n2 1\n0 5\n1 2 2", "2 2 1\n2 1\n3 5\n2 2 2", "2 2 1\n2 1\n3 8\n2 2 2", "2 2 1\n3 1\n3 8\n2 2 2", "2 4 1\n2 1\n3 5\n2 2 2"], "outputs": ["4\n", "5\n", "2\n", "1\n", "4\n", "4\n", "4\n", "4\n"]}
432
206
coding
Solve the programming task below in a Python markdown code block. Penny has an array of $n$ integers, $[a_0,a_1,\ldots,a_{n-1}]$. She wants to find the number of unique multisets she can form using elements from the array such that the bitwise XOR of all the elements of the multiset is a prime number. Recall that a multiset is a set which can contain duplicate elements. Given $\textit{q}$ queries where each query consists of an array of integers, can you help Penny find and print the number of valid multisets for each array? As these values can be quite large, modulo each answer by $10^9+7$ before printing it on a new line. Input Format The first line contains a single integer, $\textit{q}$, denoting the number of queries. The $2\cdot q$ subsequent lines describe each query in the following format: The first line contains a single integer, $n$, denoting the number of integers in the array. The second line contains $n$ space-separated integers describing the respective values of $a_0,a_1,\ldots,a_{n-1}$. Constraints $1\leq q\leq10$ $1\leq n\leq1000000$ $3500\leq a_i\leq4500$ Output Format On a new line for each query, print a single integer denoting the number of unique multisets Penny can construct using numbers from the array such that the bitwise XOR of all the multiset's elements is prime. As this value is quite large, your answer must be modulo $10^9+7$. Sample Input 1 3 3511 3671 4153 Sample Output 4 Explanation The valid multisets are: $\{3511\}\rightarrow3511$ is prime. $\{3671\}\to3671$ is prime. $\{4153\}\rightarrow4153$ is prime. $\{3511,3671,4153\}\to3511\oplus3671\oplus4153\text{=5081}$, which is prime. Because there are four valid multisets, we print the value of $4\%(10^9+7)=4$ on a new line.
{"inputs": ["1 \n3 \n3511 3671 4153 \n"], "outputs": ["4\n"]}
535
37
coding
Solve the programming task below in a Python markdown code block. As we know, Danny has a huge army and each time she wins a battle the size of her army is increased. Missandei, her advisor and Handmaiden, keeps the log of each battle and the amount and strength of soldiers won in a battle. Now, Danny has some questions for Missandei in which she asks the strength of the army gained during some battles. She will ask the query of the form $L, R$ and Missandei has to tell the strength gained in between the $L$ and $R$ battles. Help Missandei. You are given an array $A$, where $A[i]$ represents the number of soldiers won in $i^{th}$ battle and the strength of a soldier who was won in $i^{th}$ battle is given in another array $B[i]$. ( $B[i]$ represents the strength of each soldier who was won in the $i^{th}$ battle). ------ Input: ------ First line will contain $N$ and $Q$, number of battles fought and number of queries. Next line contains $N$ space separated integers denoting the array $A$. Next line contains $N$ space separated integers denoting the array $B$. Next $Q$ lines contain two integers $L$ and $R$. ------ Output: ------ For each query, output the strength gained in between the $L$ and $R$ battles (both inclusive). ------ Constraints ------ $1 ≤ N, Q ≤ 1000$ $0 ≤ A[i] ≤ 10^{6}$ $0 ≤ B[i] ≤ 10^{6}$ $1 ≤ L ≤ R ≤ N$ ----- Sample Input 1 ------ 5 3 2 3 1 6 5 1 2 3 4 5 2 3 1 1 1 5 ----- Sample Output 1 ------ 9 2 60 ----- explanation 1 ------ For the first query, strength gained in $2^{nd}$ battle is $2*3 = 6$ and in $3^{rd}$ battle is $3*1 = 3$. Total = $6 + 3 = 9$.
{"inputs": ["5 3\n2 3 1 6 5\n1 2 3 4 5\n2 3\n1 1\n1 5"], "outputs": ["9\n2\n60"]}
481
51
coding
Solve the programming task below in a Python markdown code block. You are given two integers K and S. Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K. How many different assignments of values to X, Y and Z are there such that X + Y + Z = S? -----Constraints----- - 2≤K≤2500 - 0≤S≤3K - K and S are integers. -----Input----- The input is given from Standard Input in the following format: K S -----Output----- Print the number of the triples of X, Y and Z that satisfy the condition. -----Sample Input----- 2 2 -----Sample Output----- 6 There are six triples of X, Y and Z that satisfy the condition: - X = 0, Y = 0, Z = 2 - X = 0, Y = 2, Z = 0 - X = 2, Y = 0, Z = 0 - X = 0, Y = 1, Z = 1 - X = 1, Y = 0, Z = 1 - X = 1, Y = 1, Z = 0
{"inputs": ["2 2\n", "5 15\n", "1050 0\n", "148 35\n", "602 293\n", "446 491\n", "2500 3750\n", "2500 7500\n"], "outputs": ["6\n", "1\n", "1\n", "666\n", "43365\n", "118173\n", "4691251\n", "1\n"]}
260
130
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value. If it is possible, return any [i, j] with i + 1 < j, such that: arr[0], arr[1], ..., arr[i] is the first part, arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part, and arr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part. All three parts have equal binary values. If it is not possible, return [-1, -1]. Note that the entire part is used when considering what binary value it represents. For example, [1,1,0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.   Please complete the following python code precisely: ```python class Solution: def threeEqualParts(self, arr: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(arr = [1,0,1,0,1]) == [0,3]\n assert candidate(arr = [1,1,0,1,1]) == [-1,-1]\n assert candidate(arr = [1,1,0,0,1]) == [0,2]\n\n\ncheck(Solution().threeEqualParts)"}
254
89
coding
Solve the programming task below in a Python markdown code block. -----Problem Statement----- Sereja has a sequence of n integers a[1], a[2], ..., a[n]. Sereja can do following transformation of the array: - create a new sequence of n integers b[1], b[2], ..., b[n]in this way: (1 ≤ i ≤ n) - Replace the sequence a by b, i.e., a[i] = b[i] for all i in [1, n] Sereja decided to use his transformation k times. Then he computed the value of , where r — the sequence obtained after k transformations of sequence a, as described above. Sereja lost sequence a, but he was left with the numbers q(r) and k. Now Sereja is interested in the question : what is the number of the sequences of the integers с[1], с[2], ..., с[n], such that 1 ≤ c[i] ≤ m and q(d) = q(r), where d — the sequence obtained after k transformations of sequence c, as described above. -----Input----- The first lines contains a single integer T, denoting the number of test cases. Each test case consist of four integers : n, m, q(r), k. -----Output----- In a single line print the remainder of division the answer of the problem on number 10^9 + 7. -----Constraints----- - 1 ≤ T ≤ 10000 - 1 ≤ n, m, q(r), k ≤ 10^9 -----Example----- Input: 3 1 1 1 1 2 2 1 1 2 3 1 1 Output: 0 2 4
{"inputs": ["3\n1 1 1 1\n2 2 1 1\n2 3 1 1", "3\n0 1 1 1\n2 2 1 1\n2 3 1 1", "3\n0 1 1 1\n4 2 1 1\n2 3 1 1", "3\n0 1 1 1\n4 1 1 1\n2 3 1 1", "3\n1 1 1 1\n2 2 1 1\n2 3 1 1", "3\n-1 1 1 0\n4 1 1 1\n2 3 0 1", "3\n-1 1 1 1\n4 1 1 1\n2 3 1 1", "3\n-1 1 1 0\n4 1 1 1\n2 3 1 1"], "outputs": ["0\n2\n4", "0\n2\n4\n", "0\n14\n4\n", "0\n0\n4\n", "0\n2\n4", "0\n0\n6\n", "0\n0\n4\n", "0\n0\n4\n"]}
398
288
coding
Solve the programming task below in a Python markdown code block. There's a waiting room with N chairs set in single row. Chairs are consecutively numbered from 1 to N. First is closest to the entrance (which is exit as well). For some reason people choose a chair in the following way 1. Find a place as far from other people as possible 2. Find a place as close to exit as possible All chairs must be occupied before the first person will be served So it looks like this for 10 chairs and 10 patients Chairs 1 2 3 4 5 6 7 8 9 10 Patients 1 7 5 8 3 9 4 6 10 2 Your task is to find last patient's chair's number. Input - N - integer greater than 2 - number of chairs. Output should positive integer too - last patient's chair's number Have fun :) Also feel free to reuse/extend the following starter code: ```python def last_chair(n): ```
{"functional": "_inputs = [[10]]\n_outputs = [[9]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(last_chair(*i), o[0])"}
230
156
coding
Solve the programming task below in a Python markdown code block. Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. There are three friends; let's call them A, B, C. They made the following statements: A: "I have $x$ Rupees more than B." B: "I have $y$ rupees more than C." C: "I have $z$ rupees more than A." You do not know the exact values of $x, y, z$. Instead, you are given their absolute values, i.e. $X = |x|$, $Y = |y|$ and $Z = |z|$. Note that $x$, $y$, $z$ may be negative; "having $-r$ rupees more" is the same as "having $r$ rupees less". Find out if there is some way to assign amounts of money to A, B, C such that all of their statements are true. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains three space-separated integers $X$, $Y$ and $Z$. ------ Output ------ For each test case, print a single line containing the string "yes" if the presented scenario is possible or "no" otherwise (without quotes). ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ X, Y, Z ≤ 1,000$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ T ≤ 30$ $1 ≤ X, Y, Z ≤ 3$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 2 1 2 1 1 1 1 ----- Sample Output 1 ------ yes no ----- explanation 1 ------ Example 1: One possible way to satisfy all conditions is: A has $10$ rupees, B has $9$ rupees and C has $11$ rupees. Therefore, we have $x = 1$, $y = -2$, $z = 1$. Example 2: There is no way for all conditions to be satisfied.
{"inputs": ["2\n1 2 1\n1 1 1"], "outputs": ["yes\nno"]}
512
26
coding
Solve the programming task below in a Python markdown code block. The Chef has reached the finals of the Annual Inter-school Declamation contest. For the finals, students were asked to prepare 10 topics. However, Chef was only able to prepare three topics, numbered A, B and C — he is totally blank about the other topics. This means Chef can only win the contest if he gets the topics A, B or C to speak about. On the contest day, Chef gets topic X. Determine whether Chef has any chances of winning the competition. Print "Yes" if it is possible for Chef to win the contest, else print "No". You may print each character of the string in either uppercase or lowercase (for example, the strings yEs, yes, Yes, and YES will all be treated as identical). ------ Input Format ------ - The first and only line of input will contain a single line containing four space-separated integers A, B, C, and X — the three topics Chef has prepared and the topic that was given to him on contest day. ------ Output Format ------ - For each testcase, output in a single line "Yes" or "No". - You may print each character of the string in either uppercase or lowercase (for example, the strings yEs, yes, Yes, and YES will all be treated as identical). ------ Constraints ------ $1 ≤ A,B,C,X ≤ 10$ $A, B, C$ are distinct. ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 2 3 7 3 ----- Sample Output 1 ------ Yes ----- explanation 1 ------ Chef had prepared the topics: $2, 3, 7$. Chef gets to speak on the topic: $3$. Since Chef had already prepared this, there is a chance that he can win the contest. ----- Sample Input 2 ------ 4 6 8 5 ----- Sample Output 2 ------ No ----- explanation 2 ------ Chef had prepared the topics: $4, 6, 8$. Chef gets to speak on the topic: $5$. Since Chef didn't prepare this topic, there is no chance that he can win the contest.
{"inputs": ["2 3 7 3", "4 6 8 5"], "outputs": ["Yes", "No"]}
468
30
coding
Solve the programming task below in a Python markdown code block. For given two circles $c1$ and $c2$, print 4 if they do not cross (there are 4 common tangent lines), 3 if they are circumscribed (there are 3 common tangent lines), 2 if they intersect (there are 2 common tangent lines), 1 if a circle is inscribed in another (there are 1 common tangent line), 0 if a circle includes another (there is no common tangent line). Constraints * $-1,000 \leq c1x, c1y, c2x, c2y \leq 1,000$ * $1 \leq c1r, c2r \leq 1,000$ * $c1$ and $c2$ are different Input Coordinates and radii of $c1$ and $c2$ are given in the following format. $c1x \; c1y \; c1r$ $c2x \; c2y \; c2r$ $c1x$, $c1y$ and $c1r$ represent the center coordinate and radius of the first circle. $c2x$, $c2y$ and $c2r$ represent the center coordinate and radius of the second circle. All input values are given in integers. Output Print "4", "3", "2", "1" or "0" in a line. Examples Input 1 1 1 6 2 2 Output 4 Input 1 2 1 4 2 2 Output 3 Input 1 2 1 3 2 2 Output 2 Input 0 0 1 1 0 2 Output 1 Input 0 0 1 0 0 2 Output 0
{"inputs": ["0 0 0\n1 0 2", "1 2 1\n3 4 2", "1 2 0\n4 2 2", "0 0 1\n0 1 2", "0 0 0\n0 0 2", "1 1 1\n9 2 2", "0 1 0\n0 0 2", "1 2 1\n5 4 2"], "outputs": ["0\n", "2\n", "4\n", "1\n", "0\n", "4\n", "0\n", "4\n"]}
413
142
coding
Solve the programming task below in a Python markdown code block. Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread. Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one. The program cannot delete the letters from the list or rearrange them. Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read. -----Output----- Print a single number — the minimum number of operations needed to make all the letters read. -----Examples----- Input 5 0 1 0 1 0 Output 3 Input 5 1 1 0 0 1 Output 4 Input 2 0 0 Output 0 -----Note----- In the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one. In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter. In the third sample all letters are already read.
{"inputs": ["1\n1\n", "1\n0\n", "1\n0\n", "1\n1\n", "2\n0 0\n", "2\n1 0\n", "2\n1 0\n", "2\n1 1\n"], "outputs": ["1\n", "0\n", "0\n", "1\n", "0\n", "1\n", "1\n", "2\n"]}
466
94
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums and an integer k, find three non-overlapping subarrays of length k with maximum sum and return them. Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.   Please complete the following python code precisely: ```python class Solution: def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,1,2,6,7,5,1], k = 2) == [0,3,5]\n assert candidate(nums = [1,2,1,2,1,2,1,2,1], k = 2) == [0,2,4]\n\n\ncheck(Solution().maxSumOfThreeSubarrays)"}
118
97
coding
Solve the programming task below in a Python markdown code block. These days, chef is very much interested in Mathematics. He has started attending Recitations too! His hunger for problems is increasing day by day! Today, chef was a given a crumpled maths problem, which he is stuck with . He needs your help to do it Here's what his teacher said: "Find sum of all numbers till N, do not include numbers which are powers of K from K, K2, K3... which are less than or equal to N" Easy, right? Can you solve it? -----INPUT----- The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test contains two integers N and K, as per the above given problem specification. -----OUTPUT----- For each test case, output a single line printing the sum of the each test case, in format Case #T: S, where T is the Tth test case running and S is sum of corresponding test case. -----CONSTRAINTS----- 10 < T < 50 10 < N < 104 0 < K < 100 -----EXAMPLE----- Input: 2 10 3 20 2 Output: Case #1: 43 Case #2: 180
{"inputs": ["2\n10 3\n20 2"], "outputs": ["Case #1: 43\nCase #2: 180"]}
287
37
coding
Solve the programming task below in a Python markdown code block. The Collatz conjecture is one of the most famous one. Take any positive integer n, if it is even divide it by 2, if it is odd multiply it by 3 and add 1 and continue indefinitely.The conjecture is that whatever is n the sequence will reach 1. There is many ways to approach this problem, each one of them had given beautifull graphs and impressive display of calculation power. The simplest approach can be found in this kata: http://www.codewars.com/kata/5286b2e162056fd0cb000c20 You look at the Collatz sequence of a number and see when it reaches 1. In this kata we will take a look at the length of collatz sequences. And how they evolve. Write a function that take a positive integer n and return the number between 1 and n that has the maximum Collatz sequence length and the maximum length. The output has to take the form of an array [number, maxLength] For exemple the Collatz sequence of 4 is [4,2,1], 3 is [3,10,5,16,8,4,2,1], 2 is [2,1], 1 is [1], so `MaxCollatzLength(4)` should return `[3,8]`. If n is not a positive integer, the function have to return []. * As you can see, numbers in Collatz sequences may exceed n. The last tests use random big numbers so you may consider some optimisation in your code: * You may get very unlucky and get only hard numbers: try submitting 2-3 times if it times out; if it still does, probably you need to optimize your code more; * Optimisation 1: when calculating the length of a sequence, if n is odd, what 3n+1 will be ? * Optimisation 2: when looping through 1 to n, take i such that i<n/2, what will be the lenght of the sequence for 2i ? Also feel free to reuse/extend the following starter code: ```python def max_collatz_length(n): ```
{"functional": "_inputs = [[0], [1], [4], [30], [1000], [1000000], ['a'], [-1], [['a']]]\n_outputs = [[[]], [[1, 1]], [[3, 8]], [[27, 112]], [[871, 179]], [[837799, 525]], [[]], [[]], [[]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(max_collatz_length(*i), o[0])"}
470
241
coding
Solve the programming task below in a Python markdown code block. You are given an array A of size N. You are also given an integer Q. Can you figure out the answer to each of the Q queries? Each query contains 2 integers x and y, and you need to find whether the value find(x,y) is Odd or Even: find(int x,int y) { if(x>y) return 1; ans = pow(A[x],find(x+1,y)) return ans } Note : pow(a,b) = a^{b}. Input Format The first line of the input contains an integer N. The next line contains N space separated non-negative integers(whole numbers less than or equal to 9). The line after that contains a positive integer, Q , the denotes the number of queries to follow. Q lines follow, each line contains two positive integer x and y separated by a single space. Output Format For each query, display 'Even' if the value returned is Even, otherwise display 'Odd'. Constraints 2 ≤ N ≤ 10^{5} 2 ≤ Q ≤ 10^{5} 1 ≤ x,y ≤ N x ≤ y Array is 1-indexed. No 2 consecutive entries in the array will be zero. Sample Input 3 3 2 7 2 1 2 2 3 Sample Output Odd Even Explanation find(1,2) = 9, which is Odd find(2,3) = 128, which is even
{"inputs": ["3\n3 2 7\n2\n1 2\n2 3\n"], "outputs": ["Odd\nEven\n"]}
331
32
coding
Solve the programming task below in a Python markdown code block. A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time. Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smelt immediately after each event. please. If there are multiple participants with the highest number of participants (or if all participants are 0), output the one with the lowest participant number. input The input is given in the following format. n q a1 v1 a2 v2 :: aq vq n (1 ≤ n ≤ 1000000) represents the number of participants and q (1 ≤ q ≤ 100000) represents the number of events. ai (1 ≤ ai ≤ n) vi (-100 ≤ vi ≤ 100) indicates that participant ai acquired or released vi at the i-th event. For vi, a positive value indicates acquisition, a negative value indicates release, and 0 is never given. output For each event, the participant number and the number of participants who have acquired the most smelt at hand are output on one line separated by one blank. Example Input 3 5 1 4 2 5 1 3 3 6 2 7 Output 1 4 2 5 1 7 1 7 2 12
{"inputs": ["3 5\n1 4\n2 5\n1 3\n3 4\n2 7", "3 5\n1 4\n2 3\n1 3\n3 6\n2 7", "3 5\n1 4\n2 3\n1 3\n3 6\n2 1", "3 5\n1 4\n2 3\n2 3\n3 6\n2 1", "3 5\n1 4\n2 0\n2 3\n3 6\n2 1", "3 5\n1 4\n2 5\n2 3\n3 4\n2 7", "3 5\n1 2\n2 3\n1 3\n3 6\n2 7", "3 5\n1 4\n2 3\n1 3\n3 8\n2 1"], "outputs": ["1 4\n2 5\n1 7\n1 7\n2 12\n", "1 4\n1 4\n1 7\n1 7\n2 10\n", "1 4\n1 4\n1 7\n1 7\n1 7\n", "1 4\n1 4\n2 6\n2 6\n2 7\n", "1 4\n1 4\n1 4\n3 6\n3 6\n", "1 4\n2 5\n2 8\n2 8\n2 15\n", "1 2\n2 3\n1 5\n3 6\n2 10\n", "1 4\n1 4\n1 7\n3 8\n3 8\n"]}
321
386
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer n, return the least number of perfect square numbers that sum to n. A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not.   Please complete the following python code precisely: ```python class Solution: def numSquares(self, n: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 12) == 3 \n assert candidate(n = 13) == 2\n\n\ncheck(Solution().numSquares)"}
123
47
coding
Solve the programming task below in a Python markdown code block. Polycarp has an array $a$ consisting of $n$ integers. He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $n-1$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move. Formally: If it is the first move, he chooses any element and deletes it; If it is the second or any next move: if the last deleted element was odd, Polycarp chooses any even element and deletes it; if the last deleted element was even, Polycarp chooses any odd element and deletes it. If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero. Help Polycarp find this value. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2000$) — the number of elements of $a$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^6$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game. -----Examples----- Input 5 1 5 7 8 2 Output 0 Input 6 5 1 2 4 6 3 Output 0 Input 2 1000000 1000000 Output 1000000
{"inputs": ["5\n1 5 7 8 2\n", "5\n2 1 1 1 1\n", "5\n1 1 1 1 1\n", "5\n1 1 1 1 1\n", "5\n2 1 1 1 1\n", "5\n2 1 1 1 2\n", "5\n1 5 7 1 2\n", "5\n1 5 7 2 2\n"], "outputs": ["0\n", "2\n", "4\n", "4\n", "2\n", "0\n", "2\n", "0\n"]}
480
150
coding
Solve the programming task below in a Python markdown code block. You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following: Hello firstname lastname! You just delved into python. Function Description Complete the print_full_name function in the editor below. print_full_name has the following parameters: string first: the first name string last: the last name Prints string: 'Hello $first name$ $last name$! You just delved into python' where $first name$ and $last name$ are replaced with $first$ and $last$. Input Format The first line contains the first name, and the second line contains the last name. Constraints The length of the first and last names are each ≤ $10$. Sample Input 0 Ross Taylor Sample Output 0 Hello Ross Taylor! You just delved into python. Explanation 0 The input read by the program is stored as a string data type. A string is a collection of characters.
{"inputs": ["Ross\nTaylor\n"], "outputs": ["Hello Ross Taylor! You just delved into python.\n"]}
223
26
coding
Solve the programming task below in a Python markdown code block. G: Working Kou decided to do the same number of jobs every day for the next $ N $. $ A_i $ jobs are added on the $ i $ day of the $ N $ day. Mr. Kou has no work to do now, and he doesn't have to finish all the work by the $ N $ day. How many jobs can you do in a day? However, Mr. Kou is excellent, so he can do as many jobs as he has. input $ N $ is given on the first line. On the second line, $ N $ integers $ A_1, A_2, A_3, \ dots, A_N $ are given, separated by blanks. output Output the maximum number of jobs you can do in a day. Insert a line break at the end. Constraint * $ N $ is an integer greater than or equal to $ 1 $ and less than or equal to $ 100 $ * $ A_1, A_2, A_3, \ dots, A_N $ are integers between $ 1 $ and $ 100 $ Input example 1 Five 4 2 5 3 1 Output example 1 3 If you decide to work more than $ 4 $ a day, you'll run out of work on the second day. Input example 2 Five 9 9 1 9 9 Output example 2 6 Example Input 5 4 2 5 3 1 Output 3
{"inputs": ["5\n4 2 5 3 0", "5\n4 2 3 3 0", "5\n6 2 3 3 0", "5\n6 3 0 3 0", "5\n6 3 0 2 0", "5\n8 3 0 2 0", "5\n9 3 0 2 0", "5\n9 3 0 4 0"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "3\n"]}
339
142
coding
Solve the programming task below in a Python markdown code block. There are $n$ cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from $1$ to $n$. Two fairs are currently taking place in Berland — they are held in two different cities $a$ and $b$ ($1 \le a, b \le n$; $a \ne b$). Find the number of pairs of cities $x$ and $y$ ($x \ne a, x \ne b, y \ne a, y \ne b$) such that if you go from $x$ to $y$ you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities $x,y$ such that any path from $x$ to $y$ goes through $a$ and $b$ (in any order). Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs $(x,y)$ and $(y,x)$ must be taken into account only once. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 4\cdot10^4$) — the number of test cases in the input. Next, $t$ test cases are specified. The first line of each test case contains four integers $n$, $m$, $a$ and $b$ ($4 \le n \le 2\cdot10^5$, $n - 1 \le m \le 5\cdot10^5$, $1 \le a,b \le n$, $a \ne b$) — numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively. The following $m$ lines contain descriptions of roads between cities. Each of road description contains a pair of integers $u_i, v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$) — numbers of cities connected by the road. Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities. The sum of the values of $n$ for all sets of input data in the test does not exceed $2\cdot10^5$. The sum of the values of $m$ for all sets of input data in the test does not exceed $5\cdot10^5$. -----Output----- Print $t$ integers — the answers to the given test cases in the order they are written in the input. -----Example----- Input 3 7 7 3 5 1 2 2 3 3 4 4 5 5 6 6 7 7 5 4 5 2 3 1 2 2 3 3 4 4 1 4 2 4 3 2 1 1 2 2 3 4 1 Output 4 0 1
{"inputs": ["3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n3 4\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n", "3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n4 4\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n", "3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n3 4\n4 1\n4 2\n4 3 4 1\n1 2\n2 3\n4 1\n", "3\n7 7 6 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n1 3\n4 4\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n", "3\n7 7 3 5\n1 2\n2 3\n3 4\n4 5\n3 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n3 4\n4 1\n4 2\n4 3 4 1\n1 2\n2 3\n4 1\n", "3\n7 7 3 6\n1 2\n2 3\n3 4\n6 5\n3 6\n6 7\n7 5\n4 5 2 3\n1 2\n2 3\n1 4\n4 1\n4 2\n4 3 4 1\n1 2\n2 3\n4 1\n", "3\n7 7 4 5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 6\n7 5\n4 5 2 3\n1 2\n1 3\n4 1\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n", "3\n7 7 3 5\n1 4\n2 3\n3 4\n4 5\n5 6\n6 7\n7 5\n4 5 2 3\n1 2\n1 3\n4 1\n4 1\n4 2\n4 3 2 1\n1 2\n2 3\n4 1\n"], "outputs": ["4\n0\n1\n", "4\n0\n1\n", "4\n0\n0\n", "0\n0\n1\n", "0\n0\n0\n", "6\n0\n0\n", "6\n0\n1\n", "2\n0\n1\n"]}
696
774
coding
Solve the programming task below in a Python markdown code block. The Kingdom of Kremland is a tree (a connected undirected graph without cycles) consisting of $n$ vertices. Each vertex $i$ has its own value $a_i$. All vertices are connected in series by edges. Formally, for every $1 \leq i < n$ there is an edge between the vertices of $i$ and $i+1$. Denote the function $f(l, r)$, which takes two integers $l$ and $r$ ($l \leq r$):    We leave in the tree only vertices whose values ​​range from $l$ to $r$.    The value of the function will be the number of connected components in the new graph. Your task is to calculate the following sum: $$\sum_{l=1}^{n} \sum_{r=l}^{n} f(l, r) $$ -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of vertices in the tree. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the values of the vertices. -----Output----- Print one number — the answer to the problem. -----Examples----- Input 3 2 1 3 Output 7 Input 4 2 1 1 3 Output 11 Input 10 1 5 2 5 5 3 10 6 5 1 Output 104 -----Note----- In the first example, the function values ​​will be as follows: $f(1, 1)=1$ (there is only a vertex with the number $2$, which forms one component) $f(1, 2)=1$ (there are vertices $1$ and $2$ that form one component) $f(1, 3)=1$ (all vertices remain, one component is obtained) $f(2, 2)=1$ (only vertex number $1$) $f(2, 3)=2$ (there are vertices $1$ and $3$ that form two components) $f(3, 3)=1$ (only vertex $3$) Totally out $7$. In the second example, the function values ​​will be as follows: $f(1, 1)=1$ $f(1, 2)=1$ $f(1, 3)=1$ $f(1, 4)=1$ $f(2, 2)=1$ $f(2, 3)=2$ $f(2, 4)=2$ $f(3, 3)=1$ $f(3, 4)=1$ $f(4, 4)=0$ (there is no vertex left, so the number of components is $0$) Totally out $11$.
{"inputs": ["1\n1\n", "1\n1\n", "3\n2 1 3\n", "3\n2 1 2\n", "3\n2 0 2\n", "3\n3 0 2\n", "3\n2 1 3\n", "4\n2 1 1 3\n"], "outputs": ["1", "1\n", "7", "7\n", "8\n", "7\n", "7\n", "11"]}
674
110
coding
Solve the programming task below in a Python markdown code block. The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Two rebel spies have provided her with the maps of the solar system. Each map is an N × N grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information. Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out. -----Input----- The first line of the input contains one number N (1 ≤ N ≤ 10) – the dimension of each map. Next N lines each contain N characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next N lines each contain N characters, depicting the second map in the same format. -----Output----- The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations. -----Examples----- Input 4 XOOO XXOO OOOO XXXX XOOO XOOO XOXO XOXX Output Yes Input 2 XX OO XO OX Output No -----Note----- In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise.
{"inputs": ["1\nO\nO\n", "1\nX\nO\n", "1\nO\nO\n", "1\nX\nO\n", "2\nXX\nOO\nXO\nOX\n", "2\nOX\nXX\nOX\nXX\n", "2\nOX\nXO\nXO\nOX\n", "2\nOX\nOX\nXX\nOX\n"], "outputs": ["Yes\n", "No\n", "Yes\n", "No\n", "No\n", "Yes\n", "Yes\n", "No\n"]}
449
121
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums and an integer k. Find the longest subsequence of nums that meets the following requirements: The subsequence is strictly increasing and The difference between adjacent elements in the subsequence is at most k. Return the length of the longest subsequence that meets the requirements. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.   Please complete the following python code precisely: ```python class Solution: def lengthOfLIS(self, nums: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,2,1,4,3,4,5,8,15], k = 3) == 5\n assert candidate(nums = [7,4,5,1,8,12,4,7], k = 5) == 4\n assert candidate(nums = [1,5], k = 1) == 1\n\n\ncheck(Solution().lengthOfLIS)"}
142
106
coding
Solve the programming task below in a Python markdown code block. Chef has opened a new airline. Chef has 10 airplanes where each airplane has a capacity of X passengers. On the first day itself, Y people are willing to book a seat in any one of Chef's airplanes. Given that Chef charges Z rupees for each ticket, find the maximum amount he can earn on the first day. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three space-separated integers X, Y, and Z — the capacity of each airplane, the number of people willing to book a seat in any one of Chef's airplanes on the first day, and the cost of each seat respectively. ------ Output Format ------ For each test case, output on a new line, the maximum amount Chef can earn on the first day. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ X, Y, Z ≤ 100$ ----- Sample Input 1 ------ 4 2 15 10 1 10 1 5 60 100 1 11 7 ----- Sample Output 1 ------ 150 10 5000 70 ----- explanation 1 ------ Test case $1$: Chef has $10$ airplanes and each airplane has a capacity of $2$ passengers. Thus, there are $20$ seats available in total. There are $15$ people who want to book a seat. Since we have enough seats for everyone, all $15$ people can book their seats and pay $10$ rupees each. The total money Chef earns is $15\cdot 10 = 150$. Test case $2$: Chef has $10$ airplanes and each airplane has a capacity of $1$ passenger. Thus, there are $10$ seats available in total. There are $10$ people who want to book a seat. Since we have enough seats for everyone, all $10$ people can book their seats and pay $1$ rupee each. The total money Chef earns is $10\cdot 1 = 10$. Test case $3$: Chef has $10$ airplanes and each airplane has a capacity of $5$ passengers. Thus, there are $50$ seats available in total. There are $60$ people who want to book a seat. Since we have only $50$ seats, only $50$ people can book their seats and pay $100$ rupees each. The total money Chef earns is $50\cdot 100 = 5000$. Test case $4$: Chef has $10$ airplanes and each airplane has a capacity of $1$ passenger. Thus, there are $10$ seats available in total. There are $11$ people who want to book a seat. Since we have only $10$ seats, only $10$ people can book their seats and pay $7$ rupees each. The total money Chef earns is $10\cdot 7 = 70$.
{"inputs": ["4\n2 15 10\n1 10 1\n5 60 100\n1 11 7\n"], "outputs": ["150\n10\n5000\n70\n"]}
689
58
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word. Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text. Return the string after rearranging the spaces.   Please complete the following python code precisely: ```python class Solution: def reorderSpaces(self, text: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(text = \" this is a sentence \") == \"this is a sentence\"\n assert candidate(text = \" practice makes perfect\") == \"practice makes perfect \"\n assert candidate(text = \"hello world\") == \"hello world\"\n assert candidate(text = \" walks udp package into bar a\") == \"walks udp package into bar a \"\n assert candidate(text = \"a\") == \"a\"\n\n\ncheck(Solution().reorderSpaces)"}
159
132
coding
Solve the programming task below in a Python markdown code block. As you might know, cooking is the process of taking a food item and subjecting it to various processes(like heating, roasting, baking etc). A food item gets prepared after it has been subjected to exactly N processes. The order in which the processes are applied matters(heating and then baking is different from baking and then heating). Also, the same processes cannot be aplied twice in succession. For example, heating → baking → heating is allowed, but heating → heating → baking is not allowed because 'heating' comes twice in succession. Any given sequence A_{1}, A_{2}, A_{3}, ... A_{N} of N processes can be used to cook a food item if and only if A_{i} ≠ A_{i+1} for all 1 ≤ i ≤ N-1. The chefs kitchen has got K equipments for K different processes. Chef has to cook two dishes in parallel. This means that if the first dish is prepared by applying processes A_{1}, A_{2}, A_{3}, ... A_{N} in this order, and the second dish made by processes B_{1}, B_{2}, B_{3}, ... B_{N}, then A_{i} ≠ B_{i} for any 1 ≤ i ≤ N, because otherwise chef would need two equipments for the process A_{i}. Needless to say, 1 ≤ A_{i}, B_{i} ≤ K, no two consecutive elements of A are same, and no two consecutive elements of B are same. Given N, K your task is to find the number of ways in which in which he can prepare the two dishes. Since the number of ways can be very huge, you have to report it modulo 1000000007. ------ Input Description ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case is described by line containing two space separated integers, N and K as per the problem description. ------ Output Description ------ For each Test case, output a separate line containing the answer modulo 1000000007. ------ Constraints ------ $T ≤ 100 $ $1 ≤ N, K ≤ 10^{9}$ Subtask 1 (30 points): N, K ≤ 5 ------ Subtask 2 (20 points): ------ N, K ≤ 10000 the answer(without taking modulo 1000000007) will be at most 10^{4}. Subtask 3 (25 points): N, K ≤ 10000 Subtask 4 (25 points): No special constraints ----- Sample Input 1 ------ 3 2 2 2 3 1 3 ----- Sample Output 1 ------ 2 18 6 ----- explanation 1 ------ For first test case, there are two ways: a) A = {1, 2} and B = {2, 1} and b) A = {2, 1} and B = {1,2}. For third test case, A and B are of length 1. A0 can take three different values and for each value of A0, B0 can take any of the other two values.
{"inputs": ["3\n2 2\n2 3\n1 3", "3\n0 2\n2 3\n1 3", "3\n0 2\n2 3\n1 5", "3\n0 2\n2 5\n1 5", "3\n2 2\n2 3\n2 3", "3\n0 2\n2 4\n1 5", "3\n0 1\n2 5\n1 5", "3\n0 2\n3 4\n1 5"], "outputs": ["2\n18\n6", "2\n18\n6\n", "2\n18\n20\n", "2\n260\n20\n", "2\n18\n18\n", "2\n84\n20\n", "0\n260\n20\n", "2\n588\n20\n"]}
732
206
coding
Solve the programming task below in a Python markdown code block. Let's say we have a number, `num`. Find the number of values of `n` such that: there exists `n` consecutive **positive** values that sum up to `num`. A positive number is `> 0`. `n` can also be 1. ```python #Examples num = 1 #1 return 1 num = 15 #15, (7, 8), (4, 5, 6), (1, 2, 3, 4, 5) return 4 num = 48 #48, (15, 16, 17) return 2 num = 97 #97, (48, 49) return 2 ``` The upper limit is `$10^8$` Also feel free to reuse/extend the following starter code: ```python def consecutive_sum(num): ```
{"functional": "_inputs = [[1], [15], [48], [97]]\n_outputs = [[1], [4], [2], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(consecutive_sum(*i), o[0])"}
215
176
coding
Solve the programming task below in a Python markdown code block. Allen has a LOT of money. He has $n$ dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are $1$, $5$, $10$, $20$, $100$. What is the minimum number of bills Allen could receive after withdrawing his entire balance? -----Input----- The first and only line of input contains a single integer $n$ ($1 \le n \le 10^9$). -----Output----- Output the minimum number of bills that Allen could receive. -----Examples----- Input 125 Output 3 Input 43 Output 5 Input 1000000000 Output 10000000 -----Note----- In the first sample case, Allen can withdraw this with a $100$ dollar bill, a $20$ dollar bill, and a $5$ dollar bill. There is no way for Allen to receive $125$ dollars in one or two bills. In the second sample case, Allen can withdraw two $20$ dollar bills and three $1$ dollar bills. In the third sample case, Allen can withdraw $100000000$ (ten million!) $100$ dollar bills.
{"inputs": ["4\n", "5\n", "1\n", "7\n", "7\n", "6\n", "1\n", "5\n"], "outputs": ["4\n", "1\n", "1\n", "3\n", "3\n", "2\n", "1\n", "1\n"]}
298
70
coding
Solve the programming task below in a Python markdown code block. Given an array $a=[a_1,a_2,\dots,a_n]$ of $n$ positive integers, you can do operations of two types on it: Add $1$ to every element with an odd index. In other words change the array as follows: $a_1 := a_1 +1, a_3 := a_3 + 1, a_5 := a_5+1, \dots$. Add $1$ to every element with an even index. In other words change the array as follows: $a_2 := a_2 +1, a_4 := a_4 + 1, a_6 := a_6+1, \dots$. Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations. Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The first line of each test case contains an integer $n$ ($2 \leq n \leq 50$) — the length of the array. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^3$) — the elements of the array. Note that after the performed operations the elements in the array can become greater than $10^3$. -----Output----- Output $t$ lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). -----Examples----- Input 4 3 1 2 1 4 2 2 2 3 4 2 2 2 2 5 1000 1 1000 1 1000 Output YES NO YES YES -----Note----- For the first test case, we can increment the elements with an even index, obtaining the array $[1, 3, 1]$, which contains only odd numbers, so the answer is "YES". For the second test case, we can show that after performing any number of operations we won't be able to make all elements have the same parity, so the answer is "NO". For the third test case, all elements already have the same parity so the answer is "YES". For the fourth test case, we can perform one operation and increase all elements at odd positions by $1$, thus obtaining the array $[1001, 1, 1001, 1, 1001]$, and all elements become odd so the answer is "YES".
{"inputs": ["1\n2\n514 514\n", "4\n3\n1 2 1\n4\n2 2 2 3\n4\n2 2 2 2\n5\n1000 1 1000 1 1000\n"], "outputs": ["YES\n", "YES\nNO\nYES\nYES\n"]}
708
87
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group. Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.   Please complete the following python code precisely: ```python class Solution: def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 4, dislikes = [[1,2],[1,3],[2,4]]) == True\n assert candidate(n = 3, dislikes = [[1,2],[1,3],[2,3]]) == False\n assert candidate(n = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]) == False\n\n\ncheck(Solution().possibleBipartition)"}
147
108
coding
Solve the programming task below in a Python markdown code block. Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session. The game involves $n$ players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session. Find the minimum possible amount of balls that were involved in the game. -----Input----- There are several test cases in the input data. The first line contains a single integer $t$ ($1 \leq t \leq 5 \cdot 10^4$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^5$) — the number of players. The second line of the test case contains a sequence of integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$), where $a_i$ is the number of passes delivered by the $i$-th player. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print a single integer — the answer to the problem. -----Examples----- Input 4 4 2 3 3 2 3 1 5 2 2 0 0 4 1000000000 1000000000 1000000000 1000000000 Output 1 2 0 1 -----Note----- In the first test case, with the only ball, the game can go like this: $2 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2$. In the second test case, there is no possible way to play the game with only one ball. One possible way to play with two balls: $2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1$. $2 \rightarrow 3 \rightarrow 2 \rightarrow 1$ In the third example, there were no passes, so $0$ balls are possible.
{"inputs": ["1\n2\n1000000000 0\n", "1\n5\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "1\n6\n100000000 999999999 999999999 999999999 999999999 1000000000\n", "4\n4\n2 3 3 2\n3\n1 5 2\n2\n0 0\n4\n1000000000 1000000000 1000000000 1000000000\n", "1\n10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n", "1\n200\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5\n"], "outputs": ["1000000000\n", "1\n", "1\n", "1\n2\n0\n1\n", "1\n", "1\n"]}
552
791
coding
Solve the programming task below in a Python markdown code block. 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}, \dots, 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, \dots, a_{2k}$ for integer $k = \lfloor\frac{n-1}{2}\rfloor$ should be maximum possible). You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. The second line of the test case contains $n$ integers $a_0, a_1, \dots, a_{n-1}$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$. It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of $a$. -----Example----- 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
{"inputs": ["3\n5\n17 6 4 4 4\n7\n19 5 13 11 12 13 5\n1\n213567876\n", "3\n5\n17 6 4 4 4\n7\n19 5 13 11 12 13 5\n1\n213567876\n", "3\n5\n27 6 4 4 4\n7\n19 5 13 11 12 13 5\n1\n213567876\n", "3\n5\n27 6 4 4 4\n7\n36 2 13 11 12 13 8\n1\n213567876\n", "3\n5\n27 6 4 4 4\n7\n19 2 13 11 12 13 5\n1\n213567876\n", "3\n5\n27 6 4 4 4\n7\n19 2 13 11 12 13 8\n1\n213567876\n", "4\n8\n1 7 3 4 7 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n", "4\n8\n1 7 3 4 8 6 2 9\n5\n1 2 1 2 1\n10\n7 8 4 5 7 6 8 9 7 3\n4\n3 1 2 1\n"], "outputs": ["27\n57\n213567876\n", "27\n57\n213567876\n", "37\n57\n213567876\n", "37\n74\n213567876\n", "37\n57\n213567876\n", "37\n57\n213567876\n", "26\n5\n37\n5\n", "26\n5\n37\n5\n"]}
520
572
coding
Solve the programming task below in a Python markdown code block. We held two competitions: Coding Contest and Robot Maneuver. In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places receive 100000, 200000, and 300000 yen (the currency of Japan), respectively. Furthermore, a contestant taking the first place in both competitions receives an additional 400000 yen. DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot Maneuver. Find the total amount of money he earned. Constraints * 1 \leq X \leq 205 * 1 \leq Y \leq 205 * X and Y are integers. Input Input is given from Standard Input in the following format: X Y Output Print the amount of money DISCO-Kun earned, as an integer. Examples Input 1 1 Output 1000000 Input 3 101 Output 100000 Input 4 4 Output 0
{"inputs": ["1 2", "4 5", "4 2", "2 2", "2 1", "5 4", "4 8", "9 3"], "outputs": ["500000\n", "0\n", "200000\n", "400000\n", "500000\n", "0\n", "0\n", "100000\n"]}
254
103
coding
Solve the programming task below in a Python markdown code block. # Task Imagine `n` horizontal lines and `m` vertical lines. Some of these lines intersect, creating rectangles. How many rectangles are there? # Examples For `n=2, m=2,` the result should be `1`. there is only one 1x1 rectangle. For `n=2, m=3`, the result should be `3`. there are two 1x1 rectangles and one 1x2 rectangle. So `2 + 1 = 3`. For n=3, m=3, the result should be `9`. there are four 1x1 rectangles, two 1x2 rectangles, two 2x1 rectangles and one 2x2 rectangle. So `4 + 2 + 2 + 1 = 9`. # Input & Output - `[input]` integer `n` Number of horizontal lines. Constraints: `0 <= n <= 100` - `[input]` integer `m` Number of vertical lines. Constraints: `0 <= m <= 100` - `[output]` an integer Number of rectangles. Also feel free to reuse/extend the following starter code: ```python def rectangles(n, m): ```
{"functional": "_inputs = [[2, 3], [2, 2], [1, 1], [0, 1], [3, 3], [100, 100]]\n_outputs = [[3], [1], [0], [0], [9], [24502500]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(rectangles(*i), o[0])"}
274
213
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents all the integers from starti to endi inclusively. A containing set is an array nums where each interval from intervals has at least two integers in nums. For example, if intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9] are containing sets. Return the minimum possible size of a containing set.   Please complete the following python code precisely: ```python class Solution: def intersectionSizeTwo(self, intervals: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(intervals = [[1,3],[3,7],[8,9]]) == 5\n assert candidate(intervals = [[1,3],[1,4],[2,5],[3,5]]) == 3\n assert candidate(intervals = [[1,2],[2,3],[2,4],[4,5]]) == 5\n\n\ncheck(Solution().intersectionSizeTwo)"}
166
97
coding
Solve the programming task below in a Python markdown code block. You receive the name of a city as a string, and you need to return a string that shows how many times each letter shows up in the string by using an asterisk (`*`). For example: ``` "Chicago" --> "c:**,h:*,i:*,a:*,g:*,o:*" ``` As you can see, the letter `c` is shown only once, but with 2 asterisks. The return string should include **only the letters** (not the dashes, spaces, apostrophes, etc). There should be no spaces in the output, and the different letters are separated by a comma (`,`) as seen in the example above. Note that the return string must list the letters in order of their first appearence in the original string. More examples: ``` "Bangkok" --> "b:*,a:*,n:*,g:*,k:**,o:*" "Las Vegas" --> "l:*,a:**,s:**,v:*,e:*,g:*" ``` Have fun! ;) Also feel free to reuse/extend the following starter code: ```python def get_strings(city): ```
{"functional": "_inputs = [['Chicago'], ['Bangkok'], ['Las Vegas'], ['Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch']]\n_outputs = [['c:**,h:*,i:*,a:*,g:*,o:*'], ['b:*,a:*,n:*,g:*,k:**,o:*'], ['l:*,a:**,s:**,v:*,e:*,g:*'], ['l:***********,a:***,n:****,f:*,i:***,r:****,p:*,w:****,g:*******,y:*****,o:******,e:*,c:**,h:**,d:*,b:*,t:*,s:*']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_strings(*i), o[0])"}
263
309
coding
Solve the programming task below in a Python markdown code block. Katsusando loves omelette rice. Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone. To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not. The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food. Find the number of the foods liked by all the N people. -----Constraints----- - All values in input are integers. - 1 \leq N, M \leq 30 - 1 \leq K_i \leq M - 1 \leq A_{ij} \leq M - For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct. -----Constraints----- Input is given from Standard Input in the following format: N M K_1 A_{11} A_{12} ... A_{1K_1} K_2 A_{21} A_{22} ... A_{2K_2} : K_N A_{N1} A_{N2} ... A_{NK_N} -----Output----- Print the number of the foods liked by all the N people. -----Sample Input----- 3 4 2 1 3 3 1 2 3 2 3 2 -----Sample Output----- 1 As only the third food is liked by all the three people, 1 should be printed.
{"inputs": ["1 50\n1 4 3 1", "1 52\n5 9 7 8", "1 50\n1 7 6 3", "1 30\n5 4 2 30", "1 30\n6 4 2 30", "1 30\n3 2 3 30", "1 30\n6 6 2 30", "1 30\n5 4 10 6"], "outputs": ["3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n", "3\n"]}
363
155
coding
Solve the programming task below in a Python markdown code block. Petya has n positive integers a_1, a_2, ..., a_{n}. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'. Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 1 000) — the number of Petya's numbers. Each of the following lines contains non-empty string s_{i} consisting of lowercase Latin letters from 'a' to 'j' — the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters. -----Output----- Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests. -----Examples----- Input 3 ab de aj Output 47 Input 5 abcdef ghij bdef accbd g Output 136542 Input 3 aa jj aa Output 44 -----Note----- In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration. In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. In the second example the numbers after the restoration can look like: [11, 22, 11].
{"inputs": ["1\nh\n", "1\na\n", "1\na\n", "1\nh\n", "1\nb\n", "1\ni\n", "1\nc\n", "2\na\na\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n"]}
556
88
coding
Solve the programming task below in a Python markdown code block. You are given a permutation p consisting of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once). Let's call an array a bipartite if the following undirected graph is bipartite: * the graph consists of n vertices; * two vertices i and j are connected by an edge if i < j and a_i > a_j. Your task is to find a bipartite array of integers a of size n, such that a_i = p_i or a_i = -p_i, or report that no such array exists. If there are multiple answers, print any of them. Input The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 10^6) — the size of the permutation. The second line contains n integers p_1, p_2, ..., p_n. The sum of n over all test cases doesn't exceed 10^6. Output For each test case, print the answer in the following format. If such an array a does not exist, print "NO" in a single line. Otherwise, print "YES" in the first line and n integers — array a in the second line. Example Input 4 3 1 2 3 6 1 3 2 6 5 4 4 4 1 3 2 8 3 2 1 6 7 8 5 4 Output YES 1 2 3 NO YES -4 -1 -3 -2 YES -3 -2 1 6 7 -8 -5 -4
{"inputs": ["4\n3\n1 2 3\n6\n1 3 2 6 5 4\n4\n4 1 3 2\n8\n3 2 1 6 7 8 5 4\n", "4\n3\n1 2 3\n6\n1 3 2 6 5 4\n4\n4 1 3 2\n8\n3 0 1 6 7 8 5 4\n", "4\n3\n1 2 3\n6\n1 3 2 6 5 4\n4\n4 1 3 2\n8\n3 2 1 6 7 1 5 4\n", "4\n3\n1 2 0\n6\n1 3 2 6 5 4\n4\n4 1 3 2\n8\n3 0 1 6 7 8 5 4\n", "4\n3\n1 2 3\n6\n1 3 2 6 5 1\n4\n4 1 3 2\n8\n3 2 1 6 7 8 5 4\n", "4\n3\n1 2 3\n6\n1 3 2 0 5 4\n4\n4 1 3 2\n8\n3 2 1 6 7 1 5 4\n", "4\n3\n1 2 0\n6\n1 3 2 6 5 4\n4\n4 1 3 2\n8\n3 0 1 0 7 8 5 4\n", "4\n3\n1 2 0\n6\n1 3 2 6 5 4\n4\n2 1 3 2\n8\n6 0 1 0 7 9 5 4\n"], "outputs": ["YES\n1 2 3\nNO\nYES\n-4 -1 -3 2\nYES\n-3 -2 1 6 7 -8 -5 4\n", "YES\n1 2 3\nNO\nYES\n-4 -1 -3 2\nYES\n-3 0 1 6 7 -8 -5 4\n", "YES\n1 2 3\nNO\nYES\n-4 -1 -3 2\nNO\n", "YES\n-1 -2 0\nNO\nYES\n-4 -1 -3 2\nYES\n-3 0 1 6 7 -8 -5 4\n", "YES\n1 2 3\nNO\nYES\n-4 -1 -3 2\nYES\n-3 -2 1 6 7 -8 -5 4\n", "YES\n1 2 3\nYES\n-1 -3 -2 0 5 4\nYES\n-4 -1 -3 2\nNO\n", "YES\n-1 -2 0\nNO\nYES\n-4 -1 -3 2\nNO\n", "YES\n-1 -2 0\nNO\nYES\n-2 -1 -3 2\nNO\n"]}
403
722
coding
Solve the programming task below in a Python markdown code block. Madison is a little girl who is fond of toys. Her friend Mason works in a toy manufacturing factory . Mason has a 2D board $\mbox{A}$ of size $H\times W$ with $\mbox{H}$ rows and $\mbox{W}$ columns. The board is divided into cells of size $1\times1$ with each cell indicated by its coordinate $(i,j)$. The cell $(i,j)$ has an integer $A_{ij}$ written on it. To create the toy Mason stacks $A_{ij}$ number of cubes of size $1\times1\times1$ on the cell $(i,j)$. Given the description of the board showing the values of $A_{ij}$ and that the price of the toy is equal to the 3d surface area find the price of the toy. Input Format The first line contains two space-separated integers $\mbox{H}$ and $\mbox{W}$ the height and the width of the board respectively. The next $\mbox{H}$ lines contains $\mbox{W}$ space separated integers. The $j^{th}$ integer in $i^{\mbox{th}}$ line denotes $A_{ij}$. Constraints $1\leq H,W\leq100$ $1\le A_{i,j}\le100$ Output Format Print the required answer, i.e the price of the toy, in one line. Sample Input 0 1 1 1 Sample Output 0 6 Explanation 0 The surface area of $1\times1\times1$ cube is 6. Sample Input 1 3 3 1 3 4 2 2 3 1 2 4 Sample Output 1 60 Explanation 1 The object is rotated so the front row matches column 1 of the input, heights 1, 2, and 1. The front face is 1 + 2 + 1 = 4 units in area. The top is 3 units. The sides are 4 units. None of the rear faces are exposed. The underside is 3 units. The front row contributes 4 + 3 + 4 + 3 = 14 units to the surface area.
{"inputs": ["1 1\n1\n", "3 3\n1 3 4\n2 2 3\n1 2 4\n"], "outputs": ["6\n", "60\n"]}
506
47
coding
Solve the programming task below in a Python markdown code block. Chef is on his way to become the new big bull of the stock market but is a bit weak at calculating whether he made a profit or a loss on his deal. Given that Chef bought the stock at value X and sold it at value Y. Help him calculate whether he made a profit, loss, or was it a neutral deal. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of a single line of input containing two space-separated integers X and Y, denoting the value at which Chef bought and sold the stock respectively. ------ Output Format ------ For each test case, output PROFIT if Chef made a profit on the deal, LOSS if Chef incurred a loss on the deal, and NEUTRAL otherwise. The checker is case-insensitive so answers like pROfiT, profit, and PROFIT would be considered the same. ------ Constraints ------ $1 ≤ T ≤ 500$ $1 ≤ X, Y ≤ 100$ ----- Sample Input 1 ------ 4 4 2 8 8 3 4 2 1 ----- Sample Output 1 ------ LOSS NEUTRAL PROFIT LOSS ----- explanation 1 ------ Test case $1$: Since the cost price is greater than the selling price, Chef made a loss in the deal. Test case $2$: Since the cost price is equal to the selling price, the deal was neutral. Test case $3$: Since the cost price is less than the selling price, Chef made a profit in the deal. Test case $4$: Since the cost price is greater than the selling price, Chef made a loss in the deal.
{"inputs": ["4\n4 2\n8 8\n3 4\n2 1\n"], "outputs": ["LOSS\nNEUTRAL\nPROFIT\nLOSS\n"]}
372
39
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Sheokand loves strings. Chef has $N$ strings $S_{1}, S_{2}, \dots, S_{N}$ which he wants to give to Sheokand; however, he doesn't want to give them away for free, so Sheokand must first correctly answer $Q$ queries Chef asks him. In each query, Chef tells Sheokand an integer $R$ and a string $P$. Consider Chef's strings $S_{1}$ through $S_{R}$. Out of these strings, consider all strings such that their longest common prefix with $P$ is maximum possible. Sheokand should find the lexicographically smallest of these strings. Sheokand is busy with his exams. Can you solve the queries for him? ------ Input ------ The first line of the input contains a single integer $N$. $N$ lines follow. For each valid $i$, the $i$-th of these lines contains Chef's string $S_{i}$. The next line contains a single integer $Q$. The following $Q$ lines describe queries. Each of these lines contains an interger $R$, followed by a space and a string $P$. ------ Output ------ For each query, print a single line containing the string that satisfies the required conditions — the answer to that query. ------ Constraints ------ $1 ≤ N ≤ 100,000$ $1 ≤ |S_{i}| ≤ 10$ for each valid $i$ $1 ≤ Q ≤ 100,000$ $1 ≤ R ≤ N$ $1 ≤ |P| ≤ 10$ ------ Subtasks ------ Subtask #1 (30 points): $1 ≤ N, R ≤ 1,000$ Subtask #2 (70 points): original constraints ----- Sample Input 1 ------ 4 abcd abce abcdex abcde 3 3 abcy 3 abcde 4 abcde ----- Sample Output 1 ------ abcd abcdex abcde ----- explanation 1 ------ Query 1: For strings $S_{1}$ through $S_{3}$, the longest common prefix is always "abc", but "abcd" is the lexicographically smallest of these three strings. Query 2: For strings $S_{1}$ through $S_{3}$, the longest common prefix with maximum length is "abcde" and the only string for which it is the LCP is "abcdex", so this is the answer. Query 3: For strings $S_{1}$ through $S_{4}$, the longest common prefix with maximum length is "abcde"; it is the LCP for strings "abcdex" and "abcde", but "abcde" is the lexicographically smaller string, so it is the answer.
{"inputs": ["4\nabcd\nabce\nabcdex\nabcde\n3\n3 abcy\n3 abcde\n4 abcde"], "outputs": ["abcd\nabcdex\nabcde"]}
630
43
coding
Solve the programming task below in a Python markdown code block. There is data that records the customer number of the business partner and the trading date on a monthly basis. Please create a program that reads this month's data and last month's data and outputs the customer number of the company with which you have transactions and the number of transactions for two consecutive months from last month. However, the number of monthly business partners is 1,000 or less. Input This month's data and last month's data are given separated by one blank line. Each data is given in the following format. c1, d1 c2, d2 ... ... ci (1 ≤ ci ≤ 1,000) is an integer representing the customer number, and di (1 ≤ di ≤ 31) is an integer representing the trading day. Output For companies that have transactions for two consecutive months, the customer number and the total number of transactions are output separated by a blank in ascending order of customer number. Example Input 123,10 56,12 34,14 123,3 56,4 123,5 Output 56 2 123 3
{"inputs": ["123,10\n56,12\n35,14\n\n123,3\n56,4\n123,5", "123,10\n56,12\n45,13\n\n113,4\n56,4\n123,5", "132,10\n56,12\n45,13\n\n113,4\n56,4\n123,5", "123,10\n51,62\n34,14\n\n124,3\n56,4\n123,5", "123,10\n65,12\n34,14\n\n123,3\n56,4\n123,5", "123,10\n57,12\n35,14\n\n123,3\n57,4\n123,5", "0032,1\n56,12\n21,45\n\n12,34\n56,4\n32,25", "0032,1\n16,52\n21,45\n\n22,34\n56,4\n32,15"], "outputs": ["56 2\n123 3\n", "56 2\n123 2\n", "56 2\n", "123 2\n", "123 3\n", "57 2\n123 3\n", "32 2\n56 2\n", "32 2\n"]}
261
391
coding
Solve the programming task below in a Python markdown code block. Vasya decided to go to the grocery store. He found in his wallet $a$ coins of $1$ burle and $b$ coins of $2$ burles. He does not yet know the total cost of all goods, so help him find out $s$ ($s > 0$): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins. For example, if $a=1$ and $b=1$ (he has one $1$-burle coin and one $2$-burle coin), then: he can pay $1$ burle without change, paying with one $1$-burle coin, he can pay $2$ burle without change, paying with one $2$-burle coin, he can pay $3$ burle without change by paying with one $1$-burle coin and one $2$-burle coin, he cannot pay $4$ burle without change (moreover, he cannot pay this amount at all). So for $a=1$ and $b=1$ the answer is $s=4$. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. The description of each test case consists of one line containing two integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^8$) — the number of $1$-burle coins and $2$-burles coins Vasya has respectively. -----Output----- For each test case, on a separate line print one integer $s$ ($s > 0$): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all. -----Examples----- Input 5 1 1 4 0 0 2 0 0 2314 2374 Output 4 5 1 1 7063 -----Note----- The first test case of the example is clarified into the main part of the statement. In the second test case, Vasya has only $1$ burle coins, and he can collect either any amount from $1$ to $4$, but $5$ can't. In the second test case, Vasya has only $2$ burle coins, and he cannot pay $1$ burle without change. In the fourth test case you don't have any coins, and he can't even pay $1$ burle.
{"inputs": ["1\n69 74\n", "1\n99 101\n", "1\n3947394 7\n", "1\n3534 1834\n", "1\n9999999 0\n", "1\n12345 12345\n", "1\n112311 11332\n", "1\n1223111 133212\n"], "outputs": ["218\n", "302\n", "3947409\n", "7203\n", "10000000\n", "37036\n", "134976\n", "1489536\n"]}
567
188
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Write an algorithm to print all ways of arranging n queens on an n x n chess board so that none of them share the same row, column, or diagonal. In this case, "diagonal" means all diagonals, not just the two that bisect the board. Notes: This problem is a generalization of the original one in the book. Please complete the following python code precisely: ```python class Solution: def solveNQueens(self, n: int) -> List[List[str]]: ```
{"functional": "def check(candidate):\n assert candidate(4) == [[\".Q..\",\"...Q\",\"Q...\",\"..Q.\"],[\"..Q.\",\"Q...\",\"...Q\",\".Q..\"]]\n\n\ncheck(Solution().solveNQueens)"}
125
62
coding
Solve the programming task below in a Python markdown code block. Vlad went into his appartment house entrance, now he is on the $1$-th floor. He was going to call the elevator to go up to his apartment. There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor $a$ (it is currently motionless), the second elevator is located on floor $b$ and goes to floor $c$ ($b \ne c$). Please note, if $b=1$, then the elevator is already leaving the floor $1$ and Vlad does not have time to enter it. If you call the first elevator, it will immediately start to go to the floor $1$. If you call the second one, then first it will reach the floor $c$ and only then it will go to the floor $1$. It takes $|x - y|$ seconds for each elevator to move from floor $x$ to floor $y$. Vlad wants to call an elevator that will come to him faster. Help him choose such an elevator. -----Input----- The first line of the input contains the only $t$ ($1 \le t \le 10^4$) — the number of test cases. This is followed by $t$ lines, three integers each $a$, $b$ and $c$ ($1 \le a, b, c \le 10^8$, $b \ne c$) — floor numbers described in the statement. -----Output----- Output $t$ numbers, each of which is the answer to the corresponding test case. As an answer, output: $1$, if it is better to call the first elevator; $2$, if it is better to call the second one; $3$, if it doesn't matter which elevator to call (both elevators will arrive in the same time). -----Examples----- Input 3 1 2 3 3 1 2 3 2 1 Output 1 3 2 -----Note----- In the first test case of the example, the first elevator is already on the floor of $1$. In the second test case of the example, when called, the elevators would move as follows: At the time of the call, the first elevator is on the floor of $3$, and the second one is on the floor of $1$, but is already going to another floor; in $1$ second after the call, the first elevator would be on the floor $2$, the second one would also reach the floor $2$ and now can go to the floor $1$; in $2$ seconds, any elevator would reach the floor $1$. In the third test case of the example, the first elevator will arrive in $2$ seconds, and the second in $1$.
{"inputs": ["1\n1902 3 5\n", "1\n2001 1 2\n", "1\n2001 1 3\n", "1\n5086 1 2\n", "1\n2002 1 2\n", "1\n2003 3 2\n", "1\n2002 2 3\n", "1\n5086 5607 4560\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "1\n"]}
598
148
coding
Solve the programming task below in a Python markdown code block. You are batting at the pitch and all eyes are on you, the best player in all of IPL! The opposing team will relentlessly bowl at you an infinite number of balls! You notice that you only ever score a boundary on the M^{th} ball, if N mod M ≤ N/2, where N is the lucky number of your T-shirt. Now you wanna know how many boundaries will you hit in total? Formally, you are given a positive integer N and you need to find the number of positive integers M such that N mod M ≤ N/2. ------ Input Format ------ - The first line contains a single integer T, which denotes the number of test cases. The description of each test case follows. - The first and the only line of each test case contains a single integer N. ------ Output Format ------ For each test case, print in a new line, the number of integers M which follow the above condition. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 2\cdot 10^{9}$ ----- Sample Input 1 ------ 1 3 ----- Sample Output 1 ------ 3 ----- explanation 1 ------ For $ N = 3 $, $N$ $mod$ $M ≤ N/2 $ only for $M={1, 2, 3}$. Hence, the number of possible values of $M$ are 3.
{"inputs": ["1\n3"], "outputs": ["3"]}
313
14
coding
Solve the programming task below in a Python markdown code block. Find a cycle in a directed graph G(V, E). Constraints * 1 ≤ |V| ≤ 100 * 0 ≤ |E| ≤ 1,000 * si ≠ ti Input A directed graph G is given in the following format: |V| |E| s0 t0 s1 t1 : s|E|-1 t|E|-1 |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively. si and ti represent source and target nodes of i-th edge (directed). Output Print 1 if G has cycle(s), 0 otherwise. Examples Input 3 3 0 1 0 2 1 2 Output 0 Input 3 3 0 1 1 2 2 0 Output 1
{"inputs": ["5 3\n0 1\n0 2\n1 2", "8 3\n0 1\n0 2\n1 2", "8 3\n0 2\n0 2\n1 2", "5 3\n0 2\n0 2\n1 2", "8 2\n0 1\n0 2\n1 2", "8 3\n1 2\n0 2\n1 2", "8 2\n0 1\n0 2\n1 4", "8 2\n0 1\n0 2\n2 4"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
219
174
coding
Solve the programming task below in a Python markdown code block. Find the last element of the given argument(s). ## Examples ```python last([1, 2, 3, 4]) ==> 4 last("xyz") ==> "z" last(1, 2, 3, 4) ==> 4 ``` In **javascript** and **CoffeeScript** a **list** will be an `array`, a `string` or the list of `arguments`. (courtesy of [haskell.org](http://www.haskell.org/haskellwiki/99_questions/1_to_10)) Also feel free to reuse/extend the following starter code: ```python def last(*args): ```
{"functional": "_inputs = [[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [['a', 'b', 'c', 'k', 'x', 'y', 'z']], ['abckxyz'], ['a', 'b', 'c', 'z']]\n_outputs = [[10], [10], ['z'], ['z'], ['z']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(last(*i), o[0])"}
157
264
coding
Solve the programming task below in a Python markdown code block. You will be given a string (x) featuring a cat 'C', a dog 'D' and a mouse 'm'. The rest of the string will be made up of '.'. You need to find out if the cat can catch the mouse from it's current position. The cat can jump (j) characters. Also, the cat cannot jump over the dog. So: if j = 5: ```..C.....m.``` returns 'Caught!' <-- not more than j characters between ```.....C............m......``` returns 'Escaped!' <-- as there are more than j characters between the two, the cat can't jump far enough if j = 10: ```...m.........C...D``` returns 'Caught!' <--Cat can jump far enough and jump is not over dog ```...m....D....C.......``` returns 'Protected!' <-- Cat can jump far enough, but dog is in the way, protecting the mouse Finally, if all three animals are not present, return 'boring without all three' Also feel free to reuse/extend the following starter code: ```python def cat_mouse(x,j): ```
{"functional": "_inputs = [['..D.....C.m', 2], ['............C.............D..m...', 8], ['m.C...', 5], ['.CD......m.', 10], ['.CD......m.', 1]]\n_outputs = [['Caught!'], ['Escaped!'], ['boring without all three'], ['Protected!'], ['Escaped!']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(cat_mouse(*i), o[0])"}
255
220
coding
Solve the programming task below in a Python markdown code block. You are given an array consisting of all integers from $[l, r]$ inclusive. For example, if $l = 2$ and $r = 5$, the array would be $[2, 3, 4, 5]$. What's the minimum number of elements you can delete to make the bitwise AND of the array non-zero? A bitwise AND is a binary operation that takes two equal-length binary representations and performs the AND operation on each pair of the corresponding bits. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then $t$ cases follow. The first line of each test case contains two integers $l$ and $r$ ($1 \leq l \leq r \leq 2 \cdot 10^5$) — the description of the array. -----Output----- For each test case, output a single integer — the answer to the problem. -----Examples----- Input 5 1 2 2 8 4 5 1 5 100000 200000 Output 1 3 0 2 31072 -----Note----- In the first test case, the array is $[1, 2]$. Currently, the bitwise AND is $0$, as $1\ \& \ 2 = 0$. However, after deleting $1$ (or $2$), the array becomes $[2]$ (or $[1]$), and the bitwise AND becomes $2$ (or $1$). This can be proven to be the optimal, so the answer is $1$. In the second test case, the array is $[2, 3, 4, 5, 6, 7, 8]$. Currently, the bitwise AND is $0$. However, after deleting $4$, $5$, and $8$, the array becomes $[2, 3, 6, 7]$, and the bitwise AND becomes $2$. This can be proven to be the optimal, so the answer is $3$. Note that there may be other ways to delete $3$ elements.
{"inputs": ["5\n1 2\n2 8\n4 5\n1 5\n100000 200000\n", "5\n1 2\n2 8\n4 5\n2 5\n100000 200000\n", "5\n1 2\n2 8\n4 5\n4 5\n100000 200000\n", "5\n2 2\n2 8\n4 5\n2 5\n100000 200000\n", "5\n1 2\n2 8\n4 5\n4 5\n110000 200000\n", "5\n2 2\n2 8\n4 5\n2 5\n100100 200000\n", "5\n1 2\n2 8\n4 5\n1 5\n100001 200000\n", "5\n1 2\n2 8\n4 5\n4 5\n100000 104325\n"], "outputs": ["1\n3\n0\n2\n31072\n", "1\n3\n0\n2\n31072\n", "1\n3\n0\n0\n31072\n", "0\n3\n0\n2\n31072\n", "1\n3\n0\n0\n21072\n", "0\n3\n0\n2\n30972\n", "1\n3\n0\n2\n31071\n", "1\n3\n0\n0\n0\n"]}
486
402
coding
Solve the programming task below in a Python markdown code block. You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}]. There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed. -----Input----- The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100). The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n). The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array. -----Output----- Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done. -----Example----- Input 6 3 5 1 2 3 4 5 6 2 1 3 2 3 6 1 1 6 2 2 1 5 3 Output 3 3 1 5 2
{"inputs": ["1 1 1\n474812122\n2 1 1\n1\n", "1 1 1\n474812122\n2 1 1\n1\n", "1 1 1\n474812122\n4 1 1\n1\n", "1 1 1\n474702042\n5 1 1\n1\n", "1 1 1\n474812122\n5 1 1\n1\n", "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5\n", "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5\n", "5 2 5\n64 3 1 665 2\n1 1 3\n2 1 5\n1 2 3 4 5\n"], "outputs": ["474812122 \n", "474812122 ", "474812122\n", "474702042\n", "474812122\n", "2 665 3 64 4 \n", "2 665 3 64 4 ", "2 665 3 64 1\n"]}
497
370
coding
Solve the programming task below in a Python markdown code block. The Chef commutes to work every day using the city's underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the station nearest to his restaurant. The Chef doesn't want to change the route he took before, so he simply has to find out how long it will take to reach his restaurant along his usual route. This route is given by a sequence of stations s_{0}, s_{1}, ..., s_{n} where s_{0} is the station where the Chef enters the metro and s_{n} is the station where the Chef exits the metro. Trains are scheduled to run between every two consecutive stations s_{i-1} and s_{i}. Such a schedule is specified by three integers x_{i}, l_{i}, and f_{i}. This means that the first train on this line starts operating at time x_{i}. The time it takes this train to travel from s_{i-1} and s_{i} is exactly l_{i} units. Finally, a train departs from station s_{i-1} every f_{i} minutes following the previous train. That is, a train departs at time x_{i}, x_{i}+f_{i}, x_{i}+2f_{i}, and so on. The Chef is very experienced at navigating the metro so the time it takes him to transfer between trains at a given station is essentially zero. Thus, if the Chef arrives at a station, say s_{i}, the moment that the train from s_{i} to s_{i+1} is scheduled to depart, he skillfully hops on this next train. However, if the Chef arrives when no train to s_{i+1} is scheduled to depart, he must wait until the scheduled departure time. Help the Chef figure out how long it will take him to travel from station s_{0} to station s_{n}. You may assume that the Chef is already at station s_{0} at time 0. ------ Input ------ The first line consists of a single integer denoting the number of test cases (at most 50). Each test case begins with a line containing a single integer n between 1 and 1000 indicating the number of lines the Chef must traverse (so there are n+1 stations s_{0}, s_{1}, ..., s_{n}). The next n lines describe the train schedules between stations, one per line. The i'th such line gives the values x_{i}, l_{i}, and f_{i} for the train that travels between stations s_{i-1} and s_{i}. The x_{i} values will be between 0 and 1000 and the l_{i} and f_{i} values will be between 1 and 1000. ------ Output ------ For each test case you are to output a single integer denoting the minimum time t for which the Chef can reach station s_{n} using the given route. Remember, the Chef starts at s_{0} at time 0. ----- Sample Input 1 ------ 3 2 0 4 7 0 6 5 2 0 1 2 6 2 10 2 1 2 3 0 2 3 ----- Sample Output 1 ------ 11 8 5
{"inputs": ["3\n2\n1 1 7\n0 6 5\n2\n0 1 1\n0 1 1\n2\n0 2 3\n0 1 3", "3\n2\n0 4 7\n0 6 5\n2\n0 1 2\n6 2 10\n2\n1 2 3\n0 2 3", "3\n2\n0 4 7\n0 6 5\n2\n0 1 2\n6 2 10\n2\n1 2 5\n0 2 3", "3\n2\n0 4 7\n0 6 5\n2\n0 1 2\n6 4 10\n2\n1 2 5\n0 2 3", "3\n2\n0 3 7\n0 6 5\n2\n0 1 2\n8 4 10\n2\n1 1 5\n0 2 3", "3\n2\n0 3 7\n0 6 5\n2\n0 1 3\n8 4 10\n2\n1 1 5\n0 4 3", "3\n2\n0 3 7\n0 6 5\n2\n0 1 3\n8 4 10\n2\n1 1 5\n0 4 2", "3\n2\n0 4 7\n0 6 5\n2\n0 1 2\n6 0 10\n2\n1 2 3\n0 2 3"], "outputs": ["11\n2\n4\n", "11\n8\n5", "11\n8\n5\n", "11\n10\n5\n", "11\n12\n5\n", "11\n12\n7\n", "11\n12\n6\n", "11\n6\n5\n"]}
737
448
coding
Solve the programming task below in a Python markdown code block. You are given a binary array$^{\dagger}$ of length $n$. You are allowed to perform one operation on it at most once. In an operation, you can choose any element and flip it: turn a $0$ into a $1$ or vice-versa. What is the maximum number of inversions$^{\ddagger}$ the array can have after performing at most one operation? $^\dagger$ A binary array is an array that contains only zeroes and ones. $^\ddagger$ The number of inversions in an array is the number of pairs of indices $i,j$ such that $i<j$ and $a_i > a_j$. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \leq n \leq 2\cdot10^5$) — the length of the array. The following line contains $n$ space-separated positive integers $a_1$, $a_2$,..., $a_n$ ($0 \leq a_i \leq 1$) — the elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, output a single integer — the maximum number of inversions the array can have after performing at most one operation. -----Examples----- Input 5 4 1 0 1 0 6 0 1 0 0 1 0 2 0 0 8 1 0 1 1 0 0 0 1 3 1 1 1 Output 3 7 1 13 2 -----Note----- For the first test case, the inversions are initially formed by the pairs of indices ($1, 2$), ($1, 4$), ($3, 4$), being a total of $3$, which already is the maximum possible. For the second test case, the inversions are initially formed by the pairs of indices ($2, 3$), ($2, 4$), ($2, 6$), ($5, 6$), being a total of four. But, by flipping the first element, the array becomes ${1, 1, 0, 0, 1, 0}$, which has the inversions formed by the pairs of indices ($1, 3$), ($1, 4$), ($1, 6$), ($2, 3$), ($2, 4$), ($2, 6$), ($5, 6$) which total to $7$ inversions which is the maximum possible.
{"inputs": ["5\n4\n1 0 1 0\n6\n0 1 0 0 1 0\n2\n0 0\n8\n1 0 1 1 0 0 0 1\n3\n1 1 1\n"], "outputs": ["3\n7\n1\n13\n2\n"]}
635
79
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Chef had a hard time arguing with his friend, and after getting a great old kick Chef saw a colored array with N cells, numbered from 1 to N. The kick was so strong that Chef suddenly understood the rules of the game. Each cell is painted with a color. Here the colors are numbered from 1 to M. For any cell i, Chef can repaint it with any color q, and the cost of such operation is C_{i,q} points. However Chef can do at most K repaintings (0 repaintings is possible). After performing all repaintings, each cell will have some color. For each cell i, if cell i has color q then Chef will receive B_{i,q} points. Now Chef is wondering how many points can he receive in total when he repaints optimally. ------ Input ------ The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three space-separated integers N, M and K, denoting the number of cells and the number of colors, the maximal possible number of repaintings respectively. The next line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}, denoting the initial colors of the cells. Then N lines follow. The i^{th} line of them contains M integers B_{i}_{1}, B_{i}_{2}, ..., B_{i}_{M}, where B_{ij} denotes how many points Chef will receive if the cell i will be painted with j-th color after all operations. Then N lines follow. The i^{th} line of them contains M integers C_{i}_{1}, C_{i}_{2}, ..., C_{i}_{M}, where C_{ij} denotes how many points Chef will lose if he repaints the cell i with color j. Note: Be careful that the size of input files can be large. ------ Output ------ For each test case, output a single line containing the maximal possible points. ------ Constraints ------ $1 ≤ T ≤ 5$ $0 ≤ K ≤ 1000$ $1 ≤ N, M ≤ 1000$ $1 ≤ A_{i} ≤ M$ $0 ≤ B_{i,j} ≤ 1000$ $0 ≤ C_{i,j} ≤ 1000$ $If j = A_{i}, then C_{i,j} = 0$ ----- Sample Input 1 ------ 1 4 2 1 1 1 2 2 1 1 1 1 1 1 3 1 0 1 0 1 1 0 1 0 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ For this sample, we can repaint only once, since K = 1. We should repaint 4th cell with color 1. We will pay 1 for this, and receive: 1 (1st cell - 1st color) + 1 (2nd cell -1st color) + 1 (3rd cell - 2nd color) + 3 (4th cell - 1st color) = 6. Hence we get 6 ? 1 = 5 points in total, and it is the optimal answer.
{"inputs": ["1\n4 2 1\n1 1 2 2\n1 1\n1 1\n1 1\n3 1\n0 1\n0 1\n1 0\n1 0", "1\n4 2 1\n1 1 2 2\n1 1\n1 1\n1 1\n3 1\n0 1\n0 1\n1 0\n1 0"], "outputs": ["5", "5"]}
728
110
coding
Solve the programming task below in a Python markdown code block. Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively). It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second. Find the skier's time to roll all the path. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow. Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters. The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$. -----Output----- For each test case, print the desired path time in seconds. -----Example----- Input 5 NNN NS WWEN WWEE NWNWS Output 15 6 16 12 25
{"inputs": ["5\nNNN\nNS\nWWEN\nWWEE\nNWNWS\n", "5\nNNN\nNS\nWWEN\nEEWW\nNWNWS\n", "5\nNNN\nNS\nWWEN\nEWEW\nNWNWS\n", "5\nNNN\nNS\nWWEN\nEWEW\nNSNWW\n", "5\nNNN\nNS\nNEWW\nWWEE\nNSNWW\n", "5\nNNN\nNS\nNEWW\nEEWW\nSNNWW\n", "5\nNNN\nNS\nWEWN\nEWWE\nSWNWN\n", "5\nNNN\nNS\nENWW\nWWEE\nSNNWW\n"], "outputs": ["15\n6\n16\n12\n25\n", "15\n6\n16\n12\n25\n", "15\n6\n16\n8\n25\n", "15\n6\n16\n8\n17\n", "15\n6\n16\n12\n17\n", "15\n6\n16\n12\n21\n", "15\n6\n12\n12\n25\n", "15\n6\n20\n12\n21\n"]}
319
286
coding
Solve the programming task below in a Python markdown code block. In this Kata, you're to complete the function `sum_square_even_root_odd`. You will be given a list of numbers, `nums`, as the only argument to the function. Take each number in the list and *square* it if it is even, or *square root* the number if it is odd. Take this new list and find the *sum*, rounding to two decimal places. The list will never be empty and will only contain values that are greater than or equal to zero. Good luck! Also feel free to reuse/extend the following starter code: ```python def sum_square_even_root_odd(nums): ```
{"functional": "_inputs = [[[4, 5, 7, 8, 1, 2, 3, 0]], [[1, 14, 9, 8, 17, 21]]]\n_outputs = [[91.61], [272.71]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(sum_square_even_root_odd(*i), o[0])"}
141
211
coding
Solve the programming task below in a Python markdown code block. Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? Input The first line contains a single integer n (1 ≤ n ≤ 105) — the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. Output In a single line print the answer to the problem — the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. Examples Input 2 ?ab ??b Output xab Input 2 a b Output ? Input 1 ?a?b Output cacb Note Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on.
{"inputs": ["1\n?\n", "1\nq\n", "1\np\n", "1\nr\n", "1\ns\n", "1\nt\n", "1\nu\n", "1\no\n"], "outputs": ["a", "q", "p\n", "r\n", "s\n", "t\n", "u\n", "o\n"]}
487
84
coding
Solve the programming task below in a Python markdown code block. Nikita has a row of $N$ white tiles indexed from $\mbox{1}$ to $N$. This time, she's painting them green! Find the number of ways Nikita can paint certain tiles in green so that the indices of the green tiles form an Arithmetic Progression. As this value can be quite large, your answer must be modulo $(10^9+7)$. Note: Nikita must paint at least $\mbox{1}$ tile. Input Format The first line contains a single integer, $\mathbf{T}$, denoting the number of test cases. Each test case consists of a single line containing an integer, $N$, denoting the length of row of tiles. Constraints $1\leq T\leq10$ $1\leq N\leq10^{10}$ Scoring $1\leq N\leq2000$ for $20\%$ of test data. $1\leq N\leq10^5$ for $50\%$ of test data. $1\leq N\leq10^{10}$ for $\textbf{100\%}$ of test data. Output Format On a new line for each test case, print the number of ways Nikita can paint her white tiles green so that the indices of the green tiles form an Arithmetic Progression. Because this number can be quite large, your answer must be modulo $(10^9+7)$. Sample Input 3 3 4 5 Sample Output 7 13 22 Explanation Test Case 0: There are $7$ valid ways to paint the tiles: Thus, we print the result of $7\%(10^9+7)$ on a new line, which is $7$. Test Case 1: There are $13$ valid ways to paint the tiles: Thus, we print the result of $13\%(10^9+7)$ on a new line, which is $13$.
{"inputs": ["3\n3\n4\n5\n"], "outputs": ["7\n13\n22\n"]}
454
26
coding
Solve the programming task below in a Python markdown code block. # Task Christmas is coming. In the [previous kata](https://www.codewars.com/kata/5a405ba4e1ce0e1d7800012e), we build a custom Christmas tree with the specified characters and the specified height. Now, we are interested in the center of the Christmas tree. Please imagine that we build a Christmas tree with `chars = "abc" and n = Infinity`, we got: ``` a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b a b c a b c a b a b c a b c a b a b c a b c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | | . . ``` If we keep only the center part of leaves, we will got: ``` a b a a b a . . . ``` As you can see, it's a infinite string, but it has a repeat substring "aba"(spaces will be removed). If we join them together, it looks like:`"abaabaabaabaaba......"`. So, your task is to find the repeat substring of the center part of leaves. # Inputs: - `chars`: the specified characters. In this kata, they always be lowercase letters. # Output: - The repeat substring that satisfy the above conditions. Still not understand the task? Look at the following example ;-) # Examples For `chars = "abc"`,the output should be `"aba"` ``` center leaves sequence: "abaabaabaabaabaaba....." ``` For `chars = "abab"`,the output should be `a` ``` center leaves sequence: "aaaaaaaaaa....." ``` For `chars = "abcde"`,the output should be `aecea` ``` center leaves sequence: "aeceaaeceaaecea....." ``` For `chars = "aaaaaaaaaaaaaa"`,the output should be `a` ``` center leaves sequence: "aaaaaaaaaaaaaa....." ``` For `chars = "abaabaaab"`,the output should be `aba` ``` center leaves sequence: "abaabaabaaba....." ``` Also feel free to reuse/extend the following starter code: ```python def center_of(chars): ```
{"functional": "_inputs = [[''], ['a'], ['ab'], ['abc'], ['abab'], ['abcde'], ['aaaaaaaaaaaaaa'], ['abaabaaab'], ['dbdbebedbddbedededeeddbbdeddbeddeebdeddeebbbb'], ['vttussvutvuvvtustsvsvtvu']]\n_outputs = [[''], ['a'], ['a'], ['aba'], ['a'], ['aecea'], ['a'], ['aba'], ['deededebddeebeddeddeddbddeddeddebeeddbededeed'], ['vsvvtvvtvvsv']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(center_of(*i), o[0])"}
593
266
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two positive integer arrays nums1 and nums2, both of length n. The absolute sum difference of arrays nums1 and nums2 is defined as the sum of |nums1[i] - nums2[i]| for each 0 <= i < n (0-indexed). You can replace at most one element of nums1 with any other element in nums1 to minimize the absolute sum difference. Return the minimum absolute sum difference after replacing at most one element in the array nums1. Since the answer may be large, return it modulo 109 + 7. |x| is defined as: x if x >= 0, or -x if x < 0.   Please complete the following python code precisely: ```python class Solution: def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,7,5], nums2 = [2,3,5]) == 3\n assert candidate(nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]) == 0\n assert candidate(nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]) == 20\n\n\ncheck(Solution().minAbsoluteSumDiff)"}
197
126
coding
Solve the programming task below in a Python markdown code block. Chef is making polygon cakes in his kitchen today! Since the judge panel is very strict, Chef's cakes must be beautiful and have sharp and precise $internal$ angles in arithmetic progression. Given the number of sides, $N$, of the cake Chef is baking today and also the measure of its first angle(smallest angle), $A$, find the measure of the $K^{th}$ angle. -----Input:----- - The first line contains a single integer $T$, the number of test cases. - The next $T$ lines contain three space separated integers $N$, $A$ and $K$, the number of sides of polygon, the first angle and the $K^{th}$ angle respectively. -----Output:----- For each test case, print two space separated integers $X$ and $Y$, such that the $K^{th}$ angle can be written in the form of $X/Y$ and $gcd(X, Y) = 1$ -----Constraints----- - $1 \leq T \leq 50$ - $3 \leq N \leq 1000$ - $1 \leq A \leq 1000000000$ - $1 \leq K \leq N$ - It is guaranteed the answer is always valid. -----Sample Input:----- 1 3 30 2 -----Sample Output:----- 60 1
{"inputs": ["1\n3 30 2"], "outputs": ["60 1"]}
312
22
coding
Solve the programming task below in a Python markdown code block. There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
{"inputs": ["5\nWEEWW\n", "8\nWWWWWEEE\n", "12\nWEWEWEEEWWWE\n"], "outputs": ["1\n", "3\n", "4\n"]}
333
46
coding
Solve the programming task below in a Python markdown code block. The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the princess immediately. Looks like there's no chance to success, but the princess noticed that the dragon is very greedy and not too smart. To delay him, the princess decides to borrow a couple of bijous from his treasury. Once the dragon overtakes the princess, she will drop one bijou to distract him. In this case he will stop, pick up the item, return to the cave and spend f hours to straighten the things out in the treasury. Only after this will he resume the chase again from the very beginning. The princess is going to run on the straight. The distance between the cave and the king's castle she's aiming for is c miles. How many bijous will she need to take from the treasury to be able to reach the castle? If the dragon overtakes the princess at exactly the same moment she has reached the castle, we assume that she reached the castle before the dragon reached her, and doesn't need an extra bijou to hold him off. Input The input data contains integers vp, vd, t, f and c, one per line (1 ≤ vp, vd ≤ 100, 1 ≤ t, f ≤ 10, 1 ≤ c ≤ 1000). Output Output the minimal number of bijous required for the escape to succeed. Examples Input 1 2 1 1 10 Output 2 Input 1 2 1 1 8 Output 1 Note In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more hours; meanwhile the princess will be 4 miles away from the cave. Next time the dragon will overtake the princess 8 miles away from the cave, and she will need the second bijou, but after this she will reach the castle without any further trouble. The second case is similar to the first one, but the second time the dragon overtakes the princess when she has reached the castle, and she won't need the second bijou.
{"inputs": ["1\n2\n1\n1\n6\n", "1\n2\n2\n1\n8\n", "1\n2\n1\n1\n8\n", "2\n1\n1\n1\n10\n", "2\n2\n1\n1\n10\n", "1\n2\n1\n1\n10\n", "83\n7\n7\n7\n46\n", "5\n3\n3\n3\n999\n"], "outputs": ["1\n", "1\n", "1\n", "0\n", "0\n", "2\n", "0\n", "0\n"]}
532
141
coding
Solve the programming task below in a Python markdown code block. The Chessboard Distance for any two points (X_{1}, Y_{1}) and (X_{2}, Y_{2}) on a Cartesian plane is defined as max(|X_{1} - X_{2}|, |Y_{1} - Y_{2}|). You are given two points (X_{1}, Y_{1}) and (X_{2}, Y_{2}). Output their Chessboard Distance. Note that, |P| denotes the absolute value of integer P. For example, |-4| = 4 and |7| = 7. ------ Input Format ------ - First line will contain T, the number of test cases. Then the test cases follow. - Each test case consists of a single line of input containing 4 space separated integers - X_{1}, Y_{1}, X_{2}, Y_{2} - as defined in the problem statement. ------ Output Format ------ For each test case, output in a single line the chessboard distance between (X_{1}, Y_{1}) and (X_{2}, Y_{2}) ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ X_{1}, Y_{1}, X_{2}, Y_{2} ≤ 10^{5}$ ------ subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 2 4 5 1 5 5 5 3 1 4 3 3 ----- Sample Output 1 ------ 3 2 2 ----- explanation 1 ------ - In the first case, the distance between $(2, 4)$ and $(5, 1)$ is $max(|2- 5|, |4 - 1|) = max(|-3|, |3|) = 3$. - In the second case, the distance between $(5, 5)$ and $(5, 3)$ is $max(|5- 5|, |5 - 3|) = max(|0|, |2|) = 2$. - In the third case, the distance between $(1, 4)$ and $(3, 3)$ is $max(|1- 3|, |4 - 3|) = max(|-2|, |1|) = 2$.
{"inputs": ["3\n2 4 5 1\n5 5 5 3\n1 4 3 3\n"], "outputs": ["3\n2\n2\n"]}
513
42
coding
Solve the programming task below in a Python markdown code block. I assume most of you are familiar with the ancient legend of the rice (but I see wikipedia suggests [wheat](https://en.wikipedia.org/wiki/Wheat_and_chessboard_problem), for some reason) problem, but a quick recap for you: a young man asks as a compensation only `1` grain of rice for the first square, `2` grains for the second, `4` for the third, `8` for the fourth and so on, always doubling the previous. Your task is pretty straightforward (but not necessarily easy): given an amount of grains, you need to return up to which square of the chessboard one should count in order to get at least as many. As usual, a few examples might be way better than thousands of words from me: ```python squares_needed(0) == 0 squares_needed(1) == 1 squares_needed(2) == 2 squares_needed(3) == 2 squares_needed(4) == 3 ``` Input is always going to be valid/reasonable: ie: a non negative number; extra cookie for *not* using a loop to compute square-by-square (at least not directly) and instead trying a smarter approach [hint: some peculiar operator]; a trick converting the number might also work: impress me! Also feel free to reuse/extend the following starter code: ```python def squares_needed(grains): ```
{"functional": "_inputs = [[0], [1], [2], [3], [4]]\n_outputs = [[0], [1], [2], [2], [3]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(squares_needed(*i), o[0])"}
311
179
coding
Solve the programming task below in a Python markdown code block. WW3 is near and Gru wants to recruit minions for his team. Gru went to the planet of minions to recruit minions, he saw that there are two villages separated by a river. He cannot recruit minions from both villages because then his team will have internal conflicts. Gru is now in a dilemma about which village to recruit as he wants to have the strongest possible team. You are given coordinates of houses on the planet. Each house has exactly one minion and his power is given. The planet of minion is considered as a 2-D plane and the river is denoted by a straight line ( $y=mx+c$ ). $Note:$ None of the houses are situated on the river. ------ Input: ------ First-line will contain $N$, number of houses. Second-line will contain two integers, $m$ and $c$ denoting the river. Next $N$ lines will have exactly 3 integers $X[i], Y[i], P[i]$ denoting the coordinates of houses and the power of minion in that house ------ Output: ------ Print the maximum power of the team which Gru can recruit. ------ Constraints ------ $1 ≤ N ≤ 10^{5}$ $-10^{4} ≤ m,c ≤ 10^{4}$ $-10^{4} ≤ X[i], Y[i] ≤ 10^{4}$ $ 1 ≤ P[i] ≤ 10^{4}$ ----- Sample Input 1 ------ 3 1 0 0 5 5 0 6 5 0 -8 20 ----- Sample Output 1 ------ 20
{"inputs": ["3\n1 0\n0 5 5\n0 6 5\n0 -8 20"], "outputs": ["20"]}
360
36
coding
Solve the programming task below in a Python markdown code block. In the world of birding there are four-letter codes for the common names of birds. These codes are created by some simple rules: * If the bird's name has only one word, the code takes the first four letters of that word. * If the name is made up of two words, the code takes the first two letters of each word. * If the name is made up of three words, the code is created by taking the first letter from the first two words and the first two letters from the third word. * If the name is four words long, the code uses the first letter from all the words. *(There are other ways that codes are created, but this Kata will only use the four rules listed above)* Complete the function that takes an array of strings of common bird names from North America, and create the codes for those names based on the rules above. The function should return an array of the codes in the same order in which the input names were presented. Additional considertations: * The four-letter codes in the returned array should be in UPPER CASE. * If a common name has a hyphen/dash, it should be considered a space. ## Example If the input array is: `["Black-Capped Chickadee", "Common Tern"]` The return array would be: `["BCCH", "COTE"]` Also feel free to reuse/extend the following starter code: ```python def bird_code(arr): ```
{"functional": "_inputs = [[['American Redstart', 'Northern Cardinal', 'Pine Grosbeak', 'Barred Owl', 'Starling', \"Cooper's Hawk\", 'Pigeon']], [['Great Crested Flycatcher', 'Bobolink', 'American White Pelican', 'Red-Tailed Hawk', 'Eastern Screech Owl', 'Blue Jay']], [['Black-Crowned Night Heron', 'Northern Mockingbird', 'Eastern Meadowlark', 'Dark-Eyed Junco', 'Red-Bellied Woodpecker']], [['Scarlet Tanager', 'Great Blue Heron', 'Eastern Phoebe', 'American Black Duck', 'Mallard', 'Canvasback', 'Merlin', 'Ovenbird']], [['Fox Sparrow', 'White-Winged Crossbill', 'Veery', 'American Coot', 'Sora', 'Northern Rough-Winged Swallow', 'Purple Martin']]]\n_outputs = [[['AMRE', 'NOCA', 'PIGR', 'BAOW', 'STAR', 'COHA', 'PIGE']], [['GCFL', 'BOBO', 'AWPE', 'RTHA', 'ESOW', 'BLJA']], [['BCNH', 'NOMO', 'EAME', 'DEJU', 'RBWO']], [['SCTA', 'GBHE', 'EAPH', 'ABDU', 'MALL', 'CANV', 'MERL', 'OVEN']], [['FOSP', 'WWCR', 'VEER', 'AMCO', 'SORA', 'NRWS', 'PUMA']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(bird_code(*i), o[0])"}
316
476
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer n. There is an undirected graph with n vertices, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting vertices ai and bi. Return the number of complete connected components of the graph. A connected component is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph. A connected component is said to be complete if there exists an edge between every pair of its vertices.   Please complete the following python code precisely: ```python class Solution: def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]) == 3\n assert candidate(n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]) == 1\n\n\ncheck(Solution().countCompleteComponents)"}
186
86
coding
Solve the programming task below in a Python markdown code block. A total of n depots are located on a number line. Depot i lies at the point x_i for 1 ≤ i ≤ n. You are a salesman with n bags of goods, attempting to deliver one bag to each of the n depots. You and the n bags are initially at the origin 0. You can carry up to k bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods. Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 500). Description of the test cases follows. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5). The second line of each test case contains n integers x_1, x_2, …, x_n (-10^9 ≤ x_i ≤ 10^9). It is possible that some depots share the same position. It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots. Example Input 4 5 1 1 2 3 4 5 9 3 -5 -10 -15 6 5 8 3 7 4 5 3 2 2 3 3 3 4 2 1000000000 1000000000 1000000000 1000000000 Output 25 41 7 3000000000 Note In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: 0 → 2 → 0 → 4 → 0 → 3 → 0 → 1 → 0 → 5, where each 0 means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of 25 units. It must be noted that there are other sequences that give the same distance. In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: 0 → 6 → 8 → 7 → 0 → 5 → 4 → 3 → 0 → (-5) → (-10) → (-15), with distance 41. It can be shown that 41 is the optimal distance for this test case.
{"inputs": ["4\n5 1\n1 1 3 4 1\n9 3\n-9 -6 -6 6 5 8 3 7 4\n5 3\n1 1 6 3 4\n4 2\n1000000000 1000000000 1000000000 1010100000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -18 -6 6 6 8 3 7 4\n5 3\n1 2 6 3 4\n4 2\n1000000010 1000000000 1000000000 1000000000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -22 -6 6 6 8 3 7 4\n5 3\n1 2 6 3 4\n4 2\n1000000010 1000000000 1000000000 1000000000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -12 -6 6 6 8 3 7 4\n5 3\n1 2 6 3 4\n4 2\n1000000010 1000000000 1000000000 1000000000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -12 -6 6 6 8 3 7 4\n5 3\n1 2 6 3 4\n4 1\n1000000010 1000000000 1000000000 1000000000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -18 -6 6 6 8 3 7 4\n5 3\n1 1 6 3 4\n4 2\n1000000000 1000000000 1000000000 1000000000\n", "4\n5 1\n1 1 3 4 1\n9 3\n-9 -22 -6 6 6 8 3 7 4\n5 3\n1 3 6 3 4\n4 2\n1000000010 1000000000 1000000000 1000000000\n", "4\n5 1\n1 2 3 4 5\n9 3\n-9 -4 -15 6 5 8 3 7 4\n5 3\n2 2 5 3 3\n4 2\n1000000000 1000100000 1000000000 1000000000\n"], "outputs": ["16\n35\n8\n3010100000\n", "16\n46\n10\n3000000010\n", "16\n50\n10\n3000000010\n", "16\n40\n10\n3000000010\n", "16\n40\n10\n7000000010\n", "16\n46\n8\n3000000000\n", "16\n50\n12\n3000000010\n", "25\n41\n9\n3000100000\n"]}
670
1,010
coding
Solve the programming task below in a Python markdown code block. Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers t_{i} and c_{i} — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^3) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers t_{i} and c_{i} (1 ≤ t_{i}, c_{i} ≤ 10^6) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, t_{i} < t_{i} + 1 for all integer i (1 ≤ i < n). -----Output----- In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time. -----Examples----- Input 2 1 1 2 1 Output 3 1 Input 1 1000000 10 Output 1000010 10 Input 3 3 3 4 3 5 3 Output 12 7 -----Note----- In the first test sample: second 1: the first message has appeared in the queue, the queue's size is 1; second 2: the first message is sent, the second message has been received, the queue's size is 1; second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
{"inputs": ["1\n1 1\n", "1\n1 1\n", "2\n1 1\n2 1\n", "2\n1 1\n4 1\n", "2\n1 1\n4 2\n", "2\n1 1\n4 4\n", "2\n1 2\n4 6\n", "2\n2 3\n4 9\n"], "outputs": ["2 1\n", "2 1", "3 1\n", "5 1\n", "6 2\n", "8 4\n", "10 6\n", "14 10\n"]}
759
144
coding
Solve the programming task below in a Python markdown code block. An onion array is an array that satisfies the following condition for all values of `j` and `k`: If all of the following are `true`: * `j >= 0` * `k >= 0` * `j + k = array.length - 1` * `j != k` then: * `a[j] + a[k] <= 10` ### Examples: ``` [1, 2, 19, 4, 5] => true (as 1+5 <= 10 and 2+4 <= 10) [1, 2, 19, 4, 10] => false (as 1+10 > 10) ``` Write a function named `isOnionArray`/`IsOnionArray`/`is_onion_array()` that returns `true` if its argument is an onion array and returns `false` if it is not. ~~~if:php Your solution should at least be moderately efficient. Make sure you don't do any unnecessary looping ;) ~~~ Also feel free to reuse/extend the following starter code: ```python def is_onion_array(a): ```
{"functional": "_inputs = [[[6, 0, 4]], [[1, 1, 15, 10, -1]]]\n_outputs = [[True], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(is_onion_array(*i), o[0])"}
274
182
coding
Solve the programming task below in a Python markdown code block. Let's solve the geometric problem Mr. A is still solving geometric problems today. It is important to be aware of floating point errors when solving geometric problems. Floating-point error is the error caused by the rounding that occurs when representing a number in binary finite decimal numbers. For example, 0.1 in decimal is an infinite decimal number of 0.00011001100110011 ... in binary, but an error occurs when rounding this to a finite number of digits. Positive integers p and q are given in decimal notation. Find the b-ary system (b is an integer greater than or equal to 2) so that the rational number p / q can be expressed as a decimal number with a finite number of digits. If there are more than one, output the smallest one. Constraints * 0 <p <q <10 ^ 9 Input Format Input is given from standard input in the following format. p q Output Format Print the answer in one line. Sample Input 1 1 2 Sample Output 1 2 1/2 is binary 0.1 Sample Input 2 21 30 Sample Output 2 Ten 21/30 is 0.7 in decimal Example Input 1 2 Output 2
{"inputs": ["1 3", "1 6", "3 2", "4 5", "1 2", "3 10", "3 14", "2 14"], "outputs": ["3\n", "6\n", "2\n", "5\n", "2", "10\n", "14\n", "7\n"]}
295
82