task_type
stringclasses
4 values
problem
stringlengths
14
5.23k
solution
stringlengths
1
8.29k
problem_tokens
int64
9
1.02k
solution_tokens
int64
1
1.98k
coding
Solve the programming task below in a Python markdown code block. Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below: * Each person simultaneously divides his cookies in half and gives one half to each of the other two persons. This action will be repeated until there is a person with odd number of cookies in hand. How many times will they repeat this action? Note that the answer may not be finite. Constraints * 1 ≤ A,B,C ≤ 10^9 Input Input is given from Standard Input in the following format: A B C Output Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print `-1` instead. Examples Input 4 12 20 Output 3 Input 14 14 14 Output -1 Input 454 414 444 Output 1
{"inputs": ["0 0 0", "4 0 13", "3 0 13", "6 0 13", "6 0 24", "1 25 5", "1 25 0", "2 25 0"], "outputs": ["-1\n", "0\n", "0\n", "0\n", "1\n", "0\n", "0\n", "0\n"]}
223
102
coding
Solve the programming task below in a Python markdown code block. We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation: * Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal". * If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid. * If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid. * Then, paint one of the added squares black, and the other squares white. Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353. Constraints * 1 \leq A \leq C \leq 3000 * 1 \leq B \leq D \leq 3000 * A, B, C, and D are integers. Input Input is given from Standard Input in the following format: A B C D Output Print the number of ways in which the squares can be painted in the end, modulo 998244353. Examples Input 1 1 2 2 Output 3 Input 2 1 3 4 Output 65 Input 31 41 59 265 Output 387222020
{"inputs": ["2 0 3 4", "1 0 2 2", "2 0 3 0", "1 0 2 3", "2 0 3 1", "2 0 3 2", "1 0 2 1", "2 0 6 2"], "outputs": ["130\n", "3\n", "0\n", "7\n", "2\n", "10\n", "1\n", "94\n"]}
346
114
coding
Solve the programming task below in a Python markdown code block. Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a_1, a_2, ..., a_{n}, where a_{i} is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b_1, b_2, ..., b_{n} (b_{i} ≥ a_{i}), where b_{i} means the total number of walks with the dog on the i-th day. -----Input----- The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned. -----Output----- In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b_1, b_2, ..., b_{n}, where b_{i} — the total number of walks on the i-th day according to the found solutions (a_{i} ≤ b_{i} for all i from 1 to n). If there are multiple solutions, print any of them. -----Examples----- Input 3 5 2 0 1 Output 4 2 3 2 Input 3 1 0 0 0 Output 1 0 1 0 Input 4 6 2 4 3 5 Output 0 2 4 3 5
{"inputs": ["1 1\n1\n", "1 4\n2\n", "1 5\n1\n", "1 2\n1\n", "1 5\n2\n", "1 3\n0\n", "1 3\n1\n", "1 5\n0\n"], "outputs": ["0\n1\n", "0\n2\n", "0\n1\n", "0\n1\n", "0\n2\n", "0\n0\n", "0\n1\n", "0\n0\n"]}
616
118
coding
Solve the programming task below in a Python markdown code block. INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method. The integers of the array are processed one by one, starting from the first. Processing i-th element of the array is done in three steps: 1. The 8-bit binary notation of the ASCII-code of the previous printed character is reversed. When the first element of the array is processed, the result of this step is considered to be 0. 2. The i-th element of the array is subtracted from the result of the previous step modulo 256. 3. The binary notation of the result of the previous step is reversed again to produce ASCII-code of the i-th character to be printed. You are given the text printed using this method. Restore the array used to produce this text. Input The input will consist of a single line text which contains the message printed using the described method. String text will contain between 1 and 100 characters, inclusive. ASCII-code of each character of text will be between 32 (space) and 126 (tilde), inclusive. Output Output the initial array, which was used to produce text, one integer per line. Examples Input Hello, World! Output 238 108 112 0 64 194 48 26 244 168 24 16 162 Note Let's have a closer look at the beginning of the example. The first character is "H" with ASCII-code 72 = 010010002. Its reverse is 000100102 = 18, and this number should become the result of the second step of processing. The result of the first step is considered to be 0, so the first element of the array has to be (0 - 18) mod 256 = 238, where a mod b is the remainder of division of a by b.
{"inputs": ["N\n", "M\n", "o2^\"t\n", "t\"^2o\n", "\\Pk|a\\'\n", "'\\a|kP\\\n", "s3=MS8%X\n", "X%8SM=3s\n"], "outputs": ["142\n", "78\n", "10\n170\n210\n54\n22\n", "210\n234\n202\n46\n86\n", "198\n48\n52\n152\n184\n76\n86\n", "28\n170\n180\n72\n104\n204\n208\n", "50\n2\n16\n10\n232\n174\n120\n138\n", "230\n118\n136\n82\n24\n246\n240\n254\n"]}
475
231
coding
Solve the programming task below in a Python markdown code block. Congratulations !!! You have successfully completed the heist by looting all the gifts in Santa's locker. Now it's time to decide who gets to take all the gifts, you or the Grinch, there will be no splitting. So you and Grinch decide to play a game. To start the game, an Integer N will be given. The game is played in turns and for each turn, the player can make any one of the following moves: - Divide N by any of it's odd divisors greater than 1. - Subtract 1 from N if N is greater than 1. Divisor of a number includes the number itself. The player who is unable to make a move loses the game. Since you are the mastermind of the heist, you get to play the first move. -----Input----- The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer — N (1 ≤ N ≤ 109). -----Output----- For each test case, print " Me" if you win, and " Grinch" if otherwise (without quotes). -----Sample Input----- 7 1 2 3 4 5 6 12 -----Sample Output----- Grinch Me Me Grinch Me Grinch Me
{"inputs": ["7\n1\n2\n3\n4\n5\n6\n12"], "outputs": ["Grinch\nMe\nMe\nGrinch\nMe\nGrinch\nMe"]}
303
42
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 nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR. In other words, let Bij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(Bik) where i <= k <= n - 1. The bitwise OR of an array is the bitwise OR of all the numbers in it. Return an integer array answer of size n where answer[i] is the length of the minimum sized subarray starting at i with maximum bitwise OR. A subarray is a contiguous non-empty sequence of elements within an array.   Please complete the following python code precisely: ```python class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,0,2,1,3]) == [3,3,2,2,1]\n assert candidate(nums = [1,2]) == [2,1]\n\n\ncheck(Solution().smallestSubarrays)"}
229
67
coding
Solve the programming task below in a Python markdown code block. You are given an array of $n$ integers $a_1, a_2, \ldots, a_n$. The integers are either $1$ or $-1$. You have to perform the following operation exactly once on the array $a$: Choose an index $i$ ($1 \leq i < n$) and flip the signs of $a_i$ and $a_{i+1}$. Here, flipping the sign means $-1$ will be $1$ and $1$ will be $-1$. What is the maximum possible value of $a_1 + a_2 + \ldots + a_n$ after applying the above operation? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$), the length of the array $a$. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i = 1$ or $a_i = -1$). The sum of $n$ over all cases doesn't exceed $10^5$. -----Output----- For each test case, print the maximum possible sum of the array $a$ you can get in a separate line. -----Examples----- Input 4 5 -1 1 1 -1 -1 5 1 1 -1 -1 -1 2 1 1 4 1 -1 -1 1 Output 3 3 -2 4 -----Note----- In the first case, we can choose index $4$ and flip the signs of $a_4$ and $a_5$. After this operation, the sum will be $-1+1+1+1+1 = 3$. We can't make the sum larger than this. In the third case, the only option is to choose the index $1$.
{"inputs": ["4\n5\n-1 1 1 -1 -1\n5\n1 1 -1 -1 -1\n2\n1 1\n4\n1 -1 -1 1\n"], "outputs": ["3\n3\n-2\n4\n"]}
460
62
coding
Solve the programming task below in a Python markdown code block. Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of a_{i} meters. Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times. -----Input----- The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). -----Output----- Print a single integer — the minimum number of strokes needed to paint the whole fence. -----Examples----- Input 5 2 2 1 2 1 Output 3 Input 2 2 2 Output 2 Input 1 5 Output 1 -----Note----- In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank. In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes. In the third sample there is only one plank that can be painted using a single vertical stroke.
{"inputs": ["1\n5\n", "1\n1\n", "1\n1\n", "1\n2\n", "1\n8\n", "1\n3\n", "1\n4\n", "1\n5\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
431
86
coding
Solve the programming task below in a Python markdown code block. Take a string and return a hash with all the ascii values of the characters in the string. Returns nil if the string is empty. The key is the character, and the value is the ascii value of the character. Repeated characters are to be ignored and non-alphebetic characters as well. Also feel free to reuse/extend the following starter code: ```python def char_to_ascii(string): ```
{"functional": "_inputs = [[''], ['a'], ['aaa'], ['hello world'], ['ABaa ^']]\n_outputs = [[None], [{'a': 97}], [{'a': 97}], [{'h': 104, 'e': 101, 'l': 108, 'o': 111, 'w': 119, 'r': 114, 'd': 100}], [{'A': 65, 'B': 66, 'a': 97}]]\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_to_ascii(*i), o[0])"}
95
261
coding
Solve the programming task below in a Python markdown code block. Time to win the lottery! Given a lottery ticket (ticket), represented by an array of 2-value arrays, you must find out if you've won the jackpot. Example ticket: To do this, you must first count the 'mini-wins' on your ticket. Each sub array has both a string and a number within it. If the character code of any of the characters in the string matches the number, you get a mini win. Note you can only have one mini win per sub array. Once you have counted all of your mini wins, compare that number to the other input provided (win). If your total is more than or equal to (win), return 'Winner!'. Else return 'Loser!'. All inputs will be in the correct format. Strings on tickets are not always the same length. Also feel free to reuse/extend the following starter code: ```python def bingo(ticket, win): ```
{"functional": "_inputs = [[[['ABC', 65], ['HGR', 74], ['BYHT', 74]], 2], [[['ABC', 65], ['HGR', 74], ['BYHT', 74]], 1], [[['HGTYRE', 74], ['BE', 66], ['JKTY', 74]], 3]]\n_outputs = [['Loser!'], ['Winner!'], ['Loser!']]\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(bingo(*i), o[0])"}
203
244
coding
Solve the programming task below in a Python markdown code block. As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices. One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale. * Up to m vegetables can be packed in one bag. * For bags packed with m vegetables, the cheapest vegetables are free. * Bags with less than m vegetables are not eligible for the discount. The three went shopping at Seven Mart immediately. When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one. Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price. Input A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format: n m p1 p2 ... pn The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable. The number of datasets does not exceed 100. Output Prints the minimum purchase price on one line for each input dataset. Example Input 4 2 50 40 100 80 7 3 400 300 100 700 200 600 500 0 0 Output 150 2100
{"inputs": ["4 1\n50 77 110 80\n7 3\n3 300 110 194 77 600 75\n0 0", "4 1\n50 77 110 80\n7 6\n3 300 110 194 77 600 75\n0 0", "4 3\n50 59 100 80\n7 3\n400 290 000 700 4 600 57\n0 0", "4 4\n50 59 100 80\n7 3\n400 290 000 700 4 600 57\n0 0", "4 2\n50 40 101 80\n7 4\n400 45 000 700 87 650 57\n0 0", "4 1\n27 77 100 80\n7 3\n24 300 100 194 77 600 57\n0 0", "4 2\n50 40 100 35\n7 4\n400 284 010 700 701 0 57\n0 0", "4 3\n50 6 100 80\n7 3\n400 300 100 194 24 600 57\n0 0"], "outputs": ["0\n1090\n", "0\n1284\n", "230\n1647\n", "239\n1647\n", "151\n1852\n", "0\n1101\n", "140\n1868\n", "186\n1318\n"]}
522
494
coding
Solve the programming task below in a Python markdown code block. Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months — in the i-th month the company had income equal to a_i (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the first ⌈ n/2 ⌉ months income might have been completely unstable, but then everything stabilized and for the last ⌊ n/2 ⌋ months the income was the same. Levian decided to tell the directors n-k+1 numbers — the total income of the company for each k consecutive months. In other words, for each i between 1 and n-k+1 he will say the value a_i + a_{i+1} + … + a_{i + k - 1}. For example, if a=[-1, 0, 1, 2, 2] and k=3 he will say the numbers 0, 3, 5. Unfortunately, if at least one total income reported by Levian is not a profit (income ≤ 0), the directors will get angry and fire the failed accountant. Save Levian's career: find any such k, that for each k months in a row the company had made a profit, or report that it is impossible. Input The first line contains a single integer n (2 ≤ n ≤ 5⋅ 10^5) — the number of months for which Levian must account. The second line contains ⌈{n/2}⌉ integers a_1, a_2, …, a_{⌈{n/2}⌉}, where a_i (-10^9 ≤ a_i ≤ 10^9) — the income of the company in the i-th month. Third line contains a single integer x (-10^9 ≤ x ≤ 10^9) — income in every month from ⌈{n/2}⌉ + 1 to n. Output In a single line, print the appropriate integer k or -1, if it does not exist. If there are multiple possible answers, you can print any. Examples Input 3 2 -1 2 Output 2 Input 5 2 2 -8 2 Output -1 Input 6 -2 -2 6 -1 Output 4 Note In the first example, k=2 and k=3 satisfy: in the first case, Levian will report the numbers 1, 1, and in the second case — one number 3. In the second example, there is no such k. In the third example, the only answer is k=4: he will report the numbers 1,2,3.
{"inputs": ["2\n0\n0\n", "2\n2\n2\n", "2\n4\n2\n", "2\n2\n0\n", "2\n1\n2\n", "2\n1\n0\n", "2\n0\n1\n", "2\n2\n1\n"], "outputs": ["-1", "2", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n"]}
602
101
coding
Solve the programming task below in a Python markdown code block. Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. Input The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements. Output In a single line print a single integer — the maximum number of points Artem can get. Examples Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
{"inputs": ["1\n4\n", "1\n7\n", "1\n9\n", "1\n87\n", "1\n17\n", "1\n10\n", "1\n64\n", "2\n3 1\n"], "outputs": [" 0\n", "0\n", "0\n", " 0\n", "0\n", "0\n", "0\n", " 0\n"]}
287
98
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping: "1" -> 'A' "2" -> 'B' ... "25" -> 'Y' "26" -> 'Z' However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25"). For example, "11106" can be decoded into: "AAJF" with the grouping (1, 1, 10, 6) "KJF" with the grouping (11, 10, 6) The grouping (1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid). Note: there may be strings that are impossible to decode. Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0. The test cases are generated so that the answer fits in a 32-bit integer.   Please complete the following python code precisely: ```python class Solution: def numDecodings(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"12\") == 2\n assert candidate(s = \"226\") == 3\n assert candidate(s = \"06\") == 0\n\n\ncheck(Solution().numDecodings)"}
292
60
coding
Solve the programming task below in a Python markdown code block. Let $f(x)$ be the sum of digits of a decimal number $x$. Find the smallest non-negative integer $x$ such that $f(x) + f(x + 1) + \dots + f(x + k) = n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 150$) — the number of test cases. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 150$, $0 \le k \le 9$). -----Output----- For each test case, print one integer without leading zeroes. If there is no such $x$ that $f(x) + f(x + 1) + \dots + f(x + k) = n$, print $-1$; otherwise, print the minimum $x$ meeting that constraint. -----Example----- Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
{"inputs": ["2\n6 9\n5 0\n", "2\n9 9\n53 9\n", "2\n4 9\n51 5\n", "2\n6 9\n10 0\n", "2\n4 6\n47 7\n", "2\n9 9\n27 9\n", "2\n9 9\n17 4\n", "2\n9 8\n53 9\n"], "outputs": ["-1\n5\n", "-1\n8\n", "-1\n24\n", "-1\n19\n", "-1\n17\n", "-1\n-1\n", "-1\n-1\n", "-1\n8\n"]}
278
163
coding
Solve the programming task below in a Python markdown code block. An array $b$ of length $k$ is called good if its arithmetic mean is equal to $1$. More formally, if $$\frac{b_1 + \cdots + b_k}{k}=1.$$ Note that the value $\frac{b_1+\cdots+b_k}{k}$ is not rounded up or down. For example, the array $[1,1,1,2]$ has an arithmetic mean of $1.25$, which is not equal to $1$. You are given an integer array $a$ of length $n$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good? We have a proof that it is always possible with finitely many operations. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 50$) — the length of the initial array $a$. The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($-10^4\leq a_i \leq 10^4$), the elements of the array. -----Output----- For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly $1$. -----Examples----- Input 4 3 1 1 1 2 1 2 4 8 4 6 2 1 -2 Output 0 1 16 1 -----Note----- In the first test case, we don't need to add any element because the arithmetic mean of the array is already $1$, so the answer is $0$. In the second test case, the arithmetic mean is not $1$ initially so we need to add at least one more number. If we add $0$ then the arithmetic mean of the whole array becomes $1$, so the answer is $1$. In the third test case, the minimum number of elements that need to be added is $16$ since only non-negative integers can be added. In the fourth test case, we can add a single integer $4$. The arithmetic mean becomes $\frac{-2+4}{2}$ which is equal to $1$.
{"inputs": ["1\n1\n1\n", "4\n3\n1 1 1\n2\n1 2\n4\n8 4 6 2\n1\n-2\n"], "outputs": ["0\n", "0\n1\n16\n1\n"]}
557
62
coding
Solve the programming task below in a Python markdown code block. Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated. -----Input----- The first line of the input contains three integers: n, k and a (1 ≤ n, k, a ≤ 2·10^5) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≤ m ≤ n) — the number of Bob's moves. The third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. -----Output----- Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". -----Examples----- Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 Output 1
{"inputs": ["5 1 3\n1\n3\n", "1 1 1\n1\n1\n", "1 1 1\n1\n1\n", "2 1 1\n1\n1\n", "2 1 2\n1\n1\n", "3 1 2\n1\n1\n", "5 1 3\n1\n2\n", "5 1 2\n1\n1\n"], "outputs": ["1\n", "1\n", "1\n", "-1\n", "1\n", "-1\n", "-1\n", "-1\n"]}
484
134
coding
Solve the programming task below in a Python markdown code block. Takahashi and Aoki will play a game on a tree. The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i. At the beginning of the game, each vertex contains a coin. Starting from Takahashi, he and Aoki will alternately perform the following operation: * Choose a vertex v that contains one or more coins, and remove all the coins from v. * Then, move each coin remaining on the tree to the vertex that is nearest to v among the adjacent vertices of the coin's current vertex. The player who becomes unable to play, loses the game. That is, the player who takes his turn when there is no coin remaining on the tree, loses the game. Determine the winner of the game when both players play optimally. Constraints * 1 \leq N \leq 2 \times 10^5 * 1 \leq a_i, b_i \leq N * a_i \neq b_i * The graph given as input is a tree. Input Input is given from Standard Input in the following format: N a_1 b_1 a_2 b_2 : a_{N-1} b_{N-1} Output Print `First` if Takahashi will win, and print `Second` if Aoki will win. Examples Input 3 1 2 2 3 Output First Input 6 1 2 2 3 2 4 4 6 5 6 Output Second Input 7 1 7 7 4 3 4 7 5 6 3 2 1 Output First
{"inputs": ["3\n2 2\n2 3", "3\n2 1\n2 3", "3\n1 2\n2 3", "6\n1 2\n2 1\n2 4\n4 6\n5 6", "6\n1 2\n2 3\n2 4\n6 6\n5 6", "6\n1 2\n2 1\n2 4\n4 6\n4 6", "6\n1 3\n2 3\n2 4\n6 6\n5 6", "6\n2 2\n2 3\n2 4\n6 6\n5 6"], "outputs": ["First\n", "First\n", "First", "Second\n", "First\n", "First\n", "First\n", "First\n"]}
385
185
coding
Solve the programming task below in a Python markdown code block. Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is v_{i}. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} v_{i}$. Let u_{i} be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her $\sum_{i = l}^{r} u_{i}$. For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. -----Input----- The first line contains an integer n (1 ≤ n ≤ 10^5). The second line contains n integers: v_1, v_2, ..., v_{n} (1 ≤ v_{i} ≤ 10^9) — costs of the stones. The third line contains an integer m (1 ≤ m ≤ 10^5) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. -----Output----- Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. -----Examples----- Input 6 6 4 2 7 2 7 3 2 3 6 1 3 4 1 1 6 Output 24 9 28 Input 4 5 5 2 3 10 1 2 4 2 1 4 1 1 1 2 1 4 2 1 2 1 1 1 1 3 3 1 1 3 1 4 4 1 2 2 Output 10 15 5 15 5 5 2 12 3 5 -----Note----- Please note that the answers to the questions may overflow 32-bit integer type.
{"inputs": ["1\n1\n2\n1 1 1\n2 1 1\n", "1\n1\n2\n1 1 1\n2 1 1\n", "6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6\n", "6\n6 6 3 8 5 4\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n6 6 3 8 5 4\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n6 6 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n1 6 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n", "6\n1 1 3 8 5 6\n3\n2 2 3\n2 1 5\n1 1 5\n"], "outputs": ["1\n1\n", "1\n1\n", "24\n9\n28\n", "9\n24\n28\n", "9\n24\n28\n", "11\n26\n28\n", "8\n21\n23\n", "4\n16\n18\n"]}
603
335
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements. Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.   Please complete the following python code precisely: ```python class Solution: def findShortestSubArray(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,2,3,1]) == 2\n assert candidate(nums = [1,2,2,3,1,4,2]) == 6\n\n\ncheck(Solution().findShortestSubArray)"}
108
66
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed 2D matrix grid of size m x n, where (r, c) represents: A land cell if grid[r][c] = 0, or A water cell containing grid[r][c] fish, if grid[r][c] > 0. A fisher can start at any water cell (r, c) and can do the following operations any number of times: Catch all the fish at cell (r, c), or Move to any adjacent water cell. Return the maximum number of fish the fisher can catch if he chooses his starting cell optimally, or 0 if no water cell exists. An adjacent cell of the cell (r, c), is one of the cells (r, c + 1), (r, c - 1), (r + 1, c) or (r - 1, c) if it exists.   Please complete the following python code precisely: ```python class Solution: def findMaxFish(self, grid: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]) == 7\n assert candidate(grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]) == 1\n\n\ncheck(Solution().findMaxFish)"}
240
104
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.   Please complete the following python code precisely: ```python class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]) == True\n assert candidate(matrix = [[1,2],[2,2]]) == False\n\n\ncheck(Solution().isToeplitzMatrix)"}
96
72
coding
Solve the programming task below in a Python markdown code block. problem Given a sequence of n integers a1, a2, ..., an and a positive integer k (1 ≤ k ≤ n), then the sum of k consecutive integers Si = ai + ai + Create a program that outputs the maximum value of 1 + ... + ai + k-1 (1 ≤ i ≤ n --k + 1). input The input consists of multiple datasets. Each dataset is given in the following format. The input ends on a line containing two zeros. On the first line, the positive integer n (1 ≤ n ≤ 100000) and the positive integer k (1 ≤ k ≤ n) are written in this order, separated by blanks. The first + i lines after the second line. (1 ≤ i ≤ n) contains the i-th term ai (-10000 ≤ ai ≤ 10000) in the sequence. Of the scoring data, 60% of the points are n ≤ 5000, k ≤ 1000. Meet. The number of datasets does not exceed 5. output The maximum value of Si is output to one line for each data set. Examples Input 5 3 2 5 -4 10 3 0 0 Output 11 Input None Output None
{"inputs": ["5 3\n2\n5\n0\n5\n3\n0 0", "5 1\n0\n0\n0\n3\n1\n0 0", "5 3\n2\n5\n0\n8\n0\n0 0", "5 3\n2\n5\n1\n8\n0\n0 0", "5 3\n4\n5\n1\n8\n0\n0 0", "5 3\n4\n5\n2\n8\n0\n0 0", "5 3\n2\n5\n-4\n8\n3\n0 0", "5 3\n1\n9\n-4\n2\n1\n0 0"], "outputs": ["10\n", "3\n", "13\n", "14\n", "14\n", "15\n", "9\n", "7\n"]}
294
197
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are: Players take turns placing characters into empty squares ' '. The first player A always places 'X' characters, while the second player B always places 'O' characters. 'X' and 'O' characters are always placed into empty squares, never on filled ones. The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal. The game also ends if all squares are non-empty. No more moves can be played if the game is over. Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game ends in a draw return "Draw". If there are still movements to play return "Pending". You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.   Please complete the following python code precisely: ```python class Solution: def tictactoe(self, moves: List[List[int]]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]) == \"A\"\n assert candidate(moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]) == \"B\"\n assert candidate(moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]) == \"Draw\"\n assert candidate(moves = [[0,0],[1,1]]) == \"Pending\"\n\n\ncheck(Solution().tictactoe)"}
290
157
coding
Solve the programming task below in a Python markdown code block. Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
{"inputs": ["2\n2 2\n2 3\n"], "outputs": ["3\n12"]}
611
24
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given a string s, return the maximum number of unique substrings that the given string can be split into. You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique. A substring is a contiguous sequence of characters within a string.   Please complete the following python code precisely: ```python class Solution: def maxUniqueSplit(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"ababccc\") == 5\n assert candidate(s = \"aba\") == 2\n assert candidate(s = \"aa\") == 1\n\n\ncheck(Solution().maxUniqueSplit)"}
131
58
coding
Solve the programming task below in a Python markdown code block. Chef is deriving weird ways to sort his array. Currently he is trying to sort his arrays in increasing order by reversing some of his subarrays. To make it more challenging for himself, Chef decides that he can reverse only those subarrays that have sum of its elements at most X. Soon he notices that it might not be always possible to sort the array with this condition. Can you help the Chef by telling him if the given array can be sorted by reversing subarrays with sum at most X. More formally, for a given array A and an integer X, check whether the array can be sorted in increasing order by reversing some (possibly none) of the subarrays such that the sum of all elements of the subarray is at most X. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - The first line of each test case contains of two space-separated integers N and X denoting the length of the array and the maximum sum of subarrays that you can reverse. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} representing the initial array. ------ Output Format ------ For each test case, output \texttt{YES} if Chef can sort the array using a finite number of operations, else output \texttt{NO}. You may print each character of the string in uppercase or lowercase (for example, the strings \texttt{YeS}, \texttt{yEs}, \texttt{yes} and \texttt{YES} will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 5\cdot 10^{4}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 2\cdot 10^{9}$ $1 ≤ X ≤ 2\cdot 10^{9}$ - Sum of $N$ over all test cases does not exceeds $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 4 1 1 2 3 4 4 1 2 1 3 4 5 7 3 2 2 3 3 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case $1$: The array is already sorted so we need not make any operations. Test case $2$: There is no subarray with sum less than or equal to $1$ so we cannot sort the array. Test case $3$: We can reverse the subarray $A[1, 3]$ which has a sum of $3+2+2=7$. Thus, the reversed subarray is $[2, 2, 3]$. The resulting array is $[2, 2, 3, 3, 3]$. The array is sorted in $1$ operation.
{"inputs": ["3\n4 1\n1 2 3 4\n4 1\n2 1 3 4\n5 7\n3 2 2 3 3\n"], "outputs": ["YES\nNO\nYES\n"]}
624
56
coding
Solve the programming task below in a Python markdown code block. Given a string $s$. You can perform the following operation on given string any number of time. Delete two successive elements of the string if they are same. After performing the above operation you have to return the least possible length of the string. -----Input:----- - First line will contain $T$, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, a string $s$. -----Output:----- For each testcase, output in a single line answer- minimum length of string possible after performing given operations. -----Constraints----- - $1 \leq T \leq 1000$ - $2 \leq length of string \leq 10^5$ $s$ contains only lowercase letters. -----Sample Input:----- 3 abccd abbac aaaa -----Sample Output:----- 3 1 0 -----EXPLANATION:----- - In first case, $"abd"$ will be final string. - in second case, $"c"$ will be final string
{"inputs": ["3\nabccd\nabbac\naaaa"], "outputs": ["3\n1\n0"]}
230
24
coding
Solve the programming task below in a Python markdown code block. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
{"inputs": ["1\n2\n", "1\n3\n", "1\n10\n", "2\n2 1\n", "2\n2 3\n", "2\n1 2\n", "2\n1 1\n", "2\n2 2\n"], "outputs": ["2\n", "3\n", "10\n", "3\n", "7\n", "4\n", "2\n", "5\n"]}
545
98
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Mandarin], [Bengali], [Russian], and [Vietnamese] as well. Two drunken players Alice and Bob are playing a modified version of Tic-Tac-Toe. Initially, there is a $N \times M$ empty grid. Alice and Bob take alternate turns starting with Alice. In her turn, Alice chooses an empty cell on the board and puts a "A" on the chosen cell. In Bob's turn, he chooses an empty cell and puts a "B" on the chosen cell. The player who first gets any $K\times K$ subgrid completely filled with his/her initial wins the game. By a $K\times K$ subgrid, we mean the intersection of $K$ consecutive rows and $K$ consecutive columns. The players do not stop making turns after one of them wins and they play all $N \cdot M$ turns until the grid is filled. You are given the sequence of moves played by each player (The cell selected by the player in his/her turn). You have to output the winner of the game or report that there is no winner. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The first line of each test case contains three integers $N$, $M$, $K$. Each of the next $N\cdot M$ lines contains two integers $X$, $Y$ (the row and column of the cell chosen by the player with the current turn) ------ Output ------ For each test case, output "Alice" if Alice wins the game or "Bob" if Bob wins the game or "Draw" if no one wins. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N, M ≤ 1000$ $1 ≤ K ≤ \min(N,M)$ $1 ≤ X ≤ N$ $1 ≤ Y ≤ M$ The sum of $N\cdot M$ over all test cases does not exceed $10^{6}$. ------ Subtasks ------ Subtask 1 (10 points): $1 ≤ N ≤ 10,\ 1 ≤ M ≤ 10$ Subtask 2 (20 points): $1 ≤ N ≤ 50,\ 1 ≤ M ≤ 50$ Subtask 3 (70 points): Original Constraints ------ Sample Input ------ 1 3 4 2 1 1 3 1 1 2 2 3 2 1 2 4 3 2 3 4 1 3 3 3 2 2 1 4 ------ Sample Output ------ Bob ------ Explanation ------ After $10$ turns of the game the state of the grid is: AAA. A.BB BABB At this moment, Bob has achieved a subgrid of $2 \times 2$ with his initials while Alice has not achieved it yet so Bob is the winner.
{"inputs": ["1\n3 4 2\n1 1\n3 1\n1 2\n2 3\n2 1\n2 4\n3 2\n3 4\n1 3\n3 3\n2 2\n1 4\n"], "outputs": ["Bob\n"]}
649
68
coding
Solve the programming task below in a Python markdown code block. You are given a string $s$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0 for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 else cur = cur - 1 if cur < 0 ok = false break if ok break Note that the $inf$ denotes infinity, and the characters of the string are numbered from $1$ to $|s|$. You have to calculate the value of the $res$ after the process ends. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The only lines of each test case contains string $s$ ($1 \le |s| \le 10^6$) consisting only of characters + and -. It's guaranteed that sum of $|s|$ over all test cases doesn't exceed $10^6$. -----Output----- For each test case print one integer — the value of the $res$ after the process ends. -----Example----- Input 3 --+- --- ++--+- Output 7 9 6
{"inputs": ["3\n--+-\n---\n++--+-\n", "3\n-+--\n---\n++--+-\n", "3\n--+-\n---\n+--++-\n", "3\n-+--\n---\n+--++-\n", "3\n--+-\n---\n-+-++-\n", "3\n---+\n---\n++--+-\n", "3\n-+--\n---\n---+++\n", "3\n--+-\n---\n-+--++\n"], "outputs": ["7\n9\n6\n", "9\n9\n6\n", "7\n9\n9\n", "9\n9\n9\n", "7\n9\n7\n", "10\n9\n6\n", "9\n9\n12\n", "7\n9\n11\n"]}
316
203
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. You are a warrior and you have to fight $N$ enemy warriors (numbered $1$ through $N$) one by one, in any order you choose. You have to win as many of these fights as possible. Each warrior has some amount of power, which changes when the warrior fights. For each $i$, the $i$-th enemy warrior has power $P_{i}$. When you have power $x$ and you fight an enemy warrior with power $y$, the following happens: if $x > y$, you kill the enemy warrior and your power changes to $2(x-y)$ otherwise (if $x ≤ y$), the enemy warrior kills you You should answer $Q$ queries. In each query, you are given your initial power $X$ and you should find the maximum number of warriors you can kill if you are starting with power $X$. ------ 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 line of each test case contains two space-separated integers $N$ and $Q$. The second line contains $N$ space-separated integers $P_{1}, P_{2}, \ldots, P_{N}$. $Q$ lines follow. Each of these lines contains a single integer $X$ describing a query. ------ Output ------ For each query, print a single line containing one integer — the maximum number of warriors you can kill. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N, Q ≤ 10^{5}$ $1 ≤ P_{i} ≤ 10^{9}$ for each valid $i$ $1 ≤ X ≤ 10^{9}$ ----- Sample Input 1 ------ 1 3 4 1 2 1 10 2 3 1 ----- Sample Output 1 ------ 3 2 3 0
{"inputs": ["1\n3 4\n1 2 1\n10\n2\n3\n1"], "outputs": ["3\n2\n3\n0"]}
462
37
coding
Solve the programming task below in a Python markdown code block. Chef has $N$ doggo (dogs) , Lets number them $1$ to $N$. Chef decided to build houses for each, but he soon realizes that keeping so many dogs at one place may be messy. So he decided to divide them into several groups called doggo communities. Let the total no. of groups be $K$ . In a community, paths between pairs of houses can be made so that doggo can play with each other. But there cannot be a path between two houses of different communities for sure. Chef wanted to make maximum no. of paths such that the total path is not greater then $K$. Let’s visualize this problem in an engineer's way :) The problem is to design a graph with max edge possible such that the total no. of edges should not be greater than the total no. of connected components. -----INPUT FORMAT----- - First line of each test case file contain $T$ , denoting total number of test cases. - $ith$ test case contain only one line with a single integer $N$ , denoting the number of dogs(vertex) -----OUTPUT FORMAT----- - For each test case print a line with a integer , denoting the maximum possible path possible. -----Constraints----- - $1 \leq T \leq 10000$ - $1 \leq N \leq 10^9$ -----Sub-Tasks----- - $20 Points$ - $1 \leq N \leq 10^6$ -----Sample Input----- 1 4 -----Sample Output----- 2 -----Explanation----- 4 houses can be made with like this: community #1 : [1 - 2 ] community #2 : [3 - 4 ] or [1 - 2 - 3] , [ 4 ] In both cases the maximum possible path is 2.
{"inputs": ["1\n4"], "outputs": ["2"]}
406
14
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. An array A is larger than some array B if for the first index i where A[i] != B[i], A[i] > B[i]. For example, consider 0-indexing: [1,3,2,4] > [1,2,2,4], since at index 1, 3 > 2. [1,4,4,4] < [2,1,1,1], since at index 0, 1 < 2. A subarray is a contiguous subsequence of the array. Given an integer array nums of distinct integers, return the largest subarray of nums of length k.   Please complete the following python code precisely: ```python class Solution: def largestSubarray(self, nums: List[int], k: int) -> List[int]: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,4,5,2,3], k = 3) == [5,2,3]\n assert candidate(nums = [1,4,5,2,3], k = 4) == [4,5,2,3]\n assert candidate(nums = [1,4,5,2,3], k = 1) == [5]\n\n\ncheck(Solution().largestSubarray)"}
187
109
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two 0-indexed binary arrays nums1 and nums2. Find the widest pair of indices (i, j) such that i <= j and nums1[i] + nums1[i+1] + ... + nums1[j] == nums2[i] + nums2[i+1] + ... + nums2[j]. The widest pair of indices is the pair with the largest distance between i and j. The distance between a pair of indices is defined as j - i + 1. Return the distance of the widest pair of indices. If no pair of indices meets the conditions, return 0.   Please complete the following python code precisely: ```python class Solution: def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums1 = [1,1,0,1], nums2 = [0,1,1,0]) == 3\n assert candidate(nums1 = [0,1], nums2 = [1,1]) == 1\n assert candidate(nums1 = [0], nums2 = [1]) == 0\n\n\ncheck(Solution().widestPairOfIndices)"}
184
95
coding
Solve the programming task below in a Python markdown code block. Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si. In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r (1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si). After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none. In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight. Input The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony. The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants. The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases. Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query. Output Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri]. Examples Input 5 1 3 2 4 2 4 1 5 2 5 3 5 4 5 Output 4 4 1 1 Note In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5. In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole. In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4. In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
{"inputs": ["5\n1 5 2 4 2\n4\n1 5\n2 5\n3 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n3 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n3 5\n2 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n1 5\n4 5\n", "5\n1 3 4 4 2\n4\n1 5\n2 5\n1 5\n3 5\n", "5\n1 3 4 4 2\n4\n1 3\n2 5\n1 5\n3 5\n", "5\n1 3 4 2 2\n4\n1 5\n2 5\n3 5\n2 5\n", "5\n1 3 4 4 2\n4\n1 5\n3 5\n1 5\n4 5\n"], "outputs": ["4\n4\n1\n1\n", "4\n4\n2\n1\n", "4\n4\n2\n4\n", "4\n4\n4\n1\n", "4\n4\n4\n2\n", "2\n4\n4\n2\n", "4\n4\n1\n4\n", "4\n2\n4\n1\n"]}
602
342
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow. Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi. This year, there will be a big event in the capital (city 0), and many people want to travel to this city. Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed. It's guaranteed that each city can reach city 0 after reorder.   Please complete the following python code precisely: ```python class Solution: def minReorder(self, n: int, connections: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]) == 3\n assert candidate(n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]) == 2\n assert candidate(n = 3, connections = [[1,0],[2,0]]) == 0\n\n\ncheck(Solution().minReorder)"}
209
109
coding
Solve the programming task below in a Python markdown code block. Initially, you have the array $a$ consisting of one element $1$ ($a = [1]$). In one move, you can do one of the following things: Increase some (single) element of $a$ by $1$ (choose some $i$ from $1$ to the current length of $a$ and increase $a_i$ by one); Append the copy of some (single) element of $a$ to the end of the array (choose some $i$ from $1$ to the current length of $a$ and append $a_i$ to the end of the array). For example, consider the sequence of five moves: You take the first element $a_1$, append its copy to the end of the array and get $a = [1, 1]$. You take the first element $a_1$, increase it by $1$ and get $a = [2, 1]$. You take the second element $a_2$, append its copy to the end of the array and get $a = [2, 1, 1]$. You take the first element $a_1$, append its copy to the end of the array and get $a = [2, 1, 1, 2]$. You take the fourth element $a_4$, increase it by $1$ and get $a = [2, 1, 1, 3]$. Your task is to find the minimum number of moves required to obtain the array with the sum at least $n$. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 10^9$) — the lower bound on the sum of the array. -----Output----- For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least $n$. -----Example----- Input 5 1 5 42 1337 1000000000 Output 0 3 11 72 63244
{"inputs": ["5\n2\n2\n18\n2\n1000001010\n", "5\n1\n4\n9\n129\n1000001000\n", "5\n2\n2\n16\n13\n1000000000\n", "5\n2\n13\n78\n7\n1000001100\n", "5\n2\n2\n18\n13\n1000001000\n", "5\n2\n7\n9\n284\n1000001000\n", "5\n1\n2\n9\n129\n1000001000\n", "5\n1\n4\n9\n110\n1000001010\n"], "outputs": ["1\n1\n7\n1\n63244\n", "0\n2\n4\n21\n63244\n", "1\n1\n6\n6\n63244\n", "1\n6\n16\n4\n63244\n", "1\n1\n7\n6\n63244\n", "1\n4\n4\n32\n63244\n", "0\n1\n4\n21\n63244\n", "0\n2\n4\n19\n63244\n"]}
518
338
coding
Solve the programming task below in a Python markdown code block. There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital. Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times. King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds: * Starting from any town, one will be at the capital after using the Teleporters exactly K times in total. Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire. Constraints * 2≤N≤10^5 * 1≤a_i≤N * One can get to the capital from any town by using the Teleporters some number of times. * 1≤K≤10^9 Input The input is given from Standard Input in the following format: N K a_1 a_2 ... a_N Output Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire. Examples Input 3 1 2 3 1 Output 2 Input 4 2 1 1 2 2 Output 0 Input 8 2 4 1 2 3 1 2 3 4 Output 3
{"inputs": ["3 1\n3 3 1", "3 1\n2 1 1", "3 2\n3 3 1", "3 4\n3 3 1", "3 4\n1 3 1", "3 7\n1 3 1", "3 2\n3 3 2", "3 2\n1 3 1"], "outputs": ["2\n", "1\n", "1\n", "1\n", "0\n", "0\n", "1\n", "0\n"]}
349
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an integer array nums, find three numbers whose product is maximum and return the maximum product.   Please complete the following python code precisely: ```python class Solution: def maximumProduct(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,2,3]) == 6\n assert candidate(nums = [1,2,3,4]) == 24\n assert candidate(nums = [-1,-2,-3]) == -6\n\n\ncheck(Solution().maximumProduct)"}
67
70
coding
Solve the programming task below in a Python markdown code block. # Task Given an array `arr`, find the rank of the element at the ith position. The `rank` of the arr[i] is a value equal to the number of elements `less than or equal to` arr[i] standing before arr[i], plus the number of elements `less than` arr[i] standing after arr[i]. # Example For `arr = [2,1,2,1,2], i = 2`, the result should be `3`. There are 2 elements `less than or equal to` arr[2] standing before arr[2]: `arr[0] <= arr[2]` `arr[1] <= arr[2]` There is only 1 element `less than` arr[2] standing after arr[2]: `arr[3] < arr[2]` So the result is `2 + 1 = 3`. # Input/Output - `[input]` integer array `arr` An array of integers. `3 <= arr.length <= 50.` - `[input]` integer `i` Index of the element whose rank is to be found. - `[output]` an integer Rank of the element at the ith position. Also feel free to reuse/extend the following starter code: ```python def rank_of_element(arr,i): ```
{"functional": "_inputs = [[[2, 1, 2, 1, 2], 2], [[2, 1, 2, 2, 2], 2], [[3, 2, 3, 4, 1], 0], [[3, 2, 3, 4, 1], 1], [[3, 2, 3, 4, 1], 2]]\n_outputs = [[3], [2], [2], [1], [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(rank_of_element(*i), o[0])"}
304
254
coding
Solve the programming task below in a Python markdown code block. C: Skewering problem One day, when Homura was playing with blocks, Tempura came. Homura decided to play with blocks with Tempura. There is a rectangular parallelepiped of A \ times B \ times C, which is made by stacking A \ times B \ times C blocks of cubic blocks with a side length of 1 without any gaps. Each side of all cubes and rectangular parallelepipeds is parallel to the x-axis, y-axis, or z-axis. Homura-chan and Tempura-kun alternately repeat the following operations. * Select a row of blocks of building blocks lined up in a row from a rectangular parallelepiped in any of the vertical, horizontal, and depth directions, and paint all the blocks included in the row in red. However, you cannot select columns that contain blocks that are already painted red. More precisely * Select one of the blocks contained in the rectangular parallelepiped and one of the three directions x, y, and z. * When the selected block is moved in the selected direction by an integer distance, all the blocks that completely overlap are painted in red (think of moving the distance of 0 or a negative integer). However, this operation cannot be performed if there is at least one block that meets the conditions and has already been painted. Homura-chan is the first player to lose the game if he can't operate it first. Also, initially all cubes are uncolored. Determine which one wins when the two act optimally. Input format A B C Constraint * 1 \ leq A, B, C \ leq 100 * All inputs are integers. Output format When the two act optimally, if Homura wins, `Hom` is output, and if Tempura wins,` Tem` is output on one line. Input example 1 1 1 10 Output example 1 Hom * The first time Homura can paint all the blocks red. Input example 2 4 3 5 Output example 2 Hom Input example 3 6 4 10 Output example 3 Tem Example Input 1 1 10 Output Hom
{"inputs": ["2 1 1", "2 1 2", "4 1 2", "2 1 0", "4 1 3", "2 2 0", "0 1 2", "7 1 3"], "outputs": ["Hom\n", "Tem\n", "Tem\n", "Tem\n", "Hom\n", "Tem\n", "Tem\n", "Hom\n"]}
476
94
coding
Solve the programming task below in a Python markdown code block. You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote. However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes. $n$ reviewers enter the site one by one. Each reviewer is one of the following types: type $1$: a reviewer has watched the movie, and they like it — they press the upvote button; type $2$: a reviewer has watched the movie, and they dislike it — they press the downvote button; type $3$: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once. Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one. What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. Then the descriptions of $t$ testcases follow. The first line of each testcase contains a single integer $n$ ($1 \le n \le 50$) — the number of reviewers. The second line of each testcase contains $n$ integers $r_1, r_2, \dots, r_n$ ($1 \le r_i \le 3$) — the types of the reviewers in the same order they enter the site. -----Output----- For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to. -----Examples----- Input 4 1 2 3 1 2 3 5 1 1 1 1 1 3 3 3 2 Output 0 2 5 2 -----Note----- In the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes. In the second testcase of the example you can send all reviewers to the first server: the first reviewer upvotes; the second reviewer downvotes; the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves. There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server: the first reviewer upvotes on the first server; the second reviewer downvotes on the first server; the last reviewer sees no upvotes or downvotes on the second server — upvote themselves.
{"inputs": ["1\n1\n2\n", "1\n1\n2\n", "1\n1\n3\n", "1\n1\n1\n", "4\n1\n2\n3\n1 2 3\n5\n1 1 1 1 1\n3\n3 3 2\n", "4\n1\n2\n3\n1 3 3\n5\n1 1 1 1 1\n3\n3 3 2\n", "4\n1\n1\n3\n1 3 3\n5\n1 1 1 1 1\n3\n3 3 2\n", "4\n1\n1\n3\n1 3 3\n5\n1 1 1 1 1\n3\n3 3 3\n"], "outputs": ["0\n", "0\n", "1\n", "1\n", "0\n2\n5\n2\n", "0\n3\n5\n2\n", "1\n3\n5\n2\n", "1\n3\n5\n3\n"]}
674
238
coding
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the length of the string. You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$. If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$). Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Input----- The first line of the input contains one string $s$ consisting of at least $1$ and at most $200$ lowercase Latin letters. The second line of the input contains one string $t$ consisting of at least $1$ and at most $200$ lowercase Latin letters. It is guaranteed that $t$ is a subsequence of $s$. -----Output----- Print one integer — the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$. -----Examples----- Input bbaba bb Output 3 Input baaba ab Output 2 Input abcde abcde Output 0 Input asdfasdf fasd Output 3
{"inputs": ["m\nm\n", "m\nm\n", "td\nt\n", "aa\na\n", "td\nt\n", "ba\na\n", "ba\nb\n", "bb\nb\n"], "outputs": ["0\n", "0", "1\n", "1", "1", "1\n", "1\n", "1\n"]}
471
83
coding
Solve the programming task below in a Python markdown code block. # Task Your task is to sort the characters in a string according to the following rules: ``` - Rule1: English alphabets are arranged from A to Z, case insensitive. ie. "Type" --> "epTy" - Rule2: If the uppercase and lowercase of an English alphabet exist at the same time, they are arranged in the order of oringal input. ie. "BabA" --> "aABb" - Rule3: non English alphabet remain in their original position. ie. "By?e" --> "Be?y" ``` # Input/Output `[input]` string `s` A non empty string contains any characters(English alphabets or non English alphabets). `[output]` a string A sorted string according to the rules above. # Example For `s = "cba"`, the output should be `"abc"`. For `s = "Cba"`, the output should be `"abC"`. For `s = "cCBbAa"`, the output should be `"AaBbcC"`. For `s = "c b a"`, the output should be `"a b c"`. For `s = "-c--b--a-"`, the output should be `"-a--b--c-"`. For `s = "Codewars"`, the output should be `"aCdeorsw"`. Also feel free to reuse/extend the following starter code: ```python def sort_string(s): ```
{"functional": "_inputs = [['a'], ['cba'], ['Cba'], ['cCBbAa'], ['!'], ['c b a'], ['-c--b--a-'], ['cbaCcC'], ['Codewars'], [' MkWD{RB=//k-^ J@,xH Vfi uAz+$ kV _[ }a!}%pSBwn !kKB (b q PQF +}wS .kfU r wFNEs#NsR UVMdG']]\n_outputs = [['a'], ['abc'], ['abC'], ['AaBbcC'], ['!'], ['a b c'], ['-a--b--c-'], ['abcCcC'], ['aCdeorsw'], [' AaBB{Bb=//D-^ d@,Ef FfF GHi+$ Jk _[ }k!}%kkKkM !MnN (N p PqQ +}Rr .RSS s suUUV#VVW wwwxz']]\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_string(*i), o[0])"}
338
359
coding
Solve the programming task below in a Python markdown code block. Chef has a grid of size N \times M. In one move, Chef can move from his current cell to an adjacent cell. If the Chef is currently at (i, j) then, in one move, Chef can move to (i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1). There are also K *special* cells in the grid. These special cells support a corner move as well. If the Chef is currently at a *special* cell (i, j) then, in one move, Chef can move to (i + 1, j + 1), (i - 1, j - 1), (i + 1, j - 1) or (i - 1, j + 1) in addition to the adjacent cells ((i + 1, j), (i - 1, j), (i, j + 1) or (i, j - 1)). Chef is not allowed to leave the grid at any moment. Determine the minimum number of moves Chef needs to go from (1, 1) to (N, M). ------ 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 space-separated integers N, M and K — the number of rows in the grid, the number of columns in the grid and the number of special cells. - K lines follow. The i-th line of these K lines contains two space-separated integers X_{i} and Y_{i} such that (X_{i}, Y_{i}) is the i-th special cell. ------ Output Format ------ For each test case, output the minimum number of moves required by the Chef to go to (N, M) from (1, 1). ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N, M ≤ 10^{9}$ $0 ≤ K ≤ 2 \cdot 10^{5}$ $1 ≤ X_{i} ≤ N$, $1 ≤ Y_{i} ≤ M$ - All $(X_{i}, Y_{i})$ are pairwise distinct. - Sum of $K$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 2 3 3 0 3 3 2 2 2 2 3 ----- Sample Output 1 ------ 4 3 ----- explanation 1 ------ Test Case 1: There is no special cell in this case. One of the optimal ways is: $(1,1) \rightarrow (1,2) \rightarrow (1,3) \rightarrow (2,3) \rightarrow (3,3)$. Therefore a total of $4$ moves is required. Test Case 2: There are $2$ special cells in this case. One of the optimal ways is: $(1,1) \rightarrow (2,1) \rightarrow (2,2) \rightarrow (3,3)$. Therefore a total of $3$ moves is required.
{"inputs": ["2\n3 3 0\n3 3 2\n2 2\n2 3\n"], "outputs": ["4\n3\n"]}
691
36
coding
Solve the programming task below in a Python markdown code block. With a friend we used to play the following game on a chessboard (8, rows, 8 columns). On the first row at the *bottom* we put numbers: `1/2, 2/3, 3/4, 4/5, 5/6, 6/7, 7/8, 8/9` On row 2 (2nd row from the bottom) we have: `1/3, 2/4, 3/5, 4/6, 5/7, 6/8, 7/9, 8/10` On row 3: `1/4, 2/5, 3/6, 4/7, 5/8, 6/9, 7/10, 8/11` until last row: `1/9, 2/10, 3/11, 4/12, 5/13, 6/14, 7/15, 8/16` When all numbers are on the chessboard each in turn we toss a coin. The one who get "head" wins and the other gives him, in dollars, the **sum of the numbers on the chessboard**. We play for fun, the dollars come from a monopoly game! ### Task How much can I (or my friend) win or loses for each game if the chessboard has n rows and n columns? Add all of the fractional values on an n by n sized board and give the answer as a simplified fraction. - Ruby, Python, JS, Coffee, Clojure, PHP, Elixir, Crystal, Typescript, Go: The function called 'game' with parameter n (integer >= 0) returns as result an irreducible fraction written as an array of integers: [numerator, denominator]. If the denominator is 1 return [numerator]. - Haskell: 'game' returns either a "Left Integer" if denominator is 1 otherwise "Right (Integer, Integer)" - Prolog: 'game' returns an irreducible fraction written as an array of integers: [numerator, denominator]. If the denominator is 1 return [numerator, 1]. - Java, C#, C++, F#, Swift, Reason, Kotlin: 'game' returns a string that mimicks the array returned in Ruby, Python, JS, etc... - Fortran, Bash: 'game' returns a string - Forth: return on the stack the numerator and the denominator (even if the denominator is 1) - In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings. #### See Example Test Cases for each language Also feel free to reuse/extend the following starter code: ```python def game(n): ```
{"functional": "_inputs = [[0], [1], [8], [40], [101], [204], [807], [1808], [5014], [120000], [750000], [750001], [3000000], [3000001]]\n_outputs = [[[0]], [[1, 2]], [[32]], [[800]], [[10201, 2]], [[20808]], [[651249, 2]], [[1634432]], [[12570098]], [[7200000000]], [[281250000000]], [[562501500001, 2]], [[4500000000000]], [[9000006000001, 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(game(*i), o[0])"}
627
370
coding
Solve the programming task below in a Python markdown code block. You are given an array A of length N. An element X is said to be *dominant* if the frequency of X in A is strictly greater than the frequency of any other element in the A. For example, if A = [2, 1, 4, 4, 4] then 4 is a dominant element since its frequency is higher than the frequency of any other element in A. Find if there exists any dominant element in A. ------ Input Format ------ - The first line of input contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output YES if there exists any dominant element in A. Otherwise, output NO. You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical). ------ Constraints ------ $1 ≤T ≤500$ $1 ≤N ≤1000$ $1 ≤A_{i} ≤N$ ----- Sample Input 1 ------ 4 5 2 2 2 2 2 4 1 2 3 4 4 3 3 2 1 6 1 1 2 2 3 4 ----- Sample Output 1 ------ YES NO YES NO ----- explanation 1 ------ Test case $1$: $2$ is the dominant element. Test case $2$: There does not exist any dominant element. Test case $3$: $3$ is the dominant element.
{"inputs": ["4\n5\n2 2 2 2 2\n4\n1 2 3 4\n4\n3 3 2 1\n6\n1 1 2 2 3 4\n"], "outputs": ["YES\nNO\nYES\nNO\n"]}
394
66
coding
Solve the programming task below in a Python markdown code block. There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second block, he gets the integer 12. Wet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 10^9 + 7. Note, that the number of ways to choose some digit in the block is equal to the number of it's occurrences. For example, there are 3 ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5. -----Input----- The first line of the input contains four space-separated integers, n, b, k and x (2 ≤ n ≤ 50 000, 1 ≤ b ≤ 10^9, 0 ≤ k < x ≤ 100, x ≥ 2) — the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself. The next line contains n space separated integers a_{i} (1 ≤ a_{i} ≤ 9), that give the digits contained in each block. -----Output----- Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x. -----Examples----- Input 12 1 5 10 3 5 6 7 8 9 5 1 1 1 1 5 Output 3 Input 3 2 1 2 6 2 2 Output 0 Input 3 2 1 2 3 1 2 Output 6 -----Note----- In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2. In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.
{"inputs": ["3 2 1 2\n6 2 2\n", "3 2 1 2\n3 1 2\n", "3 2 1 2\n6 3 2\n", "3 2 1 2\n3 6 3\n", "3 2 0 2\n3 3 9\n", "3 2 0 2\n4 5 1\n", "3 2 0 2\n1 3 2\n", "3 2 1 2\n5 9 3\n"], "outputs": ["0\n", "6\n", "3\n", "6\n", "0\n", "3\n", "3\n", "9\n"]}
535
166
coding
Solve the programming task below in a Python markdown code block. Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) For example, the smallest pair of amicable numbers is (220, 284); for the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220. Derive function ```amicableNumbers(num1, num2)``` which returns ```true/True``` if pair ```num1 num2``` are amicable, ```false/False``` if not. See more at https://en.wikipedia.org/wiki/Amicable_numbers Also feel free to reuse/extend the following starter code: ```python def amicable_numbers(n1,n2): ```
{"functional": "_inputs = [[220, 284], [220, 280], [1184, 1210], [220221, 282224], [10744, 10856], [299920, 9284], [999220, 2849], [122265, 139815]]\n_outputs = [[True], [False], [True], [False], [True], [False], [False], [True]]\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(amicable_numbers(*i), o[0])"}
283
280
coding
Solve the programming task below in a Python markdown code block. Consider a sequence generation that follows the following steps. We will store removed values in variable `res`. Assume `n = 25`: ```Haskell -> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] Let's remove the first number => res = [1]. We get.. -> [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]. Let's remove 2 (so res = [1,2]) and every 2-indexed number. We get.. -> [3,5,7,9,11,13,15,17,19,21,23,25]. Now remove 3, then every 3-indexed number. res = [1,2,3]. -> [5,7,11,13,17,19,23,25]. Now remove 5, and every 5-indexed number. res = [1,2,3,5]. We get.. -> [7,11,13,17,23,25]. Now remove 7 and every 7-indexed. res = [1,2,3,5,7]. But we know that there are no other 7-indexed elements, so we include all remaining numbers in res. So res = [1,2,3,5,7,11,13,17,23,25] and sum(res) = 107. Note that when we remove every n-indexed number, we must remember that indices start at 0. So for every 3-indexed number above: [3,5,7,9,11,13,15,17,19,21,23], we remove index0=3, index3= 9, index6=15,index9=21, etc. Note also that if the length of sequence is greater than x, where x is the first element of the sequence, you should continue the remove step: remove x, and every x-indexed number until the length of sequence is shorter than x. In our example above, we stopped at 7 because the the length of the remaining sequence [7,11,13,17,23,25] is shorter than 7. ``` You will be given a number `n` and your task will be to return the sum of the elements in res, where the maximum element in res is `<= n`. For example: ```Python Solve(7) = 18, because this is the sum of res = [1,2,3,5,7]. Solve(25) = 107 ``` More examples in the test cases. Good luck! Also feel free to reuse/extend the following starter code: ```python def solve(n): ```
{"functional": "_inputs = [[7], [25], [50], [100], [1000], [10000]]\n_outputs = [[18], [107], [304], [993], [63589], [4721110]]\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(solve(*i), o[0])"}
730
212
coding
Solve the programming task below in a Python markdown code block. Fafa owns a company that works on huge projects. There are n employees in Fafa's company. Whenever the company has a new project to start working on, Fafa has to divide the tasks of this project among all the employees. Fafa finds doing this every time is very tiring for him. So, he decided to choose the best l employees in his company as team leaders. Whenever there is a new project, Fafa will divide the tasks among only the team leaders and each team leader will be responsible of some positive number of employees to give them the tasks. To make this process fair for the team leaders, each one of them should be responsible for the same number of employees. Moreover, every employee, who is not a team leader, has to be under the responsibility of exactly one team leader, and no team leader is responsible for another team leader. Given the number of employees n, find in how many ways Fafa could choose the number of team leaders l in such a way that it is possible to divide employees between them evenly. -----Input----- The input consists of a single line containing a positive integer n (2 ≤ n ≤ 10^5) — the number of employees in Fafa's company. -----Output----- Print a single integer representing the answer to the problem. -----Examples----- Input 2 Output 1 Input 10 Output 3 -----Note----- In the second sample Fafa has 3 ways: choose only 1 employee as a team leader with 9 employees under his responsibility. choose 2 employees as team leaders with 4 employees under the responsibility of each of them. choose 5 employees as team leaders with 1 employee under the responsibility of each of them.
{"inputs": ["2\n", "3\n", "4\n", "6\n", "9\n", "4\n", "3\n", "6\n"], "outputs": ["1\n", "1\n", "2\n", "3\n", "2\n", "2", "1", "3"]}
368
67
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. Mike likes to invent new functions. The latest one he has invented is called SuperFunction. Let's consider how it can be calculated: You are given two integers N and K. SuperFunction of N and K equals to the sum of K'th powers of the positive numbers, which are coprime with N and also not greater than N. I.e. SuperFuntion of 6 and 3 equals to 1^{3} + 5^{3} = 126. Mike knows how to calculate SuperFunction in case N and K are rather small integers. Now he wants you to write him a solution, which can calculate SuperFuntion for bigger constraints. As far as SuperFunction could be extremely huge, you are asked to do all the calculation under modulo M. Help Mike! ------ Input ------ The first line contains three integers N, K and M. ------ Output ------ The first line of contains the only integer, denoting SuperFuntion of N and K under modulo M. ------ Example ------ Input: 5 2 100 Output: 30 ------ Explanation ------ In the test case N equals to 5, K equals to 2, M equals to 100. The answer is (1^{2} + 2^{2} + 3^{2} + 4^{2}) mod 100 = 30. ------ Scoring ------ Subtask 1 (12 points): 1 ≤ N ≤ 100, 1 ≤ K ≤ 2, 1 ≤ M ≤ 40000; Subtask 2 (28 points): 1 ≤ N ≤ 1 000 000, 1 ≤ K ≤ 2, 1 ≤ M ≤ 10^{9}; Subtask 3 (30 points): 1 ≤ N ≤ 1 000 000 000 000, 1 ≤ K ≤ 3, 1 ≤ M ≤ 10^{9}; Subtask 4 (30 points): 1 ≤ N ≤ 1 000 000 000 000, 1 ≤ K ≤ 10, 1 ≤ M ≤ 10^{18}.
{"inputs": ["5 2 100"], "outputs": ["30"]}
509
19
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. We have an array of length $n$. Initially, each element is equal to $0$ and there is a pointer located on the first element. We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $1$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $1$. Then move it to the previous element. But there is one additional rule. After we are done, the pointer has to be on the first element. You are given an array $a$. Determine whether it's possible to obtain $a$ after some operations or not. -----Input----- The first line contains a single integer $t$ $(1\le t\le 1000)$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ $(1\le n\le 2 \cdot 10^5)$ — the size of array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print "Yes" (without quotes) if it's possible to obtain $a$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). -----Examples----- Input 7 2 1 0 4 2 -1 -1 0 4 1 -4 3 0 4 1 -1 1 -1 5 1 2 3 4 -10 7 2 -1 1 -2 0 0 0 1 0 Output No Yes No No Yes Yes Yes -----Note----- In the first test case we can obtain the array after some operations, but the pointer won't be on the first element. One way of obtaining the array in the second test case is shown below. $\langle \underline{0}, 0, 0, 0\rangle \to \langle 1, \underline{0}, 0, 0 \rangle \to \langle \underline{1}, -1, 0, 0\rangle \to \langle 2, \underline{-1}, 0, 0\rangle \to \langle 2, 0, \underline{0}, 0\rangle \to \langle 2, \underline{0}, -1, 0\rangle \to \langle \underline{2}, -1, -1, 0\rangle$
{"inputs": ["1\n1\n1\n", "1\n2\n2 2\n", "1\n2\n-800372697 800372697\n", "7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0\n"], "outputs": ["No\n", "No\n", "No\n", "No\nYes\nNo\nNo\nYes\nYes\nYes\n"]}
677
152
coding
Solve the programming task below in a Python markdown code block. VK news recommendation system daily selects interesting publications of one of $n$ disjoint categories for each user. Each publication belongs to exactly one category. For each category $i$ batch algorithm selects $a_i$ publications. The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of $i$-th category within $t_i$ seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm. -----Input----- The first line of input consists of single integer $n$ — the number of news categories ($1 \le n \le 200\,000$). The second line of input consists of $n$ integers $a_i$ — the number of publications of $i$-th category selected by the batch algorithm ($1 \le a_i \le 10^9$). The third line of input consists of $n$ integers $t_i$ — time it takes for targeted algorithm to find one new publication of category $i$ ($1 \le t_i \le 10^5)$. -----Output----- Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size. -----Examples----- Input 5 3 7 9 7 8 5 2 5 7 5 Output 6 Input 5 1 2 3 4 5 1 1 1 1 1 Output 0 -----Note----- In the first example, it is possible to find three publications of the second type, which will take 6 seconds. In the second example, all news categories contain a different number of publications.
{"inputs": ["1\n1\n7\n", "1\n2\n7\n", "1\n2\n4\n", "1\n1\n72\n", "1\n5\n16\n", "1\n3\n31\n", "1\n1\n72\n", "1\n3\n31\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
409
107
coding
Solve the programming task below in a Python markdown code block. A string $s$ of length $n$, consisting of lowercase letters of the English alphabet, is given. You must choose some number $k$ between $0$ and $n$. Then, you select $k$ characters of $s$ and permute them however you want. In this process, the positions of the other $n-k$ characters remain unchanged. You have to perform this operation exactly once. For example, if $s={"andrea"}$, you can choose the $k=4$ characters ${"a_d_ea"}$ and permute them into ${"d_e_aa"}$ so that after the operation the string becomes ${"dneraa"}$. Determine the minimum $k$ so that it is possible to sort $s$ alphabetically (that is, after the operation its characters appear in alphabetical order). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 40$) — the length of the string. The second line of each test case contains the string $s$. It is guaranteed that $s$ contains only lowercase letters of the English alphabet. -----Output----- For each test case, output the minimum $k$ that allows you to obtain a string sorted alphabetically, through the operation described above. -----Examples----- Input 4 3 lol 10 codeforces 5 aaaaa 4 dcba Output 2 6 0 4 -----Note----- In the first test case, we can choose the $k=2$ characters ${"_ol"}$ and rearrange them as ${"_lo"}$ (so the resulting string is ${"llo"}$). It is not possible to sort the string choosing strictly less than $2$ characters. In the second test case, one possible way to sort $s$ is to consider the $k=6$ characters ${"_o__force_"}$ and rearrange them as ${"_c__efoor_"}$ (so the resulting string is ${"ccdeefoors"}$). One can show that it is not possible to sort the string choosing strictly less than $6$ characters. In the third test case, string $s$ is already sorted (so we can choose $k=0$ characters). In the fourth test case, we can choose all $k=4$ characters ${"dcba"}$ and reverse the whole string (so the resulting string is ${"abcd"}$).
{"inputs": ["1\n20\naaaaaaaaaaaaaaaaaqwq\n", "1\n20\naaaaaaaaaaaaaaaaaqwq\n", "1\n20\naaaaaaabaaaaaaaaaqwq\n", "1\n20\nabaaaaabaaaaaaaaaqwq\n", "1\n20\nqwqaaaaaaaaabaaaaaba\n", "1\n20\nqwqaaaaabaaabaaaaaba\n", "1\n20\nabaaaaabaaabaaaaaqwq\n", "1\n20\nqwqaaaaaaaaabaaaabba\n"], "outputs": ["2\n", "2\n", "4\n", "6\n", "9\n", "11\n", "8\n", "10\n"]}
569
158
coding
Solve the programming task below in a Python markdown code block. You are given an integer sequence of length N, a = {a_1, a_2, …, a_N}, and an integer K. a has N(N+1)/2 non-empty contiguous subsequences, {a_l, a_{l+1}, …, a_r} (1 ≤ l ≤ r ≤ N). Among them, how many have an arithmetic mean that is greater than or equal to K? Constraints * All input values are integers. * 1 ≤ N ≤ 2 \times 10^5 * 1 ≤ K ≤ 10^9 * 1 ≤ a_i ≤ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K. Examples Input 3 6 7 5 7 Output 5 Input 1 2 1 Output 0 Input 7 26 10 20 30 40 30 20 10 Output 13
{"inputs": ["1 4\n1", "1 2\n0", "1 6\n1", "1 2\n1", "3 5\n7\n5\n7", "3 6\n7\n5\n7", "3 6\n7\n5\n10", "3 6\n7\n5\n12"], "outputs": ["0\n", "0\n", "0\n", "0", "6\n", "5", "5\n", "5\n"]}
265
110
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a string s. You can convert s to a palindrome by adding characters in front of it. Return the shortest palindrome you can find by performing this transformation.   Please complete the following python code precisely: ```python class Solution: def shortestPalindrome(self, s: str) -> str: ```
{"functional": "def check(candidate):\n assert candidate(s = \"aacecaaa\") == \"aaacecaaa\"\n assert candidate(s = \"abcd\") == \"dcbabcd\"\n\n\ncheck(Solution().shortestPalindrome)"}
81
54
coding
Solve the programming task below in a Python markdown code block. It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws n sticks in a row. After that the players take turns crossing out exactly k sticks from left or right in each turn. Sasha moves first, because he is the inventor of the game. If there are less than k sticks on the paper before some turn, the game ends. Sasha wins if he makes strictly more moves than Lena. Sasha wants to know the result of the game before playing, you are to help him. -----Input----- The first line contains two integers n and k (1 ≤ n, k ≤ 10^18, k ≤ n) — the number of sticks drawn by Sasha and the number k — the number of sticks to be crossed out on each turn. -----Output----- If Sasha wins, print "YES" (without quotes), otherwise print "NO" (without quotes). You can print each letter in arbitrary case (upper of lower). -----Examples----- Input 1 1 Output YES Input 10 4 Output NO -----Note----- In the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins. In the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha doesn't win.
{"inputs": ["1 1\n", "6 2\n", "6 3\n", "6 4\n", "6 5\n", "6 6\n", "2 2\n", "6 1\n"], "outputs": ["YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES\n", "YES\n", "NO\n"]}
363
86
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In LeetCode Store, there are n items to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. You are given an integer array price where price[i] is the price of the ith item, and an integer array needs where needs[i] is the number of pieces of the ith item you want to buy. You are also given an array special where special[i] is of size n + 1 where special[i][j] is the number of pieces of the jth item in the ith offer and special[i][n] (i.e., the last integer in the array) is the price of the ith offer. Return the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.   Please complete the following python code precisely: ```python class Solution: def shoppingOffers(self, price: List[int], special: List[List[int]], needs: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]) == 14\n assert candidate(price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]) == 11\n\n\ncheck(Solution().shoppingOffers)"}
270
102
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given n rectangles represented by a 0-indexed 2D integer array rectangles, where rectangles[i] = [widthi, heighti] denotes the width and height of the ith rectangle. Two rectangles i and j (i < j) are considered interchangeable if they have the same width-to-height ratio. More formally, two rectangles are interchangeable if widthi/heighti == widthj/heightj (using decimal division, not integer division). Return the number of pairs of interchangeable rectangles in rectangles.   Please complete the following python code precisely: ```python class Solution: def interchangeableRectangles(self, rectangles: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(rectangles = [[4,8],[3,6],[10,20],[15,30]]) == 6\n assert candidate(rectangles = [[4,5],[7,8]]) == 0\n\n\ncheck(Solution().interchangeableRectangles)"}
153
72
coding
Solve the programming task below in a Python markdown code block. For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the $n$ days of summer. On the $i$-th day, $a_i$ millimeters of rain will fall. All values $a_i$ are distinct. The mayor knows that citizens will watch the weather $x$ days before the celebration and $y$ days after. Because of that, he says that a day $d$ is not-so-rainy if $a_d$ is smaller than rain amounts at each of $x$ days before day $d$ and and each of $y$ days after day $d$. In other words, $a_d < a_j$ should hold for all $d - x \le j < d$ and $d < j \le d + y$. Citizens only watch the weather during summer, so we only consider such $j$ that $1 \le j \le n$. Help mayor find the earliest not-so-rainy day of summer. -----Input----- The first line contains three integers $n$, $x$ and $y$ ($1 \le n \le 100\,000$, $0 \le x, y \le 7$) — the number of days in summer, the number of days citizens watch the weather before the celebration and the number of days they do that after. The second line contains $n$ distinct integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ denotes the rain amount on the $i$-th day. -----Output----- Print a single integer — the index of the earliest not-so-rainy day of summer. We can show that the answer always exists. -----Examples----- Input 10 2 2 10 9 6 7 8 3 2 1 4 5 Output 3 Input 10 2 3 10 9 6 7 8 3 2 1 4 5 Output 8 Input 5 5 5 100000 10000 1000 100 10 Output 5 -----Note----- In the first example days $3$ and $8$ are not-so-rainy. The $3$-rd day is earlier. In the second example day $3$ is not not-so-rainy, because $3 + y = 6$ and $a_3 > a_6$. Thus, day $8$ is the answer. Note that $8 + y = 11$, but we don't consider day $11$, because it is not summer.
{"inputs": ["1 0 0\n7\n", "1 0 1\n7\n", "1 0 0\n10\n", "1 0 0\n10\n", "1 0 1\n10\n", "2 1 0\n2 1\n", "2 0 1\n2 1\n", "2 2 2\n1 2\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2\n", "1\n"]}
628
127
coding
Solve the programming task below in a Python markdown code block. An array of integers is called *good* if all its elements are perfect squares. You are given an array A of N integers. In one move, you can do the following: Pick a subset of indices of the array, say \{i_{1}, i_{2}, \ldots, i_{k}\} where 1 ≤ i_{1} < i_{2} < \ldots < i_{k} ≤ N Next, pick an integer X > 0 Finally, multiply the value at each chosen index by X, i.e, set A_{i_{j}} to A_{i_{j}} \cdot X for each 1 ≤ j ≤ k Find the minimum number of moves required to make the array good. Note: The value of X is fixed for a given move, but can vary between moves. ------ Input Format ------ - The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows. - Each test case contains two lines of input. - The first line of each test case contains N, the size of the array. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each testcase, output in a single line the minimum number of moves required to make the array good. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{4}$ $1 ≤ A_{i} ≤ 10^{8}$ - The sum of $N$ across all testcases won't exceed $10^{4}$. ------ subtasks ------ Subtask #1 (100 points): Original constraints ----- Sample Input 1 ------ 5 3 15 2 4 4 15 14 4 9 3 10 3 6 2 4 9 2 2 8 ----- Sample Output 1 ------ 2 2 3 0 1 ----- explanation 1 ------ Test case $1$: One possible sequence of moves is: - Multiply the first element by $15$ - Multiply the second element by $2$ The array is now $[225, 4, 4] = [15^{2}, 2^{2}, 2^{2}]$, which is good. Test case $3$: One possible sequence of moves is: - Multiply the first and third elements by $8$ - Multiply the second and third elements by $3$ - Multiply the first element by $20$ The array is now $[1600, 9, 144] = [40^{2}, 3^{2}, 12^{2}]$, which is good. Test case $4$: The array is already good, since $[4, 9] = [2^{2}, 3^{2}]$, so no moves need to be made.
{"inputs": ["5\n3\n15 2 4\n4\n15 14 4 9\n3\n10 3 6\n2\n4 9\n2\n2 8\n"], "outputs": ["2\n2\n3\n0\n1\n"]}
649
64
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese here ------ Problem Statement ------ Maxim likes dividers of the numbers. Also Maxim is fond of lucky numbers of small elephant from Lviv city.   If you remember, lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky, 5, 17, 467 — aren't.   Now Maxim is interested in the next information: what is the number of the integer positive dividers of number n, which are overlucky.   We call number overlucky if it is possible to remove some, but not all, digits and during bonding the remaining digits we will receive a lucky number. For example, number 72344 — overlucky, because it is possible to remove digits 2 and 3, and get number 744, which is lucky. Number 223 isn't overlucky.  ------ Input ------ The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. Single line of each test case contains an integer n.  ------ Output ------ For each test case on different lines print the answer to the problem. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ n ≤ 10^{9}$   ----- Sample Input 1 ------ 10 1 2 3 4 5 6 7 8 9 10 ----- Sample Output 1 ------ 0 0 0 1 0 0 1 1 0 0
{"inputs": ["10\n2\n4\n2\n7\n5\n6\n7\n8\n9\n2", "10\n2\n2\n2\n4\n5\n8\n1\n2\n9\n5", "10\n2\n2\n1\n4\n5\n6\n3\n8\n9\n4", "10\n2\n3\n2\n5\n5\n4\n1\n2\n9\n5", "10\n2\n2\n1\n6\n5\n6\n3\n8\n9\n4", "10\n2\n4\n2\n7\n4\n6\n7\n8\n9\n2", "10\n2\n5\n4\n5\n5\n4\n1\n2\n9\n2", "10\n3\n4\n2\n9\n4\n6\n7\n8\n9\n2"], "outputs": ["0\n1\n0\n1\n0\n0\n1\n1\n0\n0\n", "0\n0\n0\n1\n0\n1\n0\n0\n0\n0\n", "0\n0\n0\n1\n0\n0\n0\n1\n0\n1\n", "0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n1\n0\n1\n", "0\n1\n0\n1\n1\n0\n1\n1\n0\n0\n", "0\n0\n1\n0\n0\n1\n0\n0\n0\n0\n", "0\n1\n0\n0\n1\n0\n1\n1\n0\n0\n"]}
370
374
coding
Solve the programming task below in a Python markdown code block. B: Periodic Sequence- problem Dr. Period, a professor at H University, is studying a property called the cycle that is supposed to be hidden in all things. As a generally known basic cycle, a cycle hidden in a sequence may be considered. That is, if the sequence S = S_1, S_2, ..., S_N of length N satisfies the following properties, it has a period t (t \ ≤ N). For 1 \ ≤ i \ ≤ N − t, S_i = S_ {i + t}. Now, Dr. Period is paying attention to a sequence that can be described more simply using a period. For example, if a sequence of length N has a period t (\ ≤ N) and you can write N = kt using an integer k, then that sequence is a sequence of length t S_1, ..., S_t is k. It can be described that the pieces are continuous. When Dr. Period could describe a sequence as an example, he decided to say that the sequence was a k-part. Dr. Period is interested in the k-part with the largest k. So, as an assistant, you are tasked with writing a program that takes a sequence as input and outputs the largest k when it is a k-part. Create a program that exactly meets Dr. Period's demands. Input format N S_1 ... S_N The first row is given the integer N, which represents the length of the sequence. In the second row, the integer S_i (1 \ ≤ i \ ≤ N) representing each element of the sequence of length N is given, separated by blanks. Also, the inputs satisfy 1 \ ≤ N \ ≤ 200,000 and 1 \ ≤ S_i \ ≤ 100,000 (1 \ ≤ i \ ≤ N). Output format For a given sequence, output the maximum value of k when it is k-part in one row. Input example 1 6 1 2 3 1 2 3 Output example 1 2 Input example 2 12 1 2 1 2 1 2 1 2 1 2 1 2 Output example 2 6 Input example 3 6 1 2 3 4 5 6 Output example 3 1 Example Input 6 1 2 3 1 2 3 Output 2
{"inputs": ["6\n1 2 3 1 1 3", "6\n1 2 3 1 0 3", "6\n2 2 3 1 0 3", "6\n2 2 3 2 0 3", "6\n4 2 3 2 0 3", "6\n4 2 3 2 0 4", "6\n4 4 3 2 0 4", "6\n7 4 3 2 0 4"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
530
158
coding
Solve the programming task below in a Python markdown code block. Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first k Latin letters and not containing any other ones. Also, the title should be a palindrome, that is it should be read similarly from the left to the right and from the right to the left. Vasya has already composed the approximate variant of the title. You are given the title template s consisting of lowercase Latin letters and question marks. Your task is to replace all the question marks by lowercase Latin letters so that the resulting word satisfies the requirements, described above. Each question mark should be replaced by exactly one letter, it is not allowed to delete characters or add new ones to the template. If there are several suitable titles, choose the first in the alphabetical order, for Vasya's book to appear as early as possible in all the catalogues. Input The first line contains an integer k (1 ≤ k ≤ 26) which is the number of allowed alphabet letters. The second line contains s which is the given template. In s only the first k lowercase letters of Latin alphabet and question marks can be present, the length of s is from 1 to 100 characters inclusively. Output If there is no solution, print IMPOSSIBLE. Otherwise, a single line should contain the required title, satisfying the given template. The title should be a palindrome and it can only contain the first k letters of the Latin alphabet. At that, each of those k letters must be present at least once. If there are several suitable titles, print the lexicographically minimal one. The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if exists such an i (1 ≤ i ≤ |s|), that ai < bi, and for any j (1 ≤ j < i) aj = bj. |s| stands for the length of the given template. Examples Input 3 a?c Output IMPOSSIBLE Input 2 a??a Output abba Input 2 ?b?a Output abba
{"inputs": ["2\n?\n", "1\na\n", "1\n?\n", "1\n?a\n", "3\nb?c\n", "3\na?c\n", "2\n????\n", "3\n????\n"], "outputs": ["IMPOSSIBLE\n", "a\n", "a\n", "aa\n", "IMPOSSIBLE\n", "IMPOSSIBLE\n", "abba\n", "IMPOSSIBLE\n"]}
499
105
coding
Solve the programming task below in a Python markdown code block. You are given an integer N. You have to find a [permutation] P of the integers \{1, 2, \ldots, N\} that satisfies M conditions of the following form: (X_{i}, Y_{i}), denoting that the element X_{i}\;(1≤ X_{i} ≤ N) must appear in the prefix of length Y_{i}. Formally, if the index of the element X_{i} is K_{i} (i.e, P_{K_{i}} = X_{i}), then the condition 1≤ K_{i} ≤ Y_{i} must hold. Print -1 if no such permutation exists. In case multiple permutations that satisfy all the conditions exist, find the lexicographically smallest one. Note: If two arrays A and B have the same length N, then A is called lexicographically smaller than B only if there exists an index i \; (1≤ i ≤ N) such that A_{1}=B_{1}, A_{2}=B_{2}, \ldots, A_{i-1}=B_{i-1}, A_{i} < B_{i}. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first line of each test case contains two space-separated integers N and M — the length of permutation and the number of conditions to be satisfied respectively. - The next M lines describe the conditions. The i-th of these M lines contains two space-separated integers X_{i} and Y_{i}. ------ Output Format ------ For each test case, output a single line containing the answer: - If no permutation satisfies the given conditions, print −1. - Otherwise, print N space-separated integers, denoting the elements of the permutation. If there are multiple answers, output the lexicographically smallest one. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $1 ≤ M ≤ N$ $1 ≤ X_{i}, Y_{i} ≤ N$ $X_{i} \neq X_{j}$ for each $1 ≤ i < j ≤ M$. - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 4 3 2 2 1 1 2 4 3 1 2 4 1 3 2 4 1 2 3 5 2 5 4 3 2 ----- Sample Output 1 ------ 2 1 3 -1 1 2 3 4 1 3 2 5 4 ----- explanation 1 ------ Test case $1$: The only permutation of length $3$ that satisfies all the conditions is $[2,1,3]$. Test case $2$: There is no permutation of length $4$ that satisfies all the given conditions. Test case $3$: There are many permutations of length $4$ that satisfy all the conditions, such as $[1,2,3,4],[1, 4, 2, 3],[1, 3, 2, 4], [2, 1, 4, 3], [2, 1, 3, 4]$, etc. $[1,2,3,4]$ is the lexicographically smallest among them.
{"inputs": ["4\n3 2\n2 1\n1 2\n4 3\n1 2\n4 1\n3 2\n4 1\n2 3\n5 2\n5 4\n3 2\n"], "outputs": ["2 1 3\n-1\n1 2 3 4\n1 3 2 5 4\n"]}
751
87
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two strings a and b that consist of lowercase letters. In one operation, you can change any character in a or b to any lowercase letter. Your goal is to satisfy one of the following three conditions: Every letter in a is strictly less than every letter in b in the alphabet. Every letter in b is strictly less than every letter in a in the alphabet. Both a and b consist of only one distinct letter. Return the minimum number of operations needed to achieve your goal.   Please complete the following python code precisely: ```python class Solution: def minCharacters(self, a: str, b: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(a = \"aba\", b = \"caa\") == 2\n assert candidate(a = \"dabadd\", b = \"cda\") == 3\n\n\ncheck(Solution().minCharacters)"}
150
56
coding
Solve the programming task below in a Python markdown code block. Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not." And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times $k$, so they got $k+1$ pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled. In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using $3$ cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts. More formally, Sasha wants for the given palindrome $s$ find such minimum $k$, that you can cut this string into $k + 1$ parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string $s$. It there is no answer, then print "Impossible" (without quotes). -----Input----- The first line contains one string $s$ ($1 \le |s| \le 5\,000$) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that $s$ is a palindrome. -----Output----- Print one integer $k$ — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes). -----Examples----- Input nolon Output 2 Input otto Output 1 Input qqqq Output Impossible Input kinnikkinnik Output 1 -----Note----- In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut. In the second example, you can cut the string right in the middle, and swap peaces, so you get toot. In the third example, you can't make a string, that won't be equal to the initial one. In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
{"inputs": ["a\n", "f\n", "a\n", "f\n", "`\n", "e\n", "b\n", "d\n"], "outputs": ["Impossible\n", "Impossible\n", "Impossible", "Impossible", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n"]}
676
68
coding
Solve the programming task below in a Python markdown code block. Takahashi's house has only one socket. Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets. One power strip with A sockets can extend one empty socket into A empty sockets. Find the minimum number of power strips required. -----Constraints----- - All values in input are integers. - 2 \leq A \leq 20 - 1 \leq B \leq 20 -----Input----- Input is given from Standard Input in the following format: A B -----Output----- Print the minimum number of power strips required. -----Sample Input----- 4 10 -----Sample Output----- 3 3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.
{"inputs": ["8 4", "4 9", "2 5", "7 9", "2 9", "4 1", "2 8", "2 7"], "outputs": ["1\n", "3\n", "4\n", "2\n", "8\n", "0\n", "7\n", "6\n"]}
177
78
coding
Solve the programming task below in a Python markdown code block. Chef has scored A, B, and C marks in 3 different subjects respectively. Chef will fail if the average score of any two subjects is less than 35. Determine whether Chef will pass or fail. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of a single line of input, three integers A, B, C, denoting the Chef's score in the three subjects. ------ Output Format ------ For each test case, if Chef will pass, print PASS, otherwise print FAIL. You may print each character of the string in uppercase or lowercase (for example, the strings pass, Pass, pAss, and PASS will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $0 ≤ A,B,C ≤ 100$ ----- Sample Input 1 ------ 4 23 47 52 28 36 80 0 100 0 35 35 35 ----- Sample Output 1 ------ Pass Fail Fail Pass ----- explanation 1 ------ Test case $1$: The average of the first two subjects is $35$, the average of the first and last subject is $37.5$, whereas the average of the last two subjects is $49.5$. Since all averages are greater than or equal to $35$, Chef will pass. Test case $2$: Since the average of the first two subjects is $32$ which is less than $35$, Chef will fail. Test case $3$: Since the average of the first and last subjects subjects is $0$ which is less than $35$, Chef will fail. Test case $4$: Since the average of any two subjects is $35$ which is greater than or equal to $35$, Chef will pass.
{"inputs": ["4\n23 47 52\n28 36 80\n0 100 0\n35 35 35\n"], "outputs": ["Pass\nFail\nFail\nPass"]}
418
54
coding
Solve the programming task below in a Python markdown code block. Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more. Bizon the Champion wonders whether the "Bizons" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order. -----Input----- The first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters. -----Output----- In the single line print the answer to the problem. Print "need tree" (without the quotes) if word s cannot be transformed into word t even with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without the quotes) if you need only the suffix array to solve the problem. Print "both" (without the quotes), if you need both data structures to solve the problem. It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton. -----Examples----- Input automaton tomat Output automaton Input array arary Output array Input both hot Output both Input need tree Output need tree -----Note----- In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot".
{"inputs": ["y\nu\n", "y\nu\n", "z\nu\n", "y\nt\n", "z\nt\n", "z\nzz\n", "z\nzz\n", "ba\naa\n"], "outputs": ["need tree\n", "need tree\n", "need tree\n", "need tree\n", "need tree\n", "need tree\n", "need tree\n", "need tree\n"]}
557
94
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]: numberOfBoxesi is the number of boxes of type i. numberOfUnitsPerBoxi is the number of units in each box of the type i. You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize. Return the maximum total number of units that can be put on the truck.   Please complete the following python code precisely: ```python class Solution: def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4) == 8\n assert candidate(boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10) == 91\n\n\ncheck(Solution().maximumUnits)"}
189
84
coding
Solve the programming task below in a Python markdown code block. William has array of $n$ numbers $a_1, a_2, \dots, a_n$. He can perform the following sequence of operations any number of times: Pick any two items from array $a_i$ and $a_j$, where $a_i$ must be a multiple of $2$ $a_i = \frac{a_i}{2}$ $a_j = a_j \cdot 2$ Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains an integer $n$ $(1 \le n \le 15)$, the number of elements in William's array. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i < 16)$, the contents of William's array. -----Output----- For each test case output the maximal sum of array elements after performing an optimal sequence of operations. -----Examples----- Input 5 3 6 4 2 5 1 2 3 4 5 1 10 3 2 3 4 15 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 Output 50 46 10 26 35184372088846 -----Note----- In the first example test case the optimal sequence would be: Pick $i = 2$ and $j = 1$. After performing a sequence of operations $a_2 = \frac{4}{2} = 2$ and $a_1 = 6 \cdot 2 = 12$, making the array look as: [12, 2, 2]. Pick $i = 2$ and $j = 1$. After performing a sequence of operations $a_2 = \frac{2}{2} = 1$ and $a_1 = 12 \cdot 2 = 24$, making the array look as: [24, 1, 2]. Pick $i = 3$ and $j = 1$. After performing a sequence of operations $a_3 = \frac{2}{2} = 1$ and $a_1 = 24 \cdot 2 = 48$, making the array look as: [48, 1, 1]. The final answer $48 + 1 + 1 = 50$. In the third example test case there is no way to change the sum of elements, so the answer is $10$.
{"inputs": ["5\n3\n6 4 2\n5\n1 2 3 4 5\n1\n10\n3\n2 3 4\n15\n8 8 8 8 8 8 8 8 8 8 8 8 8 8 8\n"], "outputs": ["50\n46\n10\n26\n35184372088846\n"]}
632
105
coding
Solve the programming task below in a Python markdown code block. ##Task: You have to write a function **pattern** which creates the following pattern upto n number of rows. *If the Argument is 0 or a Negative Integer then it should return "" i.e. empty string.* ##Examples: pattern(4): 1234 234 34 4 pattern(6): 123456 23456 3456 456 56 6 ```Note: There are no blank spaces``` ```Hint: Use \n in string to jump to next line``` Also feel free to reuse/extend the following starter code: ```python def pattern(n): ```
{"functional": "_inputs = [[1], [2], [5], [0], [-25]]\n_outputs = [['1'], ['12\\n2'], ['12345\\n2345\\n345\\n45\\n5'], [''], ['']]\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(pattern(*i), o[0])"}
177
202
coding
Solve the programming task below in a Python markdown code block. Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor. Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed «codeforces», «coding» and «codeforces» once again, then there will be no autocompletion attempt for «cod», but if he proceeds with «code», the editor will propose «codeforces». With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols? A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in «it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.». -----Input----- The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: «.», «,», «?», «!», «'» and «-». The total amount of symbols doesn't exceed 3·10^5. It's guaranteed that all lines are non-empty. -----Output----- Print a single integer — the minimum number of clicks. -----Examples----- Input snow affects sports such as skiing, snowboarding, and snowmachine travel. snowboarding is a recreational activity and olympic and paralympic sport. Output 141 Input 'co-co-co, codeforces?!' Output 25 Input thun-thun-thunder, thunder, thunder thunder, thun-, thunder thun-thun-thunder, thunder thunder, feel the thunder lightning then the thunder thunder, feel the thunder lightning then the thunder thunder, thunder Output 183 -----Note----- In sample case one it's optimal to use autocompletion for the first instance of «snowboarding» after typing up «sn» and for the second instance of «snowboarding» after typing up «snowb». This will save 7 clicks. In sample case two it doesn't matter whether to use autocompletion or not.
{"inputs": ["aaa aaaaaa\n", "aaa aaaaaa\n", "aaa aabaaa\n", "aaa aaabaa\n", "aaa aaabab\n", "aaa baaaab\n", "'co-co-co, codeforces?!'\n", "'co-co-co, '!?secrofedoc\n"], "outputs": ["10\n", "10\n", "11\n", "10\n", "10\n", "11\n", "25\n", "25\n"]}
610
112
coding
Solve the programming task below in a Python markdown code block. Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Only $x$ hours are left for the March Long Challenge and Chef is only left with the last problem unsolved. However, he is sure that he cannot solve the problem in the remaining time. From experience, he figures out that he needs exactly $H$ hours to solve the problem. Now Chef finally decides to use his special power which he has gained through years of intense yoga. He can travel back in time when he concentrates. Specifically, his power allows him to travel to $N$ different time zones, which are $T_{1}, T_{2}, \ldots, T_{N}$ hours respectively behind his current time. Find out whether Chef can use one of the available time zones to solve the problem and submit it before the contest ends. ------ Input ------ The first line of the input contains three space-separated integers $N$, $H$ and $x$. The second line contains $N$ space-separated integers $T_{1}, T_{2}, \ldots, T_{N}$. ------ Output ------ Print a single line containing the string "YES" if Chef can solve the problem on time or "NO" if he cannot. You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≤ N ≤ 100$ $1 ≤ x < H ≤ 100$ $1 ≤ T_{i} ≤ 100$ for each valid $i$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 2 5 3 1 2 ----- Sample Output 1 ------ YES ----- explanation 1 ------ Chef already has $3$ hours left. He can go to the $2$-nd time zone, which is $2$ hours back in time. Then he has a total of $3 + 2 = 5$ hours, which is sufficient to solve the problem. ----- Sample Input 2 ------ 2 6 3 1 2 ----- Sample Output 2 ------ NO ----- explanation 2 ------ If Chef goes to the $1$-st time zone, he will have $3 + 1 = 4$ hours, which is insufficient to solve the problem. If he goes to the $2$-nd time zone, he will have $3 + 2 = 5$ hours, which is also insufficient to solve the problem. Since none of the time travel options can be used to gain sufficient time to solve the problem, Chef is incapable of solving it.
{"inputs": ["2 5 3\n1 2", "2 6 3\n1 2"], "outputs": ["YES", "NO"]}
602
34
coding
Solve the programming task below in a Python markdown code block. You are given an input string. For each symbol in the string if it's the first character occurrence, replace it with a '1', else replace it with the amount of times you've already seen it... But will your code be **performant enough**? ___ ## Examples: ``` input = "Hello, World!" result = "1112111121311" input = "aaaaaaaaaaaa" result = "123456789101112" ``` There might be some non-ascii characters in the string. ~~~if:java Note: there will be no int domain overflow (character occurrences will be less than 2 billion). ~~~ ~~~if:c (this does not apply to the C language translation) ~~~ Also feel free to reuse/extend the following starter code: ```python def numericals(s): ```
{"functional": "_inputs = [['Hello, World!'], [\"Hello, World! It's me, JomoPipi!\"], ['hello hello'], ['Hello'], ['11111'], ['hope you 123456789 expected numbers in the string'], [\"In this string, I'll make sure the amounts of a character go over 9\"]]\n_outputs = [['1112111121311'], ['11121111213112111131224132411122'], ['11121122342'], ['11121'], ['12345'], ['1111112121111111113212311414121151151262267232231'], ['11111112221221132112411115312263237221234482193101343525441123124155131']]\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(numericals(*i), o[0])"}
210
425
coding
Solve the programming task below in a Python markdown code block. You are given a string $t$ and a set $S$ of $N$ different strings. You need to separate $t$ such that each part is included in $S$. For example, the following 4 separation methods satisfy the condition when $t = abab$ and $S = \\{a, ab, b\\}$. * $a,b,a,b$ * $a,b,ab$ * $ab,a,b$ * $ab,ab$ Your task is to count the number of ways to separate $t$. Because the result can be large, you should output the remainder divided by $1,000,000,007$. Input The input consists of a single test case formatted as follows. $N$ $s_1$ : $s_N$ $t$ The first line consists of an integer $N$ ($1 \leq N \leq 100,000$) which is the number of the elements of $S$. The following $N$ lines consist of $N$ distinct strings separated by line breaks. The $i$-th string $s_i$ represents the $i$-th element of $S$. $s_i$ consists of lowercase letters and the length is between $1$ and $100,000$, inclusive. The summation of length of $s_i$ ($1 \leq i \leq N$) is at most $200,000$. The next line consists of a string $t$ which consists of lowercase letters and represents the string to be separated and the length is between $1$ and $100,000$, inclusive. Output Calculate the number of ways to separate $t$ and print the remainder divided by $1,000,000,007$. Examples Input 3 a b ab abab Output 4 Input 3 a b c xyz Output 0 Input 7 abc ab bc a b c aa aaabcbccababbc Output 160 Input 10 a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Output 461695029
{"inputs": ["3\na\nb\nc\nxy{", "3\nb\nb\nc\nxy{", "3\nb\nb\nc\nyx{", "3\nb\nb\nc\nxx{", "3\nb\nb\nd\nxx{", "3\nb\nb\nd\nx{x", "3\nb\na\nd\nx{x", "3\na\nb\nc\nzyx"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
517
134
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array bloomDay, an integer m and an integer k. You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden. The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.   Please complete the following python code precisely: ```python class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(bloomDay = [1,10,3,10,2], m = 3, k = 1) == 3\n assert candidate(bloomDay = [1,10,3,10,2], m = 3, k = 2) == -1\n assert candidate(bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3) == 12\n assert candidate(bloomDay = [1000000000,1000000000], m = 1, k = 1) == 1000000000\n assert candidate(bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2) == 9\n\n\ncheck(Solution().minDays)"}
161
221
coding
Solve the programming task below in a Python markdown code block. # Task You are given a positive integer `n`. We intend to make some ascending sequences according to the following rules: 1. Make a sequence of length 1: [ n ] 2. Or, insert a number to the left side of the sequence. But this number can not exceed half of the first number of the sequence. 3. Follow rule 2, continue insert number to the left side of the sequence. Your task is to count the number of all possible sequences, and return it. If you do not understand the task, please read the rewritten version below: You are given a positive integer `n`. Your task is to count the number of such sequences: - It should be an ascending sequence; - It should end with number `n`. - Each number in the sequence should smaller or equals to the half of its right, except for the last number `n`. - We define that a sequence containing only a number `n` is a valid ascending sequence. # Examples For `n = 6`, the output should be `6`. All sequences we made are: ``` [6] insert a number to the left: [1,6] [2,6] [3,6] continue insert number: [1,2,6] [1,3,6] ``` There are 6 sequences in total. For `n = 10`, the output should be `14`. All sequences we made are: ``` [10] insert a number to the left: [1,10] [2,10] [3,10] [4,10] [5,10] continue insert number: [1,2,10] [1,3,10] [1,4,10] [2,4,10] [1,5,10] [2,5,10] continue insert number: [1,2,4,10] [1,2,5,10] ``` There are 14 sequences in total. # Note - `1 <= n <= 1000` - `3` fixed testcases - `100` random testcases, testing for correctness of solution - All inputs are valid. - If my reference solution gives the wrong result in the random tests, please let me know(post an issue). Also feel free to reuse/extend the following starter code: ```python def make_sequences(n): ```
{"functional": "_inputs = [[6], [10], [1000]]\n_outputs = [[6], [14], [1981471878]]\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_sequences(*i), o[0])"}
534
180
coding
Solve the programming task below in a Python markdown code block. Internet search engines, such as Google, automatically sort and categorize web pages around the world to create a huge database. It also parses the search keywords entered by the user and creates an inquiry statement for database search. In each case, complicated processing is performed to realize efficient search, but for the time being, all the basics are cutting out words from sentences. So, please try to cut out the word from the sentence. This time, we will target English sentences with clear word breaks as follows. * Target text: English text of 1024 characters or less, not including line breaks * Delimiter: All are half-width characters, only spaces, periods, and commas. * Words to cut out: Words with 3 to 6 letters (ignore words with 2 or less letters or 7 or more letters) input An English sentence consisting of delimiters and alphanumeric characters is given on one line (all half-width characters). output Please output the words separated by one space character (half-width) on one line. Examples Input Rain, rain, go to Spain. Output Rain rain Spain Input Win today's preliminary contest and be qualified to visit University of Aizu. Output Win and visit Aizu
{"inputs": ["Rain, rain, go ot Spain.", "Rain, iarn, og ot Spain.", "Rain, arin, go to Spain.", "Rain, raio, go ot Spain.", "Rain, rain, og ot .niapS", "Rajn, rain, og ot .niapS", "Rain, rain, go to Spain.", "Win today's preliminary contest and be qualified to visit University fo Aizu."], "outputs": ["Rain rain Spain\n", "Rain iarn Spain\n", "Rain arin Spain\n", "Rain raio Spain\n", "Rain rain niapS\n", "Rajn rain niapS\n", "Rain rain Spain", "Win and visit Aizu\n"]}
266
155
coding
Solve the programming task below in a Python markdown code block. An array $a_1, a_2, \ldots, a_n$ is good if and only if for every subsegment $1 \leq l \leq r \leq n$, the following holds: $a_l + a_{l + 1} + \ldots + a_r = \frac{1}{2}(a_l + a_r) \cdot (r - l + 1)$. You are given an array of integers $a_1, a_2, \ldots, a_n$. In one operation, you can replace any one element of this array with any real number. Find the minimum number of operations you need to make this array good. -----Input----- The first line of input contains one integer $t$ ($1 \leq t \leq 100$): the number of test cases. Each of the next $t$ lines contains the description of a test case. In the first line you are given one integer $n$ ($1 \leq n \leq 70$): the number of integers in the array. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-100 \leq a_i \leq 100$): the initial array. -----Output----- For each test case, print one integer: the minimum number of elements that you need to replace to make the given array good. -----Examples----- Input 5 4 1 2 3 4 4 1 1 2 2 2 0 -1 6 3 -2 4 -1 -4 0 1 -100 Output 0 2 0 3 0 -----Note----- In the first test case, the array is good already. In the second test case, one of the possible good arrays is $[1, 1, \underline{1}, \underline{1}]$ (replaced elements are underlined). In the third test case, the array is good already. In the fourth test case, one of the possible good arrays is $[\underline{-2.5}, -2, \underline{-1.5}, -1, \underline{-0.5}, 0]$.
{"inputs": ["5\n4\n1 2 3 4\n4\n1 1 2 2\n2\n0 -1\n6\n3 -2 4 -1 -4 0\n1\n-100\n"], "outputs": ["0\n2\n0\n3\n0\n"]}
496
69
coding
Solve the programming task below in a Python markdown code block. We call a 4-digit integer with three or more consecutive same digits, such as 1118, good. You are given a 4-digit integer N. Answer the question: Is N good? -----Constraints----- - 1000 ≤ N ≤ 9999 - N is an integer. -----Input----- Input is given from Standard Input in the following format: N -----Output----- If N is good, print Yes; otherwise, print No. -----Sample Input----- 1118 -----Sample Output----- Yes N is good, since it contains three consecutive 1.
{"inputs": ["860", "374", "425", "647", "317", "943", "308", "164"], "outputs": ["No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n", "No\n"]}
139
78
coding
Solve the programming task below in a Python markdown code block. Timur initially had a binary string$^{\dagger}$ $s$ (possibly of length $0$). He performed the following operation several (possibly zero) times: Add ${0}$ to one end of the string and ${1}$ to the other end of the string. For example, starting from the string ${1011}$, you can obtain either ${{0}}{1011}{{1}}$ or ${{1}}{1011}{{0}}$. You are given Timur's final string. What is the length of the shortest possible string he could have started with? $^{\dagger}$ A binary string is a string (possibly the empty string) whose characters are either ${0}$ or ${1}$. -----Input----- The first line of the input contains an integer $t$ ($1 \leq t \leq 100$) — the number of testcases. The first line of each test case contains an integer $n$ ($1 \leq n \leq 2000$) — the length of Timur's final string. The second line of each test case contains a string $s$ of length $n$ consisting of characters ${0}$ or ${1}$, denoting the final string. -----Output----- For each test case, output a single nonnegative integer — the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output $0$. -----Examples----- Input 9 3 100 4 0111 5 10101 6 101010 7 1010110 1 1 2 10 2 11 10 1011011010 Output 1 2 5 0 3 1 0 2 4 -----Note----- In the first test case, the shortest possible string Timur started with is ${0}$, and he performed the following operation: ${0} \to {{1}}{0}{{0}}$. In the second test case, the shortest possible string Timur started with is ${11}$, and he performed the following operation: ${11} \to {{0}}{11}{{1}}$. In the third test case, the shortest possible string Timur started with is ${10101}$, and he didn't perform any operations. In the fourth test case, the shortest possible string Timur started with is the empty string (which we denote by $\varepsilon$), and he performed the following operations: $\varepsilon \to {{1}}{}{{0}} \to {{0}}{10}{{1}} \to {{1}}{0101}{{0}}$. In the fifth test case, the shortest possible string Timur started with is ${101}$, and he performed the following operations: ${101} \to {{0}}{101}{{1}} \to {{1}}{01011}{{0}}$.
{"inputs": ["1\n1\n1\n", "9\n3\n100\n4\n0111\n5\n10101\n6\n101010\n7\n1010110\n1\n1\n2\n10\n2\n11\n10\n1011011010\n"], "outputs": ["1\n", "1\n2\n5\n0\n3\n1\n0\n2\n4\n"]}
687
110
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There is an undirected connected tree with n nodes labeled from 1 to n and n - 1 edges. You are given the integer n. The parent node of a node with a label v is the node with the label floor (v / 2). The root of the tree is the node with the label 1. For example, if n = 7, then the node with the label 3 has the node with the label floor(3 / 2) = 1 as its parent, and the node with the label 7 has the node with the label floor(7 / 2) = 3 as its parent. You are also given an integer array queries. Initially, every node has a value 0 on it. For each query queries[i], you should flip all values in the subtree of the node with the label queries[i]. Return the total number of nodes with the value 1 after processing all the queries. Note that: Flipping the value of a node means that the node with the value 0 becomes 1 and vice versa. floor(x) is equivalent to rounding x down to the nearest integer.   Please complete the following python code precisely: ```python class Solution: def numberOfNodes(self, n: int, queries: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 5 , queries = [1,2,5]) == 3\n assert candidate(n = 3, queries = [2,3,3]) == 1\n\n\ncheck(Solution().numberOfNodes)"}
285
61
coding
Solve the programming task below in a Python markdown code block. Let's start from some definitions. Strings A and B are called anagrams if it's possible to rearrange the letters of string A using all the original letters exactly once and achieve string B; in other words A and B are permutations of each other. For example, remote and meteor are anagrams, race and race are anagrams as well, while seat and tease aren't anagrams as tease contains an extra 'e'. String A is called a subsequence of string B if A can be obtained from B by removing some (possibly none) characters. For example, cat is a subsequence of scratch, rage is a subsequence of rage, and tame is not a subsequence of steam. String A is lexicographically smaller than string B of the same length if at the first position where A and B differ A contains a letter which is earlier in the alphabet than the corresponding letter in B. Recently, Ann received a set of strings consisting of small Latin letters a..z. 'What can I do with them?' -- she asked herself. 'What if I try to find the longest string which is a subsequence of every string from the set?'. Ann spent a lot of time trying to solve the problem... but all her attempts happened to be unsuccessful. She then decided to allow the sought string to be an anagram of some subsequence of every string from the set. This problem seemed to be easier to Ann, but she was too tired to solve it, so Ann asked for your help. So your task is, given a set of strings, to find the longest non-empty string which satisfies Ann. Moreover, if there are many such strings, choose the lexicographically smallest one. ------ Input ------ The first line of the input file contains one integer N -- the number of strings in the set (1 ≤ N ≤ 100). Each of the next N lines contains a non-empty string consisting only of small Latin letters a..z representing a string from the set. None of the strings contain more than 100 letters. ------ Output ------ Output the longest non-empty string satisfying Ann. If there are several such strings, output the lexicographically smallest one. If there are no such strings, output 'no such string' (quotes for clarity). ----- Sample Input 1 ------ 3 hope elephant path ----- Sample Output 1 ------ hp ----- explanation 1 ------ In the first test case the longest string appears to be two characters long. String 'hp' satisfies the requirements as it's an anagram of 'hp' which is a subsequence of 'hope' and an anagram of 'ph' which is a subsequence of both 'elephant' and 'path'. Note that string 'ph' also satisfies the requirements, but 'hp' is lexicographically smaller. ----- Sample Input 2 ------ 2 wall step ----- Sample Output 2 ------ no such string ----- explanation 2 ------ In this test case there is no such string.
{"inputs": ["2\nwall\nstep", "2\nllaw\nstep", "2\namlu\nutfq", "2\nwmck\nmhqt", "2\nllwa\nstep", "2\nllwa\nstfp", "2\nmlwa\nstfp", "2\nawlm\nstfp"], "outputs": ["no such string", "no such string\n", "u\n", "m\n", "no such string\n", "no such string\n", "no such string\n", "no such string\n"]}
632
117
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length. In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary. Return a list of all words from queries, that match with some word from dictionary after a maximum of two edits. Return the words in the same order they appear in queries.   Please complete the following python code precisely: ```python class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: ```
{"functional": "def check(candidate):\n assert candidate(queries = [\"word\",\"note\",\"ants\",\"wood\"], dictionary = [\"wood\",\"joke\",\"moat\"]) == [\"word\",\"note\",\"wood\"]\n assert candidate(queries = [\"yes\"], dictionary = [\"not\"]) == []\n\n\ncheck(Solution().twoEditWords)"}
159
83
coding
Solve the programming task below in a Python markdown code block. This is the hard version of this problem. The only difference between the easy and hard versions is the constraints on $k$ and $m$. In this version of the problem, you need to output the answer by modulo $10^9+7$. You are given a sequence $a$ of length $n$ consisting of integers from $1$ to $n$. The sequence may contain duplicates (i.e. some elements can be equal). Find the number of tuples of $m$ elements such that the maximum number in the tuple differs from the minimum by no more than $k$. Formally, you need to find the number of tuples of $m$ indices $i_1 < i_2 < \ldots < i_m$, such that $$\max(a_{i_1}, a_{i_2}, \ldots, a_{i_m}) - \min(a_{i_1}, a_{i_2}, \ldots, a_{i_m}) \le k.$$ For example, if $n=4$, $m=3$, $k=2$, $a=[1,2,4,3]$, then there are two such triples ($i=1, j=2, z=4$ and $i=2, j=3, z=4$). If $n=4$, $m=2$, $k=1$, $a=[1,1,1,1]$, then all six possible pairs are suitable. As the result can be very large, you should print the value modulo $10^9 + 7$ (the remainder when divided by $10^9 + 7$). -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains three integers $n$, $m$, $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 100$, $1 \le k \le n$) — the length of the sequence $a$, number of elements in the tuples and the maximum difference of elements in the tuple. The next line contains $n$ integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le n$) — the sequence $a$. It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- Output $t$ answers to the given test cases. Each answer is the required number of tuples of $m$ elements modulo $10^9 + 7$, such that the maximum value in the tuple differs from the minimum by no more than $k$. -----Examples----- Input 4 4 3 2 1 2 4 3 4 2 1 1 1 1 1 1 1 1 1 10 4 3 5 6 1 3 2 9 8 1 2 4 Output 2 6 1 20 -----Note----- None
{"inputs": ["3\n1 1 1\n1\n1 1 1\n1\n1 1 1\n1\n", "4\n4 3 2\n1 2 4 3\n4 2 1\n1 1 1 1\n1 1 1\n1\n10 4 3\n5 6 1 3 2 9 8 1 2 4\n", "4\n4 3 2\n1 2 4 3\n4 3 2\n1 1 1 1\n1 3 1\n1\n10 3 2\n5 6 1 3 2 9 8 1 2 4\n"], "outputs": ["1\n1\n1\n", "2\n6\n1\n20\n", "2\n4\n0\n15\n"]}
702
198
coding
Solve the programming task below in a Python markdown code block. 'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2. The finals will have n questions, m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of the i-th question is a_{i} points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price. The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again. All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve. -----Input----- The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100; m ≤ min(n, 30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the prices of the questions. The third line contains m distinct integers b_{i} (1 ≤ b_{i} ≤ n) — the numbers of auction questions. Assume that the questions are numbered from 1 to n. -----Output----- In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type. -----Examples----- Input 4 1 1 3 7 5 3 Output 18 Input 3 2 10 3 8 2 3 Output 40 Input 2 2 100 200 1 2 Output 400
{"inputs": ["1 1\n1\n1\n", "1 1\n1\n1\n", "1 1\n2\n1\n", "2 1\n1 2\n1\n", "2 1\n1 2\n2\n", "2 1\n1 2\n2\n", "2 1\n1 2\n1\n", "2 1\n1 0\n1\n"], "outputs": ["1\n", " 1", "2\n", "4\n", "3\n", " 3", " 4", "1\n"]}
593
131
coding
Solve the programming task below in a Python markdown code block. You are in a grid of dimensions N \times M. You are allowed to perform two types of operations: Go down, left, up, or right each for a cost of X. Formally, if you are at the cell (i,j) of the grid, you can go to either of the cells (i + 1, j), (i, j - 1), (i - 1, j) or (i, j + 1) at a cost of X. Go diagonally down-left, down-right, up-left, up-right, for a cost of Y. Formally, if you are at the cell (i,j) of the grid, you can go to either of the cells (i + 1, j - 1), (i + 1, j + 1), (i - 1, j - 1) or (i - 1, j + 1) at a cost of Y. You cannot exit the grid at any point. Find the minimum cost of going to the bottom-right corner (N, M) from the top-left corner (1, 1). \textbf{Note}: Since input-output is large, prefer using fast input-output methods. ------ Input Format ------ - First line will contain T, number of testcases. Then the testcases follow. - Each testcase contains of a single line of input, four space separated integers integers: N, M, X, Y. ------ Output Format ------ For each testcase, output the answer in a new line. ------ Constraints ------ $1 ≤ T ≤ 5 \cdot 10^{5}$ $1 ≤ N, M, X, Y ≤ 10^{6}$ ----- Sample Input 1 ------ 3 5 6 2 5 4 7 5 6 7 8 6 5 ----- Sample Output 1 ------ 18 33 36 ----- explanation 1 ------ Test Case $1$: We can go $4$ steps down from $(1, 1)$ to $(5, 1)$ and then $5$ steps right to $(5, 6)$. The total cost is $2 \times 9 = 18$ Test Case $2$: We can go $3$ steps diagonally down-right from $(1, 1)$ to $(4, 4)$ and then $3$ steps right to $(4, 7)$. The total cost is $3 \times 6 + 3 \times 5 = 18 + 15 = 33$ Test Case $3$: We can go $6$ steps diagonally down-right from $(1, 1)$ to $(7, 7)$ and then $1$ step right to $(7, 8)$. The total cost is $5 \times 6 + 6 = 36$
{"inputs": ["3\n5 6 2 5\n4 7 5 6\n7 8 6 5"], "outputs": ["18\n33\n36"]}
625
43
coding
Solve the programming task below in a Python markdown code block. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers. Input The first line contains two integers n and k (1 ≤ n, k ≤ 109) — the number of elements in the permutation and the lexicographical number of the permutation. Output If the k-th permutation of numbers from 1 to n does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers. Examples Input 7 4 Output 1 Input 4 7 Output 1 Note A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 ≤ i ≤ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations. In the first sample the permutation looks like that: 1 2 3 4 6 7 5 The only suitable position is 4. In the second sample the permutation looks like that: 2 1 3 4 The only suitable position is 4.
{"inputs": ["3 7\n", "2 3\n", "1 2\n", "1 1\n", "7 1\n", "2 4\n", "2 1\n", "2 2\n"], "outputs": ["-1\n", "-1\n", "-1\n", "0\n", "2\n", "-1\n", "0\n", "0\n"]}
425
87
coding
Solve the programming task below in a Python markdown code block. Chandrima likes the XOR operation very much and keeps finding and solving related problems. One day she comes across a problem and gets stuck on it, which makes her sad. You being her friend decide to help her out by writing a code for the same. Consider a list of all natural numbers. Now you remove all numbers whose binary representation has at least two consecutive ${1}$ bits, from the list, hence generating a new list having elements $1,2,4,5,8,\ldots$ and so on. Now you are given $N$ numbers from this newly generated list. You have to find the XOR of these numbers. Input Format The first line has an integer $N$, denoting the number of integers in list ${A}$. The next line contains $N$ space-separated integers. $A_i$ represents the $A_i^{\text{th}}$ number of the generated list. Since the answer can be very large, print the answer modulo $(10^9+7)$. Output Format Just print one line containing the final answer. Constraints $1\leq N\leq5\cdot10^5$ $1\leq A_i\leq10^{18}$ Sample Input 1 3 1 2 3 Sample Output 1 7 Sample Input 2 3 1 3 4 Sample Output 2 0 Explanation Sample 1: The values to be considered are $1,2,4$, and the answer is $1\oplus2\oplus4=7$. Sample 2: The values to be considered are $1,4,5$, and the answer is $1\oplus4\oplus5=0$.
{"inputs": ["3\n1 2 3\n", "3\n1 3 4\n"], "outputs": ["7\n", "0\n"]}
388
34
coding
Solve the programming task below in a Python markdown code block. You're a statistics professor and the deadline for submitting your students' grades is tonight at midnight. Each student's grade is determined by their mean score across all of the tests they took this semester. You've decided to automate grade calculation by writing a function `calculate_grade()` that takes a list of test scores as an argument and returns a one character string representing the student's grade calculated as follows: * 90% <= mean score <= 100%: `"A"`, * 80% <= mean score < 90%: `"B"`, * 70% <= mean score < 80%: `"C"`, * 60% <= mean score < 70%: `"D"`, * mean score < 60%: `"F"` For example, `calculate_grade([92, 94, 99])` would return `"A"` since the mean score is `95`, and `calculate_grade([50, 60, 70, 80, 90])` would return `"C"` since the mean score is `70`. Your function should handle an input list of any length greater than zero. Also feel free to reuse/extend the following starter code: ```python def calculate_grade(scores): ```
{"functional": "_inputs = [[[92, 94, 99]], [[50, 60, 70, 80, 90]], [[50, 55]]]\n_outputs = [['A'], ['C'], ['F']]\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(calculate_grade(*i), o[0])"}
284
198
coding
Solve the programming task below in a Python markdown code block. Jeff got 2n real numbers a1, a2, ..., a2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: * choose indexes i and j (i ≠ j) that haven't been chosen yet; * round element ai to the nearest integer that isn't more than ai (assign to ai: ⌊ ai ⌋); * round element aj to the nearest integer that isn't less than aj (assign to aj: ⌈ aj ⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference. Input The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a1, a2, ..., a2n (0 ≤ ai ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. Output In a single line print a single real number — the required difference with exactly three digits after the decimal point. Examples Input 3 0.000 0.500 0.750 1.000 2.000 3.000 Output 0.250 Input 3 4469.000 6526.000 4864.000 9356.383 7490.000 995.896 Output 0.279 Note In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25.
{"inputs": ["1\n1.100 1.200\n", "1\n0.061 0.330\n", "1\n0.900 0.900\n", "1\n0.001 0.001\n", "1\n6418.669 157.986\n", "1\n6418.000 157.986\n", "1\n6418.000 157.000\n", "2\n0.001 0.001 0.001 0.001\n"], "outputs": ["0.700\n", "0.609\n", "0.800\n", "0.998\n", "0.655\n", "0.014\n", "0.000\n", "1.996\n"]}
539
225
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. For an integer array nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j]. Given two integers n and k, return the number of different arrays consisting of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def kInversePairs(self, n: int, k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 3, k = 0) == 1\n assert candidate(n = 3, k = 1) == 2\n\n\ncheck(Solution().kInversePairs)"}
137
54
coding
Solve the programming task below in a Python markdown code block. *CodeChef recently revamped its [practice page] to make it easier for users to identify the next problems they should solve by introducing some new features:* *Recent Contest Problems - Contains only problems from the last 2 contests* *Separate Un-Attempted, Attempted, and All tabs* *Problem Difficulty Rating - The Recommended dropdown menu has various difficulty ranges so that you can attempt the problems most suited to your experience* *Popular Topics and Tags* Chef has been participating regularly in rated contests but missed the last two contests due to his college exams. He now wants to solve them and so he visits the practice page to view these [problems]. Given a list of N contest codes, where each contest code is either START38 or LTIME108, help Chef count how many problems were featured in each of the contests. ------ Input Format ------ - First line will contain T, number of test cases. Then the test cases follow. - Each test case contains of two lines of input. - First line of input contains the total count of problems that appeared in the two recent contests - N. - Second line of input contains the list of N contest codes. Each code is either START38 or LTIME108, to which each problem belongs. ------ Output Format ------ For each test case, output two integers in a single new line - the first integer should be the number of problems in START38, and the second integer should be the number of problems in LTIME108. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 1000$ - Each of the contest codes will be either START38 or LTIME108. ----- Sample Input 1 ------ 4 3 START38 LTIME108 START38 4 LTIME108 LTIME108 LTIME108 START38 2 LTIME108 LTIME108 6 START38 LTIME108 LTIME108 LTIME108 START38 LTIME108 ----- Sample Output 1 ------ 2 1 1 3 0 2 2 4 ----- explanation 1 ------ Test case $1$: There are $2$ START38s in the input, which means that there were $2$ problems in START38. Similarly, there was $1$ problem in LTIME108. Test case $2$: There is $1$ START38 in the input, which means that there was $1$ problem in START38. Similarly, there were $3$ problems in LTIME108. Test case $3$: There are no START38s in the input, which means that were $0$ problems in START38. Similarly, there were $2$ problems in LTIME108. Test case $4$: There are $2$ START38s in the input, which means that there were $2$ problems in START38. Similarly, there were $4$ problems in LTIME108.
{"inputs": ["4\n3\nSTART38 LTIME108 START38\n4\nLTIME108 LTIME108 LTIME108 START38\n2\nLTIME108 LTIME108\n6\nSTART38 LTIME108 LTIME108 LTIME108 START38 LTIME108\n"], "outputs": ["2 1\n1 3\n0 2\n2 4\n"]}
666
105
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node's values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path.   Please complete the following python code precisely: ```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxPathSum(self, root: Optional[TreeNode]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(root = tree_node([1,2,3])) == 6\n assert candidate(root = tree_node([-10,9,20,None,None,15,7])) == 42\n\n\ncheck(Solution().maxPathSum)"}
189
66
coding
Solve the programming task below in a Python markdown code block. Chef Watson uses a social network called ChefBook, which has a new feed consisting of posts by his friends. Each post can be characterized by f - the identifier of the friend who created the post, p - the popularity of the post(which is pre-calculated by ChefBook platform using some machine learning algorithm) and s - the contents of the post which is a string of lower and uppercase English alphabets. Also, Chef has some friends, which he has marked as special. The algorithm used by ChefBook for determining the order of posts in news feed is as follows: - Posts of special friends should be shown first, irrespective of popularity. Among all such posts the popular ones should be shown earlier. - Among all other posts, popular posts should be shown earlier. Given, a list of identifiers of Chef's special friends and a list of posts, you have to implement this algorithm for engineers of ChefBook and output the correct ordering of posts in the new feed. -----Input----- First line contains N, number of special friends of Chef and M, the number of posts. Next line contains N integers A1, A2, ..., AN denoting the identifiers of special friends of Chef. Each of the next M lines contains a pair of integers and a string denoting f, p and s, identifier of the friend who created the post, the popularity of the post and the contents of the post, respectively. It is guaranteed that no two posts have same popularity, but the same friend might make multiple posts. -----Output----- Output correct ordering of posts in news feed in M lines. Output only the contents of a post. -----Constraints----- - 1 ≤ N, M ≤ 103 - 1 ≤ Ai, f, p ≤ 105 - 1 ≤ length(s) ≤ 100 -----Example----- Input: 2 4 1 2 1 1 WhoDoesntLoveChefBook 2 2 WinterIsComing 3 10 TheseViolentDelightsHaveViolentEnds 4 3 ComeAtTheKingBestNotMiss Output: WinterIsComing WhoDoesntLoveChefBook TheseViolentDelightsHaveViolentEnds ComeAtTheKingBestNotMiss -----Explanation----- First we should show posts created by friends with identifiers 1 and 2. Among the posts by these friends, the one with more popularity should be shown first. Among remaining posts, we show those which are more popular first.
{"inputs": ["2 4\n1 2\n1 1 WhoDoesntLoveChefBook\n2 2 WinterIsComing\n3 10 TheseViolentDelightsHaveViolentEnds\n4 3 ComeAtTheKingBestNotMiss"], "outputs": ["WinterIsComing\nWhoDoesntLoveChefBook\nTheseViolentDelightsHaveViolentEnds\nComeAtTheKingBestNotMiss"]}
525
89