task_type
stringclasses
1 value
problem
stringlengths
261
3.34k
answer
stringlengths
35
6.15k
problem_tokens
int64
62
774
answer_tokens
int64
12
2.04k
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Write an algorithm to determine if a number n is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. Return true if n is a happy number, and false if not.   Please complete the following python code precisely: ```python class Solution: def isHappy(self, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 19) == True\n assert candidate(n = 2) == False\n\n\ncheck(Solution().isHappy)"}
146
42
coding
Solve the programming task below in a Python markdown code block. ------Read problems statements in Hindi, Mandarin chinese , Russian and Vietnamese as well. ------ The structure of the company ShareChat can be described by a simple graph (without multiedges and self-loops) with $N$ vertices numbered $1$ through $N$ and $M$ edges numbered $1$ through $M$. Each vertex of this graph is part of at most one simple cycle. For each edge, you should find the number of simple paths that contain this edge and only contain at most one edge which belongs to a cycle. ------ 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 $M$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $u$ and $v$, denoting the vertices connected by edge $i$. ------ Output ------ For each test case, print $M$ lines. For each $i$ ($1 ≤ i ≤ M$), the $i$-th of these lines should contain a single integer — the number of paths for edge $i$. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ u, v ≤ N$ the sum of $N+M$ for all test cases does not exceed $5 \cdot 10^{6}$ ------ Subtasks ------ Subtask #1 (20 points): the sum of answers for all edges in all test cases is smaller than $150 \cdot 10^{6}$ the sum of $N+M$ for all test cases does not exceed $5 \cdot 10^{5}$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 7 7 1 2 2 3 3 4 2 5 3 5 5 6 6 7 3 3 1 2 2 3 3 1 ----- Sample Output 1 ------ 6 4 6 6 6 10 6 1 1 1
{"inputs": ["2\n7 7\n1 2\n2 3\n3 4\n2 5\n3 5\n5 6\n6 7\n3 3\n1 2\n2 3\n3 1"], "outputs": ["6\n4\n6\n6\n6\n10\n6\n1\n1\n1"]}
514
79
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I can be placed before V (5) and X (10) to make 4 and 9.  X can be placed before L (50) and C (100) to make 40 and 90.  C can be placed before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer.   Please complete the following python code precisely: ```python class Solution: def romanToInt(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"III\") == 3\n assert candidate(s = \"LVIII\") == 58\n assert candidate(s = \"MCMXCIV\") == 1994\n\n\ncheck(Solution().romanToInt)"}
343
63
coding
Solve the programming task below in a Python markdown code block. Inna and Dima bought a table of size n × m in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A". Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows: initially, Inna chooses some cell of the table where letter "D" is written; then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name; Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D". Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 10^3). Then follow n lines that describe Inna and Dima's table. Each line contains m characters. Each character is one of the following four characters: "D", "I", "M", "A". Note that it is not guaranteed that the table contains at least one letter "D". -----Output----- If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA. -----Examples----- Input 1 2 DI Output Poor Dima! Input 2 2 MA ID Output Poor Inna! Input 5 5 DIMAD DIMAI DIMAM DDMAA AAMID Output 4 -----Note----- Notes to the samples: In the first test sample, Inna cannot go through name DIMA a single time. In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner. In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times.
{"inputs": ["1 1\nI\n", "1 1\nD\n", "1 1\nM\n", "1 1\nA\n", "1 1\nD\n", "1 1\nM\n", "1 1\nA\n", "1 1\nI\n"], "outputs": ["Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n", "Poor Dima!\n"]}
675
126
coding
Solve the programming task below in a Python markdown code block. # Task John won the championship of a TV show. He can get some bonuses. He needs to play a game to determine the amount of his bonus. Here are `n` rows and `m` columns of cards were placed on the ground. A non-negative number is written on each card. The rules of the game are: - Player starts from the top-left coner, walk to the bottom-right coner. - Players can only walk downward or right. - The sum of the numbers collected by the players will be used as the bonus. John has two chances to play this game on a game map. Specifically, when John finishes the game, the card on his path will be removed, and then he can walk again. Your task is to help John calculate the maximum amount of bonuses he can get. # Input - `gameMap/gamemap`: A `n` x `m` integer array. Each element represents the number on the card. - `4 <= n,m <= 40(Pyhon)/100(JS)` - All inputs are valid. # Output An integer. the maximum amount of bonuses John can get. # Eaxmple For ``` gameMap= [ [1, 3, 9], [2, 8, 5], [5, 7, 4] ] ``` The output should be `39`. One of the possible solution is: ``` 1st game: [ [>, >, v], [2, 8, v], [5, 7, v] ] 1+3+9+5+4=22 2nd game: [ [v, 0, 0], [>, v, 0], [5, >, >] ] 0+2+8+7+0=17 Final bonus = 22 + 17 = 39 ``` Also feel free to reuse/extend the following starter code: ```python def calc(gamemap): ```
{"functional": "_inputs = [[[[1, 3, 9], [2, 8, 5], [5, 7, 4]]], [[[11, 72, 38], [80, 69, 65], [68, 96, 99]]], [[[1, 5, 1, 1], [1, 5, 5, 1], [5, 5, 5, 1], [1, 1, 5, 1]]], [[[0, 0, 2, 3, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0, 0], [0, 0, 2, 0, 4, 0, 0], [0, 0, 0, 0, 0, 0, 0]]]]\n_outputs = [[39], [560], [40], [25]]\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(calc(*i), o[0])"}
434
448
coding
Solve the programming task below in a Python markdown code block. Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you) — who knows? Every one of them has an integer sequences a and b of length n. Being given a query of the form of pair of integers (l, r), Mike can instantly tell the value of $\operatorname{max}_{i = l}^{r} a_{i}$ while !Mike can instantly tell the value of $\operatorname{min}_{i = l} b_{i}$. Now suppose a robot (you!) asks them all possible different queries of pairs of integers (l, r) (1 ≤ l ≤ r ≤ n) (so he will make exactly n(n + 1) / 2 queries) and counts how many times their answers coincide, thus for how many pairs $\operatorname{max}_{i = l}^{r} a_{i} = \operatorname{min}_{i = l} b_{i}$ is satisfied. How many occasions will the robot count? -----Input----- The first line contains only integer n (1 ≤ n ≤ 200 000). The second line contains n integer numbers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the sequence a. The third line contains n integer numbers b_1, b_2, ..., b_{n} ( - 10^9 ≤ b_{i} ≤ 10^9) — the sequence b. -----Output----- Print the only integer number — the number of occasions the robot will count, thus for how many pairs $\operatorname{max}_{i = l}^{r} a_{i} = \operatorname{min}_{i = l} b_{i}$ is satisfied. -----Examples----- Input 6 1 2 3 2 1 4 6 7 1 2 3 2 Output 2 Input 3 3 3 3 1 1 1 Output 0 -----Note----- The occasions in the first sample case are: 1.l = 4,r = 4 since max{2} = min{2}. 2.l = 4,r = 5 since max{2, 1} = min{2, 3}. There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1.
{"inputs": ["3\n3 3 3\n1 1 1\n", "3\n1 1 1\n2 2 2\n", "3\n1 1 1\n2 2 2\n", "3\n1 2 0\n2 2 2\n", "3\n1 1 0\n2 2 2\n", "3\n3 3 6\n1 1 1\n", "3\n3 2 6\n1 1 1\n", "3\n0 2 0\n2 2 2\n"], "outputs": ["0\n", "0\n", "0", "4\n", "0\n", "0\n", "0\n", "4\n"]}
556
165
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums. You then do the following steps: If original is found in nums, multiply it by two (i.e., set original = 2 * original). Otherwise, stop the process. Repeat this process with the new number as long as you keep finding the number. Return the final value of original.   Please complete the following python code precisely: ```python class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [5,3,6,1,12], original = 3) == 24\n assert candidate(nums = [2,7,9], original = 4) == 4\n\n\ncheck(Solution().findFinalValue)"}
142
68
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character. A substring is a contiguous non-empty sequence of characters within a string.   Please complete the following python code precisely: ```python class Solution: def numberOfSubstrings(self, s: str) -> int: ```
{"functional": "def check(candidate):\n assert candidate(s = \"abcba\") == 7\n assert candidate(s = \"abacad\") == 9\n assert candidate(s = \"a\") == 1\n\n\ncheck(Solution().numberOfSubstrings)"}
96
58
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given integers height and width which specify the dimensions of a brick wall you are building. You are also given a 0-indexed array of unique integers bricks, where the ith brick has a height of 1 and a width of bricks[i]. You have an infinite supply of each type of brick and bricks may not be rotated. Each row in the wall must be exactly width units long. For the wall to be sturdy, adjacent rows in the wall should not join bricks at the same location, except at the ends of the wall. Return the number of ways to build a sturdy wall. Since the answer may be very large, return it modulo 109 + 7.   Please complete the following python code precisely: ```python class Solution: def buildWall(self, height: int, width: int, bricks: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(height = 2, width = 3, bricks = [1,2]) == 2\n assert candidate(height = 1, width = 1, bricks = [5]) == 0\n\n\ncheck(Solution().buildWall)"}
195
65
coding
Solve the programming task below in a Python markdown code block. You should have done Product Partitions I to do this second part. If you solved it, you should have notice that we try to obtain the multiplicative partitions with ```n ≤ 100 ```. In this kata we will have more challenging values, our ```n ≤ 10000```. So, we need a more optimized a faster code. We need the function ```prod_int_partII()``` that will give all the amount of different products, excepting the number itself multiplied by one. The function ```prod_int_partII()``` will receive two arguments, the number ```n``` for the one we have to obtain all the multiplicative partitions, and an integer s that determines the products that have an amount of factors equals to ```s```. The function will output a list with this structure: ```python [(1), (2), [(3)]] (1) Total amount of different products we can obtain, using the factors of n. (We do not consider the product n . 1) (2) Total amount of products that have an amount of factors equals to s. [(3)] A list of lists with each product represented with by a sorted list of the factors. All the product- lists should be sorted also. If we have only one product-list as a result, the function will give only the list and will not use the list of lists ``` Let's see some cases: ```python prod_int_partII(36, 3) == [8, 3, [[2, 2, 9], [2, 3, 6], [3, 3, 4]]] /// (1) ----> 8 # Amount of different products, they are: [2, 2, 3, 3], [2, 2, 9], [2, 3, 6], [2, 18], [3, 3, 4], [3, 12], [4, 9], [6, 6] (8 products) (2) ----> 3 # Amount of products with three factors (see them bellow) (3) ----> [[2, 2, 9], [2, 3, 6], [3, 3, 4]] # These are the products with 3 factors ``` ```python prod_int_partII(48, 5) == [11, 1, [2, 2, 2, 2, 3]] # Only one list. ``` Again consider that some numbers will not have multiplicative partitions. ```python prod_int_partII(37, 2) == [0, 0, []] ``` Happy coding!! (Recursion is advisable) Also feel free to reuse/extend the following starter code: ```python def prod_int_partII(n, s): ```
{"functional": "_inputs = [[36, 3], [48, 5], [37, 2], [96, 5], [96, 6]]\n_outputs = [[[8, 3, [[2, 2, 9], [2, 3, 6], [3, 3, 4]]]], [[11, 1, [2, 2, 2, 2, 3]]], [[0, 0, []]], [[18, 2, [[2, 2, 2, 2, 6], [2, 2, 2, 3, 4]]]], [[18, 1, [2, 2, 2, 2, 2, 3]]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(prod_int_partII(*i), o[0])"}
625
314
coding
Solve the programming task below in a Python markdown code block. William really likes the cellular automaton called "Game of Life" so he decided to make his own version. For simplicity, William decided to define his cellular automaton on an array containing $n$ cells, with each cell either being alive or dead. Evolution of the array in William's cellular automaton occurs iteratively in the following way: If the element is dead and it has exactly $1$ alive neighbor in the current state of the array, then on the next iteration it will become alive. For an element at index $i$ the neighbors would be elements with indices $i - 1$ and $i + 1$. If there is no element at that index, it is considered to be a dead neighbor. William is a humane person so all alive elements stay alive. Check the note section for examples of the evolution. You are given some initial state of all elements and you need to help William find the state of the array after $m$ iterations of evolution. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($2 \le n \le 10^3, 1 \le m \le 10^9$), which are the total number of cells in the array and the number of iterations. The second line of each test case contains a string of length $n$ made up of characters "0" and "1" and defines the initial state of the array. "1" means a cell is alive and "0" means it is dead. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- In each test case output a string of length $n$, made up of characters "0" and "1" — the state of the array after $m$ iterations of evolution. -----Examples----- Input 4 11 3 01000000001 10 2 0110100101 5 2 10101 3 100 000 Output 11111001111 1110111101 10101 000 -----Note----- Sequence of iterations of evolution for the first test case 01000000001 — initial state 11100000011 — first iteration of evolution 11110000111 — second iteration of evolution 11111001111 — third iteration of evolution Sequence of iterations of evolution for the second test case 0110100101 — initial state 1110111101 — first iteration of evolution 1110111101 — second iteration of evolution
{"inputs": ["1\n5 3017\n10011\n", "1\n5 42069\n11011\n", "1\n5 42069\n11011\n", "1\n5 45215\n01101\n", "1\n5 35184\n11011\n", "1\n5 21557\n11011\n", "1\n5 21557\n10011\n", "1\n5 55375\n11011\n"], "outputs": ["11111\n", "11011\n", "11011\n", "11101\n", "11011\n", "11011\n", "11111\n", "11011\n"]}
670
213
coding
Solve the programming task below in a Python markdown code block. You are given an integer N that has exactly four digits in base ten. How many times does `2` occur in the base-ten representation of N? Constraints * 1000 \leq N \leq 9999 Input Input is given from Standard Input in the following format: N Output Print the answer. Examples Input 1222 Output 3 Input 3456 Output 0 Input 9592 Output 1
{"inputs": ["0", "1", "2", "3", "95", "79", "89", "36"], "outputs": ["0\n", "0\n", "1\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
121
66
coding
Solve the programming task below in a Python markdown code block. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
{"inputs": ["5 4\n3\n1 1\n3 4\n5 6", "6 4\n3\n1 1\n1 4\n5 6", "5 4\n3\n1 1\n3 4\n5 2", "5 4\n3\n1 1\n3 4\n7 6", "6 4\n3\n1 0\n1 4\n5 6", "6 4\n3\n0 1\n1 4\n5 0", "5 4\n3\n1 1\n3 3\n5 2", "5 4\n3\n0 1\n3 4\n7 6"], "outputs": ["13\n3 4\n", "12\n1 4\n", "11\n3 2\n", "16\n3 4\n", "14\n1 4\n", "13\n1 1\n", "9\n3 2\n", "18\n3 4\n"]}
680
229
coding
Solve the programming task below in a Python markdown code block. "Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath. With that begins the watch of Jon Snow. He is assigned the task to support the stewards. This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him. Can you find how many stewards will Jon support? -----Input----- First line consists of a single integer n (1 ≤ n ≤ 10^5) — the number of stewards with Jon Snow. Second line consists of n space separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^9) representing the values assigned to the stewards. -----Output----- Output a single integer representing the number of stewards which Jon will feed. -----Examples----- Input 2 1 5 Output 0 Input 3 1 2 5 Output 1 -----Note----- In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5. In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2.
{"inputs": ["1\n1\n", "1\n6\n", "1\n0\n", "1\n3\n", "1\n3\n", "1\n1\n", "1\n6\n", "1\n0\n"], "outputs": ["0", "0", "0", "0", "0\n", "0\n", "0\n", "0\n"]}
413
82
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef likes problems related to numbers a lot. He is generally quite good at solving these kinds of problems, but today he got stuck at one number theory problem, and so he asks your help to solve it. Given a positive integer N, find out how many positive integers X satisfy the equation X + S(X) + S(S(X)) = N, where S(X) denoting sum of digits of X in decimal (base 10) representation. ------ Input ------ The only line of the input contains one integer number - N. ------ Output ------ Output single integer in a line denoting the count of number of positive integers X satisfying the above given equation. ------ Constraints ------ $1 ≤ N ≤ 10^{9}$ ----- Sample Input 1 ------ 6 ----- Sample Output 1 ------ 1 ----- explanation 1 ------ Example 1. Only one positive integer X = 2 satisfies the equation X + S(X) + S(S(X)) = 6, as X + S(X) + S(S(X)) = 2 + 2 + 2 = 6. ----- Sample Input 2 ------ 9939 ----- Sample Output 2 ------ 4 ----- explanation 2 ------ Example 2.X can be 9898, 9907, 9910 and 9913. 9898 + S(9898) + S(S(9898)) = 9898 + 34 + 7 = 9939 9907 + S(9907) + S(S(9907)) = 9907 + 25 + 7 = 9939 9910 + S(9910) + S(S(9910)) = 9910 + 19 + 10 = 9939 9913 + S(9913) + S(S(9913)) = 9913 + 22 + 4 = 9939 You can verify that there is not other positive value of X satisfying the given equation.
{"inputs": ["6", "9939"], "outputs": ["1", "4"]}
495
21
coding
Solve the programming task below in a Python markdown code block. Chef has N candies. He has to distribute them to exactly M of his friends such that each friend gets equal number of candies and each friend gets even number of candies. Determine whether it is possible to do so. NOTE: Chef will not take any candies himself and will distribute all the candies. ------ 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, two integers N and M, the number of candies and the number of friends. ------ Output Format ------ For each test case, the output will consist of a single line containing Yes if Chef can distribute the candies as per the conditions and No otherwise. You may print each character of the string in uppercase or lowercase (for example, the strings yes, Yes, yEs, and YES will all be treated as identical). ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N,M ≤ 1000$ ----- Sample Input 1 ------ 4 9 3 4 1 4 2 8 3 ----- Sample Output 1 ------ No Yes Yes No ----- explanation 1 ------ Test case $1$: Since Chef has $9$ candies and $3$ friends, each friend will get $\frac{9}{3} = 3$ candies. Since $3$ is not even, Chef doesn't satisfy the conditions. Test case $2$: Since Chef has $4$ candies and $1$ friend, each friend will get $\frac{4}{1} = 4$ candies. Since $4$ is even, Chef satisfies all the conditions. Test case $3$: Since Chef has $4$ candies and $2$ friends, each friend will get $\frac{4}{2} = 2$ candies. Since $2$ is even, Chef satisfies all the conditions. Test case $4$: Since Chef has $8$ candies and $3$ friends. Since Chef won't be able to distribute all the candies equally, Chef does not satisfy all the conditions.
{"inputs": ["4\n9 3\n4 1\n4 2\n8 3"], "outputs": ["No\nYes\nYes\nNo"]}
449
34
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. Let next(x) be the minimum lucky number which is larger than or equals x. Petya is interested what is the value of the expression next(l) + next(l + 1) + ... + next(r - 1) + next(r). Help him solve this problem. Input The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — the left and right interval limits. Output In the single line print the only number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r). Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 7 Output 33 Input 7 7 Output 7 Note In the first sample: next(2) + next(3) + next(4) + next(5) + next(6) + next(7) = 4 + 4 + 4 + 7 + 7 + 7 = 33 In the second sample: next(7) = 7
{"inputs": ["4 7\n", "1 9\n", "5 6\n", "1 2\n", "6 6\n", "3 4\n", "1 1\n", "4 8\n"], "outputs": ["25", "125", "14", "8", "7", "8\n", "4\n", "69\n"]}
334
86
coding
Solve the programming task below in a Python markdown code block. Ma5termind is going through the Euler's book of knowledge when he finds the following function: def function(L,R): sum=0 for i in range(L,R+1): #[L,R] for j in range(1,i+1): #[1,i] cnt=0 if (j)*(j+1) == 2*i : for k in range(1,i+1): #[1,i] if __gcd(k,i)==1: #__gcd Greatest Common Divisor cnt+=1 sum=sum+cnt return sum The function seems weird as well as interesting. Don't you think ?? Fascinated by the function, Ma5termind codes it and successfully compiles it in the first go itself. After all, he is a good coder. Ma5termind checks the output computed by the function for all the values of $\mathbf{L}$ and $\mathbf{R}$ in the range $\mathbf{[1,100]}$ and finds that the results are correct. But as we all know, Ma5termind loves large numbers - very large numbers. When he inputs $\mathbf{L=1}$ & $\mathbf{R=10^{12}}$, the code does not produce any output #TimeLimitExceeded. He still wants to know the answer for such large ranges. Can you help him by writing a code that can also produce an answer for such large ranges? Input Format First line of input contains a single integer $\mathbf{T}$ denoting the number of test cases. First and the only line of each test case contains two space separated integers $\mathbf{L}$ and $\mathbf{R}$ denoting inputs to the above function. Output Format Output consists of $\mathbf{T}$ lines each containing result computed by the function when corresponding range is inputted to the function. Constraints $\mathbf{1\leq T\leq10^5}$ $\mathbf{1\leq L\leq R\leq10^{12}}$ Sample Input 2 1 10 2 5 Sample Output 9 2 Explanation Pass Parameters to the function and check the result.
{"inputs": ["2\n1 10\n2 5\n"], "outputs": ["9\n2\n"]}
495
25
coding
Solve the programming task below in a Python markdown code block. An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, $A$, of size $N$, each memory location has some unique index, $i$ (where $0\leq i<N$), that can be referenced as $A[i]$ or $A_i$. Reverse an array of integers. Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this. Example $A=[1,2,3]$ Return $[3,2,1]$. Function Description Complete the function reverseArray in the editor below. reverseArray has the following parameter(s): int A[n]: the array to reverse Returns int[n]: the reversed array Input Format The first line contains an integer, $N$, the number of integers in $A$. The second line contains $N$ space-separated integers that make up $A$. Constraints $1\leq N\leq10^3$ $1\leq A[i]\leq10^4,$where $A\begin{bmatrix}i\end{bmatrix}$ is the $i^{t h}$ integer in A
{"inputs": ["4\n1 4 3 2\n"], "outputs": ["2 3 4 1\n"]}
275
28
coding
Solve the programming task below in a Python markdown code block. Lengths are measures in Baden in inches and feet. To a length from centimeters it is enough to know that an inch equals three centimeters in Baden and one foot contains 12 inches. You are given a length equal to n centimeters. Your task is to convert it to feet and inches so that the number of feet was maximum. The result should be an integer rounded to the closest value containing an integral number of inches. Note that when you round up, 1 cm rounds up to 0 inches and 2 cm round up to 1 inch. Input The only line contains an integer n (1 ≤ n ≤ 10000). Output Print two non-negative space-separated integers a and b, where a is the numbers of feet and b is the number of inches. Examples Input 42 Output 1 2 Input 5 Output 0 2
{"inputs": ["4\n", "2\n", "8\n", "3\n", "1\n", "7\n", "9\n", "6\n"], "outputs": ["0 1\n", "0 1\n", "0 3\n", "0 1\n", "0 0\n", "0 2\n", "0 3\n", "0 2\n"]}
201
86
coding
Solve the programming task below in a Python markdown code block. For a string $S$ let the unique set of characters that occur in it one or more times be $C$. Consider a permutation of the elements of $C$ as $(c_1, c_2, c_3 ... )$. Let $f(c)$ be the number of times $c$ occurs in $S$. If any such permutation of the elements of $C$ satisfies $f(c_i) = f(c_{i-1}) + f(c_{i-2})$ for all $i \ge 3$, the string is said to be a dynamic string. Mr Bancroft is given the task to check if the string is dynamic, but he is busy playing with sandpaper. Would you help him in such a state? Note that if the number of distinct characters in the string is less than 3, i.e. if $|C| < 3$, then the string is always dynamic. -----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 "Dynamic" if the given string is dynamic, otherwise print "Not". (Note that the judge is case sensitive) -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq |S| \leq 10^5$ - $S$ contains only lower case alphabets: $a$, $b$, …, $z$ -----Sample Input:----- 3 aaaabccc aabbcc ppppmmnnoooopp -----Sample Output:----- Dynamic Not Dynamic -----Explanation:----- - Testase 1: For the given string, $C = \{a, b, c\}$ and $f(a)=4, f(b)=1, f(c)=3$. $f(a) = f(c) + f(b)$ so the permutation $(b, c, a)$ satisfies the requirement. - Testcase 2: Here too $C = \{a, b, c\}$ but no permutation satisfies the requirement of a dynamic string. - Testcase 3: Here $C = \{m, n, o, p\}$ and $(m, n, o, p)$ is a permutation that makes it a dynamic string.
{"inputs": ["3\naaaabccc\naabbcc\nppppmmnnoooopp"], "outputs": ["Dynamic\nNot\nDynamic"]}
517
32
coding
Solve the programming task below in a Python markdown code block. We have N points in a two-dimensional plane. The coordinates of the i-th point (1 \leq i \leq N) are (x_i,y_i). Let us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior. Here, points on the sides of the rectangle are considered to be in the interior. Find the minimum possible area of such a rectangle. -----Constraints----- - 2 \leq K \leq N \leq 50 - -10^9 \leq x_i,y_i \leq 10^9 (1 \leq i \leq N) - x_i≠x_j (1 \leq i<j \leq N) - y_i≠y_j (1 \leq i<j \leq N) - All input values are integers. (Added at 21:50 JST) -----Input----- Input is given from Standard Input in the following format: N K x_1 y_1 : x_{N} y_{N} -----Output----- Print the minimum possible area of a rectangle that satisfies the condition. -----Sample Input----- 4 4 1 4 3 3 6 2 8 1 -----Sample Output----- 21 One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1), (8,1), (1,4) and (8,4). Its area is (8-1) × (4-1) = 21.
{"inputs": ["4 4\n1 4\n2 3\n6 2\n8 1", "4 4\n1 4\n2 3\n6 2\n8 0", "4 4\n0 4\n2 3\n6 2\n8 1", "4 4\n2 4\n3 3\n6 2\n8 1", "4 4\n1 4\n2 6\n6 2\n8 1", "4 2\n0 0\n1 1\n2 2\n3 3", "4 4\n1 4\n3 3\n6 2\n8 1", "4 4\n1 4\n3 3\n6 2\n8 1\n"], "outputs": ["21\n", "28\n", "24\n", "18\n", "35\n", "1", "21", "21\n"]}
353
212
coding
Solve the programming task below in a Python markdown code block. Finally, College has started calling students back to campus. There are so many students and thus due to some safety measures the college can’t call back all the students on the same day. It currently has the capacity of screening K students on a single day. There is a total of N students. What's the minimum number of days required for all the students to be back on the campus? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first and only line of each test case contains two space-separated integers N, K. ------ Output Format ------ For each test case, print a single line containing one integer - the minimum number of days required for all the students to be back on the campus. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{2}$ $1 ≤ K ≤ 10^{2}$ ----- Sample Input 1 ------ 3 3 3 3 2 4 3 ----- Sample Output 1 ------ 1 2 2 ----- explanation 1 ------ Test case 1: Since $K = 3$ and $N = 3$, we can call all $3$ students back to campus on the first day itself. Hence it requires only $1$ day for all students to be back on campus. Test case 2: We have $K = 2$ and $N = 3 > K$, so we can't call all the students back to campus on a single day. But we can call $1$ student on the first day and the remaining $2$ students on the second day. Therefore, we require a minimum of $2$ days for all students to be back on campus. Test case 3: We have $K = 3$ and $N = 4 > K$, so we can't call all the students back to campus on a single day. But we can call $2$ students on the first day and the remaining $2$ students on the second day. Therefore, we require a minimum of $2$ days for all students to be back on campus.
{"inputs": ["3\n3 3\n3 2\n4 3\n"], "outputs": ["1\n2\n2\n"]}
475
30
coding
Solve the programming task below in a Python markdown code block. Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door. There are $n$ lamps with Spirit Tree's light. Sein knows the time of turning on and off for the $i$-th lamp — $l_i$ and $r_i$ respectively. To open the door you have to choose $k$ lamps in such a way that there will be a moment of time when they all will be turned on. While Sein decides which of the $k$ lamps to pick, Ori is interested: how many ways there are to pick such $k$ lamps that the door will open? It may happen that Sein may be wrong and there are no such $k$ lamps. The answer might be large, so print it modulo $998\,244\,353$. -----Input----- First line contains two integers $n$ and $k$ ($1 \le n \le 3 \cdot 10^5$, $1 \le k \le n$) — total number of lamps and the number of lamps that must be turned on simultaneously. Next $n$ lines contain two integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le 10^9$) — period of time when $i$-th lamp is turned on. -----Output----- Print one integer — the answer to the task modulo $998\,244\,353$. -----Examples----- Input 7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9 Output 9 Input 3 1 1 1 2 2 3 3 Output 3 Input 3 2 1 1 2 2 3 3 Output 0 Input 3 3 1 3 2 3 3 3 Output 1 Input 5 2 1 3 2 4 3 5 4 6 5 7 Output 7 -----Note----- In first test case there are nine sets of $k$ lamps: $(1, 2, 3)$, $(1, 2, 4)$, $(1, 2, 5)$, $(1, 2, 6)$, $(1, 3, 6)$, $(1, 4, 6)$, $(2, 3, 6)$, $(2, 4, 6)$, $(2, 6, 7)$. In second test case $k=1$, so the answer is 3. In third test case there are no such pairs of lamps. In forth test case all lamps are turned on in a time $3$, so the answer is 1. In fifth test case there are seven sets of $k$ lamps: $(1, 2)$, $(1, 3)$, $(2, 3)$, $(2, 4)$, $(3, 4)$, $(3, 5)$, $(4, 5)$.
{"inputs": ["1 1\n1 1\n", "1 1\n1 1\n", "2 1\n1 1\n1 1\n", "2 2\n1 1\n1 1\n", "2 2\n1 1\n1 1\n", "2 1\n1 1\n1 1\n", "3 1\n1 1\n2 2\n3 3\n", "3 2\n1 1\n2 2\n3 3\n"], "outputs": ["1", "1\n", "2", "1", "1\n", "2\n", "3", "0"]}
745
145
coding
Solve the programming task below in a Python markdown code block. Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n). Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used). -----Input----- The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000. The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists. -----Output----- Print the smalles integer n which Vasya could pass to Kate. -----Examples----- Input 003512 021 Output 30021 Input 199966633300 63 Output 3036366999
{"inputs": ["01\n0\n", "13\n3\n", "14\n4\n", "21\n2\n", "12\n2\n", "10\n0\n", "10\n0\n", "13\n3\n"], "outputs": ["0\n", "3\n", "4\n", "2\n", "2\n", "0\n", "0", "3"]}
318
92
coding
Solve the programming task below in a Python markdown code block. You are given several queries. In the i-th query you are given a single positive integer n_{i}. You are to represent n_{i} as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings. An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself. -----Input----- The first line contains single integer q (1 ≤ q ≤ 10^5) — the number of queries. q lines follow. The (i + 1)-th line contains single integer n_{i} (1 ≤ n_{i} ≤ 10^9) — the i-th query. -----Output----- For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings. -----Examples----- Input 1 12 Output 3 Input 2 6 8 Output 1 2 Input 3 1 2 3 Output -1 -1 -1 -----Note----- 12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands. 8 = 4 + 4, 6 can't be split into several composite summands. 1, 2, 3 are less than any composite number, so they do not have valid splittings.
{"inputs": ["1\n4\n", "1\n12\n", "1\n12\n", "2\n6\n8\n", "2\n6\n4\n", "2\n8\n1\n", "2\n8\n4\n", "2\n6\n8\n"], "outputs": ["1\n", "3\n", "3\n", "1\n2\n", "1\n1\n", "2\n-1\n", "2\n1\n", "1\n2\n"]}
352
109
coding
Solve the programming task below in a Python markdown code block. Watson gives Sherlock an array of integers. Given the endpoints of an integer range, for all $M$ in that inclusive range, determine the minimum( abs(arr[i]-M) for all $1\leq i\leq|arr|$) ). Once that has been determined for all integers in the range, return the $M$ which generated the maximum of those values. If there are multiple $M$'s that result in that value, return the lowest one. For example, your array $arr=[3,5,7,9]$ and your range is from $p=6$ to $q=8$ inclusive. M |arr[1]-M| |arr[2]-M| |arr[3]-M| |arr[4]-M| Min 6 3 1 1 3 1 7 4 2 0 2 0 8 5 3 1 1 1 We look at the Min column and see the maximum of those three values is $1$. Two $M$'s result in that answer so we choose the lower value, $6$. Function Description Complete the sherlockAndMinimax function in the editor below. It should return an integer as described. sherlockAndMinimax has the following parameters: - arr: an array of integers - p: an integer that represents the lowest value of the range for $M$ - q: an integer that represents the highest value of the range for $M$ Input Format The first line contains an integer $n$, the number of elements in $arr$. The next line contains $n$ space-separated integers $i$. The third line contains two space-separated integers $p$ and $q$, the inclusive endpoints for the range of $M$. Constraints $1\leq n\leq10^2$ $1\leq ar r[i]\leq10^9$ $1\leq p\leq q\leq10^9$ Output Format Print the value of $M$ on a line. Sample Input 3 5 8 14 4 9 Sample Output 4 Explanation $arr=[5,8,14],range=[4-9]$ M |arr[1]-M| |arr[2]-M| |arr[3]-M| Min 4 1 4 10 1 5 0 3 9 0 6 1 2 8 1 7 2 1 7 1 8 3 0 6 0 9 4 1 5 1 For $M=4,6,7$, or $9$, the result is $1$. Since we have to output the smallest of the multiple solutions, we print $4$.
{"inputs": ["3\n5 8 14\n4 9\n"], "outputs": ["4\n"]}
674
25
coding
Solve the programming task below in a Python markdown code block. Alice and Bob are playing a game. At the start of the game, there are N piles of stones, the i-th of which has A_{i} stones. Alice and Bob alternate moves. On their move, a player does the following: Choose one pile of stones, and discard the remaining N-1 piles. Partition the stones in the chosen pile into exactly N new piles of stones, each containing at least 1 stone. Note that this means that the number of piles is always N. The player who cannot make a move loses. Determine the winner of the game if Alice moves first. ------ 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 number of piles. - The second line contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For every test case print exactly one line containing the answer — either "Alice" or "Bob" (without the quotes). You can output Alice and Bob in any case (for example, the strings BOb, BOB, bob and boB will be recognized as a positive response) ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $2 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{18}$ - The sum of $N$ over all test cases doesn't exceed $ 3\cdot 10^{5}$ ----- Sample Input 1 ------ 1 3 5 7 8 ----- Sample Output 1 ------ Alice ----- explanation 1 ------ In the first move, Alice can select the first pile and divide it into $3$ piles of sizes $2, 2$, and $1$. After this, Bob cannot make a move since each pile has less than $3$ stones.
{"inputs": ["1\n3\n5 7 8"], "outputs": ["Alice"]}
433
20
coding
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $n$ types of microtransactions in the game. Each microtransaction costs $2$ burles usually and $1$ burle if it is on sale. Ivan has to order exactly $k_i$ microtransactions of the $i$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $1$ burle and otherwise he can buy it for $2$ burles. There are also $m$ special offers in the game shop. The $j$-th offer $(d_j, t_j)$ means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $n$ integers $k_1, k_2, \dots, k_n$ ($0 \le k_i \le 2 \cdot 10^5$), where $k_i$ is the number of copies of microtransaction of the $i$-th type Ivan has to order. It is guaranteed that sum of all $k_i$ is not less than $1$ and not greater than $2 \cdot 10^5$. The next $m$ lines contain special offers. The $j$-th of these lines contains the $j$-th special offer. It is given as a pair of integers $(d_j, t_j)$ ($1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n$) and means that microtransactions of the $t_j$-th type are on sale during the $d_j$-th day. -----Output----- Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing. -----Examples----- Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Output 8 Input 5 3 4 2 1 3 2 3 5 4 2 2 5 Output 20
{"inputs": ["5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n", "5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n", "5 3\n1 2 1 3 2\n3 5\n4 2\n2 5\n", "5 3\n1 2 1 3 0\n3 5\n4 2\n2 5\n", "5 3\n1 2 1 3 1\n3 5\n4 2\n2 5\n", "5 3\n4 2 1 3 2\n3 5\n4 2\n2 5\n", "5 6\n1 2 0 2 0\n2 4\n3 3\n1 5\n1 2\n1 5\n2 3\n", "5 6\n1 2 0 2 0\n2 4\n3 3\n1 5\n1 2\n1 5\n2 3\n"], "outputs": ["20\n", "20\n", "14\n", "12\n", "13\n", "20\n", "8\n", "8\n"]}
671
292
coding
Solve the programming task below in a Python markdown code block. A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has $N$ favorite shots. Each shot has a particular range. The range of the $i^{\mbox{th}}$ shot is from $\mbox{A}_{i}$ to $\mbox{B}_{i}$. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player $\boldsymbol{i}$ can field from $\mbox{C}_{i}$ to $\mbox{D}_{i}$. You are given the $N$ favorite shots of Mr. X and the range of $\mbox{M}$ players. $\mbox{S}_i$ represents the strength of each player i.e. the number of shots player $\boldsymbol{i}$ can stop. Your task is to find: $\left(\textstyle\sum_{i=1}^{m}S i\right)$. Game Rules: A player can stop the $i^{\mbox{th}}$ shot if the range overlaps with the player's fielding range. For more clarity about overlapping, study the following figure: Input Format The first line consists of two space separated integers, $N$ and $\mbox{M}$. Each of the next $N$ lines contains two space separated integers. The $i^{\mbox{th}}$ line contains $A_i$ and $B_i$. Each of the next $\mbox{M}$ lines contains two integers. The $i^{\mbox{th}}$ line contains integers $C_i$ and $D_i$. Output Format You need to print the sum of the strengths of all the players: $\left(\textstyle\sum_{i=1}^{m}S i\right)$. Constraints: $1\leq N,M\leq10^5$ $1\leq A_i,B_i,C_i,D_i\leq10^8$ Sample Input 4 4 1 2 2 3 4 5 6 7 1 5 2 3 4 7 5 7 Sample Output 9 Explanation Player 1 can stop the 1st, 2nd and 3rd shot so the strength is $3$. Player 2 can stop the 1st and 2nd shot so the strength is $2$. Player 3 can stop the 3rd and 4th shot so the strength is $2$. Player 4 can stop the 3rd and 4th shot so the strength is $2$. The sum of the strengths of all the players is $3+2+2+2=9$.
{"inputs": ["4 4 \n1 2 \n2 3\n4 5\n6 7\n1 5\n2 3\n4 7\n5 7 \n"], "outputs": ["9\n"]}
610
53
coding
Solve the programming task below in a Python markdown code block. Read problems statements in [Russian] and [Bengali]. You are given two binary strings S and P. You need to convert S into P using the following operation any number of times (possibly zero): Pick three binary values X, Y, and Z, such that at least one of them is equal to 1 and at least one of them is equal to 0. Then, pick three distinct indices i, j, and k, and assign S_{i}=X, S_{j}=Y, and S_{k}=Z. Determine whether it's possible to convert S into P. ------ Input Format ------ - The first line contains an integer T denoting the number of test cases. The T test cases then follow. - The first line of each test case contains N denoting the length of S and P. - The second line of each test case contains S. - The third line of each test case contains P. ------ Output Format ------ For each test case, output on one line YES if it is possible to convert S to P, or NO if it is not possible to do so. Output is case insensitive. ------ Constraints ------ $1 ≤T ≤1000$ $3 ≤N ≤1000$ $|S| = |P| = N$ ----- Sample Input 1 ------ 2 4 1001 0101 3 111 000 ----- Sample Output 1 ------ YES NO ----- explanation 1 ------ - For the first test case, we choose $X=0, Y=1$ and $Z=1$. This satisfies the above condition. Three indices $(i,j,k)$ are chosen as $(1,2,4)$, and $(A_{1}, A_{2}, A_{4})$ are then assigned as $(0, 1, 1)$ respectively. After this operation, $S$ transforms into 0101. - For the second test case, no set of any operations is possible to make $S$ equals to $P$.
{"inputs": ["2\n4\n1001\n0101\n3\n111\n000"], "outputs": ["YES\nNO"]}
446
36
coding
Solve the programming task below in a Python markdown code block. The number n is Evil if it has an even number of 1's in its binary representation. The first few Evil numbers: 3, 5, 6, 9, 10, 12, 15, 17, 18, 20 The number n is Odious if it has an odd number of 1's in its binary representation. The first few Odious numbers: 1, 2, 4, 7, 8, 11, 13, 14, 16, 19 You have to write a function that determine if a number is Evil of Odious, function should return "It's Evil!" in case of evil number and "It's Odious!" in case of odious number. good luck :) Also feel free to reuse/extend the following starter code: ```python def evil(n): ```
{"functional": "_inputs = [[1], [2], [3]]\n_outputs = [[\"It's Odious!\"], [\"It's Odious!\"], [\"It's Evil!\"]]\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(evil(*i), o[0])"}
205
182
coding
Solve the programming task below in a Python markdown code block. Let f(X) denote the number of set bits in X. Given integers N and K, construct an array A of length N such that: 0 ≤ A_{i} ≤ K for all (1 ≤ i ≤ N); \sum_{i=1}^{N} A_{i} = K Over all such possible arrays, find the maximum value of \sum_{i=1}^{N} f(A_{i}). ------ Input Format ------ - The first line of input contains a single integer T - the number of test cases. The description of T test cases follow. - The only line of each test case contains integers N and K- the length of the array and sum of elements respectively. ------ Output Format ------ For each test case, output the maximum value of \sum_{i=1}^{N} f(A_{i}). ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{9}$ $0 ≤ K ≤ 10^{9}$ ----- Sample Input 1 ------ 2 1 7 3 6 ----- Sample Output 1 ------ 3 4 ----- explanation 1 ------ Test case $1$: Only one array is possible that is $A =[7]$. As $7 = (111)_2$, the number of set bits in $7$ is $3$. Thus, $\sum_{i=1}^{N} f(A_{i}) = f(7) = 3$. Test case $2$: A possible array satisfying all the conditions is $A =[3, 0, 3]$. As $3 = (11)_2$, the number of set bits in $3$ is $2$. Similarly, the number of set bits in $0$ is $0$. Thus, $\sum_{i=1}^{N} f(A_{i}) = f(3) + f(0) + f(3) = 4$. It can be proven that answer never exceeds $4$.
{"inputs": ["2\n1 7\n3 6\n"], "outputs": ["3\n4\n"]}
443
24
coding
Solve the programming task below in a Python markdown code block. n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends. Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition. -----Input----- The only line of input contains two integers n and m, separated by a single space (1 ≤ m ≤ n ≤ 10^9) — the number of participants and the number of teams respectively. -----Output----- The only line of the output should contain two integers k_{min} and k_{max} — the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively. -----Examples----- Input 5 1 Output 10 10 Input 3 2 Output 1 1 Input 6 3 Output 3 6 -----Note----- In the first sample all the participants get into one team, so there will be exactly ten pairs of friends. In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one. In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people.
{"inputs": ["5 1\n", "3 2\n", "6 3\n", "5 3\n", "1 1\n", "2 1\n", "2 2\n", "1 1\n"], "outputs": ["10 10\n", "1 1\n", "3 6\n", "2 3\n", "0 0\n", "1 1\n", "0 0\n", "0 0\n"]}
322
104
coding
Solve the programming task below in a Python markdown code block. Someone gave Alyona an array containing n positive integers a_1, a_2, ..., a_{n}. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all. Formally, after applying some operations Alyona will get an array of n positive integers b_1, b_2, ..., b_{n} such that 1 ≤ b_{i} ≤ a_{i} for every 1 ≤ i ≤ n. Your task is to determine the maximum possible value of mex of this array. Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1. -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of elements in the Alyona's array. The second line of the input contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the elements of the array. -----Output----- Print one positive integer — the maximum possible value of mex of the array after Alyona applies some (possibly none) operations. -----Examples----- Input 5 1 3 3 3 6 Output 5 Input 2 2 1 Output 3 -----Note----- In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5. To reach the answer to the second sample case one must not decrease any of the array elements.
{"inputs": ["1\n1\n", "1\n2\n", "1\n2\n", "1\n1\n", "1\n3\n", "2\n2 1\n", "2\n1 1\n", "2\n1 3\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "2\n", "3\n", "2\n", "3\n"]}
444
92
coding
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian as well. Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem: You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s. Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you. You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s. A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'. ------ Input ------ The first line of the input contains an ABC-string S. ------ Output ------ Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s. The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data. ------ Constraints ------ 1 ≤ |S| ≤ 1 000 000; where |S| denotes the length of the given ABC-string. ------ Example ------ Input: ABACABA Output: 2 ------ Explanation ------ In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB".
{"inputs": ["ABBCABA", "ABABBBA", "@BABBBA", "@BABCBA", "AB@BCAB", "BCABACB", "ABCBACB", "ABACBBA"], "outputs": ["2\n", "0\n", "1\n", "3\n", "4\n", "6\n", "5\n", "2\n"]}
361
82
coding
Solve the programming task below in a Python markdown code block. Write function heron which calculates the area of a triangle with sides a, b, and c. Heron's formula: sqrt (s \* (s - a) \* (s - b) \* (s - c)), where s = (a + b + c) / 2. Output should have 2 digits precision. Also feel free to reuse/extend the following starter code: ```python def heron(a,b,c): ```
{"functional": "_inputs = [[3, 4, 5], [6, 8, 10]]\n_outputs = [[6], [24]]\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(heron(*i), o[0])"}
107
175
coding
Solve the programming task below in a Python markdown code block. You are given a secret message you need to decipher. Here are the things you need to know to decipher it: For each word: - the second and the last letter is switched (e.g. `Hello` becomes `Holle`) - the first letter is replaced by its character code (e.g. `H` becomes `72`) Note: there are no special characters used, only letters and spaces Examples ``` decipherThis('72olle 103doo 100ya'); // 'Hello good day' decipherThis('82yade 115te 103o'); // 'Ready set go' ``` Also feel free to reuse/extend the following starter code: ```python def decipher_this(string): ```
{"functional": "_inputs = [['65 119esi 111dl 111lw 108dvei 105n 97n 111ka'], ['84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp'], ['84eh 108sse 104e 115eokp 116eh 109ero 104e 104dare'], ['87yh 99na 119e 110to 97ll 98e 108eki 116tah 119esi 111dl 98dri'], ['84kanh 121uo 80roti 102ro 97ll 121ruo 104ple']]\n_outputs = [['A wise old owl lived in an oak'], ['The more he saw the less he spoke'], ['The less he spoke the more he heard'], ['Why can we not all be like that wise old bird'], ['Thank you Piotr for all your help']]\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(decipher_this(*i), o[0])"}
175
417
coding
Solve the programming task below in a Python markdown code block. There are four towns, numbered 1,2,3 and 4. Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally. No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads. Determine if we can visit all the towns by traversing each of the roads exactly once. Constraints * 1 \leq a_i,b_i \leq 4(1\leq i\leq 3) * a_i and b_i are different. (1\leq i\leq 3) * No two roads connect the same pair of towns. * Any town can be reached from any other town using the roads. Input Input is given from Standard Input in the following format: a_1 b_1 a_2 b_2 a_3 b_3 Output If we can visit all the towns by traversing each of the roads exactly once, print `YES`; otherwise, print `NO`. Examples Input 4 2 1 3 2 3 Output YES Input 3 2 2 4 1 2 Output NO Input 2 1 3 2 4 3 Output YES
{"inputs": ["4 3\n1 3\n2 3", "1 1\n3 2\n4 3", "1 2\n3 2\n4 3", "1 2\n2 2\n4 3", "3 2\n2 4\n1 4", "1 1\n3 2\n4 1", "1 4\n2 2\n4 3", "3 2\n3 4\n1 4"], "outputs": ["NO\n", "YES\n", "YES\n", "NO\n", "YES\n", "NO\n", "YES\n", "YES\n"]}
303
142
coding
Solve the programming task below in a Python markdown code block. A lot of frogs want to cross a river. A river is $w$ units width, but frogs can only jump $l$ units long, where $l < w$. Frogs can also jump on lengths shorter than $l$. but can't jump longer. Hopefully, there are some stones in the river to help them. The stones are located at integer distances from the banks. There are $a_i$ stones at the distance of $i$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water. What is the maximum number of frogs that can cross the river, given that then can only jump on the stones? -----Input----- The first line contains two integers $w$ and $l$ ($1 \le l < w \le 10^5$) — the width of the river and the maximum length of a frog's jump. The second line contains $w - 1$ integers $a_1, a_2, \ldots, a_{w-1}$ ($0 \le a_i \le 10^4$), where $a_i$ is the number of stones at the distance $i$ from the bank the frogs are currently at. -----Output----- Print a single integer — the maximum number of frogs that can cross the river. -----Examples----- Input 10 5 0 0 1 0 2 0 0 1 0 Output 3 Input 10 3 1 1 1 1 2 1 1 1 1 Output 3 -----Note----- In the first sample two frogs can use the different stones at the distance $5$, and one frog can use the stones at the distances $3$ and then $8$. In the second sample although there are two stones at the distance $5$, that does not help. The three paths are: $0 \to 3 \to 6 \to 9 \to 10$, $0 \to 2 \to 5 \to 8 \to 10$, $0 \to 1 \to 4 \to 7 \to 10$.
{"inputs": ["2 1\n0\n", "2 1\n5\n", "2 1\n0\n", "2 1\n5\n", "2 1\n8\n", "10 5\n0 0 1 0 2 0 0 1 0\n", "10 3\n1 1 1 1 2 1 1 1 1\n", "10 4\n0 0 6 2 7 1 6 4 0\n"], "outputs": ["0\n", "5\n", "0\n", "5\n", "8\n", "3\n", "3\n", "8\n"]}
483
153
coding
Solve the programming task below in a Python markdown code block. Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$.
{"inputs": ["3\n000\n000\n", "3\n000\n000\n", "3\n000\n001\n", "3\n100\n000\n", "3\n010\n000\n", "3\n010\n001\n", "3\n010\n100\n", "3\n010\n101\n"], "outputs": ["0", "0\n", "-1\n", "-1\n", "-1\n", "1\n", "1\n", "-1\n"]}
619
133
coding
Solve the programming task below in a Python markdown code block. Complete the function that takes one argument, a list of words, and returns the length of the longest word in the list. For example: ```python ['simple', 'is', 'better', 'than', 'complex'] ==> 7 ``` Do not modify the input list. Also feel free to reuse/extend the following starter code: ```python def longest(words): ```
{"functional": "_inputs = [[['simple', 'is', 'better', 'than', 'complex']], [['explicit', 'is', 'better', 'than', 'implicit']], [['beautiful', 'is', 'better', 'than', 'ugly']]]\n_outputs = [[7], [8], [9]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(longest(*i), o[0])"}
93
205
coding
Solve the programming task below in a Python markdown code block. Indian National Olympiad in Informatics 2016 Boing Inc, has N employees, numbered 1 ... N. Every employee other than Mr. Hojo (the head of the company) has a manager (P[i] denotes the manager of employee i). Thus an employee may manage any number of other employees but he reports only to one manager, so that the organization forms a tree with Mr. Hojo at the root. We say that employee B is a subordinate of employee A if B appears in the subtree rooted at A. Mr. Hojo, has hired Nikhil to analyze data about the employees to suggest how to identify faults in Boing Inc. Nikhil, who is just a clueless consultant, has decided to examine wealth disparity in the company. He has with him the net wealth of every employee (denoted A[i] for employee i). Note that this can be negative if the employee is in debt. He has already decided that he will present evidence that wealth falls rapidly as one goes down the organizational tree. He plans to identify a pair of employees i and j, j a subordinate of i, such A[i] - A[j] is maximum. Your task is to help him do this. Suppose, Boing Inc has 4 employees and the parent (P[i]) and wealth information (A[i]) for each employee are as follows: i 1 2 3 4 A[i] 5 10 6 12 P[i] 2 -1 4 2 P[2] = -1 indicates that employee 2 has no manager, so employee 2 is Mr. Hojo. In this case, the possible choices to consider are (2,1) with a difference in wealth of 5, (2,3) with 4, (2,4) with -2 and (4,3) with 6. So the answer is 6. -----Input format----- There will be one line which contains (2*N + 1) space-separate integers. The first integer is N, giving the number of employees in the company. The next N integers A[1], .., A[N] give the wealth of the N employees. The last N integers are P[1], P[2], .., P[N], where P[i] identifies the manager of employee i. If Mr. Hojo is employee i then, P[i] = -1, indicating that he has no manager. -----Output format----- One integer, which is the needed answer. -----Test data----- -108 ≤ A[i] ≤ 108, for all i. Subtask 1 (30 Marks) 1 ≤ N ≤ 500. Subtask 2 (70 Marks) 1 ≤ N ≤ 105. -----Sample Input----- 4 5 10 6 12 2 -1 4 2 -----Sample Output----- 6
{"inputs": ["4 5 10 6 12 2 -1 4 2"], "outputs": ["6"]}
637
30
coding
Solve the programming task below in a Python markdown code block. Chuck has lost count of how many asses he has kicked... Chuck stopped counting at 100,000 because he prefers to kick things in the face instead of counting. That's just who he is. To stop having to count like a mere mortal chuck developed his own special code using the hairs on his beard. You do not need to know the details of how it works, you simply need to know that the format is as follows: 'A8A8A8A8A8.-A%8.88.' In Chuck's code, 'A' can be any capital letter and '8' can be any number 0-9 and any %, - or . symbols must not be changed. Your task, to stop Chuck beating your ass with his little finger, is to use regex to verify if the number is a genuine Chuck score. If not it's probably some crap made up by his nemesis Bruce Lee. Return true if the provided count passes, and false if it does not. ```Javascript Example: 'A8A8A8A8A8.-A%8.88.' <- don't forget final full stop :D\n Tests: 'A2B8T1Q9W4.-F%5.34.' == true; 'a2B8T1Q9W4.-F%5.34.' == false; (small letter) 'A2B8T1Q9W4.-F%5.3B.' == false; (last char should be number) 'A2B8T1Q9W4.£F&5.34.' == false; (symbol changed from - and %) ``` The pattern only needs to appear within the text. The full input can be longer, i.e. the pattern can be surrounded by other characters... Chuck loves to be surrounded! Ready, steady, VERIFY!! Also feel free to reuse/extend the following starter code: ```python def body_count(code): ```
{"functional": "_inputs = [['A6C2E5Z9A4.-F%8.08.'], ['PP P6A6T5F5S3.-Z%1.11.hgr'], ['A6A1E3A8M2.-Q%8.88.'], ['d G8H1E2O9N3.-W%8.56. f'], ['B4A1D1I8B4.-E%8.76.'], ['ffr65A C8K4D9U7V5.-Y%8.00.'], [' 76 B2L4D0A8C6.-T%8.90. lkd'], ['B2L4D0A8C6.-T%8.90'], ['B2L4D0AFC6.-T%8.90.'], ['B4A1D1I8B4'], ['B4A6666...'], ['B4A1D1I000.-E%0.00.)'], ['.-E%8.76.'], ['B4A1t6I7.-E%8.76.'], ['b4A1D1I8B4.-E%8.76.']]\n_outputs = [[True], [True], [True], [True], [True], [True], [True], [False], [False], [False], [False], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(body_count(*i), o[0])"}
437
476
coding
Solve the programming task below in a Python markdown code block. You've got another problem dealing with arrays. Let's consider an arbitrary sequence containing n (not necessarily different) integers a1, a2, ..., an. We are interested in all possible pairs of numbers (ai, aj), (1 ≤ i, j ≤ n). In other words, let's consider all n2 pairs of numbers, picked from the given array. For example, in sequence a = {3, 1, 5} are 9 pairs of numbers: (3, 3), (3, 1), (3, 5), (1, 3), (1, 1), (1, 5), (5, 3), (5, 1), (5, 5). Let's sort all resulting pairs lexicographically by non-decreasing. Let us remind you that pair (p1, q1) is lexicographically less than pair (p2, q2) only if either p1 < p2, or p1 = p2 and q1 < q2. Then the sequence, mentioned above, will be sorted like that: (1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5) Let's number all the pair in the sorted list from 1 to n2. Your task is formulated like this: you should find the k-th pair in the ordered list of all possible pairs of the array you've been given. Input The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n2). The second line contains the array containing n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). The numbers in the array can coincide. All numbers are separated with spaces. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout, streams or the %I64d specificator instead. Output In the single line print two numbers — the sought k-th pair. Examples Input 2 4 2 1 Output 2 2 Input 3 2 3 1 5 Output 1 3 Note In the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2). The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3).
{"inputs": ["1 1\n-4\n", "2 4\n2 0\n", "2 4\n2 1\n", "3 3\n1 1 2\n", "3 7\n5 4 3\n", "3 5\n1 1 2\n", "3 7\n5 0 3\n", "3 3\n3 1 5\n"], "outputs": ["-4 -4\n", "2 2\n", "2 2\n", "1 1\n", "5 3\n", "1 2\n", "5 0\n", "1 5\n"]}
595
144
coding
Solve the programming task below in a Python markdown code block. Apparently "Put A Pillow On Your Fridge Day is celebrated on the 29th of May each year, in Europe and the U.S. The day is all about prosperity, good fortune, and having bit of fun along the way." All seems very weird to me. Nevertheless, you will be given an array of two strings (s). First find out if the first string contains a fridge... (i've deemed this as being 'n', as it looks like it could hold something). Then check that the second string has a pillow - deemed 'B' (struggled to get the obvious pillow-esque character). If the pillow is on top of the fridge - it must be May 29th! Or a weird house... Return true; For clarity, on top means right on top, ie in the same index position. If the pillow is anywhere else in the 'house', return false; There may be multiple fridges, and multiple pillows. But you need at least 1 pillow ON TOP of a fridge to return true. Multiple pillows on fridges should return true also. 100 random tests Also feel free to reuse/extend the following starter code: ```python def pillow(s): ```
{"functional": "_inputs = [[['EvH/KNikBiyxfeyK/miCMj', 'I/HwjnHlFLlahMOKNadps']], [['\\\\DjQ\\\\[zv]SpG]Z/[Qm\\\\eLL', 'amwZArsaGRmibriXBgTRZp']], [['n', 'B']], [['yF[CeAAiNihWEmKxJc/NRMVn', 'rMeIa\\\\KAfbjuLiTnAQxNw[XB']], [['inECnBMAA/u', 'ABAaIUOUx/M']]]\n_outputs = [[False], [False], [True], [True], [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(pillow(*i), o[0])"}
261
293
coding
Solve the programming task below in a Python markdown code block. One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is. -----Input----- The first line of the input contains an integer n (1 ≤ n ≤ 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn’t exceed 100. -----Output----- For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 can’t recognize whose sentence it is. He can’t recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions. -----Examples----- Input 5 I will go to play with you lala. wow, welcome. miao.lala. miao. miao . Output Freda's OMG>.< I don't know! OMG>.< I don't know! Rainbow's OMG>.< I don't know!
{"inputs": ["1\nm\n", "1\nm\n", "1\nl\n", "1\nk\n", "1\nn\n", "1\nj\n", "1\nMiao.\n", "1\nLalA.\n"], "outputs": ["OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n", "OMG>.< I don't know!\n"]}
357
155
coding
Solve the programming task below in a Python markdown code block. A marine themed rival site to Codewars has started. Codwars is advertising their website all over the internet using subdomains to hide or obfuscate their domain to trick people into clicking on their site. Your task is to write a function that accepts a URL as a string and determines if it would result in an http request to codwars.com. Function should return true for all urls in the codwars.com domain. All other URLs should return false. The urls are all valid but may or may not contain http://, https:// at the beginning or subdirectories or querystrings at the end. For additional confusion, directories in can be named "codwars.com" in a url with the codewars.com domain and vise versa. Also, a querystring may contain codewars.com or codwars.com for any other domain - it should still return true or false based on the domain of the URL and not the domain in the querystring. Subdomains can also add confusion: for example `http://codwars.com.codewars.com` is a valid URL in the codewars.com domain in the same way that `http://mail.google.com` is a valid URL within google.com Urls will not necessarily have either codewars.com or codwars.com in them. The folks at Codwars aren't very good about remembering the contents of their paste buffers. All urls contain domains with a single TLD; you need not worry about domains like company.co.uk. ``` findCodwars("codwars.com"); // true findCodwars("https://subdomain.codwars.com/a/sub/directory?a=querystring"); // true findCodwars("codewars.com"); // false findCodwars("https://subdomain.codwars.codewars.com/a/sub/directory/codwars.com?a=querystring"); // false ``` Also feel free to reuse/extend the following starter code: ```python def find_codwars(url): ```
{"functional": "_inputs = [['codwars.com'], ['http://codwars.com'], ['http://kcodwars.com'], ['https://www.codwars.com'], ['https://www.codwars.com/kata'], ['codewars.com.codwars.com'], ['https://www.codwars.com/kata?this=is&a=querystring'], ['https://this.is.an.unneccesarily.long.subdomain.codwars.com/katas.are.really.fun.codewars.com/'], ['http://codwars.com?impersonate=codewars.com'], ['codewars.com'], ['codwars.comp'], ['codwarsecom'], ['codwars.com.com'], ['codwarss.com'], ['ecodwars.comp'], ['codwars.com.codwars.comp'], ['codwars.com.ecodwars.comp'], ['www.codewars.com/codwars'], ['http://codewars.com'], ['https://www.codewars.com'], ['https://www.codewars.com/kata'], ['http://codewars.com?impersonate=codwars.com'], ['https://www.codewars.com/kata?this=is&a=querystring'], ['https://this.is.an.unneccesarily.long.subdomain.codewars.com/katas.are.really.fun.codwars.com/'], ['https://this.is.an.unneccesarily.long.subdomain.codwars.comp/katas.are.really.fun.codwars.com/'], ['hotmail.com']]\n_outputs = [[True], [True], [False], [True], [True], [True], [True], [True], [True], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_codwars(*i), o[0])"}
430
541
coding
Solve the programming task below in a Python markdown code block. AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i. Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater. Then, for each card i, he judges whether it is unnecessary or not, as follows: - If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary. - Otherwise, card i is NOT unnecessary. Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary. -----Constraints----- - All input values are integers. - 1≤N≤5000 - 1≤K≤5000 - 1≤a_i≤10^9 (1≤i≤N) -----Partial Score----- - 300 points will be awarded for passing the test set satisfying N,K≤400. -----Input----- The input is given from Standard Input in the following format: N K a_1 a_2 ... a_N -----Output----- Print the number of the unnecessary cards. -----Sample Input----- 3 6 1 4 3 -----Sample Output----- 1 There are two good sets: {2,3} and {1,2,3}. Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary. For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary. Neither is card 3 for a similar reason, hence the answer is 1.
{"inputs": ["3 6\n1 7 3", "3 6\n1 5 2", "3 7\n1 5 0", "3 6\n1 7 2", "3 6\n1 5 0", "3 7\n0 5 0", "2 7\n0 6 0", "2 7\n0 3 0"], "outputs": ["2\n", "0\n", "3\n", "2\n", "1\n", "3\n", "2\n", "2\n"]}
418
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums and an integer x. Find the minimum absolute difference between two elements in the array that are at least x indices apart. In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized. Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart.   Please complete the following python code precisely: ```python class Solution: def minAbsoluteDifference(self, nums: List[int], x: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [4,3,2,4], x = 2) == 0\n assert candidate(nums = [5,3,2,10,15], x = 1) == 1\n assert candidate(nums = [1,2,3,4], x = 3) == 3\n\n\ncheck(Solution().minAbsoluteDifference)"}
139
93
coding
Solve the programming task below in a Python markdown code block. During the Steam Summer Sale, Jim's $N-1$ friends have purchased $\mbox{M}$ games, which are numbered from $\mbox{1}$ to $\mbox{M}$. The games are multiplayer. Jim has invited his friends to his basement where they will play by making a LAN-Party. Each friend has already decided the game he would like to play for the rest of the day. So there will be a group of friends who will play the same game together. But then, they face a problem: Currently, none of the friends' PCs are connected. So they have to be connected using the available $\mbox{Q}$ wires. Jim decides to connect friends $u_i$ and $v_i$ with the $\boldsymbol{i}^{th}$ wire one by one. So he starts with wire 1, then with wire 2 and so on. A group can start playing their game, only if all the members are connected (if not directly, then there must exist a path connecting them). They want to start playing as soon as possible. For each game, find out the wire after adding which the group can start playing. It is also possible that a group will never get connected. In such a case, this group starts crying and you should display -1. Input Format On the first line there will be $N$, $\mbox{M}$ and $\mbox{Q}$ each separated by a single space. On the second line we will give you $N$ integers separated by a single space: The $\boldsymbol{i}$-th integer denotes the game friend $\boldsymbol{i}$ wants to play (all between $\mbox{1}$ and $\mbox{M}$). The next $\mbox{Q}$ lines will denote $\mbox{Q}$ wires: $i^{th}$ line denotes $i^{th}$ wire and is denoted by $u_i$ and $v_i$ pairs each separated by a single space. Constraints $1\leq N,M\leq10^5$ For each game $\boldsymbol{i}$, the number of players playing $\boldsymbol{i}$ will be positive. $0\leq Q\leq10^5$ Note Each game is chosen by at least one player. If a group consists of only one member, then print 0, since this lucky (?) lone player can start right away! Output Format Print on the $\boldsymbol{i}^{th} $line the answer for the $\boldsymbol{i}^{th} $game. Sample Input 5 2 4 1 2 2 2 1 1 2 2 3 1 5 4 5 Sample Output 3 4 Explanation The group with the game 1 can play after the 3^{rd} wire is added. The group with game 2 can play only after the 4^{th} wire has been added because after adding the 4^{th} wire, a path between (2,3) (3,4) and (2,4) gets created.
{"inputs": ["5 2 4\n1 2 2 2 1\n1 2 \n2 3\n1 5\n4 5 \n"], "outputs": ["3\n4\n"]}
669
48
coding
Solve the programming task below in a Python markdown code block. Problem Statement The Animal School is a primary school for animal children. You are a fox attending this school. One day, you are given a problem called "Arithmetical Restorations" from the rabbit teacher, Hanako. Arithmetical Restorations are the problems like the following: * You are given three positive integers, $A$, $B$ and $C$. * Several digits in these numbers have been erased. * You should assign a digit in each blank position so that the numbers satisfy the formula $A+B=C$. * The first digit of each number must not be zero. It is also the same for single-digit numbers. You are clever in mathematics, so you immediately solved this problem. Furthermore, you decided to think of a more difficult problem, to calculate the number of possible assignments to the given Arithmetical Restorations problem. If you can solve this difficult problem, you will get a good grade. Shortly after beginning the new task, you noticed that there may be too many possible assignments to enumerate by hand. So, being the best programmer in the school as well, you are now trying to write a program to count the number of possible assignments to Arithmetical Restoration problems. Input The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. > $A$ > $B$ > $C$ Each dataset consists of three strings, $A$, $B$ and $C$. They indicate that the sum of $A$ and $B$ should be $C$. Each string consists of digits (`0`-`9`) and/or question mark (`?`). A question mark (`?`) indicates an erased digit. You may assume that the first character of each string is not `0` and each dataset has at least one `?`. It is guaranteed that each string contains between 1 and 50 characters, inclusive. You can also assume that the lengths of three strings are equal. The end of input is indicated by a line with a single zero. Output For each dataset, output the number of possible assignments to the given problem modulo 1,000,000,007. Note that there may be no way to solve the given problems because Ms. Hanako is a careless rabbit. Sample Input 3?4 12? 5?6 ?2?4 5?7? ?9?2 ????? ????? ????? 0 Output for the Sample Input 2 40 200039979 Note The answer of the first dataset is 2. They are shown below. * 384 + 122 = 506 * 394 + 122 = 516 Example Input 3?4 12? 5?6 ?2?4 5?7? ?9?2 ????? ????? ????? 0 Output 2 40 200039979
{"inputs": ["3?4\n12?\n5?6\n?1?4\n5?7?\n?9?2\n?????\n?????\n?????\n0", "3?4\n12?\n6?5\n?2?4\n5?7?\n?9?2\n?????\n?????\n?????\n0", "3?4\n12?\n6?5\n?2?4\n5?7?\n2?9?\n?????\n?????\n?????\n0", "3?4\n?21\n6?5\n?2?4\n5?7?\n2?9?\n?????\n?????\n?????\n0", "34?\n?21\n6?5\n4?2?\n5?7?\n2?9?\n?????\n?????\n?????\n0", "3?4\n12?\n5?6\n?2?4\n5?7?\n2?9?\n?????\n?????\n?????\n0", "3?4\n?21\n6?5\n4?2?\n5?7?\n?9?2\n?????\n?????\n?????\n0", "3?4\n?21\n6?5\n4?2?\n?7?5\n?9?2\n?????\n?????\n?????\n0"], "outputs": ["2\n40\n200039979\n", "0\n40\n200039979\n", "0\n0\n200039979\n", "10\n0\n200039979\n", "1\n0\n200039979\n", "2\n0\n200039979\n", "10\n93\n200039979\n", "10\n50\n200039979\n"]}
654
453
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1]. We split arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array. Return the largest number of chunks we can make to sort the array.   Please complete the following python code precisely: ```python class Solution: def maxChunksToSorted(self, arr: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [4,3,2,1,0]) == 1\n assert candidate(arr = [1,0,2,3,4]) == 4\n\n\ncheck(Solution().maxChunksToSorted)"}
125
61
coding
Solve the programming task below in a Python markdown code block. There are two types of vehicles in Chefland. Bus which has a capacity of 100 people. Car which has a capacity of 4 people. There are N people who want to travel from place A to place B. You know that a single bus emits X units of smoke while a single car emits Y units of smoke in their journey from A to B. You want to arrange some buses and cars to carry all these N people such that total smoke emitted is minimized. Output the minimized smoke value. ------ Input Format ------ - First line will contain T, the number of test cases. Then the test cases follow. - Each test case contains three integers N, X, Y - the number of people who want to travel, the units of smoke emitted by a bus and the units of smoke emitted by a car respectively. ------ Output Format ------ For each test case, output the minimum units of smoke emitted in transporting the N people. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 1000$ $1 ≤ X, Y ≤ 1000$ ----- Sample Input 1 ------ 3 9 40 8 15 20 20 105 80 10 ----- Sample Output 1 ------ 24 20 100 ----- explanation 1 ------ Test case-1: In the optimal case, we will use $3$ cars where the $1$-st and $2$-nd car will carry $4$ people each and the $3$-rd car will carry $1$ person. Therefore smoke emitted $= 3 \times 8 = 24$ units. Test case-2: In the optimal case, we will use $1$ bus to carry all the $15$ people. Therefore smoke emitted $= 1 \times 20 = 20$ units. Test case-3: In the optimal case, we will use $1$ bus to carry $100$ people and use $2$ cars to carry the remaining $5$ people. Therefore smoke emitted $= 1 \times 80 + 2 \times 10 = 100$ units.
{"inputs": ["3\n9 40 8\n15 20 20\n105 80 10\n"], "outputs": ["24\n20\n100\n"]}
488
48
coding
Solve the programming task below in a Python markdown code block. You are given two arrays $a_1, a_2, \dots , a_n$ and $b_1, b_2, \dots , b_m$. Array $b$ is sorted in ascending order ($b_i < b_{i + 1}$ for each $i$ from $1$ to $m - 1$). You have to divide the array $a$ into $m$ consecutive subarrays so that, for each $i$ from $1$ to $m$, the minimum on the $i$-th subarray is equal to $b_i$. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of $a$ compose the first subarray, the next several elements of $a$ compose the second subarray, and so on. For example, if $a = [12, 10, 20, 20, 25, 30]$ and $b = [10, 20, 30]$ then there are two good partitions of array $a$: $[12, 10, 20], [20, 25], [30]$; $[12, 10], [20, 20, 25], [30]$. You have to calculate the number of ways to divide the array $a$. Since the number can be pretty large print it modulo 998244353. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the length of arrays $a$ and $b$ respectively. The second line contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$) — the array $a$. The third line contains $m$ integers $b_1, b_2, \dots , b_m$ ($1 \le b_i \le 10^9; b_i < b_{i+1}$) — the array $b$. -----Output----- In only line print one integer — the number of ways to divide the array $a$ modulo 998244353. -----Examples----- Input 6 3 12 10 20 20 25 30 10 20 30 Output 2 Input 4 2 1 3 3 7 3 7 Output 0 Input 8 2 1 2 2 2 2 2 2 2 1 2 Output 7
{"inputs": ["1 1\n1\n1\n", "1 1\n2\n1\n", "1 1\n3\n2\n", "1 1\n5\n3\n", "1 1\n1\n1\n", "1 1\n5\n3\n", "1 1\n2\n1\n", "1 1\n3\n2\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "1", "0", "0", "0"]}
613
114
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero. Return true if and only if we can do this so that the resulting number is a power of two.   Please complete the following python code precisely: ```python class Solution: def reorderedPowerOf2(self, n: int) -> bool: ```
{"functional": "def check(candidate):\n assert candidate(n = 1) == True\n assert candidate(n = 10) == False\n\n\ncheck(Solution().reorderedPowerOf2)"}
99
45
coding
Solve the programming task below in a Python markdown code block. Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. -----Input----- The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors. Then, k lines will follow. The i-th line will contain c_{i}, the number of balls of the i-th color (1 ≤ c_{i} ≤ 1000). The total number of balls doesn't exceed 1000. -----Output----- A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. -----Examples----- Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 -----Note----- In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3
{"inputs": ["1\n1\n", "1\n5\n", "1\n5\n", "1\n1\n", "1\n3\n", "1\n2\n", "1\n4\n", "1\n6\n"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
350
86
coding
Solve the programming task below in a Python markdown code block. Vasya had three strings $a$, $b$ and $s$, which consist of lowercase English letters. The lengths of strings $a$ and $b$ are equal to $n$, the length of the string $s$ is equal to $m$. Vasya decided to choose a substring of the string $a$, then choose a substring of the string $b$ and concatenate them. Formally, he chooses a segment $[l_1, r_1]$ ($1 \leq l_1 \leq r_1 \leq n$) and a segment $[l_2, r_2]$ ($1 \leq l_2 \leq r_2 \leq n$), and after concatenation he obtains a string $a[l_1, r_1] + b[l_2, r_2] = a_{l_1} a_{l_1 + 1} \ldots a_{r_1} b_{l_2} b_{l_2 + 1} \ldots b_{r_2}$. Now, Vasya is interested in counting number of ways to choose those segments adhering to the following conditions: segments $[l_1, r_1]$ and $[l_2, r_2]$ have non-empty intersection, i.e. there exists at least one integer $x$, such that $l_1 \leq x \leq r_1$ and $l_2 \leq x \leq r_2$; the string $a[l_1, r_1] + b[l_2, r_2]$ is equal to the string $s$. -----Input----- The first line contains integers $n$ and $m$ ($1 \leq n \leq 500\,000, 2 \leq m \leq 2 \cdot n$) — the length of strings $a$ and $b$ and the length of the string $s$. The next three lines contain strings $a$, $b$ and $s$, respectively. The length of the strings $a$ and $b$ is $n$, while the length of the string $s$ is $m$. All strings consist of lowercase English letters. -----Output----- Print one integer — the number of ways to choose a pair of segments, which satisfy Vasya's conditions. -----Examples----- Input 6 5 aabbaa baaaab aaaaa Output 4 Input 5 4 azaza zazaz azaz Output 11 Input 9 12 abcabcabc xyzxyzxyz abcabcayzxyz Output 2 -----Note----- Let's list all the pairs of segments that Vasya could choose in the first example: $[2, 2]$ and $[2, 5]$; $[1, 2]$ and $[2, 4]$; $[5, 5]$ and $[2, 5]$; $[5, 6]$ and $[3, 5]$;
{"inputs": ["1 2\nt\nt\ntt\n", "1 2\nj\nj\njj\n", "1 2\nj\nj\njj\n", "1 2\nt\nt\ntt\n", "5 4\nazaza\nzazaz\nazaz\n", "5 4\nazaza\nzazaz\nazaz\n", "6 5\naabbaa\nbaaaab\naaaaa\n", "6 5\naabbaa\nbaaaab\naaaaa\n"], "outputs": ["1\n", "1\n", "1", "1", "11\n", "11", "4\n", "4"]}
689
152
coding
Solve the programming task below in a Python markdown code block. You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q. For example, the string "Hello" for p = 2, q = 3 can be split to the two strings "Hel" and "lo" or to the two strings "He" and "llo". Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test). -----Input----- The first line contains three positive integers n, p, q (1 ≤ p, q ≤ n ≤ 100). The second line contains the string s consists of lowercase and uppercase latin letters and digits. -----Output----- If it's impossible to split the string s to the strings of length p and q print the only number "-1". Otherwise in the first line print integer k — the number of strings in partition of s. Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s — from left to right. If there are several solutions print any of them. -----Examples----- Input 5 2 3 Hello Output 2 He llo Input 10 9 5 Codeforces Output 2 Codef orces Input 6 4 5 Privet Output -1 Input 8 1 1 abacabac Output 8 a b a c a b a c
{"inputs": ["1 1 1\n1\n", "1 1 1\n1\n", "1 1 2\n1\n", "3 3 2\nzyx\n", "3 2 3\nejt\n", "3 2 2\nzyx\n", "3 2 3\nzyx\n", "3 2 2\naaa\n"], "outputs": ["1\n1\n", "1\n1\n", "1\n1\n", "1\nzyx\n", "1\nejt\n", "-1\n", "1\nzyx\n", "-1\n"]}
352
137
coding
Solve the programming task below in a Python markdown code block. Everything was going well in Wasseypur until Ramadhir Singh found out that Sahid Khan plans to kill him someday and takeover his mine. To protect himself, he decided to take preemptive measures and kill Sahid Khan first. However, Sahid Khan also learnt of Ramadhir Singh's plan to kill him, and soon Wasseypur was all set for bloodshed - but Chef intervened! Instead of resorting to violence, he convinced them to settle things by playing a game called the Game of Wasseypur. The game is as follows: There is a string S of length N consisting of only 0's and 1's. Players take turns moving - on his turn, a player chooses some block of S and erases it. The remaining parts of S are then concatenated together (without changing the order), and the other player then makes a move on this modified string. A block in the string is defined as a maximal non-empty substring consisting of the same character. For example, if S = 0001110110011, there are 6 blocks: in order from left to right, 000, 111, 0, 11, 00, 11. The player who is unable to make a move at the end loses the game and has to leave Wasseypur. Assuming that both players play optimally and Sahid Khan starts, find the winner of the game. ------ Input Format ------ - The first line of 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 a single integer N denoting the length of the string. - The second line of each test case contains the binary string S. ------ Output Format ------ For each test case, output a single line containing the string “SAHID” (without quotes) if Sahid Khan will be the winner, and “RAMADHIR” otherwise. Each character of the output can be printed either in lowercase or uppercase (so if Sahid wins, “SAHID”, “sahid” and “sAHiD” are all acceptable outputs). ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $1 ≤ N ≤ 10^{5}$ $S$ is a binary string, consisting of $0$ and $1$ only - Sum of $N$ over all test cases will not exceed $10^{6}$ ----- Sample Input 1 ------ 2 4 0000 4 0011 ----- Sample Output 1 ------ SAHID RAMADHIR ----- explanation 1 ------ Test case 1: There is only one block $0000$, which Sahid Khan will erase in the first move. Ramadhir Singh is then left unable to make a move and loses. Test case 2: There are two blocks- $00$ and $11$. Sahid has to erase any one of the two, following which Ramadhir will erase the remaining block and make the string empty. Now, Sahid cannot make a move and will lose the game.
{"inputs": ["2\n4\n0000\n4\n0011"], "outputs": ["SAHID\nRAMADHIR"]}
683
32
coding
Solve the programming task below in a Python markdown code block. Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequence will be [4, 2, 1]. You want to remove some elements from a in order to maximize f(a), using zero or more operations. Find the largest possible f(a). Input The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the initial length of the sequence. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 2 ⋅ 10^5) — the initial sequence a. Output Output an integer denoting the largest f(a) that can be obtained by doing zero or more operations. Examples Input 7 2 1 4 2 5 3 7 Output 3 Input 4 4 2 3 1 Output 2 Note In the first example, f(A) = 3 by doing the following operations. [2,1,4,2,5,3,7] → [2,1,2,5,3,7] → [1,2,5,3,7] → [1,2,5,3] → [1,2,3] In the second example, f(A) = 2 and no additional operation is needed.
{"inputs": ["1\n1\n", "1\n2\n", "1\n4\n", "1\n8\n", "1\n5\n", "1\n7\n", "1\n3\n", "1\n83\n"], "outputs": ["1\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
368
87
coding
Solve the programming task below in a Python markdown code block. Meg the Rabbit decided to do something nice, specifically — to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse — as a square of side n. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0, 0), (n, 0), (0, n) and (n, n). Input The single line contains 5 space-separated integers: n, x1, y1, x2, y2 (1 ≤ n ≤ 1000, 0 ≤ x1, y1, x2, y2 ≤ n) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square. Output You must print on a single line the shortest distance between the points. Examples Input 2 0 0 1 0 Output 1 Input 2 0 1 2 1 Output 4 Input 100 0 0 100 100 Output 200
{"inputs": ["5 1 5 2 5\n", "4 0 3 1 4\n", "5 4 0 5 1\n", "6 6 0 2 6\n", "5 4 0 5 2\n", "6 6 0 1 6\n", "2 0 1 2 0\n", "2 0 0 1 0\n"], "outputs": ["1\n", "2\n", "2\n", "10\n", "3\n", "11\n", "3\n", "1\n"]}
316
136
coding
Solve the programming task below in a Python markdown code block. The re.sub() tool (sub stands for substitution) evaluates a pattern and, for each valid match, it calls a method (or lambda). The method is called for all matches and can be used to modify strings in different ways. The re.sub() method returns the modified string as an output. Learn more about $re.sub()$. Transformation of Strings Code import re #Squaring numbers def square(match): number = int(match.group(0)) return str(number**2) print re.sub(r"\d+", square, "1 2 3 4 5 6 7 8 9") Output 1 4 9 16 25 36 49 64 81 Replacements in Strings _{Code} import re html = """ <head> <title>HTML</title> </head> <object type="application/x-flash" data="your-file.swf" width="0" height="0"> <!-- <param name="movie" value="your-file.swf" /> --> <param name="quality" value="high"/> </object> """ print re.sub("(<!--.*?-->)", "", html) #remove comment Output <head> <title>HTML</title> </head> <object type="application/x-flash" data="your-file.swf" width="0" height="0"> <param name="quality" value="high"/> </object> Task You are given a text of $N$ lines. The text contains && and || symbols. Your task is to modify those symbols to the following: && → and || → or Both && and || should have a space " " on both sides. Input Format The first line contains the integer, $N$. The next $N$ lines each contain a line of the text. Constraints $0<N<100$ Neither && nor || occur in the start or end of each line. Output Format Output the modified text. Sample Input 11 a = 1; b = input(); if a + b > 0 && a - b < 0: start() elif a*b > 10 || a/b < 1: stop() print set(list(a)) | set(list(b)) #Note do not change &&& or ||| or & or | #Only change those '&&' which have space on both sides. #Only change those '|| which have space on both sides. Sample Output a = 1; b = input(); if a + b > 0 and a - b < 0: start() elif a*b > 10 or a/b < 1: stop() print set(list(a)) | set(list(b)) #Note do not change &&& or ||| or & or | #Only change those '&&' which have space on both sides. #Only change those '|| which have space on both sides.
{"inputs": ["11\na = 1;\nb = input();\n\nif a + b > 0 && a - b < 0:\n start()\nelif a*b > 10 || a/b < 1:\n stop()\nprint set(list(a)) | set(list(b)) \n#Note do not change &&& or ||| or & or |\n#Only change those '&&' which have space on both sides.\n#Only change those '|| which have space on both sides.\n"], "outputs": ["a = 1;\nb = input();\n\nif a + b > 0 and a - b < 0:\n start()\nelif a*b > 10 or a/b < 1:\n stop()\nprint set(list(a)) | set(list(b)) \n#Note do not change &&& or ||| or & or |\n#Only change those '&&' which have space on both sides.\n#Only change those '|| which have space on both sides. \n"]}
648
229
coding
Solve the programming task below in a Python markdown code block. # Task You are given two string `a` an `s`. Starting with an empty string we can do the following two operations: ``` append the given string a to the end of the current string. erase one symbol of the current string.``` Your task is to find the least number of operations needed to construct the given string s. Assume that all the letters from `s` appear in `a` at least once. # Example For `a = "aba", s = "abbaab"`, the result should be `6`. Coinstruction: `"" -> "aba" -> "ab" -> "ababa" -> "abba" -> "abbaaba" -> "abbaab".` So, the result is 6. For `a = "aba", s = "a"`, the result should be `3`. Coinstruction: `"" -> "aba" -> "ab" -> "a".` So, the result is 3. For `a = "aba", s = "abaa"`, the result should be `4`. Coinstruction: `"" -> "aba" -> "abaaba" -> "abaab" -> "abaa".` So, the result is 4. # Input/Output - `[input]` string `a` string to be appended. Contains only lowercase English letters. 1 <= a.length <= 20 - `[input]` string `s` desired string containing only lowercase English letters. 1 <= s.length < 1000 - `[output]` an integer minimum number of operations Also feel free to reuse/extend the following starter code: ```python def string_constructing(a, s): ```
{"functional": "_inputs = [['aba', 'abbabba'], ['aba', 'abaa'], ['aba', 'a'], ['a', 'a'], ['a', 'aaa'], ['abcdefgh', 'hgfedcba']]\n_outputs = [[9], [4], [3], [1], [3], [64]]\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(string_constructing(*i), o[0])"}
388
210
coding
Solve the programming task below in a Python markdown code block. Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters. Let A be a set of positions in the string. Let's call it pretty if following conditions are met: letters on positions from A in the string are all distinct and lowercase; there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a_1 < j < a_2 for some a_1 and a_2 from A). Write a program that will determine the maximum number of elements in a pretty set of positions. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s. The second line contains a string s consisting of lowercase and uppercase Latin letters. -----Output----- Print maximum number of elements in pretty set of positions for string s. -----Examples----- Input 11 aaaaBaabAbA Output 2 Input 12 zACaAbbaazzC Output 3 Input 3 ABC Output 0 -----Note----- In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position. In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements. In the third example the given string s does not contain any lowercase letters, so the answer is 0.
{"inputs": ["1\na\n", "1\nk\n", "1\nH\n", "1\nk\n", "1\na\n", "1\nH\n", "1\nj\n", "1\nb\n"], "outputs": ["1\n", "1\n", "0\n", "1\n", "1\n", "0\n", "1\n", "1\n"]}
376
86
coding
Solve the programming task below in a Python markdown code block. A professor invented Cookie Breeding Machine for his students who like cookies very much. When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine, it consumes the cookie and generates two cookies with the taste of y and (x XOR y). Here, XOR represents Bitwise Exclusive OR. At first, there is only one cookie and the taste of it is D . Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times. 1. Put one of the cookies into the machine. 2. Input a non-negative integer less than or equal to 127 on the machine. Constraints * 1 \leq T \leq 1000 * 1 \leq N_t \leq 1000 (1 \leq t \leq T) * 1 \leq D_t \leq 127 (1 \leq t \leq T) Input The input is given from Standard Input in the following format: T N_1 D_1 : N_T D_T The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1. Each test case is given on the next T lines. In the t-th test case ( 1 \leq t \leq T ), N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space. Output For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line. Example Input 3 3 1 4 108 1 10 Output 255 400 10
{"inputs": ["3\n3 1\n4 4\n1 2", "3\n3 1\n4 50\n1 2", "3\n3 1\n4 75\n1 2", "3\n3 2\n2 98\n1 9", "3\n3 1\n4 75\n1 0", "3\n3 1\n4 64\n1 2", "3\n3 1\n4 37\n1 2", "3\n3 1\n4 89\n1 2"], "outputs": ["255\n504\n2\n", "255\n458\n2\n", "255\n433\n2\n", "256\n156\n9\n", "255\n433\n0\n", "255\n444\n2\n", "255\n471\n2\n", "255\n419\n2\n"]}
410
229
coding
Solve the programming task below in a Python markdown code block. Amidakuji is a traditional method of lottery in Japan. To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line. A valid amidakuji is an amidakuji that satisfies the following conditions: - No two horizontal lines share an endpoint. - The two endpoints of each horizontal lines must be at the same height. - A horizontal line must connect adjacent vertical lines. Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left. For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left. -----Constraints----- - H is an integer between 1 and 100 (inclusive). - W is an integer between 1 and 8 (inclusive). - K is an integer between 1 and W (inclusive). -----Input----- Input is given from Standard Input in the following format: H W K -----Output----- Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007. -----Sample Input----- 1 3 2 -----Sample Output----- 1 Only the following one amidakuji satisfies the condition:
{"inputs": ["4 3 1", "1 2 2", "1 5 1", "4 6 1", "1 9 1", "8 6 1", "1 9 2", "9 6 1"], "outputs": ["35\n", "1\n", "5\n", "12371\n", "34\n", "276953291\n", "21\n", "437876582\n"]}
371
117
coding
Solve the programming task below in a Python markdown code block. Flatland is a country with a number of cities, some of which have space stations. Cities are numbered consecutively and each has a road of $1km$ length connecting it to the next city. It is not a circular route, so the first city doesn't connect with the last city. Determine the maximum distance from any city to its nearest space station. Example $n=3$ $\boldsymbol{c}=[1]$ There are $n=3$ cities and city $1$ has a space station. They occur consecutively along a route. City $\mbox{o}$ is $1-0=1$ unit away and city $2$ is $2-1=1$ units away. City $1$ is $0$ units from its nearest space station as one is located there. The maximum distance is $1$. Function Description Complete the flatlandSpaceStations function in the editor below. flatlandSpaceStations has the following parameter(s): int n: the number of cities int c[m]: the indices of cities with a space station Returns - int: the maximum distance any city is from a space station Input Format The first line consists of two space-separated integers, $n$ and $m$. The second line contains $m$ space-separated integers, the indices of each city that has a space-station. These values are unordered and distinct. Constraints $1\leq n\leq10^5$ $1\leq m\leq n$ There will be at least $1$ city with a space station. No city has more than one space station. Output Format Sample Input 0 STDIN Function ----- -------- 5 2 n = 5, c[] size m = 2 0 4 c = [0, 4] Sample Output 0 2 Explanation 0 This sample corresponds to following graphic: The distance to the nearest space station for each city is listed below: $c\left[\textbf{0}\right]$ has distance $0 km$, as it contains a space station. $c\left[1\right]$ has distance $1\ km$ to the space station in $c\left[\textbf{0}\right]$. $c[2]$ has distance $2km$ to the space stations in $c\left[\textbf{0}\right]$ and $c\left[4\right]$. $c[3]$ has distance $1\ km$ to the space station in $c\left[4\right]$. $c\left[4\right]$ has distance $0 km$, as it contains a space station. We then take $max(0,1,2,1,0)=2$. Sample Input 1 6 6 0 1 2 4 3 5 Sample Output 1 0 Explanation 1 In this sample, $n=m$ so every city has space station and we print $0$ as our answer.
{"inputs": ["5 2\n0 4\n", "6 6\n0 1 2 4 3 5\n"], "outputs": ["2\n", "0\n"]}
660
42
coding
Solve the programming task below in a Python markdown code block. In mathematics, a **pandigital number** is a number that in a given base has among its significant digits each digit used in the base at least once. For example, 1234567890 is a pandigital number in base 10. For simplification, in this kata, we will consider pandigital numbers in *base 10* and with all digits used *exactly once*. The challenge is to calculate a sorted sequence of pandigital numbers, starting at a certain `offset` and with a specified `size`. Example: ```python > get_sequence(0, 5) [1023456789, 1023456798, 1023456879, 1023456897, 1023456978] ``` Rules: - We are looking for positive pandigital numbers in base 10. - Each digit should occur `exactly once`. - A pandigital number can't start with digit zero. - The offset is an integer (negative, zero or positive number) (long in Java) - The size is a positive integer number (int in Java) - Return the `size` pandigital numbers which are not smaller than the `offset`. If there is not enough `size` pandigital numbers, just return all of them. - Return an empty array if nothing is found. Also feel free to reuse/extend the following starter code: ```python def get_sequence(offset, size): ```
{"functional": "_inputs = [[0, 5], [5432160879, 3], [9876543000, 5], [9999999999, 1], [-123456789, 1], [-9999999999, 25]]\n_outputs = [[[1023456789, 1023456798, 1023456879, 1023456897, 1023456978]], [[5432160879, 5432160897, 5432160978]], [[9876543012, 9876543021, 9876543102, 9876543120, 9876543201]], [[]], [[1023456789]], [[1023456789, 1023456798, 1023456879, 1023456897, 1023456978, 1023456987, 1023457689, 1023457698, 1023457869, 1023457896, 1023457968, 1023457986, 1023458679, 1023458697, 1023458769, 1023458796, 1023458967, 1023458976, 1023459678, 1023459687, 1023459768, 1023459786, 1023459867, 1023459876, 1023465789]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_sequence(*i), o[0])"}
347
699
coding
Solve the programming task below in a Python markdown code block. You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate the volume of the given text. -----Input----- The first line contains one integer number n (1 ≤ n ≤ 200) — length of the text. The second line contains text of single-space separated words s_1, s_2, ..., s_{i}, consisting only of small and capital Latin letters. -----Output----- Print one integer number — volume of text. -----Examples----- Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 -----Note----- In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
{"inputs": ["1\ne\n", "1\nA\n", "1\ne\n", "1\nA\n", "1\nf\n", "1\ng\n", "1\nd\n", "1\nc\n"], "outputs": ["0\n", "1\n", "0\n", "1\n", "0\n", "0\n", "0\n", "0\n"]}
217
86
coding
Solve the programming task below in a Python markdown code block. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a_1, a_2, ..., a_{n}. The number a_{k} represents that the kth destination is at distance a_{k} kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. -----Input----- The first line contains integer n (2 ≤ n ≤ 10^5). Next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7). -----Output----- Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. -----Examples----- Input 3 2 3 5 Output 22 3 -----Note----- Consider 6 possible routes: [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5; [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7; [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7; [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8; [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9; [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8. The average travel distance is $\frac{1}{6} \cdot(5 + 7 + 7 + 8 + 9 + 8)$ = $\frac{44}{6}$ = $\frac{22}{3}$.
{"inputs": ["3\n2 3 5\n", "3\n2 3 8\n", "3\n4 3 8\n", "3\n3 3 8\n", "3\n5 3 8\n", "3\n6 3 8\n", "3\n2 3 5\n", "3\n6 3 11\n"], "outputs": ["22 3", "37 3\n", "35 3\n", "34 3\n", "12 1\n", "37 3\n", "22 3", "52 3\n"]}
700
141
coding
Solve the programming task below in a Python markdown code block. Chef is standing at coordinate A while Chefina is standing at coordinate B. In one step, Chef can increase or decrease his coordinate by at most K. Determine the minimum number of steps required by Chef to reach Chefina. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of three integers X, Y, and K, the initial coordinate of Chef, the initial coordinate of Chefina and the maximum number of coordinates Chef can move in one step. ------ Output Format ------ For each test case, output the minimum number of steps required by Chef to reach Chefina. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ X, Y ≤ 100$ $1 ≤ K ≤ 100$ ----- Sample Input 1 ------ 4 10 20 3 36 36 5 50 4 100 30 4 2 ----- Sample Output 1 ------ 4 0 1 13 ----- explanation 1 ------ Test case $1$: In the first three steps, Chef increases his coordinate by $K = 3$. In the fourth step, Chef increases his coordinate by $1$ which is less than equal to $K$. It can be shown that this is the minimum number of steps required by Chef. Test case $2$: Chef is already at the same coordinate as Chefina. Thus, he needs $0$ steps. Test case $3$: Chef can use $1$ step to decrease his coordinate by $46$ which is less than $K = 100$ and reach Chefina. Test case $4$: Chef can use $13$ steps to decrease his coordinate by $K = 2$ and reach the coordinate $30-13\cdot 2 = 4$.
{"inputs": ["4\n10 20 3\n36 36 5\n50 4 100\n30 4 2\n"], "outputs": ["4\n0\n1\n13\n"]}
414
53
coding
Solve the programming task below in a Python markdown code block. For given integer n, count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n. Input n An integer n (1 ≤ n ≤ 1000000000). Output The number of totatives in a line. Examples Input 6 Output 2 Input 1000000 Output 400000
{"inputs": ["5", "2", "3", "6", "16", "31", "61", "59"], "outputs": ["4\n", "1\n", "2\n", "2", "8\n", "30\n", "60\n", "58\n"]}
107
68
coding
Solve the programming task below in a Python markdown code block. A bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. The teacher gave Dmitry's class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know. Dima suspects now that he simply missed the word "correct" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation. The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes $l$ nanoseconds, where $l$ is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for "))((" he can choose the substring ")(" and do reorder ")()(" (this operation will take $2$ nanoseconds). Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^6$) — the length of Dima's sequence. The second line contains string of length $n$, consisting of characters "(" and ")" only. -----Output----- Print a single integer — the minimum number of nanoseconds to make the sequence correct or "-1" if it is impossible to do so. -----Examples----- Input 8 ))((())( Output 6 Input 3 (() Output -1 -----Note----- In the first example we can firstly reorder the segment from first to the fourth character, replacing it with "()()", the whole sequence will be "()()())(". And then reorder the segment from the seventh to eighth character, replacing it with "()". In the end the sequence will be "()()()()", while the total time spent is $4 + 2 = 6$ nanoseconds.
{"inputs": ["1\n(\n", "1\n(\n", "1\n)\n", "2\n((\n", "2\n((\n", "2\n()\n", "3\n(()\n", "3\n((*\n"], "outputs": ["-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "0\n", "-1\n", "-1\n"]}
539
91
coding
Solve the programming task below in a Python markdown code block. Bob has got some injury in his leg and due to this he can take exactly M steps in one move. Bob enters a square field of size NxN. The field is only having one gate(for both entrance and exit) at its one of the corners. Bob started walking along the perimeter of square field.(remember Bob can only take exactly M steps in one move and cannot reverse his direction of motion). Bob wants to know how many minimum number of moves he needs to come out(i.e. he reaches the same gate from where he entered into the field) from the square field. Tell the answer to Bob ASAP. Luckily, you came to know M=N+1. -----Input----- - The first line of the input contains an integer T denoting the number of test cases. - Each test case contains a single integer N denoting the sides of the square. -----Output----- - For each test case, output a single line containing minimum number of moves Bob required to come out from the field. -----Constraints----- - 1 ≤ T ≤ 10000 - 1 ≤ N ≤ 1000000000 -----Example----- Input: 2 1 2 Output: 2 8 -----Explanation----- Example case 1.Let four corners of square be (0,0), (0,1), (1,1), (1,0). Let gate be at (0,0). Bob takes 2 steps in one move. Let movement of Bob be as follows (0,0) -> (1,1) -> (0,0). Thus minimum moves needed were 2. Example case 2.Let four corners of square be (0,0), (0,2), (2,2), (2,0). Let gate be at (0,0). Bob takes 3 steps in one move. Let movement of Bob be as follows (0,0) -> (2,1) -> (0,2) -> (1,0) -> (2,2) -> (0,1) -> (2,0) -> (1,2) -> (0,0). Thus minimum number of moves needed are 8.
{"inputs": ["2\n1\n2"], "outputs": ["2\n8"]}
471
18
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string. Note that the word should be built from left to right with each additional character being added to the end of a previous word.    Please complete the following python code precisely: ```python class Solution: def longestWord(self, words: List[str]) -> str: ```
{"functional": "def check(candidate):\n assert candidate(words = [\"w\",\"wo\",\"wor\",\"worl\", \"world\"]) == \"world\"\n assert candidate(words = [\"a\", \"banana\", \"app\", \"appl\", \"ap\", \"apply\", \"apple\"]) == \"apple\"\n\n\ncheck(Solution().longestWord)"}
138
78
coding
Solve the programming task below in a Python markdown code block. Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of set elements and their sum to be k and s, respectively, sets satisfying the conditions are limited. When n = 9, k = 3 and s = 23, {6, 8, 9} is the only such set. There may be more than one such set, in general, however. When n = 9, k = 3 and s = 22, both {5, 8, 9} and {6, 7, 9} are possible. You have to write a program that calculates the number of the sets that satisfy the given conditions. Input The input consists of multiple datasets. The number of datasets does not exceed 100. Each of the datasets has three integers n, k and s in one line, separated by a space. You may assume 1 ≤ n ≤ 20, 1 ≤ k ≤ 10 and 1 ≤ s ≤ 155. The end of the input is indicated by a line containing three zeros. Output The output for each dataset should be a line containing a single integer that gives the number of the sets that satisfy the conditions. No other characters should appear in the output. You can assume that the number of sets does not exceed 231 - 1. Example Input 9 3 23 9 3 22 10 3 28 16 10 107 20 8 102 20 10 105 20 10 155 3 4 3 4 2 11 0 0 0 Output 1 2 0 20 1542 5448 1 0 0
{"inputs": ["9 1 31\n2 3 3\n3 3 19\n16 10 3\n20 3 45\n20 8 19\n20 20 155\n3 6 6\n3 3 15\n0 0 0", "9 3 31\n2 3 3\n10 3 28\n16 6 87\n20 3 102\n20 8 19\n20 20 155\n3 6 6\n3 3 15\n0 0 0", "9 3 5\n9 1 22\n10 3 28\n16 10 107\n26 1 102\n20 10 21\n20 10 2\n3 4 3\n4 2 6\n0 0 0", "9 6 23\n2 3 3\n8 3 41\n16 10 87\n20 3 102\n20 2 19\n20 20 155\n4 6 6\n3 3 15\n0 0 0", "9 3 23\n2 3 22\n10 3 5\n16 10 63\n7 8 71\n20 8 19\n20 20 155\n3 12 4\n2 2 14\n0 0 0", "9 3 23\n14 3 22\n1 3 28\n16 5 107\n20 8 102\n2 1 105\n20 7 155\n4 5 1\n4 2 6\n0 0 0", "9 3 23\n14 3 22\n1 3 28\n16 5 107\n20 8 108\n2 1 105\n20 7 155\n4 5 1\n4 2 6\n0 0 0", "9 6 14\n2 3 3\n8 3 41\n16 10 87\n9 3 167\n20 2 19\n20 20 155\n4 12 6\n5 3 15\n0 0 0"], "outputs": ["0\n0\n0\n0\n19\n0\n0\n0\n0\n", "0\n0\n0\n0\n0\n0\n0\n0\n0\n", "0\n0\n0\n20\n0\n0\n0\n0\n1\n", "2\n0\n0\n330\n0\n9\n0\n0\n0\n", "1\n0\n0\n20\n0\n0\n0\n0\n0\n", "1\n21\n0\n0\n1542\n0\n0\n0\n1\n", "1\n21\n0\n0\n738\n0\n0\n0\n1\n", "0\n0\n0\n330\n0\n9\n0\n0\n0\n"]}
458
786
coding
Solve the programming task below in a Python markdown code block. Most of this problem is by the original author of [the harder kata](https://www.codewars.com/kata/556206664efbe6376700005c), I just made it simpler. I read a book recently, titled "Things to Make and Do in the Fourth Dimension" by comedian and mathematician Matt Parker ( [Youtube](https://www.youtube.com/user/standupmaths) ), and in the first chapter of the book Matt talks about problems he likes to solve in his head to take his mind off the fact that he is in his dentist's chair, we've all been there! The problem he talks about relates to polydivisible numbers, and I thought a kata should be written on the subject as it's quite interesting. (Well it's interesting to me, so there!) ### Polydivisib... huh what? So what are they? A polydivisible number is divisible in an unusual way. The first digit is cleanly divisible by `1`, the first two digits are cleanly divisible by `2`, the first three by `3`, and so on. ### Examples Let's take the number `1232` as an example. ``` 1 / 1 = 1 // Works 12 / 2 = 6 // Works 123 / 3 = 41 // Works 1232 / 4 = 308 // Works ``` `1232` is a polydivisible number. However, let's take the number `123220` and see what happens. ``` 1 /1 = 1 // Works 12 /2 = 6 // Works 123 /3 = 41 // Works 1232 /4 = 308 // Works 12322 /5 = 2464.4 // Doesn't work 123220 /6 = 220536.333... // Doesn't work ``` `123220` is not polydivisible. ### Your job: check if a number is polydivisible or not. Return `true` if it is, and `false` if it isn't. Note: All inputs will be valid numbers between `0` and `2^53-1 (9,007,199,254,740,991)` (inclusive). Note: All single digit numbers (including `0`) are trivially polydivisible. Note: Except for `0`, no numbers will start with `0`. Also feel free to reuse/extend the following starter code: ```python def polydivisible(x): ```
{"functional": "_inputs = [[1232], [123220], [0], [1], [141], [1234], [21234], [81352], [987654], [1020005], [9876545], [381654729], [1073741823]]\n_outputs = [[True], [False], [True], [True], [True], [False], [False], [False], [True], [True], [True], [True], [False]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(polydivisible(*i), o[0])"}
620
282
coding
Solve the programming task below in a Python markdown code block. Let's call a sequence good if the sum of all its elements is $0$. You have a sequence of integers $A_1, A_2, \ldots, A_N$. You may perform any number of operations on this sequence (including zero). In one operation, you should choose a valid index $i$ and decrease $A_i$ by $i$. Can you make the sequence good using these operations? -----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 a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing the string "YES" if it is possible to make the given sequence good or "NO" if it is impossible. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 10$ - $|A_i| \le 100$ for each valid $i$ -----Subtasks----- Subtask #1 (10 points): $N = 1$ Subtask #2 (30 points): $N \le 2$ Subtask #3 (60 points): original constraints -----Example Input----- 2 1 -1 2 1 2 -----Example Output----- NO YES -----Explanation----- Example case 2: We can perform two operations ― subtract $1$ from $A_1$ and $2$ from $A_2$.
{"inputs": ["2\n1\n-1\n2\n1 2"], "outputs": ["NO\nYES"]}
371
25
coding
Solve the programming task below in a Python markdown code block. You are given $n$ chips on a number line. The $i$-th chip is placed at the integer coordinate $x_i$. Some chips can have equal coordinates. You can perform each of the two following types of moves any (possibly, zero) number of times on any chip: Move the chip $i$ by $2$ to the left or $2$ to the right for free (i.e. replace the current coordinate $x_i$ with $x_i - 2$ or with $x_i + 2$); move the chip $i$ by $1$ to the left or $1$ to the right and pay one coin for this move (i.e. replace the current coordinate $x_i$ with $x_i - 1$ or with $x_i + 1$). Note that it's allowed to move chips to any integer coordinate, including negative and zero. Your task is to find the minimum total number of coins required to move all $n$ chips to the same coordinate (i.e. all $x_i$ should be equal after some sequence of moves). -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 100$) — the number of chips. The second line of the input contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is the coordinate of the $i$-th chip. -----Output----- Print one integer — the minimum total number of coins required to move all $n$ chips to the same coordinate. -----Examples----- Input 3 1 2 3 Output 1 Input 5 2 2 2 3 3 Output 2 -----Note----- In the first example you need to move the first chip by $2$ to the right and the second chip by $1$ to the right or move the third chip by $2$ to the left and the second chip by $1$ to the left so the answer is $1$. In the second example you need to move two chips with coordinate $3$ by $1$ to the left so the answer is $2$.
{"inputs": ["1\n1\n", "1\n5\n", "1\n2\n", "1\n2\n", "1\n5\n", "1\n1\n", "1\n3\n", "1\n0\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
489
86
coding
Solve the programming task below in a Python markdown code block. Salmon received an array $A$ of size $N$ and an array $B$ of size $M$ for Christmas. He would like to move all the elements in $B$ to $A$ using a special method described below. While $B$ is not empty, Salmon does the following operations in order: * Remove and store the leftmost element off of B (let's call this element $X$). * Place $X$ at the start of $A$. * Keep swapping $X$ and the element to the right of it repeatedly until $X$ is at the rightmost position of $A$ or $X$ is greater or equal to the element to the right of it. Before performing the operations, Salmon can rearrange both A and B in any order once. He would like to do as little work as possible -- determine the minimum possible amount of swaps. ------ Input: ------ The first line of input will consist of a two space-separated integers, $N$ and $M$ ($1 ≤ N, M ≤ 2*10^{5}$). The next line will consist of $N$ space-separated integers, with the $i$-th input representing $A[i]$ ($-10^{9} ≤ A[i] ≤ 10^{9}$). The next line will consist of $M$ space-separated integers, with the $i$-th input representing $B[i]$ ($-10^{9} ≤ B[i] ≤ 10^{9}$). ------ Output: ------ Output a single integer, the minimum number of swaps required. ------ Subtasks: ------ * Subtask 1 [15 points]: $A[i] = 1, B[i] = 1$ for all $i$. * Subtask 2 [30 points]: $N, M ≤ 10^{3}$. * Subtask 3 [55 points]: No additional constraints. ------ Note: ------ The answer may be greater than the limit for a 32-bit integer. Use long long instead of int for C++ and long for Java. Please check your language documentation for other languages on how to use 64-bit integers. Note that Python solutions won't encounter a problem as it uses BigIntegers by default. ----- Sample Input 1 ------ 5 3 10 5 2 5 4 3 1 2 ----- Sample Output 1 ------ 5 ----- explanation 1 ------ Salmon can reorder $A$ to be $[2, 10, 5, 4, 5]$ and $B$ to be $[1, 2, 3]$.
{"inputs": ["5 3\n10 5 2 5 4\n3 1 2"], "outputs": ["5"]}
574
31
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given a 0-indexed integer array nums and an integer k. You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive. You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array. Return the maximum score you can get.   Please complete the following python code precisely: ```python class Solution: def maxResult(self, nums: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [1,-1,-2,4,-7,3], k = 2) == 7\n assert candidate(nums = [10,-5,-2,4,0,3], k = 3) == 17\n assert candidate(nums = [1,-5,-20,4,-1,3,-6,-3], k = 2) == 0\n\n\ncheck(Solution().maxResult)"}
176
107
coding
Solve the programming task below in a Python markdown code block. The marmots need to prepare k problems for HC^2 over n days. Each problem, once prepared, also has to be printed. The preparation of a problem on day i (at most one per day) costs a_{i} CHF, and the printing of a problem on day i (also at most one per day) costs b_{i} CHF. Of course, a problem cannot be printed before it has been prepared (but doing both on the same day is fine). What is the minimum cost of preparation and printing? -----Input----- The first line of input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 2200). The second line contains n space-separated integers a_1, ..., a_{n} ($1 \leq a_{i} \leq 10^{9}$) — the preparation costs. The third line contains n space-separated integers b_1, ..., b_{n} ($1 \leq b_{i} \leq 10^{9}$) — the printing costs. -----Output----- Output the minimum cost of preparation and printing k problems — that is, the minimum possible sum a_{i}_1 + a_{i}_2 + ... + a_{i}_{k} + b_{j}_1 + b_{j}_2 + ... + b_{j}_{k}, where 1 ≤ i_1 < i_2 < ... < i_{k} ≤ n, 1 ≤ j_1 < j_2 < ... < j_{k} ≤ n and i_1 ≤ j_1, i_2 ≤ j_2, ..., i_{k} ≤ j_{k}. -----Example----- Input 8 4 3 8 7 9 9 4 6 8 2 5 9 4 3 8 9 1 Output 32 -----Note----- In the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8.
{"inputs": ["1 1\n96\n86\n", "1 1\n96\n86\n", "1 1\n71\n86\n", "1 1\n50\n86\n", "1 1\n126\n86\n", "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n", "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 0\n", "8 4\n3 3 7 9 9 4 6 8\n2 5 9 4 3 8 9 0\n"], "outputs": ["182", "182\n", "157\n", "136\n", "212\n", "32", "31\n", "26\n"]}
489
224
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have. Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise. Note that multiple kids can have the greatest number of candies.   Please complete the following python code precisely: ```python class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: ```
{"functional": "def check(candidate):\n assert candidate(candies = [2,3,5,1,3], extraCandies = 3) == [True,True,True,False,True] \n assert candidate(candies = [4,2,1,1,2], extraCandies = 1) == [True,False,False,False,False] \n assert candidate(candies = [12,1,12], extraCandies = 10) == [True,False,True]\n\n\ncheck(Solution().kidsWithCandies)"}
167
126
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi. If a trust relationship does not exist in trust array, then such a trust relationship does not exist. Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.   Please complete the following python code precisely: ```python class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(n = 2, trust = [[1,2]]) == 2\n assert candidate(n = 3, trust = [[1,3],[2,3]]) == 3\n assert candidate(n = 3, trust = [[1,3],[2,3],[3,1]]) == -1\n\n\ncheck(Solution().findJudge)"}
195
88
coding
Solve the programming task below in a Python markdown code block. Doremy has $n$ buckets of paint which is represented by an array $a$ of length $n$. Bucket $i$ contains paint with color $a_i$. Let $c(l,r)$ be the number of distinct elements in the subarray $[a_l,a_{l+1},\ldots,a_r]$. Choose $2$ integers $l$ and $r$ such that $l \leq r$ and $r-l-c(l,r)$ is maximized. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 10^4$) — 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 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$). It is guaranteed that the sum of $n$ does not exceed $10^5$. -----Output----- For each test case, output $l$ and $r$ such that $l \leq r$ and $r-l-c(l,r)$ is maximized. If there are multiple solutions, you may output any. -----Examples----- Input 7 5 1 3 2 2 4 5 1 2 3 4 5 4 2 1 2 1 3 2 3 3 2 2 2 1 1 9 9 8 5 2 1 1 2 3 3 Output 2 4 1 5 1 4 2 3 1 2 1 1 3 9 -----Note----- In the first test case, $a=[1,3,2,2,4]$. When $l=1$ and $r=3$, $c(l,r)=3$ (there are $3$ distinct elements in $[1,3,2]$). When $l=2$ and $r=4$, $c(l,r)=2$ (there are $2$ distinct elements in $[3,2,2]$). It can be shown that choosing $l=2$ and $r=4$ maximizes the value of $r-l-c(l,r)$ at $0$. For the second test case, $a=[1,2,3,4,5]$. When $l=1$ and $r=5$, $c(l,r)=5$ (there are $5$ distinct elements in $[1,2,3,4,5]$). When $l=3$ and $r=3$, $c(l,r)=1$ (there is $1$ distinct element in $[3]$). It can be shown that choosing $l=1$ and $r=5$ maximizes the value of $r-l-c(l,r)$ at $-1$. Choosing $l=3$ and $r=3$ is also acceptable.
{"inputs": ["7\n5\n1 3 2 2 4\n5\n1 2 3 4 5\n4\n2 1 2 1\n3\n2 3 3\n2\n2 2\n1\n1\n9\n9 8 5 2 1 1 2 3 3\n"], "outputs": ["1 5\n1 5\n1 4\n1 3\n1 2\n1 1\n1 9\n"]}
693
112
coding
Solve the programming task below in a Python markdown code block. Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1, 3, 9]$ and $[2, 3, 1]$ are not. It follows from the definition that any sequence of length one or two is an arithmetic progression. Polycarp found some sequence of positive integers $[b_1, b_2, \dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged. Determine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible. It is possible that the resulting sequence contains element equals $0$. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 100\,000)$ — the number of elements in $b$. The second line contains a sequence $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 10^{9})$. -----Output----- If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position). -----Examples----- Input 4 24 21 14 10 Output 3 Input 2 500 500 Output 0 Input 3 14 5 1 Output -1 Input 5 1 3 6 9 12 Output 1 -----Note----- In the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression. In the second example Polycarp should not change anything, because his sequence is an arithmetic progression. In the third example it is impossible to make an arithmetic progression. In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, which is an arithmetic progression.
{"inputs": ["3\n1 2 1\n", "3\n2 1 2\n", "3\n5 9 5\n", "3\n3 1 2\n", "3\n1 9 4\n", "3\n2 1 3\n", "3\n9 3 6\n", "3\n2 1 2\n"], "outputs": ["1\n", "1\n", "-1\n", "2\n", "-1\n", "2\n", "-1\n", "1\n"]}
708
118
coding
Solve the programming task below in a Python markdown code block. Your task is to calculate the sum (indicated by S) of all the k-balanced integers between [L, R]. An integer is called k-balanced when either of #1 or #2 below holds true. The length of the integer <= k Sum of the first k digits (with no leading zeros) is equal to the sum of the last k digits. Input format L R k Output format S % 1000000007 Constraints 0 < L <= R < 10^18 0 < k <= 18 Sample Input 9 23 1 Sample Output 42 Explanation 9, 11 and 22 are the only 3 integers between 9 and 23 ( both included ) that are k-balanced. Hence, the answer is 9+11+22=42
{"inputs": ["9 23 1\n"], "outputs": ["42\n"]}
203
20
coding
Solve the programming task below in a Python markdown code block. The *beauty* value of an array is defined as the difference between the largest and second largest elements of the array. Note that the largest and second largest elements can have the same value in case of duplicates. For example, *beauty* value of [2, 5, 3, 1] = 5 - 3 = 2 and *beauty* value of [7, 6, 7] = 7 - 7 = 0 You are given an array A of length N. Your task is to find the total number of distinct *beauty* values among all *subarrays* of A having length greater than 1. Note that, a subarray is obtained by deleting some (possibly zero) elements from the beginning and some (possibly zero) elements from the end of the array. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains a single integer N — the size of the array. - The second line contains N space-separated numbers - A_{1}, A_{2},\ldots ,A_{N}, the elements of the array. ------ Output Format ------ For each test case, output a single line, the total number of distinct beauty among all *subarrays* of A having length greater than 1. ------ Constraints ------ $1 ≤ T ≤ 10^{4}$ $2 ≤ N ≤ 2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 4 2 1 1 3 4 2 1 4 8 1 7 2 5 6 9 4 2 1 ----- Sample Output 1 ------ 1 2 4 4 ----- explanation 1 ------ Test case $1$: The only subarray is $[1,1]$ whose beauty is $0$. Thus, there is only $1$ distinct value of beauty. Test case $2$: The subarrays are $[4,2], [2,1],$ and $[4,2,1]$ having beauty $2, 1,$ and $2$ respectively. There are $2$ distinct values of beauty. Test case $3$: The unique values of beauty are $7,1,6,$ and $5$. Test case $4$: The unique values of beauty are $3,5,2,$ and $1$.
{"inputs": ["4\n2\n1 1\n3\n4 2 1\n4\n8 1 7 2\n5\n6 9 4 2 1\n"], "outputs": ["1\n2\n4\n4\n"]}
580
56
coding
Solve the programming task below in a Python markdown code block. For an integer ```k``` rearrange all the elements of the given array in such way, that: all elements that are less than ```k``` are placed before elements that are not less than ```k```; all elements that are less than ```k``` remain in the same order with respect to each other; all elements that are not less than ```k``` remain in the same order with respect to each other. For ```k = 6``` and ```elements = [6, 4, 10, 10, 6]```, the output should be ```splitByValue(k, elements) = [4, 6, 10, 10, 6]```. For ```k``` = 5 and ```elements = [1, 3, 5, 7, 6, 4, 2]```, the output should be ```splitByValue(k, elements) = [1, 3, 4, 2, 5, 7, 6]```. S: codefights.com Also feel free to reuse/extend the following starter code: ```python def split_by_value(k, elements): ```
{"functional": "_inputs = [[5, [1, 3, 5, 7, 6, 4, 2]], [0, [5, 2, 7, 3, 2]], [6, [6, 4, 10, 10, 6]], [1, [3, 2, 8, 3, 2, 1]], [10, [9, 2, 4, 3, 4]]]\n_outputs = [[[1, 3, 4, 2, 5, 7, 6]], [[5, 2, 7, 3, 2]], [[4, 6, 10, 10, 6]], [[3, 2, 8, 3, 2, 1]], [[9, 2, 4, 3, 4]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(split_by_value(*i), o[0])"}
262
337
coding
Solve the programming task below in a Python markdown code block. This is a hard version of the problem. In this version, there are two colors of the cards. Alice has $n$ cards, each card is either black or white. The cards are stacked in a deck in such a way that the card colors alternate, starting from a white card. Alice deals the cards to herself and to Bob, dealing at once several cards from the top of the deck in the following order: one card to herself, two cards to Bob, three cards to Bob, four cards to herself, five cards to herself, six cards to Bob, seven cards to Bob, eight cards to herself, and so on. In other words, on the $i$-th step, Alice deals $i$ top cards from the deck to one of the players; on the first step, she deals the cards to herself and then alternates the players every two steps. When there aren't enough cards at some step, Alice deals all the remaining cards to the current player, and the process stops. First Alice's steps in a deck of many cards. How many cards of each color will Alice and Bob have at the end? -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 200$). The description of the test cases follows The only line of each test case contains a single integer $n$ ($1 \le n \le 10^6$) — the number of cards. -----Output----- For each test case print four integers — the number of cards in the end for each player — in this order: white cards Alice has, black cards Alice has, white cards Bob has, black cards Bob has. -----Examples----- Input 5 10 6 17 8 1000000 Output 3 2 2 3 1 0 2 3 6 4 3 4 2 1 2 3 250278 249924 249722 250076 -----Note----- None
{"inputs": ["5\n10\n6\n17\n8\n1000000\n"], "outputs": ["3 2 2 3\n1 0 2 3\n6 4 3 4\n2 1 2 3\n250278 249924 249722 250076\n"]}
458
90
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 arr consisting of n positive integers, and a positive integer k. The array arr is called K-increasing if arr[i-k] <= arr[i] holds for every index i, where k <= i <= n-1. For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because: arr[0] <= arr[2] (4 <= 5) arr[1] <= arr[3] (1 <= 2) arr[2] <= arr[4] (5 <= 6) arr[3] <= arr[5] (2 <= 2) However, the same arr is not K-increasing for k = 1 (because arr[0] > arr[1]) or k = 3 (because arr[0] > arr[3]). In one operation, you can choose an index i and change arr[i] into any positive integer. Return the minimum number of operations required to make the array K-increasing for the given k.   Please complete the following python code precisely: ```python class Solution: def kIncreasing(self, arr: List[int], k: int) -> int: ```
{"functional": "def check(candidate):\n assert candidate(arr = [5,4,3,2,1], k = 1) == 4\n assert candidate(arr = [4,1,5,2,6,2], k = 2) == 0\n assert candidate(arr = [4,1,5,2,6,2], k = 3) == 2\n\n\ncheck(Solution().kIncreasing)"}
283
98
coding
Solve the programming task below in a Python markdown code block. In Aramic language words can only represent objects. Words in Aramic have special properties: A word is a root if it does not contain the same letter more than once. A root and all its permutations represent the same object. The root $x$ of a word $y$ is the word that contains all letters that appear in $y$ in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab". Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script? -----Input----- The first line contains one integer $n$ ($1 \leq n \leq 10^3$) — the number of words in the script. The second line contains $n$ words $s_1, s_2, \ldots, s_n$ — the script itself. The length of each string does not exceed $10^3$. It is guaranteed that all characters of the strings are small latin letters. -----Output----- Output one integer — the number of different objects mentioned in the given ancient Aramic script. -----Examples----- Input 5 a aa aaa ab abb Output 2 Input 3 amer arem mrea Output 1 -----Note----- In the first test, there are two objects mentioned. The roots that represent them are "a","ab". In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer".
{"inputs": ["3\namer arem mrea\n", "3\namer mera mrea\n", "3\nrema mera mrea\n", "3\nrema mera erma\n", "3\nrema arem erma\n", "3\nrema aerm erma\n", "3\namer arem mrea\n", "5\na aa aaa ab abb\n"], "outputs": ["1", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "2"]}
373
116
coding
Solve the programming task below in a Python markdown code block. Adilbek was assigned to a special project. For Adilbek it means that he has $n$ days to run a special program and provide its results. But there is a problem: the program needs to run for $d$ days to calculate the results. Fortunately, Adilbek can optimize the program. If he spends $x$ ($x$ is a non-negative integer) days optimizing the program, he will make the program run in $\left\lceil \frac{d}{x + 1} \right\rceil$ days ($\left\lceil a \right\rceil$ is the ceiling function: $\left\lceil 2.4 \right\rceil = 3$, $\left\lceil 2 \right\rceil = 2$). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to $x + \left\lceil \frac{d}{x + 1} \right\rceil$. Will Adilbek be able to provide the generated results in no more than $n$ days? -----Input----- The first line contains a single integer $T$ ($1 \le T \le 50$) — the number of test cases. The next $T$ lines contain test cases – one per line. Each line contains two integers $n$ and $d$ ($1 \le n \le 10^9$, $1 \le d \le 10^9$) — the number of days before the deadline and the number of days the program runs. -----Output----- Print $T$ answers — one per test case. For each test case print YES (case insensitive) if Adilbek can fit in $n$ days or NO (case insensitive) otherwise. -----Example----- Input 3 1 1 4 5 5 11 Output YES YES NO -----Note----- In the first test case, Adilbek decides not to optimize the program at all, since $d \le n$. In the second test case, Adilbek can spend $1$ day optimizing the program and it will run $\left\lceil \frac{5}{2} \right\rceil = 3$ days. In total, he will spend $4$ days and will fit in the limit. In the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program $2$ days, it'll still work $\left\lceil \frac{11}{2+1} \right\rceil = 4$ days.
{"inputs": ["1\n7 16\n", "1\n7 16\n", "1\n4 16\n", "1\n17 81\n", "1\n17 81\n", "1\n23 28\n", "1\n11 81\n", "1\n40 28\n"], "outputs": ["YES\n", "YES\n", "NO\n", "YES\n", "YES\n", "YES\n", "NO\n", "YES\n"]}
563
115
coding
Please solve the programming task below using a self-contained code snippet in a markdown code block. You are given an integer array nums. You can choose exactly one index (0-indexed) and remove the element. Notice that the index of the elements may change after the removal. For example, if nums = [6,1,7,4,1]: Choosing to remove index 1 results in nums = [6,7,4,1]. Choosing to remove index 2 results in nums = [6,1,4,1]. Choosing to remove index 4 results in nums = [6,1,7,4]. An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values. Return the number of indices that you could choose such that after the removal, nums is fair.   Please complete the following python code precisely: ```python class Solution: def waysToMakeFair(self, nums: List[int]) -> int: ```
{"functional": "def check(candidate):\n assert candidate(nums = [2,1,6,4]) == 1\n assert candidate(nums = [1,1,1]) == 3\n assert candidate(nums = [1,2,3]) == 0\n\n\ncheck(Solution().waysToMakeFair)"}
205
71
coding
Solve the programming task below in a Python markdown code block. Let s(x) be sum of digits in decimal representation of positive integer x. Given two integers n and m, find some positive integers a and b such that * s(a) ≥ n, * s(b) ≥ n, * s(a + b) ≤ m. Input The only line of input contain two integers n and m (1 ≤ n, m ≤ 1129). Output Print two lines, one for decimal representation of a and one for decimal representation of b. Both numbers must not contain leading zeros and must have length no more than 2230. Examples Input 6 5 Output 6 7 Input 8 16 Output 35 53 Note In the first sample, we have n = 6 and m = 5. One valid solution is a = 6, b = 7. Indeed, we have s(a) = 6 ≥ n and s(b) = 7 ≥ n, and also s(a + b) = s(13) = 4 ≤ m.
{"inputs": ["4 1\n", "1 1\n", "9 1\n", "4 3\n", "3 1\n", "2 1\n", "7 1\n", "4 2\n"], "outputs": ["1111\n8889", "1\n9", "111111111\n888888889", "1111\n8889", "111\n889", "11\n89", "1111111\n8888889", "1111\n8889"]}
244
146
coding
Solve the programming task below in a Python markdown code block. Find sum of all the numbers that are multiples of 10 and are less than or equal to a given number "N". (quotes for clarity and be careful of integer overflow) -----Input----- Input will start with an integer T the count of test cases, each case will have an integer N. -----Output----- Output each values, on a newline. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤1000000000 -----Example----- Input: 1 10 Output: 10 -----Explanation----- Example case 1. Only integer that is multiple 10 that is less than or equal to 10 is 10
{"inputs": ["1\n10"], "outputs": ["10"]}
160
16
coding
Solve the programming task below in a Python markdown code block. There is an integer sequence of length 2^N: A_0, A_1, ..., A_{2^N-1}. (Note that the sequence is 0-indexed.) For every integer K satisfying 1 \leq K \leq 2^N-1, solve the following problem: - Let i and j be integers. Find the maximum value of A_i + A_j where 0 \leq i < j \leq 2^N-1 and (i or j) \leq K. Here, or denotes the bitwise OR. -----Constraints----- - 1 \leq N \leq 18 - 1 \leq A_i \leq 10^9 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: N A_0 A_1 ... A_{2^N-1} -----Output----- Print 2^N-1 lines. In the i-th line, print the answer of the problem above for K=i. -----Sample Input----- 2 1 2 3 1 -----Sample Output----- 3 4 5 For K=1, the only possible pair of i and j is (i,j)=(0,1), so the answer is A_0+A_1=1+2=3. For K=2, the possible pairs of i and j are (i,j)=(0,1),(0,2). When (i,j)=(0,2), A_i+A_j=1+3=4. This is the maximum value, so the answer is 4. For K=3, the possible pairs of i and j are (i,j)=(0,1),(0,2),(0,3),(1,2),(1,3),(2,3) . When (i,j)=(1,2), A_i+A_j=2+3=5. This is the maximum value, so the answer is 5.
{"inputs": ["2\n1 2 3 2", "2\n0 2 3 2", "2\n1 2 3 1", "2\n1 2 3 1\n", "3\n3 210 47 33 0 9 17 82", "3\n3 312 47 33 0 9 17 82", "3\n3 312 59 33 0 9 17 82", "3\n2 312 59 33 0 9 17 82"], "outputs": ["3\n4\n5\n", "2\n3\n5\n", "3\n4\n5", "3\n4\n5\n", "213\n213\n257\n257\n257\n257\n292\n", "315\n315\n359\n359\n359\n359\n394\n", "315\n315\n371\n371\n371\n371\n394\n", "314\n314\n371\n371\n371\n371\n394\n"]}
432
302
coding
Solve the programming task below in a Python markdown code block. There is a girl named ''Akansha''. She is very fond of eating chocolates but she has a weak immune system due to which she gets cold after eating chocolate during morning, evening and night and can only eat at most $x$ number of chocolate each afternoon. A friend of hers gifted her some $n$ number of chocolates that she doesn't want to share with anyone. Those chocolate have to be finished before they expire. (no. of days in which they are going to expire from the day she has been gifted the chocolate is given for each chocolate) $Note:$ Chocolate cannot be consumed on the day it expires. Help Akansha to know if it is possible for her to finish all the chocolates before they expire or not. -----Input:----- - First line will contain $T$, number of test cases. Then the test cases follow. - First line contains $n$,the number of chocolates gifted to her - Second line contains $x$,the number of chocolates she can eat each afternoon - Third line contains $n$ space separated integers $A1,A2...An$,denoting the expiry of each of the $n$ chocolates -----Output:----- For each testcase, print $Possible$, if she can complete all the chocolates gifted to her. Otherwise, print $Impossible$, if she can not finish all the chocolates. -----Constraints----- - $1 \leq T \leq 100$ - $1 \leq n \leq 1500$ - $1 \leq x \leq 1500$ - $1 \leq Ai \leq 1500$ -----Subtasks----- - 100 points : $Original Constraints$ -----Sample Input:----- 3 3 5 4 1 2 5 2 4 4 3 2 2 5 1 4 2 3 1 1 -----Sample Output:----- Impossible Possible Impossible -----EXPLANATION:----- - Example case 1 1st and 3rd chocolate on the 1st afternoon as she can consume at most 5. one chocolate will be wasted. $Note:$ she cannot eat the 2nd chocolate because chocolates cannot be consumed on the day of expiry. - Example case 2 4th and 5th chocolate on 1st afternoon, 3rd and 1st chocolate on 2nd afternoon. And 2nd chocolate on the 3rd afternoon. It will take a total of 3 days to finish the chocolate. - Example case 3 She cannot eat 4th and 5th chocolate as they will expire on the very 1st day, she can eat 2nd chocolate on 1st afternoon, then 3rd chocolate on 2nd afternoon, then 1st chocolate on 3rd afternoon, and 2 chocolates 4th and 5th will expire.
{"inputs": ["3\n3\n5\n4 1 2\n5\n2\n4 4 3 2 2\n5\n1\n4 2 3 1 1"], "outputs": ["Impossible\nPossible\nImpossible"]}
628
54
coding
Solve the programming task below in a Python markdown code block. — Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: * The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; * the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; * and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? Input The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. * to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1); * to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2). Output In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal. Examples Input 5 1 2 3 1 2 Output 8 1 1 Input 5 1 -2 3 1 -2 Output 5 2 2 5
{"inputs": ["2\n0 0\n", "3\n1 1 1\n", "2\n-1 -1\n", "3\n2 1 1\n", "3\n2 2 1\n", "3\n0 -1 0\n", "3\n0 0 -1\n", "3\n-1 0 -1\n"], "outputs": ["0 0\n", "3 0\n", "-2 0\n", "2 1\n1\n", "4 1\n3\n", "0 1\n2 ", "0 1\n3\n", "-2 0\n"]}
535
139