description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [0] * len(nums) for i in range(len(dp)): dp[i] = [0, 0] ans = 0 for i, v in enumerate(nums): if v > 0: dp[i][0] = 1 + dp[i - 1][0] if dp[i - 1][1]: dp[i][1] = 1 + dp[i - 1][1] if v < 0: if dp[i - 1][1]: dp[i][0] = 1 + dp[i - 1][1] dp[i][1] = 1 + dp[i - 1][0] ans = max(ans, dp[i][0]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: for i in nums: if i > 0: i = 1 elif i < 0: i = -1 arrays = [] end = 0 for i in range(len(nums)): if nums[i] == 0: arrays.append(nums[end:i]) end = i + 1 arrays.append(nums[end:]) maximum = 0 for arr in arrays: maxi = 0 neg = 0 first = -1 for i in range(len(arr)): if arr[i] < 0: neg += 1 if first == -1: first = i + 1 last = i if neg % 2 == 0: maxi = len(arr) else: subA = len(arr) - first subB = last maxi = max(subA, subB) maximum = max(maximum, maxi) return maximum
CLASS_DEF FUNC_DEF VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, A: List[int], cnt=0, best=0) -> int: A.append(0) N = len(A) i = 0 j = 0 while i < N: while j < N and not A[j]: while i < j: cnt = cnt - 1 if A[i] < 0 else cnt i += 1 best = best if cnt & 1 else max(best, j - i) i = j + 1 j = j + 1 while j < N and A[j]: cnt = cnt + 1 if A[j] < 0 else cnt j += 1 best = best if cnt & 1 else max(best, j - i) return best
CLASS_DEF FUNC_DEF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
def sgn(n): if n > 0: return +1 elif n < 0: return -1 else: return 0 def split_0(arr): arr_buffer = [] for elem in arr: if elem != 0: arr_buffer.append(elem) else: yield arr_buffer arr_buffer = [] assert len(arr_buffer) == 0 def partial_products(arr): prod = 1 yield prod for elem in arr: prod *= elem yield prod def get_subarr_max_len(arr): first_index = {} max_len = 0 for i, prod in enumerate(partial_products(arr)): first_index.setdefault(prod, i) max_len = max(max_len, i - first_index[prod]) return max_len def get_max_len(arr): arr = [sgn(x) for x in arr] arr.append(0) if len(arr) == 0: return 0 return max(get_subarr_max_len(subarr) for subarr in split_0(arr)) class Solution: def getMaxLen(self, nums: List[int]) -> int: return get_max_len(nums)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR VAR FOR VAR VAR VAR VAR EXPR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [] product = 1 for i in range(len(nums)): if nums[i] > 0: product = product dp.append(product) elif nums[i] < 0: product = -product dp.append(product) else: product = 1 dp.append(0) print(dp) res = 0 d = {(1): 0, (0): float("inf"), (-1): float("inf")} if nums[0] == 0: d[1] = float("inf") for i, p in enumerate(dp): if p == 1: d[1] = min(d[1], i) res = max(res, i - d[1] + 1) elif p == -1: d[-1] = min(d[-1], i) res = max(res, i - d[-1]) else: d[1] = i + 1 d[-1] = float("inf") return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR STRING RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: sublists = [] i = 0 while nums and i < len(nums): if nums[i]: i += 1 else: sublists.append(nums[:i]) nums = nums[i + 1 :] i = 0 sublists.append(nums) return max([self.getMaxLenFromNonZero(sublist) for sublist in sublists]) def getMaxLenFromNonZero(self, nums: List[int]) -> int: count = 0 front = len(nums) back = 0 for i in range(len(nums)): if nums[i] < 0: count += 1 if front > i: front = i back = i if count % 2 == 0: return len(nums) else: return len(nums) - min([front + 1, len(nums) - back])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: x, y, ret = 0, 0, 0 for i in nums: if i == 0: x = y = 0 elif i > 0: x, y = 1 + x, 0 if y == 0 else y + 1 else: x, y = 0 if y == 0 else y + 1, 1 + x ret = max(ret, x) return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = 0 neg_pos = None neg_count = 0 left = -1 for i, n in enumerate(nums): if n == 0: neg_pos = None neg_count = 0 left = i continue elif n > 0: if neg_count % 2 == 0: ans = max(ans, i - left) else: ans = max(ans, i - neg_pos) elif n < 0: neg_count += 1 if neg_pos is None: neg_pos = i if neg_count % 2 == 0: ans = max(ans, i - left) else: ans = max(ans, i - neg_pos) return ans def getMaxLen1(self, nums: List[int]) -> int: ans = 0 dq = [] left = -1 for i, n in enumerate(nums): if n == 0: dq.clear() left = i continue elif n > 0: if len(dq) % 2 == 0: ans = max(ans, i - left) else: ans = max(ans, i - dq[0]) elif n < 0: dq.append(i) if len(dq) % 2 == 0: ans = max(ans, i - left) else: ans = max(ans, i - dq[0]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos, neg = -1, None P, Max = 1, 0 for i in range(len(nums)): P *= nums[i] if P == 0: pos, neg = i, None P = 1 elif P < 0 and neg is None: neg = i elif P < 0: Max = max(Max, i - neg) else: Max = max(Max, i - pos) return Max
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NONE ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NONE ASSIGN VAR NUMBER IF VAR NUMBER VAR NONE ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: plus, minus, res, c = 0, -1, 0, 0 for i in range(0, len(nums)): if nums[i] == 0: if c % 2 == 1: res = max(res, minus) else: res = max(res, max(plus, minus)) plus, minus, c = 0, -1, 0 elif nums[i] > 0: if minus != -1: minus += 1 plus += 1 else: c += 1 minus += 1 if c % 2 == 1: res = max(res, max(minus, plus)) plus += 1 if c % 2 == 1: res = max(res, minus) else: res = max(res, max(plus, minus)) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 size = len(nums) if size == 1: return 0 if nums[0] < 0 else 1 start = 0 end = 0 longest = 0 while end < size: numNeg = 0 leftNeg = -1 rightNeg = -1 while end < size and not nums[end] == 0: if nums[end] < 0: numNeg += 1 rightNeg = end if leftNeg == -1: leftNeg = end end += 1 if numNeg % 2 == 0: longest = max(longest, end - start) else: longest = max( longest, end - rightNeg - 1, rightNeg - start, end - leftNeg - 1, leftNeg - start, ) end += 1 start = end return longest
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def sign(n): if n > 0: return 1 elif n < 0: return -1 else: return 0 res = [(1) for _ in range(len(nums))] op, np, answer = -1, -1, 0 for i in range(len(nums)): if i == 0 or res[i - 1] == 0: res[i] = sign(nums[i]) else: res[i] = res[i - 1] * sign(nums[i]) if res[i] == 0: op, np = -1, -1 elif res[i] == 1: if np != -1: answer = max(answer, i - np + 1) if op == -1: op = i answer = max(answer, 1) else: answer = max(answer, i - op + 1) elif np == -1: np = i else: answer = max(answer, i - np) print(res) return answer
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: start, first_neg = -1, float("inf") maxi, positive = 0, 1 for index, value in enumerate(nums): if value == 0: start, first_neg, positive = index, float("inf"), 1 else: positive = positive == (value > 0) if positive: maxi = max(maxi, index - start) else: maxi = max(maxi, index - first_neg) first_neg = min(first_neg, index) return maxi
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: left_pos = 0 left_neg = sys.maxsize ans = 0 cp = 1 for i, num in enumerate(nums): if num == 0: cp = 1 left_pos = i + 1 left_neg = sys.maxsize else: cp *= num if cp > 0: ans = max(ans, i - left_pos + 1) else: ans = max(ans, i - left_neg) if cp < 0 and left_neg == sys.maxsize: left_neg = i return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos_p = 0 neg_p = 0 pos, neg = 0, 0 result = 0 for i in range(len(nums)): pos_p, neg_p = pos, neg if nums[i] > 0: pos = 1 + pos_p neg = 1 + neg_p if neg_p > 0 else 0 elif nums[i] < 0: pos = 1 + neg_p if neg_p > 0 else 0 neg = 1 + pos_p if pos_p > 0 else 1 else: pos, neg = 0, 0 result = max(result, pos) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_len, start, product, first_minus_index = 0, 0, 1, -1 for i, n in enumerate(nums): if n == 0: start, product, first_minus_index = i + 1, 1, -1 else: if n < 0 and first_minus_index == -1: first_minus_index = i product *= n if n > 0: max_len = max(max_len, 1) if product > 0: max_len = max(max_len, i - start + 1) if product < 0 and first_minus_index != -1: max_len = max(max_len, i - first_minus_index) return max_len
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: hashmap = {(0): -1} totalN = 0 ans = 0 for i in range(0, len(nums)): if nums[i] < 0: totalN += 1 value = totalN % 2 if nums[i] == 0: hashmap = {(0): i} totalN = 0 continue if value in hashmap: ans = max(ans, i - hashmap[value]) else: hashmap[value] = i return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR DICT NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: listt = [] a = -1 for i in range(len(nums)): if nums[i] == 0: listt.append(nums[a + 1 : i]) a = i listt.append(nums[a + 1 :]) while [] in listt: listt.remove([]) if listt == []: return 0 recordlist = {} for i in range(len(listt)): firstneg = -1 begneg = -1 recordlist[i] = 0 for m in range(len(listt[i])): if listt[i][m] < 0 and firstneg == -1: firstneg = m if begneg == -1: begneg = m continue if listt[i][m] < 0 and firstneg != -1: firstneg = -1 if firstneg == -1: recordlist[i] = len(listt[i]) else: recordlist[i] = max( [ firstneg, len(listt[i]) - firstneg - 1, begneg, len(listt[i]) - begneg - 1, ] ) m = [] for i in list(recordlist.values()): m.append(i) return max(m)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE LIST VAR EXPR FUNC_CALL VAR LIST IF VAR LIST RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 memo = collections.defaultdict(list) for i, v in enumerate(nums): memo[v].append(i) if 0 in memo: arr1 = [] for j in range(len(memo[0])): if j == 0: arr1.append(self.getMaxLen(nums[: memo[0][j]])) else: arr1.append(self.getMaxLen(nums[memo[0][j - 1] + 1 : memo[0][j]])) arr1.append(self.getMaxLen(nums[memo[0][-1] + 1 :])) return max(arr1) else: arr = [] n = len(nums) for i in range(len(nums)): if nums[i] < 0: arr.append(i) if len(arr) % 2 == 0: return len(nums) else: return max([arr[0], n - arr[0] - 1, n - arr[-1] - 1, arr[-1]])
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR LIST VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp_pos = [0] * (len(nums) + 1) dp_neg = [0] * (len(nums) + 1) for i in range(len(nums)): if nums[i] > 0: dp_pos[i + 1] = dp_pos[i] + 1 if dp_neg[i] == 0: dp_neg[i + 1] = 0 else: dp_neg[i + 1] = dp_neg[i] + 1 elif nums[i] < 0: dp_neg[i + 1] = dp_pos[i] + 1 if dp_neg[i] == 0: dp_pos[i + 1] = 0 else: dp_pos[i + 1] = dp_neg[i] + 1 return max(dp_pos)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ans = 0 fn = -1 s = -1 p = 1 for i in range(len(nums)): if nums[i] == 0: fn = -1 s = -1 p = 1 else: if s == -1: s = i p *= nums[i] if p < 0 and fn == -1: fn = i if p < 0: ans = max(ans, i - s + 1 - (fn - s + 1)) elif p > 0: ans = max(ans, i - s + 1) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = defaultdict(int) for i, num in enumerate(nums): if num > 0: dp[i, 0] = 1 if num < 0: dp[i, 1] = 1 ans = dp[0, 0] for i in range(1, len(nums)): if nums[i] > 0: dp[i, 0] = max(dp[i, 0], dp[i - 1, 0] + 1 if dp[i - 1, 0] > 0 else 0) dp[i, 1] = max(dp[i, 1], dp[i - 1, 1] + 1 if dp[i - 1, 1] > 0 else 0) if nums[i] < 0: dp[i, 0] = max(dp[i, 0], dp[i - 1, 1] + 1 if dp[i - 1, 1] > 0 else 0) dp[i, 1] = max(dp[i, 1], dp[i - 1, 0] + 1 if dp[i - 1, 0] > 0 else 0) ans = max(ans, dp[i, 0]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: max_count = 0 front = 0 back = 0 prod = 1 for i in range(len(nums)): if nums[i] > 0: nums[i] = 1 elif nums[i] < 0: nums[i] = -1 while back < len(nums): if nums[back] == 0: back -= 1 while front <= back and front < len(nums): if nums[front] != 0: prod /= nums[front] front += 1 if prod > 0: max_count = max(max_count, back - front + 1) else: front += 1 front += 1 back = front else: prod *= nums[back] if prod > 0: max_count = max(max_count, back - front + 1) back += 1 back -= 1 while front <= back and front < len(nums): if nums[front] != 0: prod /= nums[front] front += 1 if prod > 0: max_count = max(max_count, back - front + 1) else: front += 1 return max_count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ret = 0 total = 0 acc = 1 start = -1 first = -1 last = 0 for i in range(len(nums)): if nums[i] == 0: if acc > 0: ret = max(total, ret) else: ret = max(ret, first - 1, total - first, last - 1, total - last) start = i acc = 1 total = 0 first = -1 last = 0 else: total += 1 acc = acc * (1 if nums[i] > 0 else -1) if nums[i] < 0: if first == -1: first = i - start last = 0 last += 1 if acc > 0: ret = max(total, ret) else: ret = max(ret, first - 1, total - first, last - 1, total - last) return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: neg = pos = 0 ret = 0 end = start = 0 while end < len(nums): start = end while end < len(nums) and nums[end]: if nums[end] < 0: neg += 1 if nums[end] > 0: pos += 1 if neg % 2 == 0: ret = max(ret, end - start + 1) print(ret) end += 1 while neg % 2: if nums[start] < 0: neg -= 1 ret = max(ret, end - start - 1) start += 1 neg = pos = 0 while end < len(nums) and nums[end] == 0: end += 1 return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: first_neg = -1 last_neg = -1 pos = True start = 0 best = 0 n = len(nums) for i, num in enumerate(nums): if num == 0: if pos: best = max(best, i - start) elif first_neg >= start: best = max(best, i - first_neg - 1, last_neg - start) start = i + 1 pos = True elif num < 0: last_neg = i if first_neg < start: first_neg = i pos = not pos if pos: best = max(best, n - start) elif first_neg >= start: best = max(best, n - first_neg - 1, last_neg - start) return best
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def oddMinus(ls): ret = 0 for i in range(len(ls)): if ls[i] < 0: ret = max(max(ret, i), len(ls) - 1 - i) return ret def getLen(ls): minus = 0 for i in ls: if i < 0: minus += 1 if minus % 2 == 0: return len(ls) else: return oddMinus(ls) s = [] sub = [] for i in nums: if i == 0: s.append(sub) sub = [] else: sub.append(i) s.append(sub) res = 0 for ls in s: res = max(res, getLen(ls)) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ll = len(nums) if ll == 0: return 0 curmax = 0 submax = 0 q = [0] nums.append(0) for ii in range(ll + 1): if nums[ii] == 0: curmax = ii - q[0] if len(q) % 2 == 0: curmax = max(ii - q[1] - 1, q[-1] - q[0]) submax = max(submax, curmax) q = [ii + 1] curmax = 0 elif nums[ii] < 0: q.append(ii) submax = max(submax, curmax) return submax
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: start = 0 end = 0 count_negative = 0 max_len = 0 while end < len(nums): if nums[end] == 0: if count_negative % 2 != 0: while nums[start] > 0: start += 1 max_len = max(max_len, end - start - 1) start = end = end + 1 count_negative = 0 else: if nums[end] < 0: count_negative += 1 if count_negative % 2 == 0: max_len = max(max_len, end - start + 1) if end == len(nums) - 1 and count_negative % 2 == 1: while nums[start] > 0: start += 1 max_len = max(max_len, end - start) end += 1 return max_len
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if nums[-1] != 0: nums.append(0) n = len(nums) first_0 = -1 last_0 = -1 cnt_neg = 0 first_neg = -1 last_neg = -1 res = 0 for i, e in enumerate(nums): if e < 0: cnt_neg += 1 if first_neg == -1: first_neg = i last_neg = i elif e == 0: last_0 = i if cnt_neg % 2 == 0: res = max(res, last_0 - first_0 - 1) else: res = max(res, last_neg - first_0 - 1, last_0 - first_neg - 1) cnt_neg = 0 first_0 = last_0 first_neg = -1 return res
CLASS_DEF FUNC_DEF VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = 0, 0 [0, 1, -2, -3, -4] if nums[0] > 0: pos = 1 if nums[0] < 0: neg = 1 ans = pos for i in range(1, n): if nums[i] > 0: pos = 1 + pos neg = 1 + neg if neg > 0 else 0 elif nums[i] < 0: pre_pos, pre_neg = pos, neg pos = 1 + pre_neg if pre_neg > 0 else 0 neg = 1 + pre_pos else: pos, neg = 0, 0 ans = max(ans, pos) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR LIST NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: result = 0 for r in [list(range(0, len(nums))), list(range(len(nums) - 1, -1, -1))]: cur_len = 0 cur_sign = 1 for i in r: if nums[i] > 0: cur_len += 1 result = max(result, cur_sign * cur_len) elif nums[i] == 0: cur_len = 0 cur_sign = 1 else: cur_len += 1 cur_sign = -cur_sign result = max(result, cur_sign * cur_len) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: cp = 1 fp = -1 fn = None ans = 0 for i, n in enumerate(nums): cp = n * cp if cp < 0: if fn is None: fn = ln = i else: ln = i ans = max(ln - fn, ans) elif cp > 0: lp = i ans = max(lp - fp, ans) if n == 0: cp = 1 fp = i fn = None return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos_length = 0 neg_length = 0 max_length = 0 for num in nums: if num > 0: pos_length += 1 if neg_length: neg_length += 1 elif num < 0: tmp = pos_length if neg_length: pos_length = neg_length + 1 else: pos_length = 0 neg_length = tmp + 1 else: pos_length = 0 neg_length = 0 max_length = max(max_length, pos_length) max_length = max(max_length, pos_length) return max_length
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: trip_zeros = [] for num in nums: if num: if trip_zeros: trip_zeros[-1].append(num) else: trip_zeros.append([num]) else: trip_zeros.append([]) def count(arr): start = ans = 0 left_neg = None pos = 1 for end in range(len(arr)): if arr[end] < 0: if left_neg is None: left_neg = end pos ^= 1 print(pos, start, end, left_neg) if pos: ans = max(ans, end - start + 1) else: ans = max(ans, end - left_neg) return ans return max(map(count, trip_zeros))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [([0] * 2) for _ in range(len(nums))] dp[0][0] = nums[0] > 0 dp[0][1] = nums[0] < 0 ret = dp[0][0] for i in range(1, len(nums)): if nums[i] == 0: continue if nums[i] > 0: dp[i][0] = dp[i - 1][0] + 1 dp[i][1] = 0 if not dp[i - 1][1] else dp[i - 1][1] + 1 else: dp[i][0] = 0 if not dp[i - 1][1] else dp[i - 1][1] + 1 dp[i][1] = dp[i - 1][0] + 1 ret = max(ret, dp[i][0]) return int(ret)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: A = nums n = len(A) dp = [[0, 0] for i in range(n + 1)] max_len = 0 for i in range(1, n + 1): if A[i - 1] > 0: dp[i][0] = dp[i - 1][0] + 1 dp[i][1] = dp[i - 1][1] + 1 if dp[i - 1][1] != 0 else 0 elif A[i - 1] < 0: dp[i][0] = dp[i - 1][1] + 1 if dp[i - 1][1] != 0 else 0 dp[i][1] = dp[i - 1][0] + 1 else: dp[i][0] = 0 dp[i][1] = 0 max_len = max(max_len, dp[i][0]) return max_len
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: result = 0 cur = 1 min_val = -(10**10) pos_len_p = min_val neg_len_p = min_val for i in range(len(nums)): if nums[i] > 0: pos_len = max(1, pos_len_p + 1) neg_len = max(min_val, neg_len_p + 1) elif nums[i] < 0: pos_len = max(neg_len_p + 1, min_val) neg_len = max(1, pos_len_p + 1) else: pos_len = min_val neg_len = min_val neg_len_p = neg_len pos_len_p = pos_len result = max(result, pos_len) return max(result, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
William has two arrays $a$ and $b$, each consisting of $n$ items. For some segments $l..r$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $i$ from $l$ to $r$ holds $a_i = b_i$. To perform a balancing operation an even number of indices must be selected, such that $l \le pos_1 < pos_2 < \dots < pos_k \le r$. Next the items of array a at positions $pos_1, pos_3, pos_5, \dots$ get incremented by one and the items of array b at positions $pos_2, pos_4, pos_6, \dots$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. -----Input----- The first line contains a two integers $n$ and $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$), the size of arrays $a$ and $b$ and the number of segments. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ $(0 \le b_i \le 10^9)$. Each of the next $q$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i < r_i \le n)$, the edges of segments. -----Output----- For each segment output a single number — the minimal number of balancing operations needed or "-1" if it is impossible to equalize segments of arrays. -----Examples----- Input 8 5 0 1 2 9 3 2 7 5 2 2 1 9 4 1 5 8 2 6 1 7 2 4 7 8 5 8 Output 1 3 1 -1 -1 -----Note----- For the first segment from $2$ to $6$ you can do one operation with $pos = [2, 3, 5, 6]$, after this operation the arrays will be: $a = [0, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 5, 8]$. Arrays are equal on a segment from $2$ to $6$ after this operation. For the second segment from $1$ to $7$ you can do three following operations: $pos = [1, 3, 5, 6]$ $pos = [1, 7]$ $pos = [2, 7]$ After these operations, the arrays will be: $a = [2, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 7, 8]$. Arrays are equal on a segment from $1$ to $7$ after these operations. For the third segment from $2$ to $4$ you can do one operation with $pos = [2, 3]$, after the operation arrays will be: $a = [0, 2, 2, 9, 3, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 1, 5, 8]$. Arrays are equal on a segment from $2$ to $4$ after this operation. It is impossible to equalize the fourth and the fifth segment.
import sys input = sys.stdin.readline class segtree: def __init__(self, Size, op, e, A=[]): self.op = op for i in range(Size): if Size <= 1 << i: self.LeavesSize = 1 << i break self.e = e self.Tree = [e] * (self.LeavesSize * 2) for i in range(len(A)): self.Tree[i + self.LeavesSize - 1] = A[i] self.make() def make(self, i=0): if i < self.LeavesSize - 1: self.Tree[i] = self.op(self.make(i * 2 + 1), self.make(i * 2 + 2)) return self.Tree[i] def __getitem__(self, key): return self.Tree[key + self.LeavesSize - 1] def push(self, Index, Value): Index += self.LeavesSize - 1 self.Tree[Index] = Value while Index != 0: Index = (Index - 1) // 2 self.Tree[Index] = self.op( self.Tree[Index * 2 + 1], self.Tree[Index * 2 + 2] ) def get(self, A, B, Index=0, L=0, R=-1): if Index == 0: R = self.LeavesSize if B <= L or R <= A: return self.e if A <= L and R <= B: return self.Tree[Index] return self.op( self.get(A, B, Index * 2 + 1, L, (L + R) // 2), self.get(A, B, Index * 2 + 2, (L + R) // 2, R), ) def add(self, Index, Value): Value += self.Tree[Index + self.LeavesSize - 1] self.push(Index, Value) def output(self): p1 = 0 p2 = 1 print(*self.Tree[p1 : p1 + p2]) while p2 != self.LeavesSize: p1 += p2 p2 *= 2 print(*self.Tree[p1 : p1 + p2]) N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [0] + [(A[i] - B[i]) for i in range(N)] for i in range(N): C[i + 1] += C[i] TreeMax = segtree(N, max, -(10**18), C) TreeMin = segtree(N, min, 10**18, C) for _ in range(Q): L, R = map(int, input().split()) L -= 1 if C[R] - C[L] != 0 or TreeMax.get(L, R) - C[L] > 0: print(-1) else: print(C[L] - TreeMin.get(L, R))
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_DEF NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR
William has two arrays $a$ and $b$, each consisting of $n$ items. For some segments $l..r$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $i$ from $l$ to $r$ holds $a_i = b_i$. To perform a balancing operation an even number of indices must be selected, such that $l \le pos_1 < pos_2 < \dots < pos_k \le r$. Next the items of array a at positions $pos_1, pos_3, pos_5, \dots$ get incremented by one and the items of array b at positions $pos_2, pos_4, pos_6, \dots$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. -----Input----- The first line contains a two integers $n$ and $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$), the size of arrays $a$ and $b$ and the number of segments. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ $(0 \le b_i \le 10^9)$. Each of the next $q$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i < r_i \le n)$, the edges of segments. -----Output----- For each segment output a single number — the minimal number of balancing operations needed or "-1" if it is impossible to equalize segments of arrays. -----Examples----- Input 8 5 0 1 2 9 3 2 7 5 2 2 1 9 4 1 5 8 2 6 1 7 2 4 7 8 5 8 Output 1 3 1 -1 -1 -----Note----- For the first segment from $2$ to $6$ you can do one operation with $pos = [2, 3, 5, 6]$, after this operation the arrays will be: $a = [0, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 5, 8]$. Arrays are equal on a segment from $2$ to $6$ after this operation. For the second segment from $1$ to $7$ you can do three following operations: $pos = [1, 3, 5, 6]$ $pos = [1, 7]$ $pos = [2, 7]$ After these operations, the arrays will be: $a = [2, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 7, 8]$. Arrays are equal on a segment from $1$ to $7$ after these operations. For the third segment from $2$ to $4$ you can do one operation with $pos = [2, 3]$, after the operation arrays will be: $a = [0, 2, 2, 9, 3, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 1, 5, 8]$. Arrays are equal on a segment from $2$ to $4$ after this operation. It is impossible to equalize the fourth and the fifth segment.
import sys input = sys.stdin.readline class SegTree: def __init__(self, n, e, ope, lst=[]): self.N0 = 2 ** (n - 1).bit_length() self.e = e self.ope = ope self.data = [e] * (2 * self.N0) if lst: for i in range(n): self.data[self.N0 + i] = lst[i] for i in range(self.N0 - 1, 0, -1): self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1]) def f5(self): for i in range(self.N0 - 1, 0, -1): self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1]) def update(self, i, x): i += self.N0 self.data[i] = x while i > 1: i >>= 1 self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1]) def add(self, i, x): self.update(i, x + self.get(i)) def query(self, l, r): if r <= l: return self.e res = self.e l += self.N0 r += self.N0 while l < r: if l & 1: res = self.ope(res, self.data[l]) l += 1 if r & 1: r -= 1 res = self.ope(res, self.data[r]) l >>= 1 r >>= 1 return res def get(self, i): return self.data[self.N0 + i] def ope(x, y): if x >= y: return x else: return y def ope2(x, y): if x <= y: return x else: return y def main(): n, q = map(int, input().split()) alst = list(map(int, input().split())) blst = list(map(int, input().split())) cum = [0] pos = 0 i_p = [-1] * n bef = alst[0] >= blst[0] seg = SegTree(n, 0, ope) inf = 10**15 seg_min = SegTree(n + 1, inf, ope2) seg_max = SegTree(n + 1, -inf, ope) i_p_min = [0] * n i_p_max = [0] * n tot = 0 seg_min.data[seg_min.N0] = 0 seg_max.data[seg_min.N0] = 0 for i, (a, b) in enumerate(zip(alst, blst)): cum.append(cum[-1] + a - b) if not (a >= b) ^ bef: i_p[i] = pos i_p_max[pos] = i else: pos += 1 i_p[i] = pos bef = a >= b i_p_min[pos] = i i_p_max[pos] = i seg.data[seg.N0 + pos] += abs(a - b) tot += b - a seg_min.data[seg_min.N0 + i + 1] = tot seg_max.data[seg_min.N0 + i + 1] = tot seg.f5() seg_min.f5() seg_max.f5() for _ in range(q): l, r = map(int, input().split()) if cum[r] - cum[l - 1] != 0: print(-1) continue l -= 1 r -= 1 if seg_min.data[seg_min.N0 + l] > seg_min.query(l, r + 1): print(-1) continue print(seg_max.query(l, r + 1) - seg_min.query(l, r + 1)) continue posl = i_p[l] posr = i_p[r] al = abs(cum[l] - cum[i_p_min[posl]]) ar = abs(cum[i_p_max[posr] + 1] - cum[r + 1]) seg.add(posl, -al) seg.add(posr, -ar) print(seg.query(posl, posr + 1)) seg.add(posl, al) seg.add(posr, ar) for _ in range(1): main()
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF LIST ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
William has two arrays $a$ and $b$, each consisting of $n$ items. For some segments $l..r$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $i$ from $l$ to $r$ holds $a_i = b_i$. To perform a balancing operation an even number of indices must be selected, such that $l \le pos_1 < pos_2 < \dots < pos_k \le r$. Next the items of array a at positions $pos_1, pos_3, pos_5, \dots$ get incremented by one and the items of array b at positions $pos_2, pos_4, pos_6, \dots$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. -----Input----- The first line contains a two integers $n$ and $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$), the size of arrays $a$ and $b$ and the number of segments. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ $(0 \le b_i \le 10^9)$. Each of the next $q$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i < r_i \le n)$, the edges of segments. -----Output----- For each segment output a single number — the minimal number of balancing operations needed or "-1" if it is impossible to equalize segments of arrays. -----Examples----- Input 8 5 0 1 2 9 3 2 7 5 2 2 1 9 4 1 5 8 2 6 1 7 2 4 7 8 5 8 Output 1 3 1 -1 -1 -----Note----- For the first segment from $2$ to $6$ you can do one operation with $pos = [2, 3, 5, 6]$, after this operation the arrays will be: $a = [0, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 5, 8]$. Arrays are equal on a segment from $2$ to $6$ after this operation. For the second segment from $1$ to $7$ you can do three following operations: $pos = [1, 3, 5, 6]$ $pos = [1, 7]$ $pos = [2, 7]$ After these operations, the arrays will be: $a = [2, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 7, 8]$. Arrays are equal on a segment from $1$ to $7$ after these operations. For the third segment from $2$ to $4$ you can do one operation with $pos = [2, 3]$, after the operation arrays will be: $a = [0, 2, 2, 9, 3, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 1, 5, 8]$. Arrays are equal on a segment from $2$ to $4$ after this operation. It is impossible to equalize the fourth and the fifth segment.
class SegmentTree: def __init__(self, data, default=0, func=max): self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size : _size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): start += self._size stop += self._size res_left = res_right = self._default while start < stop: if start & 1: res_left = self._func(res_left, self.data[start]) start += 1 if stop & 1: stop -= 1 res_right = self._func(self.data[stop], res_right) start >>= 1 stop >>= 1 return self._func(res_left, res_right) def __repr__(self): return "SegmentTree({0})".format(self.data) n, q = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) data = [0] for i in range(n): data.append(a[i] - b[i] + data[-1]) minSeg = SegmentTree(data, default=float("inf"), func=min) maxSeg = SegmentTree(data, default=float("-inf"), func=max) out = [] for _ in range(q): l, r = map(int, input().split()) if data[l - 1] != data[r]: out.append(-1) continue start = data[l - 1] smol = minSeg.query(l - 1, r) tol = maxSeg.query(l - 1, r) if tol == start: out.append(start - smol) else: out.append(-1) print("\n".join(map(str, out)))
CLASS_DEF FUNC_DEF NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William has two arrays $a$ and $b$, each consisting of $n$ items. For some segments $l..r$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $i$ from $l$ to $r$ holds $a_i = b_i$. To perform a balancing operation an even number of indices must be selected, such that $l \le pos_1 < pos_2 < \dots < pos_k \le r$. Next the items of array a at positions $pos_1, pos_3, pos_5, \dots$ get incremented by one and the items of array b at positions $pos_2, pos_4, pos_6, \dots$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. -----Input----- The first line contains a two integers $n$ and $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$), the size of arrays $a$ and $b$ and the number of segments. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ $(0 \le b_i \le 10^9)$. Each of the next $q$ lines contains two integers $l_i$ and $r_i$ $(1 \le l_i < r_i \le n)$, the edges of segments. -----Output----- For each segment output a single number — the minimal number of balancing operations needed or "-1" if it is impossible to equalize segments of arrays. -----Examples----- Input 8 5 0 1 2 9 3 2 7 5 2 2 1 9 4 1 5 8 2 6 1 7 2 4 7 8 5 8 Output 1 3 1 -1 -1 -----Note----- For the first segment from $2$ to $6$ you can do one operation with $pos = [2, 3, 5, 6]$, after this operation the arrays will be: $a = [0, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 5, 8]$. Arrays are equal on a segment from $2$ to $6$ after this operation. For the second segment from $1$ to $7$ you can do three following operations: $pos = [1, 3, 5, 6]$ $pos = [1, 7]$ $pos = [2, 7]$ After these operations, the arrays will be: $a = [2, 2, 2, 9, 4, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 2, 7, 8]$. Arrays are equal on a segment from $1$ to $7$ after these operations. For the third segment from $2$ to $4$ you can do one operation with $pos = [2, 3]$, after the operation arrays will be: $a = [0, 2, 2, 9, 3, 2, 7, 5]$, $b = [2, 2, 2, 9, 4, 1, 5, 8]$. Arrays are equal on a segment from $2$ to $4$ after this operation. It is impossible to equalize the fourth and the fifth segment.
import sys n, q = [int(i) for i in sys.stdin.readline().split()] a = [int(i) for i in sys.stdin.readline().split()] b = [int(i) for i in sys.stdin.readline().split()] d = [(a[i] - b[i]) for i in range(n)] sd = [0] for i in d: sd.append(sd[-1] + i) ma, mi = [[]], [[]] ma[0] = mi[0] = sd for i in range(1, 17): l = 1 << i - 1 ma.append([max(ma[-1][j], ma[-1][j + l]) for j in range(len(sd) - l * 2 + 1)]) mi.append([min(mi[-1][j], mi[-1][j + l]) for j in range(len(sd) - l * 2 + 1)]) for _ in range(q): a, b = [int(i) for i in sys.stdin.readline().split()] l = (b - a).bit_length() - 1 m1 = max(ma[l][a], ma[l][b + 1 - (1 << l)]) m2 = min(mi[l][a], mi[l][b + 1 - (1 << l)]) sys.stdout.write("%d\n" % (m1 - m2 if m1 == sd[a - 1] and m1 == sd[b] else -1))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR LIST LIST LIST LIST ASSIGN VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = list(map(int, input().split())) l = list(map(int, input().split())) mus = [0] * n mus[0] = l[0] cnt = 0 ans = 0 for i in range(1, n): mus[i] = mus[i - 1] + l[i] suf = [0] * n suf[-1] = mus[-1] for i in range(n - 2, -1, -1): suf[i] = max(mus[i], suf[i + 1]) for i in range(n): if l[i] == 0 and mus[i] + cnt < 0: if d - suf[i] - cnt < 0 or d - suf[i] < abs(mus[i]): print(-1) return else: cnt += d - suf[i] - cnt ans += 1 if suf[0] > d: print(-1) return print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
R = lambda: map(int, input().split()) n, k = R() arr = list(R()) tup = [0, 0] res = 0 for x in arr: if x != 0: tup[0], tup[1] = tup[0] + x, tup[1] + x tup[1] = min(tup[1], k) elif tup[1] < 0: tup[0], tup[1] = 0, k res += 1 else: tup[0] = max(0, tup[0]) if tup[0] > k: res = -1 break print(res)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
def main(): n, d = map(int, input().split()) a = list(map(int, input().split())) pref, mx, add, ans = [0] * n, [0] * n, 0, 0 for pos in range(n): pref[pos] = a[pos] if not pos else a[pos] + pref[pos - 1] for pos in range(n - 1, -1, -1): mx[pos] = pref[pos] if pos == n - 1 else max(mx[pos + 1], pref[pos]) for i in range(n): if pref[i] + add > d: print("-1") return if a[i] == 0 and pref[i] + add < 0: ans += 1 add += max(-(pref[i] + add), d - mx[i] - add) print(ans) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = map(int, input().split()) a = [0] + list(map(int, input().split())) b = [0] * (n + 2) b[n] = a[n] now = a[n] for i in range(n - 1, 0, -1): now = a[i] + max(now, 0) b[i] = now now = 0 res = 0 for i in range(1, n + 1): if a[i] == 0: if now < 0: res += 1 now = min(d, max(0, d - b[i + 1])) else: now += a[i] if now > d: res = -1 break print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
f = lambda: map(int, input().split()) n, d = f() h = s = k = 0 for q in f(): h, s = h + q, min(d, s + q) if h > d: k = -1 break if q == 0: h = max(0, h) if s < 0: s, k = d, k + 1 print(k)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
import sys n, d = map(int, input().split()) a = list(map(int, input().split())) ub, lb = 0, 0 ans = 0 for x in a: if x == 0: if ub < 0: ub, lb = d, 0 ans += 1 lb = max(lb, 0) else: ub = min(d, ub + x) lb += x if lb > d: print(-1) exit() print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = map(int, input().split()) a = list(map(int, input().split())) pref = [(0) for i in range(n)] c = 0 for i in range(n): c += a[i] if a[i] == 0: c = max(0, c) pref[i] = c suff = [(0) for i in range(n)] suff[-1] = pref[-1] mc = suff[-1] for i in range(n - 2, -1, -1): suff[i] = max(mc, pref[i]) mc = suff[i] if a[i] == 0 and i > 0: mc = pref[i - 1] if max(suff) > d: print(-1) return c = 0 ans = 0 for i in range(n): if a[i] != 0: c += a[i] elif c < 0: ans += 1 c = max(0, c) c += d - suff[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = list(map(int, input().split())) a = list(map(int, input().split())) s = 0 m = 0 ans = 0 flag = True n = len(a) for i in range(n): if a[i] == 0: if s < 0: s = d m = d ans += 1 else: m = min(m, s) elif a[i] < 0: s = s + a[i] elif s + a[i] > d: if s + a[i] - d > m: flag = False break else: m -= s + a[i] - d s = d else: s = s + a[i] if flag: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
H, L, t = 0, 0, 0 n, d = map(int, input().split()) for i in map(int, input().split()): if i == 0: if H < 0: H = d t += 1 L = max(L, 0) L += i H = min(d, H + i) if L > d: print(-1) return () print(t)
ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
import sys input = sys.stdin.readline n, d = list(map(int, input().split())) a = list(map(int, input().split())) ans = 0 ub, lb = 0, 0 for x in a: if x == 0: if ub < 0: ub, lb = d, 0 ans += 1 if lb < 0: lb = 0 else: ub = min(d, ub + x) lb += x if lb > d: print(-1) return print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
[n, d] = [int(x) for x in input().split(" ")] A = [int(a) for a in input().split(" ")] def solve(): ans = 0 bal = 0 minGap = 0 for i in range(n): if A[i] == 0: if bal < 0: go = min(-bal, minGap) minGap -= go bal += go if bal < 0: ans += 1 bal = 0 minGap = d else: bal += A[i] if bal > d: return -1 minGap = min(minGap, d - bal) return ans print(solve())
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = map(int, input().split()) a = list(map(int, input().split())) f = True b = [a[0]] for i in range(1, n): b.append(b[i - 1] + a[i]) if max(b) > d: f = False h = [0] * n h[n - 1] = b[n - 1] for i in range(n - 2, -1, -1): h[i] = max(b[i], h[i + 1]) x, k = 0, 0 for i in range(n): if a[i] == 0 and b[i] + x < 0: k += 1 x += d - (h[i] + x) if b[i] + x < 0: f = False break if f: print(k) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = map(int, input().split()) line = list(map(int, input().split())) pref = [0] * n maxx = 0 for i in range(n): pref[i] = pref[max(i - 1, 0)] + line[i] maxx = max(maxx, pref[i]) maxr = [0] * n for i in range(n - 1, -1, -1): if i == n - 1: maxr[i] = pref[i] else: maxr[i] = max(maxr[i + 1], pref[i]) sm = 0 bon = 0 ans = 0 b = True if maxx > d: b = False for i in range(n): elem = line[i] sm += elem if elem == 0: if sm + bon < 0: ans += 1 bon += max(0, d - (maxr[i] + bon)) if sm + bon < 0: b = False break if sm + bon > d: b = False break if b == False: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card. She starts with 0 money on her account. In the evening of i-th day a transaction a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 0, then the amount of money on Luba's account is checked. In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d. It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1». Luba must not exceed this limit, and also she wants that every day her account is checked (the days when a_{i} = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her! -----Input----- The first line contains two integers n, d (1 ≤ n ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation. The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} represents the transaction in i-th day. -----Output----- Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money. -----Examples----- Input 5 10 -1 5 0 -5 3 Output 0 Input 3 4 -10 0 20 Output -1 Input 5 10 -5 0 10 -11 0 Output 2
n, d = [int(x) for x in input().split()] tr = [int(x) for x in input().split()] cash = 0 flag = False gr = [] for i in tr: if i != 0: cash += i gr.append(cash) if cash > d: flag = True break if flag: print(-1) else: mx = [-1] * n mx[-1] = gr[-1] for i in range(n - 2, -1, -1): mx[i] = max(gr[i], mx[i + 1]) acash = 0 count = 0 for i in range(n): if tr[i] == 0: if gr[i] + acash < 0: acash += d - mx[i] - acash if gr[i] + acash < 0: count = -1 break count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson. Nam created a sequence a consisting of n (1 ≤ n ≤ 10^5) elements a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). A subsequence a_{i}_1, a_{i}_2, ..., a_{i}_{k} where 1 ≤ i_1 < i_2 < ... < i_{k} ≤ n is called increasing if a_{i}_1 < a_{i}_2 < a_{i}_3 < ... < a_{i}_{k}. An increasing subsequence is called longest if it has maximum length among all increasing subsequences. Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups: group of all i such that a_{i} belongs to no longest increasing subsequences. group of all i such that a_{i} belongs to at least one but not every longest increasing subsequence. group of all i such that a_{i} belongs to every longest increasing subsequence. Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job. -----Input----- The first line contains the single integer n (1 ≤ n ≤ 10^5) denoting the number of elements of sequence a. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). -----Output----- Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to. -----Examples----- Input 1 4 Output 3 Input 4 1 3 2 5 Output 3223 Input 4 1 5 2 3 Output 3133 -----Note----- In the second sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a_1, a_2, a_4} = {1, 3, 5} and {a_1, a_3, a_4} = {1, 2, 5}. In the third sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a_1, a_3, a_4} = {1, 2, 3}.
n = int(input()) secuencia = [None] * n ma = 0 for num, e in enumerate(input().strip().split()): en = int(e) secuencia[num] = [en, 0, num] ma = max(ma, en) escritura = ["1"] * len(secuencia) bit = [0] * (ma + 1) def max_x(x, l): suma = 0 while x != 0: suma = max(suma, l[x]) x -= x & -x return suma def update_x(x, l, max_n, val): while x <= max_n: if val > l[x]: l[x] = val else: return x += x & -x def new_get_secuence(e): num = secuencia[e][0] maximo = max_x(num - 1, bit) + 1 update_x(num, bit, ma, maximo) return maximo for e in range(n): secuencia[e][1] = new_get_secuence(e) secuencia.sort(key=lambda x: (-x[1], -x[2])) ultimos = [(ma + 1, 0, n)] partir = 0 moment_max = secuencia[0][1] usados = [] for e in secuencia: if e[1] < moment_max: if len(usados) == 1: escritura[usados[0][2]] = "3" else: for y in usados: escritura[y[2]] = "2" ultimos = usados usados = [] moment_max -= 1 for element in ultimos: if e[2] < element[2]: if e[0] < element[0]: usados.append(e) break else: break if len(usados) == 1: escritura[usados[0][2]] = "3" else: for y in usados: escritura[y[2]] = "2" print("".join(escritura))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER STRING FOR VAR VAR ASSIGN VAR VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson. Nam created a sequence a consisting of n (1 ≤ n ≤ 10^5) elements a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). A subsequence a_{i}_1, a_{i}_2, ..., a_{i}_{k} where 1 ≤ i_1 < i_2 < ... < i_{k} ≤ n is called increasing if a_{i}_1 < a_{i}_2 < a_{i}_3 < ... < a_{i}_{k}. An increasing subsequence is called longest if it has maximum length among all increasing subsequences. Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups: group of all i such that a_{i} belongs to no longest increasing subsequences. group of all i such that a_{i} belongs to at least one but not every longest increasing subsequence. group of all i such that a_{i} belongs to every longest increasing subsequence. Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job. -----Input----- The first line contains the single integer n (1 ≤ n ≤ 10^5) denoting the number of elements of sequence a. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). -----Output----- Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to. -----Examples----- Input 1 4 Output 3 Input 4 1 3 2 5 Output 3223 Input 4 1 5 2 3 Output 3133 -----Note----- In the second sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a_1, a_2, a_4} = {1, 3, 5} and {a_1, a_3, a_4} = {1, 2, 5}. In the third sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a_1, a_3, a_4} = {1, 2, 3}.
N = int(input()) A = list(map(int, input().split())) maxa = max(A) def upd(ftree, x, v): while x <= maxa: ftree[x] = max(ftree[x], v) x += x & -x def qry(ftree, x): res = 0 while x: res = max(res, ftree[x]) x -= x & -x return res st_len = [(0) for i in range(N)] ftree = [(0) for i in range(maxa + 1)] for i in range(N - 1, -1, -1): st_len[i] = qry(ftree, maxa + 1 - A[i] - 1) + 1 upd(ftree, maxa + 1 - A[i], st_len[i]) ed_len = [(0) for i in range(N)] ftree = [(0) for i in range(maxa + 1)] for i in range(N): ed_len[i] = qry(ftree, A[i] - 1) + 1 upd(ftree, A[i], ed_len[i]) max_len = max(st_len) st_cnt_len = [(0) for i in range(N + 1)] for i in range(N): if ed_len[i] + st_len[i] - 1 == max_len: st_cnt_len[st_len[i]] += 1 for i in range(N): if ed_len[i] + st_len[i] - 1 != max_len: print(1, end="") elif st_cnt_len[st_len[i]] > 1: print(2, end="") else: print(3, end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station. Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations. The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive. -----Output----- Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n. -----Examples----- Input 4 4 4 4 Output 6 Input 5 2 3 5 5 Output 17 -----Note----- In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: ρ_{1, 2} = 1 ρ_{1, 3} = 2 ρ_{1, 4} = 3 ρ_{1, 5} = 3 ρ_{2, 3} = 1 ρ_{2, 4} = 2 ρ_{2, 5} = 2 ρ_{3, 4} = 1 ρ_{3, 5} = 1 ρ_{4, 5} = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
MAX_N = 100000 def maxi(a, b): if a[0] > b[0]: return a else: return b class Segment_Tree: def init(self, left, right, data, leftbound, rightbound): self.data = data self.left = left self.right = right self.leftbound = leftbound self.rightbound = rightbound return self def build(self, a, leftbound, rightbound): if len(a) == 0: return self.init(-1, -1, [0, -1], MAX_N + 1, -1) elif len(a) == 1: return self.init(-1, -1, a[0], leftbound, rightbound) else: middle = (leftbound + rightbound) // 2 self.left = Segment_Tree() self.right = Segment_Tree() return self.init( self.left.build(a[: middle - leftbound], leftbound, middle), self.right.build(a[middle - leftbound :], middle, rightbound), maxi(self.left.data, self.right.data), leftbound, rightbound, ) def get(self, l, r): if l <= self.leftbound and r >= self.rightbound: return self.data elif l < self.left.rightbound and r > self.right.leftbound: return maxi(self.left.get(l, r), self.right.get(l, r)) elif l >= self.right.leftbound: return self.right.get(l, r) else: return self.left.get(l, r) n = int(input()) a = list(map(int, input().split())) + [n] a = [[a[i] - 1, i] for i in range(n)] Tree = Segment_Tree() Tree.build(a, 0, n) dp = [0] * n ans = 0 for i in range(n - 2, -1, -1): m = Tree.get(i + 1, a[i][0] + 1)[1] dp[i] = dp[m] - (a[i][0] - m) + n - i - 1 ans += dp[i] print(ans)
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER LIST NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station. Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations. The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive. -----Output----- Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n. -----Examples----- Input 4 4 4 4 Output 6 Input 5 2 3 5 5 Output 17 -----Note----- In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: ρ_{1, 2} = 1 ρ_{1, 3} = 2 ρ_{1, 4} = 3 ρ_{1, 5} = 3 ρ_{2, 3} = 1 ρ_{2, 4} = 2 ρ_{2, 5} = 2 ρ_{3, 4} = 1 ρ_{3, 5} = 1 ρ_{4, 5} = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
import sys input = sys.stdin.readline def solve(): n = int(input()) a = list(map(int, input().split())) T = [(-1, -1)] * (2 * n) for i in range(n - 1): T[i + n] = a[i], i T[n + n - 1] = n, n - 1 for i in range(n - 1, -1, -1): T[i] = max(T[2 * i], T[2 * i + 1]) dp = [0] * n res = 0 for i in range(n - 2, -1, -1): l = i + n r = a[i] - 1 + n v = -1, -1 while l <= r: if l % 2 == 1: v = max(v, T[l]) if r % 2 == 0: v = max(v, T[r]) l = (l + 1) // 2 r = (r - 1) // 2 dp[i] = dp[v[1]] + (n - v[1]) + (v[1] - i) - (a[i] - 1 - v[1] + 1) res += dp[i] print(res) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station. Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations. The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive. -----Output----- Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n. -----Examples----- Input 4 4 4 4 Output 6 Input 5 2 3 5 5 Output 17 -----Note----- In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: ρ_{1, 2} = 1 ρ_{1, 3} = 2 ρ_{1, 4} = 3 ρ_{1, 5} = 3 ρ_{2, 3} = 1 ρ_{2, 4} = 2 ρ_{2, 5} = 2 ρ_{3, 4} = 1 ρ_{3, 5} = 1 ρ_{4, 5} = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
from sys import stdin MAXN = 1 << 17 def build(arr, n, segtree): for i in range(n): segtree[MAXN + i] = [arr[i], i] for i in range(MAXN - 1, 0, -1): segtree[i] = max(segtree[i * 2], segtree[i * 2 + 1]) def get(l, r, segtree): ans = [-1, -1] l, r = l + MAXN, r + MAXN + 1 while l < r: if l & 1: ans = max(ans, segtree[l]) l += 1 if r & 1: r -= 1 ans = max(ans, segtree[r]) l >>= 1 r >>= 1 return ans[1] n = int(input().strip()) arr = list(map(int, stdin.readline().strip().split(" "))) arr = arr + [n] for i in range(n): arr[i] -= 1 segtree = [[0, 0] for i in range(4 * MAXN)] build(arr, n, segtree) dp = [(0) for i in range(n)] ans = 0 for i in range(n - 2, -1, -1): m = get(i + 1, arr[i], segtree) dp[i] = dp[m] - (arr[i] - m) + n - i - 1 ans += dp[i] print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR LIST VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station. Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations. The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive. -----Output----- Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n. -----Examples----- Input 4 4 4 4 Output 6 Input 5 2 3 5 5 Output 17 -----Note----- In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: ρ_{1, 2} = 1 ρ_{1, 3} = 2 ρ_{1, 4} = 3 ρ_{1, 5} = 3 ρ_{2, 3} = 1 ρ_{2, 4} = 2 ρ_{2, 5} = 2 ρ_{3, 4} = 1 ρ_{3, 5} = 1 ρ_{4, 5} = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
n = int(input()) a = list(map(int, input().split())) a = [(ai - 1) for ai in a] a[n:n] = [n - 1] dp = [0] * n ans = 0 i = n - 2 nmax = 2**17 tree = [[0, 0]] * 2 * nmax j = 0 while j < n: tree[nmax + j] = [a[j], j] j = j + 1 j = nmax - 1 while j > 0: tree[j] = max(tree[j * 2], tree[j * 2 + 1]) j = j - 1 def get(left, right): ans = [-1, -1] left = left + nmax right = right + nmax + 1 while left < right: if left & 1: ans = max(ans, tree[left]) left = left + 1 if right & 1: right = right - 1 ans = max(ans, tree[right]) left = left // 2 right = right // 2 return ans[1] while i >= 0: m = get(i + 1, a[i]) dp[i] = dp[m] - (a[i] - m) + n - i - 1 ans += dp[i] i = i - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST LIST NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's call a sequence $b_1, b_2, b_3 \dots, b_{k - 1}, b_k$ almost increasing if $$\min(b_1, b_2) \le \min(b_2, b_3) \le \dots \le \min(b_{k - 1}, b_k).$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers $a_1, a_2, \dots, a_n$. Calculate the length of its longest almost increasing subsequence. You'll be given $t$ test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of independent test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) — the length of the sequence $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the sequence itself. It's guaranteed that the total sum of $n$ over all test cases doesn't exceed $5 \cdot 10^5$. -----Output----- For each test case, print one integer — the length of the longest almost increasing subsequence. -----Examples----- Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Output 6 2 7 -----Note----- In the first test case, one of the optimal answers is subsequence $1, 2, 7, 2, 2, 3$. In the second and third test cases, the whole sequence $a$ is already almost increasing.
import sys input = sys.stdin.readline def ri(): return [int(i) for i in input().split()] def ft_max_set(t, at, val): while at < len(t): t[at] = max(t[at], val) at |= at + 1 def ft_max_query(t, at): res = 0 while at >= 0: res = max(res, t[at]) at = (at & at + 1) - 1 return res def do_nxt(b): st = [] nxt = [len(b)] * len(b) for i in range(len(b)): while len(st) > 0 and st[-1][0] < b[i]: val, at = st.pop() nxt[at] = i st.append((b[i], i)) return nxt def main(): t = ri()[0] for _ in range(t): n = ri()[0] b = ri() MAX_VAL = 5 * 10**5 + 10 nxt = do_nxt(b) extra = [[] for _ in range(n)] t = [0] * MAX_VAL ft_max_set(t, 0, 1) extra[0].append((0, 2)) for i in range(n): best = ft_max_query(t, b[i]) best += 1 ft_max_set(t, at=b[i], val=best) if nxt[i] < n: extra[nxt[i]].append((b[i], best + 1)) for ai, e_best in extra[i]: ft_max_set(t, at=ai, val=e_best) print(ft_max_query(t, MAX_VAL - 1) - 1) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = [int(i) for i in input().split()] ans = v // min(a) * [9 - a[::-1].index(min(a))] v %= min(a) for i in range(len(ans)): for j in range(8, -1, -1): if j + 1 > ans[i] and a[j] - a[ans[i] - 1] <= v: v -= a[j] - a[ans[i] - 1] ans[i] = j + 1 if v <= 0: break if ans: print("".join([str(i) for i in ans])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR LIST BIN_OP NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) digits = [int(i) for i in input().split()] m, ind = digits[0], 0 for i in range(1, 9): if digits[i] <= m: m, ind = digits[i], i number = str(ind + 1) * (v // m) l = len(number) if l == 0: print(-1) else: cost = l * m for i in range(l): for j in reversed(list(range(ind + 1, 9))): if v - cost + m >= digits[j]: number = number[:i] + str(j + 1) + number[i + 1 :] cost = cost - m + digits[j] break print(number)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
import sys def index(a, lis): cur = -1 for i in range(len(lis)): if lis[i] == a: cur = i return cur n = int(input()) lis = [int(x) for x in input().split()] minn = min(lis) ind = index(minn, lis) leng = n // minn if leng == 0: print(-1) sys.exit() n = n % minn for i in range(9): lis[i] = lis[i] - minn mem = dict() for i in range(8, -1, -1): if lis[i] == 0: break mem[i] = n // lis[i] n = n % lis[i] if n == 0: break tot = sum(mem.values()) ans = "" for item in mem: ans = ans + str(item + 1) * mem[item] ans = ans + str(ind + 1) * (leng - tot) print(ans)
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
import sys Ri = lambda: [int(x) for x in sys.stdin.readline().split()] ri = lambda: sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**8 n = int(ri()) a = Ri() a = [INF] + a minn = min(a) no = -1 for i in range(9, 0, -1): if minn == a[i]: no = i break digs = n // minn ans = [str(no) for i in range(digs)] n -= digs * minn for i in range(digs): for j in range(9, 0, -1): if n + minn - a[j] >= 0: n = n + minn - a[j] ans[i] = str(j) break if len(ans) == 0: print(-1) else: print("".join(ans))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) l = list(map(int, input().split())) if min(l) > n: print(-1) else: m = min(l) ind = -1 for i in range(0, 9): if l[i] == m: ind = i a = [ind + 1] * (n // m) lo = n % m f = True ci = 0 while lo != 0 and f: if ci == len(a): break for i in range(8, -1, -1): if l[i] <= lo + m: if i < ind: f = False else: a[ci] = i + 1 ci += 1 lo = lo + m - l[i] break print("".join(map(str, a)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) m = 0 for i in range(1, 9): if a[i] < a[m]: m = i if a[m] > v: print(-1) exit() x = v // a[m] o = [m + 1] * x v -= x * a[m] for i in range(x): for j in range(o[i], 9): if v - a[j] + a[o[i] - 1] >= 0: v = v - a[j] + a[o[i] - 1] o[i] = j + 1 print("".join(map(str, o)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
from sys import exit lit = int(input()) cost = list(map(int, input().split())) st = list() while lit >= min(cost): min1 = 0 for i in range(len(cost)): if cost[min1] >= cost[i]: min1 = i lit -= cost[min1] st.append(str(min1 + 1)) if len(st) <= 0: print("-1") exit(0) for i in range(len(st)): c = cost[int(st[i]) - 1] for j in range(8, int(st[i]) - 1, -1): if lit - cost[j] + c >= 0: lit -= cost[j] lit += c st[i] = str(j + 1) break if lit == 0: break print(*st, sep="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) s = input().split() ar = [] for x in range(1, 10): ar.append(int(s[x - 1])) minn = min(ar) ln = v // minn num = ln if num == 0: print(-1) else: tmp = v while num: for i in range(8, -1, -1): if ar[i] <= tmp and (tmp - ar[i]) // minn == num - 1: print(i + 1, end="") tmp -= ar[i] num -= 1 break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR VAR VAR VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
__author__ = "runekri3" v = int(input()) digit_costs = [10**7] + list(map(int, input().split())) temp_list = list(enumerate(digit_costs[:])) temp_list.sort(key=lambda item: item[1], reverse=True) temp_list.reverse() digits_by_price = [digit for digit, cost in temp_list] cheapest_digit = digits_by_price[0] extra_costs = [(digit_cost - digit_costs[cheapest_digit]) for digit_cost in digit_costs] better_digits = [i for i in range(1, 10) if i > cheapest_digit] better_digits.sort(reverse=True) max_len = int(v / digit_costs[cheapest_digit]) if max_len > 0: current_number = [str(cheapest_digit)] * max_len leftover = v % digit_costs[cheapest_digit] for index_being_changed in range(max_len): for better_digit in better_digits: if extra_costs[better_digit] <= leftover: leftover -= extra_costs[better_digit] current_number[index_being_changed] = str(better_digit) break else: break else: current_number = "-1" print("".join(current_number))
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) arr = list(map(int, input().split())) min1 = min(arr) if v == 0: print("-1") elif min1 > v: print(-1) else: rem = v // min1 for i in range(rem, 0, -1): for j in range(9, 0, -1): if v - arr[j - 1] >= min1 * (i - 1): print(j, end="") v -= arr[j - 1] break print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) costs = list(map(int, input().split())) min_cost = min(costs) max_cost = max(costs) digit = -1 for i, c in enumerate(costs): if c == min_cost and i + 1 >= digit: digit = i + 1 length = v // min_cost number = [digit] * length leftover = v % min_cost change_costs = [(c - min_cost if c != min_cost else max_cost) for c in costs] position = 0 for digit in range(9, digit, -1): change_cost = change_costs[digit - 1] while leftover >= change_cost: number[position] = digit position += 1 leftover -= change_cost if len(number) == 0: print(-1) else: for digit in number: print(digit, end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = [int(x) for x in input().split()] mini = float("inf") for i in range(9): if a[i] <= mini: mini = a[i] ind = i if v < mini: print(-1) else: lena = v // mini ans = [ind + 1] * lena rem = v % mini x = 0 i = 9 while i > ind + 1: if a[i - 1] <= rem + mini: ans[x] = str(i) rem = rem + mini - a[i - 1] x += 1 if x == lena: break else: i -= 1 for i in ans: print(i, end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) c = [(0) for i in range(10)] globMin = float("inf") globMinIndex = -1 for i in range(9): if a[i] <= globMin: globMin = a[i] globMinIndex = i c[globMinIndex + 1] = v // globMin ans = v // globMin v = v % globMin for i in range(9, globMinIndex + 1, -1): if v <= 0: break j = i - 1 c[i] += v // (a[i - 1] - globMin) c[globMinIndex + 1] -= v // (a[i - 1] - globMin) v = v % (a[i - 1] - globMin) r = "" for i in range(9, 0, -1): r += str(i) * c[i] if ans == 0: print(-1) else: print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) ai = list(map(int, input().split())) x = ai.index(min(ai)) valofdi = ai[x] length = v // ai[x] if length == 0: print(-1) exit() val = list(str(str(x + 1) * length)) v -= length * valofdi for i in range(length): v += valofdi for ii in range(8, -1, -1): if ai[ii] <= v: val[i] = str(ii + 1) v -= ai[ii] break else: break print("".join(val))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) l = [*map(int, input().split())] minim = [10**18, 0] for i in range(9): if l[i] <= minim[0]: minim = [l[i], i + 1] leng, rem = divmod(n, minim[0]) if leng == 0: print(-1) exit() fin = [] cnt = 0 for i in range(8, minim[1] - 1, -1): if rem + minim[0] >= l[i]: tot, rem = divmod(rem, l[i] - minim[0]) fin += [str(i + 1)] * tot cnt += tot print("".join(fin + (leng - cnt) * [str(minim[1])]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR BIN_OP BIN_OP VAR VAR LIST FUNC_CALL VAR VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) m = min(a) if v < m: print(-1) else: a = [0] + a d = [0] * 10 k = 9 - a[::-1].index(m) n = v // m d[k] = n v -= n * m for i in range(0, n): for j in range(9, k, -1): if v >= a[j] - a[k]: v -= a[j] - m d[j] += 1 d[k] -= 1 break if v <= 0: break for i in range(9, k - 1, -1): print(str(i) * d[i], end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) a = list(map(int, input().split())) ans = [] minimum = 10000000000.0 min_val = -1 for i in range(0, len(a)): if a[i] <= minimum: minimum = a[i] min_val = i + 1 ans = [min_val] * (n // minimum) if min(a) > n: print(-1) elif min_val == 9 or n % minimum == 0: print("".join([str(x) for x in ans])) else: n = n % minimum x = min([(a[i] - minimum) for i in range(len(a)) if i > min_val - 1]) ind = 0 while n >= x: for i in range(9, min_val, -1): if a[i - 1] - a[min_val - 1] <= n: ans[ind] = i n = n - (a[i - 1] - a[min_val - 1]) ind += 1 break print("".join([str(x) for x in ans]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) arr = list(map(int, input().split())) max_lenth = v // min(arr) ind = arr.index(min(arr)) rem = v % min(arr) if v < min(arr): print(-1) exit() f = 0 for i in range(max_lenth): local = ind for j in range(8, ind, -1): if arr[j] - arr[ind] <= rem: rem -= arr[j] - arr[ind] local = j f = 1 break if f == 0: print(str(ind + 1) * max_lenth) exit() else: print(local + 1, end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) m, j = a[0], 0 for i, x in enumerate(a, 1): if x <= m: m, j = x, i x = int(v / m) if x == 0: print(-1) else: while x: x -= 1 i = 9 while i: i -= 1 if (v >= a[i]) & (int((v - a[i]) / m) == x): v -= a[i] print(i + 1, end="") break if i + 1 == j: break print(x * str(j))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) l = list(map(int, input().split())) m = min(l) if n < m: print("-1") elif n % m == 0: s = -1 for i in range(8, -1, -1): if l[i] == m: s = i + 1 break print(str(s) * int(n / m)) else: s = -1 for i in range(8, -1, -1): if l[i] == m: s = i + 1 break st = str(s) * int(n / m) st = list(st) n -= len(st) * m j = 8 index = 0 while n != 0 and j >= 0 and index < len(st): if l[j] <= m + n: st[index] = str(j + 1) index += 1 n = n + m - l[j] else: j -= 1 print("".join(st))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) i = 0 for j in range(1, len(a)): if a[j] <= a[i]: i = j n = v // a[i] s = a[i] * n b = [] while s <= v: j = len(a) - 1 while j > i: if s + (a[j] - a[i]) <= v: break j -= 1 if j == i: break s += a[j] - a[i] b += [j + 1] if s > 0: print("".join(map(str, b + [i + 1] * (n - len(b))))) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) a = list(map(int, input().split())) a = a[::-1] s = "" b = a[:] c = [] count = 1 for i in set(b): c.append(i) c.sort() if n < c[0]: print(-1) else: i = 0 r = n % c[0] while r > 0 and count > 0: count = 0 while i < a.index(c[0]): if c[0] + r >= a[i]: count += 1 r -= a[i] - c[0] s += str(9 - a.index(a[i])) n -= a[i] i = 0 continue i += 1 s += n // c[0] * str(9 - a.index(c[0])) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) if v >= min(a): n = max([i for i, j in enumerate(a) if j == min(a)]) + 1 res = [str(n)] * (v // min(a)) cost = v // min(a) * min(a) for i in range(len(res)): for j in range(8, int(res[i]) - 1, -1): if cost - a[int(res[i]) - 1] + a[j] <= v: cost += a[j] - a[int(res[i]) - 1] res[i] = str(j + 1) break print("".join(res)) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) l = list(map(int, input().split())) m = min(l) r1 = v // m num = r1 if num == 0: print(-1) else: while num: for i in range(8, -1, -1): if l[i] <= v and (v - l[i]) // m == num - 1: v = v - l[i] num = num - 1 print(i + 1, end="") break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
import sys v = int(input()) a = list(map(int, input().split())) ans = [] mini = sys.maxsize for i in range(8, -1, -1): if a[i] < mini: mini = a[i] ind = i if v < mini: print("-1") else: n = v // mini ans = [ind + 1] * n v = v % mini i = 0 while v > 0 and i < n: v = v + mini for j in range(8, ind - 1, -1): if a[j] <= v: ans[i] = j + 1 v = v - a[j] break i += 1 print("".join([str(elem) for elem in ans]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = [0] + list(map(int, input().split())) b = [(True) for _ in range(10)] for i in range(9, 0, -1): j = 1 while j < i and b[i]: if a[j] >= a[i]: b[j] = False j += 1 num = [] c = [] for i in range(1, 10): if b[i]: num.append(i) c.append(a[i]) dp = [[(0) for _ in range(len(num) + 2)] for _ in range(v + 1)] dp[0][0] = 1 m = 0 a = [] for i in range(1, v + 1): for j in range(len(num)): if i < c[j]: continue if dp[i - c[j]][0] == 1: if dp[i][0] != 1: for k in range(len(num) + 2): dp[i][k] = dp[i - c[j]][k] dp[i][-1] += 1 dp[i][j + 1] += 1 elif dp[i - c[j]][-1] + 1 >= dp[i][-1]: for k in range(len(num) + 2): dp[i][k] = dp[i - c[j]][k] dp[i][-1] += 1 dp[i][j + 1] += 1 if dp[i][-1] >= m: m = dp[i][-1] a = dp[i] if m == 0: print(-1) else: s = "" for i in range(1, len(a) - 1): s += str(num[i - 1]) * a[i] print(s[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
def f(t, k, d): j = k - 1 for i in range(k, 9): if d >= t[i]: j = i return d - t[j], j + 1 n = int(input()) t = list(map(int, input().split())) m, k = t[0], 1 for i, x in enumerate(t, 1): if x <= m: m, k = x, i if n < m: print(-1) else: d, j, s = n % m, k + 1, [] while j != k: d, j = f(t, k, d + m) s.append(j) print("".join(map(str, s)) + str(k) * (n // m - len(s)))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER LIST WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) w = list(map(int, input().split(" "))) idx = 0 val = w[0] for x in range(9): if val >= w[x]: val = w[x] idx = x dig = n // val ans = "" m = n % val if not dig: print(-1) else: for x in range(8, idx - 1, -1): if w[x] == w[idx]: continue asdf = m // (w[x] - w[idx]) ans += str(x + 1) * asdf m -= asdf * (w[x] - w[idx]) dig -= asdf print(ans + str(idx + 1) * dig)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
a = int(input()) A = list(map(int, input().split())) min = min(A) len = a // min if len == 0: print(-1) exit() rem = a % min word = "" S = [str(i) for i in range(1, 10)] for i in range(len): j = A.__len__() while j > 0: if A[j - 1] - min <= rem: rem -= A[j - 1] - min break j -= 1 word += S[j - 1] print(word)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) a = list(map(abs, map(int, input().split()))) if min(a) > n: print(-1) else: m = min(a) j = 8 while a[j] != m: j -= 1 c = n // m ans = [str(j + 1)] * c n -= c * a[j] i = 8 z = 0 while i > j and n != 0: if a[j] + n >= a[i]: while z < len(ans) and a[j] + n >= a[i]: n += a[j] n -= a[i] ans[z] = str(i + 1) z += 1 i -= 1 print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
paint = input() numbers = input().split(" ") def number(numbers, paint): min = 1 i = 1 while int(i < 10): if int(numbers[-i]) < int(numbers[-min]): min = int(i) i += 1 cheaper = int(paint) // int(numbers[-min]) if int(cheaper) == 0: return -1 extra = int(paint) % int(numbers[-min]) number = "" i = 1 while int(i) < int(min) and int(extra) > 0: if int(numbers[-i]) - int(numbers[-min]) <= int(extra): extra -= int(numbers[-i]) - int(numbers[-min]) number = str(number) + str(10 - i) cheaper -= 1 i = 1 else: i += 1 return str(number) + str(10 - min) * int(cheaper) print(number(numbers, paint))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = list(map(int, input().split())) m = min(a) n = v // m if n == 0: print(-1) exit() for i in range(9): if a[i] == m: d = i + 1 res = [d] * n v -= n * m for i in range(n): temp = -1 for j in range(d, 9): if a[j] <= v + m: temp = j if temp != -1: res[i] = temp + 1 v -= a[temp] - m print("".join(map(str, res)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) l = list(map(int, input().strip().split())) min1 = 10000000000 mini = 0 r = "" for i in range(9): if l[i] <= min1: min1 = l[i] mini = i digit = v // min1 left = v % min1 if digit == 0: print(-1) exit() while digit > 0 and left > 0: f = 0 for i in range(8, mini, -1): if left + min1 - l[i] >= 0: left = left + min1 - l[i] f = 1 r = r + str(i + 1) break if f == 0: break digit = digit - 1 r = r + digit * str(mini + 1) print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) a = [*map(int, input().split())] i = a[::-1].index(min(a)) if a[9 - i - 1] > n: print(-1) exit(0) ans = list(str(9 - i) * (n // a[9 - i - 1])) n = n % a[9 - i - 1] for k in range(len(ans)): if n <= 0: break for j in range(8, 9 - i - 1, -1): if a[j] - a[9 - i - 1] <= n: ans[k] = str(j + 1) n -= a[j] - a[9 - i - 1] break print("".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) a = [1000000000000] + list(map(int, input().split())) d = 0 for i in range(9, 0, -1): if a[i] == min(a): d = i break l = v // a[d] rest = v - l * a[d] essa = [] while rest: for i in range(9, d, -1): if a[i] - a[d] <= rest: rest -= a[i] - a[d] essa.append(str(i)) break else: break res = "".join(essa) + "".join([str(d)] * (l - len(essa))) print(res if res else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST WHILE VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL STRING VAR FUNC_CALL STRING BIN_OP LIST FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
n = int(input()) lis = list(map(int, input().split())) aa = lis.index(min(lis)) + 1 a = min(lis) no = list(str(aa) * (n // a)) if len(no) == 0: print(-1) exit() dig = n // a n -= dig * a for i in range(dig): ss = 0 for j in range(8, aa - 1, -1): if n + lis[int(no[i]) - 1] - lis[j] >= 0: n += lis[int(no[i]) - 1] - lis[j] no[i] = str(j + 1) ss = 1 break if ss == 0: break print("".join(no))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya's house. Igor thinks that the larger the number is, the more chance to win Tanya's heart he has. Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a_{d} liters of paint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igor won't use them in his number. Help Igor find the maximum number he can write on the fence. -----Input----- The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a_1, a_2, ..., a_9 (1 ≤ a_{i} ≤ 10^5). -----Output----- Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. -----Examples----- Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
v = int(input()) arr = list(map(int, input().split())) max_digits = max([(v // x) for x in arr]) smallest_v = min(arr) ans = list() if max_digits == 0: print(-1) exit() for digit in range(8, -1, -1): while max_digits > 0: if (max_digits - 1) * smallest_v + arr[digit] <= v: v -= arr[digit] ans.append(digit + 1) max_digits -= 1 else: break print("".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR