description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, a: List[int]) -> int: n = len(a) cache = [([[-1, -1]] * n) for i in range(n)] def dp(i, j): if i == j: return [0, a[i]] if cache[i][j] != [-1, -1]: return cache[i][j] ans = float("inf") mx = 0 for k in range(i, j): l = dp(i, k) r = dp(k + 1, j) mx = max(mx, l[1]) mx = max(mx, r[1]) ans = min(ans, l[0] + r[0] + l[1] * r[1]) cache[i][j] = [ans, mx] return [ans, mx] return dp(0, n - 1)[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN LIST NUMBER VAR VAR IF VAR VAR VAR LIST NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR RETURN LIST VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromSubArray(self, arr: List[int], l: int, r: int) -> int: if l == r - 1: return 0 if l == r - 2: return arr[l] * arr[l + 1] return min( max(arr[l:k]) * max(arr[k:r]) + self.mctFromSubArrayDp(arr, l, k) + self.mctFromSubArrayDp(arr, k, r) for k in range(l + 1, r) ) def mctFromSubArrayDp(self, arr: List[int], l: int, r: int) -> int: if (l, r) in self.dp: return self.dp[l, r] ans = self.mctFromSubArray(arr, l, r) self.dp[l, r] = ans return ans def mctFromLeafValues(self, arr: List[int]) -> int: self.dp = {} return self.mctFromSubArrayDp(arr, 0, len(arr))
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: res = 0 while len(arr) > 1: mid = arr.index(min(arr)) res += min(arr[mid - 1 : mid] + arr[mid + 1 : mid + 2]) * arr.pop(mid) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: if len(arr) <= 1: return 0 min_i = 0 min_val = arr[0] * arr[1] for i in range(1, len(arr) - 1): cur = arr[i] * arr[i + 1] if cur < min_val: min_val = cur min_i = i new_arr = arr[:min_i] + [max(arr[min_i], arr[min_i + 1])] + arr[min_i + 2 :] return min_val + self.mctFromLeafValues(new_arr)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) dp = [[(0) for _ in range(n)] for _ in range(n)] for i in range(n): dp[i][i] = 0 for i in range(n - 1): dp[i][i + 1] = arr[i] * arr[i + 1] for l in range(2, n + 1): for i in range(n - l): j = i + l dp[i][j] = float("inf") for k in range(i + 1, j + 1): q = dp[i][k - 1] + dp[k][j] + max(arr[i:k]) * max(arr[k : j + 1]) if q < dp[i][j]: dp[i][j] = q return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) dp = [[(0) for _ in range(n)] for _ in range(n)] def findMax(l, h): return max(arr[l : h + 1]) for l in range(1, n): for i in range(n - l): j = i + l for k in range(i, j): tmp = dp[i][k] + dp[k + 1][j] + findMax(i, k) * findMax(k + 1, j) dp[i][j] = tmp if dp[i][j] == 0 else min(dp[i][j], tmp) return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: res = 0 while len(arr) > 1: idx = arr.index(min(arr)) if 0 < idx < len(arr) - 1: res += min(arr[idx - 1], arr[idx + 1]) * arr[idx] arr = arr[:idx] + arr[idx + 1 :] elif idx == 0: res += arr[1] * arr[idx] arr = arr[1:] else: res += arr[-2] * arr[idx] arr = arr[:-1] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: @lru_cache(None) def cached_max(i, j): return max(arr[i:j] or [1]) @lru_cache(None) def dp(i, j): if j - i <= 1: return 0 elif j - i == 2: return arr[i] * arr[i + 1] return min( dp(i, k) + dp(k, j) + cached_max(i, k) * cached_max(k, j) for k in range(i + 1, j) ) return dp(0, len(arr))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR LIST NUMBER FUNC_CALL VAR NONE FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: self.cache = {} def dp(i, j): if (i, j) not in self.cache: if i == j: ret = arr[i], 0 else: best_p, best_s = 2**32, 2**32 for k in range(i, j): l1, s1 = dp(i, k) l2, s2 = dp(k + 1, j) if s1 + s2 + l1 * l2 < best_s: best_p = max(l1, l2) best_s = s1 + s2 + l1 * l2 ret = best_p, best_s self.cache[i, j] = ret return self.cache[i, j] return dp(0, len(arr) - 1)[-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) def partition(arr, left, right, cache): if (left, right) in cache: return cache[left, right] if left >= right: return 0 result = sys.maxsize for k in range(left, right): root_val = max(arr[left : k + 1]) * max(arr[k + 1 : right + 1]) result = min( result, root_val + partition(arr, left, k, cache) + partition(arr, k + 1, right, cache), ) cache[left, right] = result return result cache = {} partition(arr, 0, n - 1, cache) return cache[0, n - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: def to_remove(): cur_min = arr[0] * arr[1] res = 0 if arr[0] < arr[1] else 1 for i in range(len(arr) - 1): if arr[i] * arr[i + 1] < cur_min: cur_min = arr[i] * arr[i + 1] res = i if arr[i] < arr[i + 1] else i + 1 return res res = 0 while len(arr) > 1: i = to_remove() res += min(arr[i - 1 : i] + arr[i + 1 : i + 2]) * arr.pop(i) return res res = 0 while len(arr) > 1: i = arr.index(min(arr)) res += min(arr[i - 1 : i] + arr[i + 1 : i + 2]) * arr.pop(i) print(res, arr) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr): vals = [([0] * len(arr)) for i in range(len(arr))] maxes = [([0] * len(arr)) for i in range(len(arr))] for n in range(1, len(arr) + 1): for a in range(len(arr) - n + 1): b = a + n maxes[a][b - 1] = max(maxes[a][b - 2], arr[b - 1]) if n >= 2: vals[a][b - 1] = min( vals[a][i - 1] + vals[i][b - 1] + maxes[a][i - 1] * maxes[i][b - 1] for i in range(a + 1, b) ) print(vals) return vals[0][len(arr) - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: dp = [([0] * len(arr)) for i in range(len(arr))] for i in range(len(arr) - 1, -1, -1): for j in range(i + 1, len(arr)): if j - i == 1: dp[i][j] = arr[i] * arr[j] continue m = 2**32 for k in range(i + 1, j + 1): s = max(arr[i:k]) * max(arr[k : j + 1]) + dp[i][k - 1] + dp[k][j] if s < m: m = s dp[i][j] = m return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): low, high = 0, 0 for c in s: if c == "(": low += 1 high += 1 elif c == ")": if low > 0: low -= 1 high -= 1 else: if low > 0: low -= 1 high += 1 if high < 0: return False return low == 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): rem = {} def dfs(i, c): if c < 0: return False elif i == len(s): return not c elif (i, c) in rem: return rem[i, c] elif s[i] == "(": rem[i, c] = dfs(i + 1, c + 1) elif s[i] == ")": rem[i, c] = dfs(i + 1, c - 1) else: rem[i, c] = dfs(i + 1, c) or dfs(i + 1, c - 1) or dfs(i + 1, c + 1) return rem[i, c] return dfs(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): cmin = cmax = 0 for i in s: if i == "(": cmax += 1 cmin += 1 if i == ")": cmax -= 1 cmin = max(cmin - 1, 0) if i == "*": cmax += 1 cmin = max(cmin - 1, 0) if cmax < 0: return False return cmin == 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): lo, hi = 0, 0 for c in s: lo += 1 if c == "(" else -1 hi += 1 if c != ")" else -1 if hi < 0: break lo = max(lo, 0) return lo == 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR STRING NUMBER NUMBER VAR VAR STRING NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): open = 0 star = 0 count = {"(": 0, ")": 0, "*": 0} newS = [] for i in s: if i == ")": if open > 0: open -= 1 del newS[len(newS) - 1 - newS[::-1].index("(")] elif star > 0: star -= 1 del newS[len(newS) - 1 - newS[::-1].index("*")] else: return False elif i == "(": open += 1 newS += "(" else: star += 1 newS += "*" if star >= open: o = 0 s = 0 for i in newS: if i == "(": o += 1 elif i == "*": if o > 0: o -= 1 if o != 0: return False else: return True else: return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER STRING IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER STRING RETURN NUMBER IF VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): a = 0 b = 0 for i in range(len(s)): if s[i] == "*": a = a + 1 if b > 0: b = b - 1 elif s[i] == "(": a = a + 1 b = b + 1 elif s[i] == ")": a = a - 1 if b > 0: b = b - 1 if a < 0: return False if b == 0: return True else: return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. Left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string. An empty string is also valid. Example 1: Input: "()" Output: True Example 2: Input: "(*)" Output: True Example 3: Input: "(*))" Output: True Note: The string size will be in the range [1, 100].
class Solution: def checkValidString(self, s): l = r = star = 0 for c in s: if c == "(": l += 1 if c == ")": r += 1 if c == "*": star += 1 if r > star + l: return False l = r = star = 0 for c in s[::-1]: if c == "(": l += 1 if c == ")": r += 1 if c == "*": star += 1 if l > star + r: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if len(nums) == 1: return 0 else: step = 0 pos = 0 while pos != len(nums) - 1: bestStep = -1 bestValue = -1 for i in range(nums[pos], 0, -1): if len(nums) - 1 == pos + i: bestStep = i break if ( pos + i < len(nums) and nums[pos + i] != 0 and nums[pos + i] + i > bestValue ): bestStep = i bestValue = nums[pos + i] + i print(bestStep) pos += bestStep step += 1 return step
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): res = 0 n = len(nums) i, j = 0, 0 while j < n - 1: print((i, j)) ii, jj = i, j for k in range(ii, jj + 1): j = max(j, min(n - 1, k + nums[k])) i = jj + 1 res += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): p = [0] for i in range(len(nums) - 1): while i + nums[i] >= len(p) and len(p) < len(nums): p.append(p[i] + 1) return p[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if len(nums) == 0 or len(nums) == 1: return 0 s = 0 e = 0 step = 0 while e < len(nums) - 1: fst = e step += 1 for i in range(s, e + 1): if fst < nums[i] + i: fst = nums[i] + i s = e + 1 e = fst return step
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if len(nums) <= 2: return len(nums) - 1 stack = [] i = len(nums) - 2 while i >= 0: if nums[i] == 1: stack.append(i) elif nums[i] >= len(nums) - 1 - i: if stack: stack.clear() stack.append(i) else: j = 0 while stack and j < len(stack): if nums[i] >= stack[j] - i: stack = stack[0 : j + 1] stack.append(i) break else: j += 1 i -= 1 return len(stack)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if not nums: return 0 if len(nums) == 1: return 0 step, far, maxV = 0, 0, 0 for i in range(len(nums)): if i + nums[i] > maxV and i + nums[i] >= len(nums) - 1: return step + 1 maxV = max(maxV, i + nums[i]) if i == far: step += 1 far = maxV return step
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): L = len(nums) lo = 0 hi = 0 jumps = 0 while hi < L - 1: jumps += 1 nexthi = hi for i in range(lo, hi + 1): nexthi = max(nexthi, i + nums[i]) if nexthi >= L - 1: return jumps lo, hi = hi + 1, nexthi return jumps
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): n, start, end, step = len(nums), 0, 0, 0 while end < n - 1: step += 1 maxend = end + 1 for i in range(start, end + 1): if i + nums[i] >= n - 1: return step maxend = max(maxend, i + nums[i]) start, end = end + 1, maxend return step
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): cur = 0 ans = 0 maxstep = 0 for i in range(0, len(nums) - 1): maxstep = max(maxstep, i + nums[i]) if i == cur: ans += 1 cur = maxstep return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if not nums: return 0 dic = {} length = len(nums) dic[length - 1] = 0 for j in range(length - 1)[::-1]: if nums[j] >= nums[j + 1] + 1 and not j + 1 == length - 1: minstep = dic[j + 1] - 1 for k in range(max(3, nums[j + 1] + 2), min(length - j, nums[j] + 1)): if dic[j + k] < minstep: minstep = dic[j + k] dic[j] = minstep + 1 continue minstep = dic[j + 1] for k in range(2, min(length - j, nums[j] + 1)): if dic[j + k] < minstep: minstep = dic[j + k] dic[j] = minstep + 1 return dic[0]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): if nums is None or len(nums) <= 1: return 0 start = currEnd = nextEnd = jump = 0 while currEnd - start + 1 > 0: jump += 1 for i in range(start, currEnd + 1): nextEnd = max(nums[i] + i, nextEnd) if nextEnd >= len(nums) - 1: return jump start = currEnd + 1 currEnd = nextEnd return jump
CLASS_DEF FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. Example: Input: [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Note: You can assume that you can always reach the last index.
class Solution: def jump(self, nums): current_step = 0 last_step = 0 i = 0 n_jumps = 0 while last_step < len(nums) - 1: while i <= last_step: current_step = max(i + nums[i], current_step) i += 1 last_step = current_step n_jumps += 1 return n_jumps
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() n = len(s) s = [el for el in s] diff = [] for i, el in enumerate(s): if s[i] == "1": diff.append((i + 1) * (n - i)) diff[-1] *= -1 else: diff.append((i + 1) * (n - i)) ans = abs(sum([el for el in diff if el <= 0])) l = 0 temp = 0 res = 0 for r in range(n): temp += diff[r] while temp < 0: temp -= diff[l] l += 1 res = max(res, temp) print(ans + res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() n = len(s) res = maxi = ans = 0 for i in range(n): sub = (i + 1) * (n - i) if s[i] == "1": ans += sub sub = sub * -1 res += sub res = max(res, 0) maxi = max(maxi, res) print(maxi + ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
def kadanes(a, size): max_so_far = -float("inf") max_ending_here = 0 start = 0 end = 0 s = 0 for i in range(0, size): max_ending_here += a[i] if max_so_far < max_ending_here: max_so_far = max_ending_here start = s end = i if max_ending_here < 0: max_ending_here = 0 s = i + 1 return max_so_far, start, end for _ in range(int(input())): s = input() n = len(s) ans = 0 for i in range(n): if s[i] == "1": ans += (i + 1) * (n - i) arr = [] for i in range(n): if s[i] == "1": arr.append(-((i + 1) * (n - i))) else: arr.append((i + 1) * (n - i)) maxx, start, end = kadanes(arr, len(arr)) print(ans + max(0, maxx))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() ans = mx = cur = 0 for i in range(len(s)): subarrays = (i + 1) * (len(s) - i) if s[i] == "1": ans += subarrays subarrays *= -1 cur += subarrays cur = max(cur, 0) mx = max(mx, cur) print(ans + mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
def maxSubarraySum(l): localMax = globalMax = l[0] for i in range(1, len(l)): localMax = max(l[i], localMax + l[i]) if localMax > globalMax: globalMax = localMax return globalMax for _ in range(int(input())): s = input() l = [] initialSum = 0 n = len(s) for i in range(n): f = (i + 1) * (n - i) if s[i] == "1": initialSum += f l.append(-f) else: l.append(f) m = maxSubarraySum(l) if m > 0: initialSum += m print(initialSum)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
t = int(input()) for _ in range(t): s = input() cm_array = [] cm = 0 pv = s[0] n = len(s) tot = 0 for i in range(n): v = s[i] if v == "1": tot += (i + 1) * (n - i) if v == pv: cm += (i + 1) * (n - i) else: if pv == "1": cm = -cm cm_array.append(cm) pv = v cm = (i + 1) * (n - i) if pv == "1": cm = -cm cm_array.append(cm) o_max = 0 ct = 0 for v in cm_array: ct += v if ct > o_max: o_max = ct if ct <= 0: ct = 0 ans = tot + o_max print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for tea in range(int(input())): s = [int(qwera) for qwera in list(input())] n = len(s) values = [] for i in range(n): if s[i] == 1: values.append(-(i + 1) * (n - i)) else: values.append((i + 1) * (n - i)) curr = sum([(-values[i] if s[i] == 1 else 0) for i in range(n)]) pfs = [0] for j in values: pfs.append(pfs[-1] + j) mini = 0 best = 0 for k in range(1, n + 1): best = max(best, pfs[k] - mini) mini = min(pfs[k], mini) print(best + curr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
T = int(input()) for t in range(T): S = [int(x) for x in input()] N = len(S) G = [(i * (N - i + 1)) for i in range(1, N + 1)] G_nop = [(G[i - 1] if S[i - 1] == 1 else 0) for i in range(1, N + 1)] s = sum(G_nop) max_diff = 0 cur_diff = 0 for i in range(N): if S[i] == 0: cur_diff += G[i] max_diff = max(max_diff, cur_diff) else: cur_diff -= G[i] cur_diff = max(cur_diff, 0) print(s + max_diff)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
t = int(input()) def getctr(n): res = [0] * n i = 0 j = n - 1 v = n while i <= j: if i > 0: res[i] = res[i - 1] + v res[j] = res[j + 1] + v else: res[i] = n res[j] = n i += 1 j -= 1 v -= 2 return res for _ in range(t): s = list(map(int, input())) n = len(s) ctr = getctr(n) p = [0] pall = [0] for i in range(n): p.append(p[-1] + ctr[i] * s[i]) pall.append(pall[-1] + ctr[i]) newp = [(pall[i] - 2 * p[i]) for i in range(n + 1)] res = p[-1] minyet = float("inf") for el in newp: minyet = min(minyet, el) res = max(res, el - minyet + p[-1]) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() n = len(s) arr = [0] * (n + 1) bla = 0 for i in range(1, n + 1): if s[i - 1] == "1": arr[i] = -1 * i * (n - i + 1) bla += i * (n - i + 1) else: arr[i] = i * (n - i + 1) fin_ans = 0 dp = [0] * (n + 1) for i in range(1, n + 1): x = s[i - 1] dp[i] = max(dp[i - 1] + arr[i], arr[i]) fin_ans = max(fin_ans, dp[i]) print(bla + fin_ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
t = int(input()) for i in range(t): s = list(input()) n = len(s) score = 0 for j in range(n): if s[j] == "1": score = score + (j + 1) * (n - j) maxscorediff = 0 max_ending_here = 0 for j in range(n): diff = (j + 1) * (n - j) if s[j] == "1": diff = -diff max_ending_here = max_ending_here + diff if maxscorediff < max_ending_here: maxscorediff = max_ending_here if max_ending_here < 0: max_ending_here = 0 print(score + maxscorediff)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() con = [] ans = 0 for i in range(len(s)): con.append((i + 1) * (len(s) - i)) if s[i] == "1": ans += con[i] con[i] *= -1 maxi = 0 curr = 0 for i in range(len(con)): curr += con[i] if curr < 0: curr = 0 maxi = max(maxi, curr) print(ans + maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
iter = int(input()) for z in range(iter): s = input() n = len(s) score = [0] * n c = 0 m = 0 neg = 0 ans = 0 for i in range(len(s)): score[i] = (i + 1) * (n - i) if s[i] == "1": ans += score[i] score[i] = -score[i] for i in range(n): c += score[i] if score[i] < 0: neg += score[i] if c <= 0: c = 0 neg = 0 m = max(m, c) print(ans + m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
t = int(input()) for _ in range(t): s = list(map(int, list(input()))) n = len(s) a = [0] * n c = 0 for i in range(n + 1 >> 1): j = (i + 1) * (n - i) c += j * s[i] m = -1 if s[i] else 1 a[i] = m * j if i != n - i - 1: c += j * s[n - i - 1] m = -1 if s[n - i - 1] else 1 a[n - i - 1] = m * j p = 0 q = 0 for x in a: p = max(0, p + x) q = max(q, p) print(c + q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
test = int(input()) while test: s = input() n = len(s) contribution = [] original_score = 0 for i in range(n): contribution.append((i + 1) * (n - i)) if s[i] == "1": original_score += contribution[i] contribution[i] *= -1 maxi = curr_sum = 0 for i in contribution: curr_sum += i if curr_sum < 0: curr_sum = 0 maxi = max(curr_sum, maxi) if maxi > 0: print(original_score + maxi) else: print(original_score) test -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
t = int(input()) for i in range(t): s = input() n = len(s) res = 0 lst = [] for i in range(n): if s[i] == "0": lst.append((i + 1) * (n - i)) else: lst.append(-(i + 1) * (n - i)) res += (i + 1) * (n - i) d = 0 cur = 0 for i in range(n): cur += lst[i] if cur <= 0: cur = 0 else: d = max(d, cur) print(res + d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
from sys import stdin input = stdin.readline def kadane(A): max_so_far = 0 curr_max = 0 N = len(A) for i in range(N): curr_max += A[i] if curr_max < 0: curr_max = 0 elif max_so_far < curr_max: max_so_far = curr_max return max_so_far def solve(S): N = len(S) S = [int(s) for s in S] total = sum(S[i] * (i + 1) * (N - i) for i in range(N)) flip = [((1 - 2 * S[i]) * (i + 1) * (N - i)) for i in range(N)] return total + kadane(flip) T = int(input().strip()) for problem in range(1, T + 1): S = input().strip() print(solve(S))
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
maxint = 100000000000 def maxSubArraySum(a, size): max_so_far = -maxint - 1 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if max_so_far < max_ending_here: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far for t in range(int(input())): s = str(input()) a = [] n = len(s) if n % 2 == 0: a.append(n) for i in range(n - 2, 1, -2): a.append(a[-1] + i) a += reversed(a) else: a.append(n) for i in range(n - 2, 0, -2): a.append(a[-1] + i) a += reversed(a) a.pop(len(a) // 2) total = 0 for i in range(n): if s[i] == "1": total += a[i] a[i] = -a[i] print(total + max(0, maxSubArraySum(a, n)))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): S = input() ln = len(S) tot = 0 mult = [0] * ln for i in range(ln): t = (i + 1) * (ln - i) mult[i] = t if S[i] == "1": tot += t mult[i] = -t maxSoFar = -999999999999999 maxEndingHere = 0 for i in range(t): maxEndingHere += mult[i] if maxEndingHere > maxSoFar: maxSoFar = maxEndingHere if maxEndingHere < 0: maxEndingHere = 0 if maxSoFar < 0: maxSoFar = 0 print(tot + maxSoFar)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
for _ in range(int(input())): s = input() n = len(s) arr = [] prev, ind = 0, 0 bow = [] for i in range(n): front = prev bow.append(int(s[i]) * (i + 1) * (n - i)) if s[i] == "1": prev -= (i + 1) * (n - i) else: prev += (i + 1) * (n - i) prev = max(prev, 0) if prev == 0 or front == 0: ind = i arr.append([prev, ind, i]) maxy, start, end = max(arr, key=lambda x: x[0]) print(sum(bow) + maxy)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given a binary string S. You are allowed to perform the following operation at most once: Pick some [substring] of S Flip all the values in this substring, i.e, convert 0 to 1 and vice versa For example, if S = 1\underline{00101}011, you can pick the underlined substring and flip it to obtain S = 1\underline{11010}011. For the substring of S consisting of all the positions from L to R, we define a function f(L, R) to be the number of 1's in this substring. For example, if S = 100101011, then f(2, 5) = 1 and f(4, 9) = 4 (the respective substrings are 0010 and 101011). If you perform the given operation optimally, find the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. Note that the substring flip operation can be performed at most once. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of single line of input, containing a binary string S. ------ Output Format ------ For each test case, output on a new line the maximum possible value of \sum_{L=1}^N \sum_{R=L}^N f(L, R) that can be obtained. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ |S| ≤ 3\cdot 10^{5}$ - The sum of $|S|$ over all test cases won't exceed $3\cdot 10^{5}$. ----- Sample Input 1 ------ 3 111 000 00100 ----- Sample Output 1 ------ 10 10 26 ----- explanation 1 ------ Test case $1$: There is no need to apply the operation since everything is already a $1$. The answer is thus the sum of: - $f(1, 1) = 1$ - $f(1, 2) = 2$ - $f(1, 3) = 3$ - $f(2, 2) = 1$ - $f(2, 3) = 2$ - $f(3, 3) = 1$ which is $10$. Test case $2$: Flip the entire string to obtain $111$, whose answer has been computed above. Test case $3$: Flip the entire string to obtain $11011$. The sum of $f(L, R)$ across all substrings is now $26$, which is the maximum possible.
def mi(): return map(int, input().split()) def li(): return list(mi()) def si(): return str(input()) def ni(): return int(input()) for _ in range(ni()): s = input() ans = mx = cur = 0 for i in range(len(s)): subarrays = (i + 1) * (len(s) - i) if s[i] == "1": ans += subarrays subarrays *= -1 cur += subarrays cur = max(cur, 0) mx = max(mx, cur) print(ans + mx)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Mandarin], [Vietnamese], and [Russian] as well. Miana and Serdenopis have guests for dinner tonight and thus they have prepared N pairs of slippers for their guests (who shall take off their shoes before entering the house). The slippers are on a line and the i-th pair of slippers has size S_{i}. When a guest enters, he takes the first pair of slippers which are still in the line (i.e., they are not taken by a previous guest) and are big enough for him (that is, the slippers' size must be larger or equal than his shoes' size) and does not touch any other pair of slippers. If he cannot find any fitting slippers, he does not take any pair of slippers. Miana and Serdenopis do not remember how many guests they will have for dinner (and of course, they don't know their shoes' sizes either), but they are interested in the possible subsets of taken slippers. A (possibly empty) subset of the slippers is interesting if there is a configuration of guests such that all the slippers in the subset are taken and all the slippers not in the subset are not taken. Count the number of interesting subsets. Since this number may be very large, compute it modulo 998\,244\,353. ------ Input Format ------ - The first line contains a single integer N, the number of pairs of slippers. - The second line contains N integers S_{1},S_{2},\ldots, S_{N}, where the i-th pair of slippers in the line has size S_{i}. ------ Output Format ------ Print the number of interesting subsets modulo 998\,244\,353. ------ Constraints ------ $1≤N ≤200\,000$ $1≤S_{i}≤200\,000$ ----- Sample Input 1 ------ 3 38 43 41 ----- Sample Output 1 ------ 6 ----- explanation 1 ------ Explanation of the first testcase: The sizes of the slippers are, in order, $[38, 43, 41]$. The $6$ interesting subsets are (the slippers in the interesting subset are given in bold): $[38, 43, 41]$ (the empty subset is interesting) $[\textbf{38}, 43, 41]$ (only the first pair of slippers is taken) $[38, \textbf{43}, 41]$ (only the second pair of slippers is taken) $[\textbf{38}, \textbf{43}, 41]$ $[38, \textbf{43}, \textbf{41}]$ $[\textbf{38}, \textbf{43}, \textbf{41}]$ (all slippers are taken) Let us describe, for certain configurations of guests, which slippers are taken: If there is only one guest and his shoes' size is $44$, he will not take any pair of slippers because they are too small for him. The subset of taken slippers is the empty set in this case. Assume that there are two guests, the first one has shoes' size $41$ and the second one has shoes' size $42$. The first guest takes the second pair of slippers (with size $43$) and the second guest does not take any pair of slippers because the remaining slippers are too small for him. The subset of taken slippers is $[38, \textbf{43}, 41]$ in this case. Assume that there are two guests, the first one has shoes' size $42$ and the second one has shoes' size $41$. The first guest takes the second pair of slippers (with size $43$) and the second guest takes the third pair of slippers (with size $41$). The subset of taken slippers is $[38, \textbf{43}, \textbf{41}]$ in this case. Assume that there are $100$ guests, all with shoes' size equal to $36$. The first three guests take, in order, the three available pairs of slippers and the remaining guests take no slippers. The subset of taken slippers is $[\textbf{38}, \textbf{43}, \textbf{41}]$ in this case. ----- Sample Input 2 ------ 8 42 42 43 37 38 38 41 42 ----- Sample Output 2 ------ 29 ----- explanation 2 ------
n = int(input()) s = list(map(int, input().split())) a = sorted([s[i], -i] for i in range(n)) index = [-1] * n for i in range(n): index[-a[i][1]] = i mod = 998244353 size = 1 << n.bit_length() + 1 tree = [0] * (2 * size + 1) res = 0 for i in range(n): small_sum = 0 j = index[i] + size l, r = size, j - 1 while l < r: if l & 1: small_sum = (small_sum + tree[l]) % mod l += 1 if r & 1 == 0: small_sum = (small_sum + tree[r]) % mod r -= 1 l //= 2 r //= 2 small_sum = (small_sum + tree[l]) % mod res = (res + small_sum + 1) % mod tree[j] = (small_sum + 1) % mod while j // 2 > 0: j //= 2 tree[j] = (tree[j * 2] + tree[j * 2 + 1]) % mod print((res + 1) % mod)
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 LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Read problem statements in [Mandarin], [Vietnamese], and [Russian] as well. Miana and Serdenopis have guests for dinner tonight and thus they have prepared N pairs of slippers for their guests (who shall take off their shoes before entering the house). The slippers are on a line and the i-th pair of slippers has size S_{i}. When a guest enters, he takes the first pair of slippers which are still in the line (i.e., they are not taken by a previous guest) and are big enough for him (that is, the slippers' size must be larger or equal than his shoes' size) and does not touch any other pair of slippers. If he cannot find any fitting slippers, he does not take any pair of slippers. Miana and Serdenopis do not remember how many guests they will have for dinner (and of course, they don't know their shoes' sizes either), but they are interested in the possible subsets of taken slippers. A (possibly empty) subset of the slippers is interesting if there is a configuration of guests such that all the slippers in the subset are taken and all the slippers not in the subset are not taken. Count the number of interesting subsets. Since this number may be very large, compute it modulo 998\,244\,353. ------ Input Format ------ - The first line contains a single integer N, the number of pairs of slippers. - The second line contains N integers S_{1},S_{2},\ldots, S_{N}, where the i-th pair of slippers in the line has size S_{i}. ------ Output Format ------ Print the number of interesting subsets modulo 998\,244\,353. ------ Constraints ------ $1≤N ≤200\,000$ $1≤S_{i}≤200\,000$ ----- Sample Input 1 ------ 3 38 43 41 ----- Sample Output 1 ------ 6 ----- explanation 1 ------ Explanation of the first testcase: The sizes of the slippers are, in order, $[38, 43, 41]$. The $6$ interesting subsets are (the slippers in the interesting subset are given in bold): $[38, 43, 41]$ (the empty subset is interesting) $[\textbf{38}, 43, 41]$ (only the first pair of slippers is taken) $[38, \textbf{43}, 41]$ (only the second pair of slippers is taken) $[\textbf{38}, \textbf{43}, 41]$ $[38, \textbf{43}, \textbf{41}]$ $[\textbf{38}, \textbf{43}, \textbf{41}]$ (all slippers are taken) Let us describe, for certain configurations of guests, which slippers are taken: If there is only one guest and his shoes' size is $44$, he will not take any pair of slippers because they are too small for him. The subset of taken slippers is the empty set in this case. Assume that there are two guests, the first one has shoes' size $41$ and the second one has shoes' size $42$. The first guest takes the second pair of slippers (with size $43$) and the second guest does not take any pair of slippers because the remaining slippers are too small for him. The subset of taken slippers is $[38, \textbf{43}, 41]$ in this case. Assume that there are two guests, the first one has shoes' size $42$ and the second one has shoes' size $41$. The first guest takes the second pair of slippers (with size $43$) and the second guest takes the third pair of slippers (with size $41$). The subset of taken slippers is $[38, \textbf{43}, \textbf{41}]$ in this case. Assume that there are $100$ guests, all with shoes' size equal to $36$. The first three guests take, in order, the three available pairs of slippers and the remaining guests take no slippers. The subset of taken slippers is $[\textbf{38}, \textbf{43}, \textbf{41}]$ in this case. ----- Sample Input 2 ------ 8 42 42 43 37 38 38 41 42 ----- Sample Output 2 ------ 29 ----- explanation 2 ------
c = 200000 mod = 998244353 st = [0] * (4 * c) def update(idx, ss, se, summ, num): if ss == se and ss == num: st[idx] = (st[idx] + summ) % mod return mid = (ss + se) // 2 if mid >= num: update(2 * idx + 1, ss, mid, summ, num) else: update(2 * idx + 2, mid + 1, se, summ, num) st[idx] = (st[idx * 2 + 1] + st[idx * 2 + 2]) % mod def rangesum(idx, ss, se, rs, re): if rs <= ss and se <= re: return st[idx] elif re < ss or se < rs: return 0 else: mid = (ss + se) // 2 return ( rangesum(2 * idx + 1, ss, mid, rs, re) + rangesum(2 * idx + 2, mid + 1, se, rs, re) ) % mod n = int(input()) s = list(map(int, input().split())) for i in range(n): count = 1 if s[i] != 1: count = (1 + rangesum(0, 1, 200000, 1, s[i] - 1)) % mod update(0, 1, 200000, count, s[i]) print((st[0] + 1) % mod)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR
Read problem statements in [Mandarin], [Vietnamese], and [Russian] as well. Miana and Serdenopis have guests for dinner tonight and thus they have prepared N pairs of slippers for their guests (who shall take off their shoes before entering the house). The slippers are on a line and the i-th pair of slippers has size S_{i}. When a guest enters, he takes the first pair of slippers which are still in the line (i.e., they are not taken by a previous guest) and are big enough for him (that is, the slippers' size must be larger or equal than his shoes' size) and does not touch any other pair of slippers. If he cannot find any fitting slippers, he does not take any pair of slippers. Miana and Serdenopis do not remember how many guests they will have for dinner (and of course, they don't know their shoes' sizes either), but they are interested in the possible subsets of taken slippers. A (possibly empty) subset of the slippers is interesting if there is a configuration of guests such that all the slippers in the subset are taken and all the slippers not in the subset are not taken. Count the number of interesting subsets. Since this number may be very large, compute it modulo 998\,244\,353. ------ Input Format ------ - The first line contains a single integer N, the number of pairs of slippers. - The second line contains N integers S_{1},S_{2},\ldots, S_{N}, where the i-th pair of slippers in the line has size S_{i}. ------ Output Format ------ Print the number of interesting subsets modulo 998\,244\,353. ------ Constraints ------ $1≤N ≤200\,000$ $1≤S_{i}≤200\,000$ ----- Sample Input 1 ------ 3 38 43 41 ----- Sample Output 1 ------ 6 ----- explanation 1 ------ Explanation of the first testcase: The sizes of the slippers are, in order, $[38, 43, 41]$. The $6$ interesting subsets are (the slippers in the interesting subset are given in bold): $[38, 43, 41]$ (the empty subset is interesting) $[\textbf{38}, 43, 41]$ (only the first pair of slippers is taken) $[38, \textbf{43}, 41]$ (only the second pair of slippers is taken) $[\textbf{38}, \textbf{43}, 41]$ $[38, \textbf{43}, \textbf{41}]$ $[\textbf{38}, \textbf{43}, \textbf{41}]$ (all slippers are taken) Let us describe, for certain configurations of guests, which slippers are taken: If there is only one guest and his shoes' size is $44$, he will not take any pair of slippers because they are too small for him. The subset of taken slippers is the empty set in this case. Assume that there are two guests, the first one has shoes' size $41$ and the second one has shoes' size $42$. The first guest takes the second pair of slippers (with size $43$) and the second guest does not take any pair of slippers because the remaining slippers are too small for him. The subset of taken slippers is $[38, \textbf{43}, 41]$ in this case. Assume that there are two guests, the first one has shoes' size $42$ and the second one has shoes' size $41$. The first guest takes the second pair of slippers (with size $43$) and the second guest takes the third pair of slippers (with size $41$). The subset of taken slippers is $[38, \textbf{43}, \textbf{41}]$ in this case. Assume that there are $100$ guests, all with shoes' size equal to $36$. The first three guests take, in order, the three available pairs of slippers and the remaining guests take no slippers. The subset of taken slippers is $[\textbf{38}, \textbf{43}, \textbf{41}]$ in this case. ----- Sample Input 2 ------ 8 42 42 43 37 38 38 41 42 ----- Sample Output 2 ------ 29 ----- explanation 2 ------
MOD = 998244353 MAX = 1000010 sz = 400000 bTree = [] def summ(index): sum = 0 index += 1 while index > 0: sum += bTree[index] sum %= MOD index -= index & -index return sum % MOD def updateBit(n, idx, v): idx += 1 while idx <= n: bTree[idx] += v bTree[idx] %= MOD idx += idx & -idx n = int(input()) s = list(map(int, input().split())) bTree = [(0) for _ in range(MAX)] for i in range(n): updateBit(sz, s[i], summ(s[i] - 1) + 1) print((summ(sz) + 1) % MOD)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR 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 VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution: def splitArray(self, nums, m): accum = [0] N = len(nums) mmm = max(nums) if m >= N: return mmm res = 0 for i in nums: res += i accum.append(res) lower, upper = mmm, sum(nums) while lower < upper: mid = (lower + upper) // 2 if not self.isSplitable(accum, m, mid): lower = mid + 1 else: upper = mid return upper def isSplitable(self, accum, m, maxx): start = 0 N = len(accum) end = 0 count = 0 while end < N and count < m: if accum[end] - accum[start] > maxx: start = end - 1 count += 1 end += 1 if accum[-1] - accum[start] > maxx: count += 2 else: count += 1 if end != N or count > m: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution: def splitArray(self, pages, k): if not pages or k == 0: return 0 pre = sums = [0] + pages[:] for i in range(1, len(sums)): sums[i] += sums[i - 1] for n in range(2, k + 1): if n > len(pages): break cur = [sys.maxsize] * len(pre) i, j = n - 1, n while j < len(pre): left, right = pre[i], sums[j] - sums[i] cur[j] = min(cur[j], max(left, right)) if i + 1 < j and max(pre[i + 1], sums[j] - sums[i + 1]) < cur[j]: i += 1 else: j += 1 pre = cur return pre[-1]
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution(object): def splitArray(self, nums, m): def valid(mid): cnt = 0 current = 0 for n in nums: current += n if current > mid: cnt += 1 if cnt >= m: return False current = n return True l = max(nums) h = sum(nums) while l < h: mid = l + (h - l) / 2 if valid(mid): h = mid else: l = mid + 1 return int(l)
CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution: def splitArray(self, nums, m): _sum = sum(nums) avg = _sum / m self.result = 10**10 n = len(nums) cache = {} def recur(idx, m): if idx == n: return 0 if m == 0: return 10**10 if (idx, m) in cache: return cache[idx, m] _sum = 0 result = 10**10 i = idx while i < n: _sum += nums[i] if i == n - 1 or _sum + nums[i + 1] > avg: tmp = recur(i + 1, m - 1) result = min(result, max(tmp, _sum)) if _sum > tmp: cache[idx, m] = result return result i += 1 cache[idx, m] = result return result x = recur(0, m) return x
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP NUMBER NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution: def splitArray(self, nums, m): return self.use_binary_search(nums, m) def use_binary_search(self, nums, m): lo, hi = max(nums), sum(nums) while lo < hi: mid = lo + (hi - lo) // 2 if self.valid(mid, nums, m): hi = mid else: lo = mid + 1 return lo def valid(self, target, nums, m): total, count = 0, 1 for num in nums: total += num if total > target: total = num count += 1 if count > m: return False return True
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 1000 1 ≤ m ≤ min(50, n) Examples: Input: nums = [7,2,5,10,8] m = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
class Solution: def soln_exists(self, nums, m, i): index = 0 for x in range(m): total = 0 while True: total += nums[index] index += 1 if total > i: break if index == len(nums): return True index -= 1 return False def splitArray(self, nums, m): work = 1 while not self.soln_exists(nums, m, work): work *= 2 not_work = work / 2 while not_work < work - 1: middle = not_work / 2 + work / 2 if self.soln_exists(nums, m, middle): work = middle else: not_work = middle return int(work)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a sequence of integers $A_{1}, A_{2}, \ldots, A_{N}$. He takes a sheet of paper and for each non-empty subsequence $B$ of this sequence, he does the following: 1. For each integer which appears in $B$, count its number of occurrences in the sequence $B$. 2. Choose the integer with the largest number of occurrences. If there are several options, choose the smallest one. 3. Finally, write down the chosen integer (an element of $B$) on the sheet of paper. For each integer between $1$ and $N$ inclusive, find out how many times it appears on Chef's sheet of paper. Since these numbers may be very large, compute them modulo $1,000,000,007$ ($10^{9} + 7$). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should be the number of occurrences of $i$ on Chef's sheet of paper. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ A_{i} ≤ N$ for each valid $i$ the sum of $N$ over all test cases does not exceed $500,000$ ------ Subtasks ------ Subtask #1 (20 points): the sum of $N$ over all test cases does not exceed $5,000$ Subtask #2 (10 points): $A_{1}, A_{2}, \ldots, A_{N}$ is a permutation of the integers $1$ through $N$ Subtask #3 (70 points): original constraints ----- Sample Input 1 ------ 3 3 2 2 3 2 1 2 3 1 2 2 ----- Sample Output 1 ------ 0 6 1 2 1 3 4 0 ----- explanation 1 ------ Example case 3: There are $7$ non-empty subsequences: - $[1]$ (Chef writes down $1$) - $[2]$ (Chef writes down $2$) - $[2]$ (Chef writes down $2$) - $[1, 2]$ (Chef writes down $1$) - $[1, 2]$ (Chef writes down $1$) - $[2, 2]$ (Chef writes down $2$) - $[1, 2, 2]$ (Chef writes down $2$)
def mul(a, b, c): for i in range(min(len(a), len(b))): a[i] = a[i] * b[i] % 1000000007 for i in range(len(a), len(b)): a.append(b[i]) return a[:c] def preProcess(): fac = [1, 1] for i in range(2, 500001): fac.append(i * fac[i - 1] % 1000000007) return fac def solve(n, arr, fac, modInverse): m = 1000000007 ans = [0] * n cntArr = [0] * (n + 1) for i in arr: cntArr[i] += 1 nArr = [] for i in cntArr: if i == 0: continue nArr.append(i) lessC = [(0) for _ in range(max(nArr) + 1)] for i in nArr: lessC[i] += 1 for i in range(len(lessC)): lessC[i] = i * lessC[i] for i in range(1, len(lessC)): lessC[i] = lessC[i - 1] + lessC[i] for i in range(len(lessC)): lessC[i] = pow(2, lessC[i], m) ncr = [[] for _ in range(len(nArr))] for i in range(len(nArr)): for j in range(nArr[i] + 1): modInverse[j] = modInverse[j] if modInverse[j] else pow(fac[j], m - 2, m) modInverse[nArr[i] - j] = ( modInverse[nArr[i] - j] if modInverse[nArr[i] - j] else pow(fac[nArr[i] - j], m - 2, m) ) ncr[i].append(fac[nArr[i]] * modInverse[j] * modInverse[nArr[i] - j] % m) preArr = [[1] for _ in range(len(nArr))] for i in range(len(ncr)): for j in range(1, len(ncr[i])): preArr[i].append( preArr[i][j - 1] + ncr[i][j] if j < len(ncr[i]) else preArr[i][j - 1] ) nArrL = [(False) for _ in range(len(nArr))] nArrR = [(False) for _ in range(len(nArr))] currL = [] currR = [] for i in range(1, len(preArr)): nArrL[i] = mul(currL, preArr[i - 1][:-1], len(preArr[i])) nArrR[-1 - i] = mul(currR, preArr[-i], len(preArr[-1 - i])) cArr = [] for i in range(len(ncr)): sm = 0 for j in range(1, len(ncr[i])): pro = ncr[i][j] if nArrL[i] is not False and j - 1 < len(nArrL[i]): pro = pro * nArrL[i][j - 1] % m if nArrR[i] is not False and j < len(nArrR[i]): pro = pro * nArrR[i][j] % m pro = pro * lessC[j - 1] % m sm = (sm + pro) % m cArr.append(sm) ans = [] j = 0 for i in cntArr[1:]: if i == 0: ans.append(0) else: ans.append(cArr[j]) j += 1 print(" ".join(list(map(str, ans)))) def main(): fac = preProcess() modInv = [(False) for _ in range(len(fac))] for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) solve(n, arr, fac, modInv) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a sequence of integers $A_{1}, A_{2}, \ldots, A_{N}$. He takes a sheet of paper and for each non-empty subsequence $B$ of this sequence, he does the following: 1. For each integer which appears in $B$, count its number of occurrences in the sequence $B$. 2. Choose the integer with the largest number of occurrences. If there are several options, choose the smallest one. 3. Finally, write down the chosen integer (an element of $B$) on the sheet of paper. For each integer between $1$ and $N$ inclusive, find out how many times it appears on Chef's sheet of paper. Since these numbers may be very large, compute them modulo $1,000,000,007$ ($10^{9} + 7$). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should be the number of occurrences of $i$ on Chef's sheet of paper. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ A_{i} ≤ N$ for each valid $i$ the sum of $N$ over all test cases does not exceed $500,000$ ------ Subtasks ------ Subtask #1 (20 points): the sum of $N$ over all test cases does not exceed $5,000$ Subtask #2 (10 points): $A_{1}, A_{2}, \ldots, A_{N}$ is a permutation of the integers $1$ through $N$ Subtask #3 (70 points): original constraints ----- Sample Input 1 ------ 3 3 2 2 3 2 1 2 3 1 2 2 ----- Sample Output 1 ------ 0 6 1 2 1 3 4 0 ----- explanation 1 ------ Example case 3: There are $7$ non-empty subsequences: - $[1]$ (Chef writes down $1$) - $[2]$ (Chef writes down $2$) - $[2]$ (Chef writes down $2$) - $[1, 2]$ (Chef writes down $1$) - $[1, 2]$ (Chef writes down $1$) - $[2, 2]$ (Chef writes down $2$) - $[1, 2, 2]$ (Chef writes down $2$)
data_ad = {} def ncr(n, data_ad): arr = [1] num = den = 1 for i in range(n // 2): num = num * (n - i) % 1000000007 den = den * (i + 1) % 1000000007 if den not in data_ad: data_ad[den] = pow(den, 1000000005, 1000000007) arr.append(num * data_ad[den] % 1000000007) return arr data_com = {} for _ in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] l.sort(reverse=True) ques = [] last = l[0] cnt = 0 ini_count_2 = {} com = {} ad = {} count = {} for j in range(len(l)): if l[j] == last: cnt += 1 if len(ques) >= cnt: ques[cnt - 1].append(l[j]) else: ques.append([l[j]]) else: if cnt not in data_com: data_com[cnt] = ncr(cnt, data_ad) cnt = 1 last = l[j] if len(ques) >= cnt: ques[cnt - 1].append(l[j]) else: ques.append([l[j]]) if l[j] not in ad: count[l[j]] = 1 ad[l[j]] = 1 ini_count_2[l[j]] = 0 else: count[l[j]] += 1 if cnt not in data_com: data_com[cnt] = ncr(cnt, data_ad) ans = [] mul = 1 mu_1 = 1 for j in range(len(l)): ans.append(0) for i in range(len(ques)): for j in range(len(ques[i])): temp = ques[i][j] ind = ini_count_2[temp] if ad[temp] not in data_ad: data_ad[ad[temp]] = pow(ad[temp], 1000000005, 1000000007) mul = mul * data_ad[ad[temp]] % 1000000007 else: mul = mul * data_ad[ad[temp]] % 1000000007 mul_1 = mul if count[temp] // 2 >= ind + 1: ad[temp] = (ad[temp] + data_com[count[temp]][ind + 1]) % 1000000007 mul_1 = mul_1 * data_com[count[temp]][ind + 1] % 1000000007 else: ad[temp] = ( ad[temp] + data_com[count[temp]][count[temp] - (ind + 1)] ) % 1000000007 mul_1 = ( mul_1 * data_com[count[temp]][count[temp] - (ind + 1)] % 1000000007 ) mul = mul * ad[temp] % 1000000007 ans[temp - 1] = (ans[temp - 1] + mul_1) % 1000000007 ini_count_2[temp] += 1 print(*ans)
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a sequence of integers $A_{1}, A_{2}, \ldots, A_{N}$. He takes a sheet of paper and for each non-empty subsequence $B$ of this sequence, he does the following: 1. For each integer which appears in $B$, count its number of occurrences in the sequence $B$. 2. Choose the integer with the largest number of occurrences. If there are several options, choose the smallest one. 3. Finally, write down the chosen integer (an element of $B$) on the sheet of paper. For each integer between $1$ and $N$ inclusive, find out how many times it appears on Chef's sheet of paper. Since these numbers may be very large, compute them modulo $1,000,000,007$ ($10^{9} + 7$). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should be the number of occurrences of $i$ on Chef's sheet of paper. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ A_{i} ≤ N$ for each valid $i$ the sum of $N$ over all test cases does not exceed $500,000$ ------ Subtasks ------ Subtask #1 (20 points): the sum of $N$ over all test cases does not exceed $5,000$ Subtask #2 (10 points): $A_{1}, A_{2}, \ldots, A_{N}$ is a permutation of the integers $1$ through $N$ Subtask #3 (70 points): original constraints ----- Sample Input 1 ------ 3 3 2 2 3 2 1 2 3 1 2 2 ----- Sample Output 1 ------ 0 6 1 2 1 3 4 0 ----- explanation 1 ------ Example case 3: There are $7$ non-empty subsequences: - $[1]$ (Chef writes down $1$) - $[2]$ (Chef writes down $2$) - $[2]$ (Chef writes down $2$) - $[1, 2]$ (Chef writes down $1$) - $[1, 2]$ (Chef writes down $1$) - $[2, 2]$ (Chef writes down $2$) - $[1, 2, 2]$ (Chef writes down $2$)
M = 1000000007 N = 500005 fact = [(0) for i in range(N)] fact[0] = 1 for i in range(1, N): fact[i] = fact[i - 1] * i % M inv_fact = [(0) for i in range(N)] inv_fact[N - 1] = pow(fact[N - 1], M - 2, M) for i in range(N - 2, -1, -1): inv_fact[i] = (i + 1) * inv_fact[i + 1] % M def ncr(n1, r1): return fact[n1] * inv_fact[r1] % M * inv_fact[n1 - r1] % M def inv(v): return pow(v, M - 2, M) t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) cnt = [(0) for i in range(n + 1)] for x in a: cnt[x] += 1 st = sorted(list(set(a))) mul_arr_small = [(1) for i in range(n + 5)] prefix_small = [(1) for i in range(n + 5)] mul_arr_small[0] = 0 final_add = [] for x in st: l = [(0) for i in range(cnt[x])] for i in range(cnt[x]): l[i] = ncr(cnt[x], i + 1) final_add.append(l) for index in range(len(st)): x = st[index] temp = 1 for i in range(cnt[x]): temp = temp * prefix_small[i + 1] % M final_add[index][i] = ( final_add[index][i] * (temp * mul_arr_small[i + 1]) % M % M ) temp = 0 for i in range(cnt[x]): temp = (temp + ncr(cnt[x], i)) % M mul_arr_small[i + 1] = mul_arr_small[i + 1] * temp % M temp = (temp + 1) % M prefix_small[cnt[x] + 1] = prefix_small[cnt[x] + 1] * temp % M mul_arr_big = [(1) for i in range(n + 5)] prefix_big = [(1) for i in range(n + 5)] for index in range(len(st) - 1, -1, -1): x = st[index] temp = 1 for i in range(cnt[x]): temp = temp * prefix_big[i + 1] % M final_add[index][i] = ( final_add[index][i] * (temp * mul_arr_big[i + 1]) % M % M ) temp = 1 for i in range(cnt[x]): temp = (temp + ncr(cnt[x], i + 1)) % M mul_arr_big[i + 1] = mul_arr_big[i + 1] * temp % M prefix_big[cnt[x] + 1] = prefix_big[cnt[x] + 1] * temp % M ans = {} for index in range(len(st)): a = 0 for i in range(cnt[st[index]]): a = (a + final_add[index][i]) % M ans[st[index]] = a for i in range(1, n + 1): if i in ans: print(ans[i], end=" ") else: print(0, end=" ") print()
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef has a sequence of integers $A_{1}, A_{2}, \ldots, A_{N}$. He takes a sheet of paper and for each non-empty subsequence $B$ of this sequence, he does the following: 1. For each integer which appears in $B$, count its number of occurrences in the sequence $B$. 2. Choose the integer with the largest number of occurrences. If there are several options, choose the smallest one. 3. Finally, write down the chosen integer (an element of $B$) on the sheet of paper. For each integer between $1$ and $N$ inclusive, find out how many times it appears on Chef's sheet of paper. Since these numbers may be very large, compute them modulo $1,000,000,007$ ($10^{9} + 7$). ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. ------ Output ------ For each test case, print a single line containing $N$ space-separated integers. For each valid $i$, the $i$-th of these integers should be the number of occurrences of $i$ on Chef's sheet of paper. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ A_{i} ≤ N$ for each valid $i$ the sum of $N$ over all test cases does not exceed $500,000$ ------ Subtasks ------ Subtask #1 (20 points): the sum of $N$ over all test cases does not exceed $5,000$ Subtask #2 (10 points): $A_{1}, A_{2}, \ldots, A_{N}$ is a permutation of the integers $1$ through $N$ Subtask #3 (70 points): original constraints ----- Sample Input 1 ------ 3 3 2 2 3 2 1 2 3 1 2 2 ----- Sample Output 1 ------ 0 6 1 2 1 3 4 0 ----- explanation 1 ------ Example case 3: There are $7$ non-empty subsequences: - $[1]$ (Chef writes down $1$) - $[2]$ (Chef writes down $2$) - $[2]$ (Chef writes down $2$) - $[1, 2]$ (Chef writes down $1$) - $[1, 2]$ (Chef writes down $1$) - $[2, 2]$ (Chef writes down $2$) - $[1, 2, 2]$ (Chef writes down $2$)
N = 500001 factorialNumInverse = [None] * (N + 1) naturalNumInverse = [None] * (N + 1) fact = [None] * (N + 1) naturalNumInverse[0] = naturalNumInverse[1] = 1 for i in range(2, N + 1, 1): naturalNumInverse[i] = ( naturalNumInverse[1000000007 % i] * (1000000007 - 1000000007 // i) % 1000000007 ) factorialNumInverse[0] = factorialNumInverse[1] = 1 for i in range(2, N + 1, 1): factorialNumInverse[i] = ( naturalNumInverse[i] * factorialNumInverse[i - 1] % 1000000007 ) fact[0] = 1 for i in range(1, N + 1): fact[i] = fact[i - 1] * i % 1000000007 def Binomial(N, R): return ( fact[N] * factorialNumInverse[R] % 1000000007 * factorialNumInverse[N - R] % 1000000007 ) def modInverse(a, m): m0 = m y = 0 x = 1 if m == 1: return 0 while a > 1: q = a // m t = m m = a % m a = t t = y y = x - q * y x = t if x < 0: x = x + m0 return x for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) cts = {} rc = {} ans = [(0) for _ in range(n)] acts = [(0) for _ in range(n)] for x in a: acts[x - 1] += 1 myarray = [] for x in range(n, 0, -1): if acts[x - 1] > 0: cts[x] = acts[x - 1] myarray.append(x) rc[x] = 1 nl = [] nextlist = [] for y in range(1, max(acts) + 1): justlist = [] nextlist = [] for x in myarray: justlist.append(x) if acts[x - 1] > y: nextlist.append(x) nl.append(justlist) myarray = nextlist p = 1 for g, x in enumerate(nl): for i in x: c = Binomial(cts[i], g + 1) p = p * modInverse(rc[i], 1000000007) % 1000000007 ans[i - 1] = (ans[i - 1] + p * c % 1000000007) % 1000000007 rc[i] = (rc[i] + c) % 1000000007 p = rc[i] * p % 1000000007 for i in ans: print(i, end=" ") print()
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) ls = [int(x) for x in input().split()] d = dict() for i in range(n): x = ls[i] - i if x in d: d[x] += ls[i] else: d[x] = ls[i] ans = -1 for i in d: ans = max(ans, d[i]) print(ans)
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
inf = int(10000000000.0) n = int(input()) lis = list(map(int, input().split())) c = sorted([(lis[i] - i, lis[i]) for i in range(n)]) v = c[0][1] ans = 0 for i in range(1, n): if c[i - 1][0] == c[i][0]: v += c[i][1] else: ans = max(ans, v) v = c[i][1] print(max(ans, v))
ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
from sys import stdin input = stdin.readline n = int(input()) l = list(map(int, input().split())) adj = [(l[i] - i) for i in range(n)] ind_odp = [] d = {} for i in range(n): d[adj[i]] = [] for i in range(n): d[adj[i]].append(i) odp = 0 for elt in d: cyk = len(d[elt]) * elt for i in d[elt]: cyk += i odp = max(cyk, odp) print(odp)
ASSIGN VAR VAR 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 VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = input().split() for i in range(n): b[i] = int(b[i]) ls = [(0) for i in range(2000000)] for i in range(n): ls[b[i] - i] += b[i] print(max(ls))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
def main(): n = int(input()) b = list(map(int, input().split())) c = b[:] for i in range(n): c[i] -= i b[i] = [b[i], c[i]] b.sort(key=lambda x: x[1]) max_count = 0 for i in range(n): if i == 0: a = b[i][1] count = b[i][0] max_count = count elif a == b[i][1]: count += b[i][0] max_count = max(max_count, count) elif a != b[i][1]: a = b[i][1] max_count = max(max_count, count) count = b[i][0] max_count = max(max_count, count) print(max_count) main()
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 VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
from sys import gettrace, stdin if not gettrace(): def input(): return next(stdin)[:-1] def main(): n = int(input()) bb = [int(a) for a in input().split()] seq = {} for i, b in enumerate(bb): if b - i in seq: seq[b - i] += b else: seq[b - i] = b print(max(seq.values())) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) for i in range(n): b[i] = b[i], b[i] - i b.sort(key=lambda x: x[1]) k = b[0][1] mf = b[0][0] s = b[0][0] for i in range(1, n): if b[i][1] == k: s += b[i][0] else: k = b[i][1] mf = max(mf, s) s = b[i][0] print(max(mf, s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() a = [0] * int(800000.0 + 100) b = list(mints()) for i in range(n): a[b[i] - i] += b[i] print(max(a)) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
import sys input = sys.stdin.readline n = int(input()) it = list(map(int, input().split())) s = {} for i in range(n): try: x = s[i - it[i]] s[i - it[i]] += it[i] except: s[i - it[i]] = it[i] print(max(s.values()))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
class Solution: def getMaxBeauty(self, n: int, b) -> int: Hash = {} for i in range(n): Hash[b[i] - i] = Hash.get(b[i] - i, 0) + b[i] return max(Hash.values()) n = int(input()) b = list(map(int, input().split())) obj = Solution() print(obj.getMaxBeauty(n, b))
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) arr = [int(i) for i in input().split(" ")] d = dict() m = 0 for i in range(n): ind = arr[i] - (i + 1) if ind in d: d[ind] += arr[i] else: d[ind] = arr[i] m = max(m, d[ind]) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) (*b,) = map(int, input().split()) cities = {} starts_idx = [(-1) for _ in range(n)] ends_idx = [(-1) for _ in range(n)] cur_val = [(0) for _ in range(n)] for i in range(n): cities.setdefault(b[i] - i, []) cities[b[i] - i].append(b[i]) print(max(sum(route) for route in cities.values()))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
from itertools import groupby def keyfunc(key): return key[0] - key[1] input() arr = list(map(int, input().split())) ans = max( map( lambda g: sum(list(zip(*g[1]))[1]), groupby(sorted(enumerate(arr), key=keyfunc), keyfunc), ) ) print(ans)
FUNC_DEF RETURN BIN_OP VAR NUMBER VAR NUMBER EXPR 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) arr = list(map(int, input().split())) result = [0] * n maxx = 0 for i in range(n): result[i] = arr[i] - i dic = {} for i in range(n): if result[i] in dic: val = dic[result[i]] val += arr[i] dic[result[i]] = val else: val = arr[i] dic[result[i]] = val maxx = max(maxx, val) print(maxx)
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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) mem = {} for i in range(n): if i - b[i] not in mem: mem[i - b[i]] = 0 mem[i - b[i]] += b[i] ans = 0 for i in mem: ans = max(ans, mem[i]) 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 DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) ar = list(map(int, input().split())) pref = [] for i in range(n): pref.append([]) pref[-1].append(i + 1 - ar[i]) pref[-1].append(i) ans = [] pref.sort() lst = pref[0][0] s = ar[pref[0][1]] for i in range(1, len(pref)): if pref[i][0] == lst: s += ar[pref[i][1]] else: ans.append(s) s = ar[pref[i][1]] lst = pref[i][0] ans.append(s) ans.sort() print(ans[-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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
import sys def input(): return sys.stdin.readline()[:-1] n = int(input()) b = [int(x) for x in input().split()] cnt = {} ans = 0 for i, e in enumerate(b): if e - i not in cnt: cnt[e - i] = 0 cnt[e - i] += e ans = max(ans, cnt[e - i]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b_aux = input().split() b = [] for i in range(n): b.append((int(b_aux[i]) - i, int(b_aux[i]))) a = sorted(b) current = a[0][0] - 1 cont = 0 res = -1 for e in a: if e[0] == current: cont += e[1] else: res = max(res, cont) cont = e[1] current = e[0] res = max(cont, res) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
MAXVAL = 4 * 10**5 n = int(input()) beau = list(map(int, input().split())) dp = [0] * (2 * MAXVAL + 2) for i in range(n): dp[beau[i] - i - MAXVAL] += beau[i] print(max(dp))
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER 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 BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = input().split() mylist = [] for x in range(n): mylist.append([int(b[x]) - x, int(b[x])]) mylist = sorted(mylist) gt = [0, 0] a = mylist[0][0] for x in range(n): if a != mylist[x][0]: if gt[1] > gt[0]: gt[0] = gt[1] gt[1] = 0 gt[1] = gt[1] + mylist[x][1] a = mylist[x][0] if gt[1] > gt[0]: gt[0] = gt[1] print(gt[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) d = {} for i in range(n): b[i] -= i d[b[i]] = 0 for i in range(n): d[b[i]] += b[i] + i print(max(d.values()))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) bVar = [int(i) for i in str(input()).split()] dp = [(0) for i in range(n)] con = 4 * 100000 diffArray = {} maxValue = -1 for i in range(n): if i == 0: dp[i] = bVar[0] maxValue = max(maxValue, dp[i]) diff = i + 1 - bVar[i] diffArray[diff] = [i] continue diff = i + 1 - bVar[i] boolCon = diff in diffArray if boolCon == False: dp[i] = bVar[i] maxValue = max(maxValue, dp[i]) diffArray[diff] = [i] else: l = len(diffArray[diff]) dp[i] = max(bVar[i], bVar[i] + dp[diffArray[diff][l - 1]]) maxValue = max(maxValue, dp[i]) diffArray[diff].append(i) print(maxValue)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) s = dict() for i in range(n): x = b[i] - i y = b[i] if x in s: s[x] += y else: s[x] = y print(max(s.values()))
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
def MaxBeauty(cities): types = [(city - num) for num, city in enumerate(cities)] journeys = dict() for num in range(len(cities)): try: journeys[types[num]] += cities[num] except KeyError: journeys[types[num]] = cities[num] return max(journeys.values()) assert MaxBeauty([111]) == 111 assert MaxBeauty([1, 1, 1]) == 1 assert MaxBeauty([1, 2, 3]) == 6 assert MaxBeauty([10, 7, 1, 9, 10, 15]) == 26 count = input() cities = list(map(int, input().split())) print(MaxBeauty(cities))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) l = list(map(int, input().split())) if n == 1: print(l[0]) else: d = {} for i in range(n): if l[i] - i not in d: d[l[i] - i] = [l[i]] else: d[l[i] - i].append(l[i]) r = max([sum(i) for i in d.values()]) print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input().strip()) arr = list(map(int, input().strip().split())) dic = {} for i in range(n): key = arr[i] - (i + 1) if key not in dic: dic[key] = arr[i] else: dic[key] += arr[i] print(max(dic.values()))
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 ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) c = [] for i in range(n): c.append(b[i] - i) d = {} for i in range(n): if c[i] in d: d[c[i]] += b[i] else: d[c[i]] = b[i] print(max(d.values()))
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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) bl = list(map(int, input().split())) nl = [(bl[i], bl[i] - i) for i in range(n)] nl.sort(key=lambda b: b[1]) p = nl[0] s = p[0] mx = s for i in range(1, n): if nl[i][1] == p[1]: s += nl[i][0] else: if s > mx: mx = s s = nl[i][0] p = nl[i] print(max(s, mx))
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 BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
import sys input = lambda: sys.stdin.readline().rstrip() n = int(input()) a = [int(i) for i in input().split()] ar = a[:] for i in range(n - 1, -1, -1): a[i] += n - i - 1 d = {} for i in range(n): if a[i] in d: d[a[i]].append(ar[i]) else: d[a[i]] = [ar[i]] maxx = 0 for i in d: maxx = max(maxx, sum(d[i])) print(maxx)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
input() nums = list(map(int, input().split())) res = {} for i, n in enumerate(nums): if n - i not in res: res[n - i] = 0 res[n - i] += n print(max(res.values()))
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") n = INI() X = INL() D = dict() ans = float("-inf") for _ in range(n): D[X[_] - _] = D.get(X[_] - _, 0) + X[_] ans = max(ans, D[X[_] - _]) OPS(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
res = 0 n = int(input()) x = [0] * (4 * 10**6 + 1) bi = [*map(int, input().split())] for i in range(0, n): a = bi[i] x[a - i] += a res = max(res, x[a - i]) print(res)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) r = list(map(int, input().split())) s = [(r[i] - i - 1) for i in range(n)] h = {} ans = 0 for i in range(n): if str(s[i]) in h: h[str(s[i])] += r[i] ans = max(ans, h[str(s[i])]) else: h[str(s[i])] = r[i] ans = max(ans, h[str(s[i])]) 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 BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$. Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$, then to some other city $c_3 > c_2$, and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$. So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$, the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold. For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$, there are several three possible ways to plan a journey: $c = [1, 2, 4]$; $c = [3, 5, 6, 8]$; $c = [7]$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? -----Input----- The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cities in Berland. The second line contains $n$ integers $b_1$, $b_2$, ..., $b_n$ ($1 \le b_i \le 4 \cdot 10^5$), where $b_i$ is the beauty value of the $i$-th city. -----Output----- Print one integer — the maximum beauty of a journey Tanya can choose. -----Examples----- Input 6 10 7 1 9 10 15 Output 26 Input 1 400000 Output 400000 Input 7 8 9 26 11 12 29 14 Output 55 -----Note----- The optimal journey plan in the first example is $c = [2, 4, 5]$. The optimal journey plan in the second example is $c = [1]$. The optimal journey plan in the third example is $c = [3, 6]$.
n = int(input()) b = list(map(int, input().split())) c = [] d = [] for item in range(1, n + 1): c.append(item) for i in range(n): d.append(b[i] - c[i]) hashmap = {} for i in range(n): if d[i] in hashmap: hashmap[d[i]] += b[i] elif d[i] not in hashmap: hashmap[d[i]] = b[i] print(max(hashmap.values()))
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 LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR