description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: taps = [] for i in range(len(ranges)): taps.append((i - ranges[i], i + ranges[i])) taps.sort() @lru_cache(None) def helper(i): if i >= n + 1: return float("inf") if taps[i][1] >= n: return 1 ans = float("inf") for j in range(i + 1, n + 1): if taps[j][0] > taps[i][1]: break if taps[j][0] <= taps[i][1] and taps[j][1] > taps[i][1]: ans = min(ans, 1 + helper(j)) return ans ans = float("inf") for i in range(n, -1, -1): res = helper(i) if taps[i][0] <= 0 and res < ans: ans = res return -1 if ans == float("inf") else ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: if len(ranges) == 0: return 0 segments = [ (max(0, index - num), min(n, index + num)) for index, num in enumerate(ranges) ] segments = sorted(segments) left, right = -1, 0 cnt = 0 for a, b in segments: if a > right: break if right == n: break if right >= a > left: cnt += 1 left = right right = max(right, b) if right == n: return cnt return -1
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [n for _ in range(n + 1)] for index, ranged in enumerate(ranges): start = max(0, index - ranged) end = min(n, index + ranged) if start == 0: extra = 0 else: extra = dp[start] for i in range(start + 1, end + 1): dp[i] = min(dp[i], 1 + extra) if dp[-1] != n: return dp[-1] else: return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR RETURN VAR NUMBER RETURN NUMBER VAR
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [float("inf")] * (n + 1) dp[0] = 0 for idx, r in enumerate(ranges): for j in range(max(idx - r + 1, 0), min(idx + r, n) + 1): dp[j] = min(dp[j], dp[max(0, idx - r)] + 1) return dp[n] if dp[n] < float("inf") else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR
Pak Chanek has $n$ blank heart-shaped cards. Card $1$ is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card $i$ ($i > 1$) is hanging onto card $p_i$ ($p_i < i$). In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation $a$ of $[1, 2, \dots, n]$. Then, the number written on card $i$ is $a_i$. After that, Pak Chanek must do the following operation $n$ times while maintaining a sequence $s$ (which is initially empty): Choose a card $x$ such that no other cards are hanging onto it. Append the number written on card $x$ to the end of $s$. If $x \neq 1$ and the number on card $p_x$ is larger than the number on card $x$, replace the number on card $p_x$ with the number on card $x$. Remove card $x$. After that, Pak Chanek will have a sequence $s$ with $n$ elements. What is the maximum length of the longest non-decreasing subsequence$^\dagger$ of $s$ at the end if Pak Chanek does all the steps optimally? $^\dagger$ A sequence $b$ is a subsequence of a sequence $c$ if $b$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$, $[4,3,1]$ and $[3,1]$, but not $[1,3,3,7]$ and $[3,10,4]$. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of heart-shaped cards. The second line contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) describing which card that each card hangs onto. -----Output----- Print a single integer — the maximum length of the longest non-decreasing subsequence of $s$ at the end if Pak Chanek does all the steps optimally. -----Examples----- Input 6 1 2 1 4 2 Output 4 Input 2 1 Output 2 -----Note----- The following is the structure of the cards in the first example. Pak Chanek can choose the permutation $a = [1, 5, 4, 3, 2, 6]$. Let $w_i$ be the number written on card $i$. Initially, $w_i = a_i$. Pak Chanek can do the following operations in order: Select card $5$. Append $w_5 = 2$ to the end of $s$. As $w_4 > w_5$, the value of $w_4$ becomes $2$. Remove card $5$. After this operation, $s = [2]$. Select card $6$. Append $w_6 = 6$ to the end of $s$. As $w_2 \leq w_6$, the value of $w_2$ is left unchanged. Remove card $6$. After this operation, $s = [2, 6]$. Select card $4$. Append $w_4 = 2$ to the end of $s$. As $w_1 \leq w_4$, the value of $w_1$ is left unchanged. Remove card $4$. After this operation, $s = [2, 6, 2]$. Select card $3$. Append $w_3 = 4$ to the end of $s$. As $w_2 > w_3$, the value of $w_2$ becomes $4$. Remove card $3$. After this operation, $s = [2, 6, 2, 4]$. Select card $2$. Append $w_2 = 4$ to the end of $s$. As $w_1 \leq w_2$, the value of $w_1$ is left unchanged. Remove card $2$. After this operation, $s = [2, 6, 2, 4, 4]$. Select card $1$. Append $w_1 = 1$ to the end of $s$. Remove card $1$. After this operation, $s = [2, 6, 2, 4, 4, 1]$. One of the longest non-decreasing subsequences of $s = [2, 6, 2, 4, 4, 1]$ is $[2, 2, 4, 4]$. Thus, the length of the longest non-decreasing subsequence of $s$ is $4$. It can be proven that this is indeed the maximum possible length.
n = int(input()) p = [-1] + [(x - 1) for x in list(map(int, input().split()))] h, dp = [1] * n, [0] * n for i in range(n - 1, -1, -1): dp[i] = max(dp[i], h[i]) if i: dp[p[i]] += dp[i] h[p[i]] = max(h[p[i]], h[i] + 1) print(dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Pak Chanek has $n$ blank heart-shaped cards. Card $1$ is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card $i$ ($i > 1$) is hanging onto card $p_i$ ($p_i < i$). In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation $a$ of $[1, 2, \dots, n]$. Then, the number written on card $i$ is $a_i$. After that, Pak Chanek must do the following operation $n$ times while maintaining a sequence $s$ (which is initially empty): Choose a card $x$ such that no other cards are hanging onto it. Append the number written on card $x$ to the end of $s$. If $x \neq 1$ and the number on card $p_x$ is larger than the number on card $x$, replace the number on card $p_x$ with the number on card $x$. Remove card $x$. After that, Pak Chanek will have a sequence $s$ with $n$ elements. What is the maximum length of the longest non-decreasing subsequence$^\dagger$ of $s$ at the end if Pak Chanek does all the steps optimally? $^\dagger$ A sequence $b$ is a subsequence of a sequence $c$ if $b$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$, $[4,3,1]$ and $[3,1]$, but not $[1,3,3,7]$ and $[3,10,4]$. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of heart-shaped cards. The second line contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) describing which card that each card hangs onto. -----Output----- Print a single integer — the maximum length of the longest non-decreasing subsequence of $s$ at the end if Pak Chanek does all the steps optimally. -----Examples----- Input 6 1 2 1 4 2 Output 4 Input 2 1 Output 2 -----Note----- The following is the structure of the cards in the first example. Pak Chanek can choose the permutation $a = [1, 5, 4, 3, 2, 6]$. Let $w_i$ be the number written on card $i$. Initially, $w_i = a_i$. Pak Chanek can do the following operations in order: Select card $5$. Append $w_5 = 2$ to the end of $s$. As $w_4 > w_5$, the value of $w_4$ becomes $2$. Remove card $5$. After this operation, $s = [2]$. Select card $6$. Append $w_6 = 6$ to the end of $s$. As $w_2 \leq w_6$, the value of $w_2$ is left unchanged. Remove card $6$. After this operation, $s = [2, 6]$. Select card $4$. Append $w_4 = 2$ to the end of $s$. As $w_1 \leq w_4$, the value of $w_1$ is left unchanged. Remove card $4$. After this operation, $s = [2, 6, 2]$. Select card $3$. Append $w_3 = 4$ to the end of $s$. As $w_2 > w_3$, the value of $w_2$ becomes $4$. Remove card $3$. After this operation, $s = [2, 6, 2, 4]$. Select card $2$. Append $w_2 = 4$ to the end of $s$. As $w_1 \leq w_2$, the value of $w_1$ is left unchanged. Remove card $2$. After this operation, $s = [2, 6, 2, 4, 4]$. Select card $1$. Append $w_1 = 1$ to the end of $s$. Remove card $1$. After this operation, $s = [2, 6, 2, 4, 4, 1]$. One of the longest non-decreasing subsequences of $s = [2, 6, 2, 4, 4, 1]$ is $[2, 2, 4, 4]$. Thus, the length of the longest non-decreasing subsequence of $s$ is $4$. It can be proven that this is indeed the maximum possible length.
import sys def read(): return [int(x) for x in sys.stdin.readline().split(" ")] [n] = read() P = read() deg = n * [0] for i in range(len(P)): P[i] -= 1 deg[P[i]] += 1 P = [0] + P Q = [] for i in range(n): if deg[i] == 0: Q.append(i) M = n * [0] S = n * [0] while len(Q) > 0: i = Q.pop() M[i] += 1 if i == 0: continue S[P[i]] += max(M[i], S[i]) M[P[i]] = max(M[P[i]], M[i]) deg[P[i]] -= 1 if deg[P[i]] == 0: Q.append(P[i]) print(max(M[0], S[0]))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN LIST VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Pak Chanek has $n$ blank heart-shaped cards. Card $1$ is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card $i$ ($i > 1$) is hanging onto card $p_i$ ($p_i < i$). In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation $a$ of $[1, 2, \dots, n]$. Then, the number written on card $i$ is $a_i$. After that, Pak Chanek must do the following operation $n$ times while maintaining a sequence $s$ (which is initially empty): Choose a card $x$ such that no other cards are hanging onto it. Append the number written on card $x$ to the end of $s$. If $x \neq 1$ and the number on card $p_x$ is larger than the number on card $x$, replace the number on card $p_x$ with the number on card $x$. Remove card $x$. After that, Pak Chanek will have a sequence $s$ with $n$ elements. What is the maximum length of the longest non-decreasing subsequence$^\dagger$ of $s$ at the end if Pak Chanek does all the steps optimally? $^\dagger$ A sequence $b$ is a subsequence of a sequence $c$ if $b$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$, $[4,3,1]$ and $[3,1]$, but not $[1,3,3,7]$ and $[3,10,4]$. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of heart-shaped cards. The second line contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$) describing which card that each card hangs onto. -----Output----- Print a single integer — the maximum length of the longest non-decreasing subsequence of $s$ at the end if Pak Chanek does all the steps optimally. -----Examples----- Input 6 1 2 1 4 2 Output 4 Input 2 1 Output 2 -----Note----- The following is the structure of the cards in the first example. Pak Chanek can choose the permutation $a = [1, 5, 4, 3, 2, 6]$. Let $w_i$ be the number written on card $i$. Initially, $w_i = a_i$. Pak Chanek can do the following operations in order: Select card $5$. Append $w_5 = 2$ to the end of $s$. As $w_4 > w_5$, the value of $w_4$ becomes $2$. Remove card $5$. After this operation, $s = [2]$. Select card $6$. Append $w_6 = 6$ to the end of $s$. As $w_2 \leq w_6$, the value of $w_2$ is left unchanged. Remove card $6$. After this operation, $s = [2, 6]$. Select card $4$. Append $w_4 = 2$ to the end of $s$. As $w_1 \leq w_4$, the value of $w_1$ is left unchanged. Remove card $4$. After this operation, $s = [2, 6, 2]$. Select card $3$. Append $w_3 = 4$ to the end of $s$. As $w_2 > w_3$, the value of $w_2$ becomes $4$. Remove card $3$. After this operation, $s = [2, 6, 2, 4]$. Select card $2$. Append $w_2 = 4$ to the end of $s$. As $w_1 \leq w_2$, the value of $w_1$ is left unchanged. Remove card $2$. After this operation, $s = [2, 6, 2, 4, 4]$. Select card $1$. Append $w_1 = 1$ to the end of $s$. Remove card $1$. After this operation, $s = [2, 6, 2, 4, 4, 1]$. One of the longest non-decreasing subsequences of $s = [2, 6, 2, 4, 4, 1]$ is $[2, 2, 4, 4]$. Thus, the length of the longest non-decreasing subsequence of $s$ is $4$. It can be proven that this is indeed the maximum possible length.
n = int(input()) a = [-1] + [(int(o) - 1) for o in input().split()] f = [0] * n dp = [0] * n for i in range(n - 1, 0, -1): f[a[i]] = max(f[i] + 1, f[a[i]]) for i in range(n - 1, 0, -1): dp[i] = max(dp[i], f[i] + 1) dp[a[i]] += dp[i] print(max(dp[0], f[0] + 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
def solve(n, cols): right = {} left = {} cnt = {} for i in range(n): if cols[i] not in left: left[cols[i]] = i cnt[cols[i]] = 0 right[cols[i]] = i dp = [0] * (n + 1) for i in reversed(range(n)): cnt[cols[i]] += 1 if i == left[cols[i]]: dp[i] = max(dp[i + 1], cnt[cols[i]] + dp[right[cols[i]] + 1]) else: dp[i] = max(dp[i + 1], cnt[cols[i]]) return n - dp[0] n = int(input().strip()) cols = [int(x) for x in input().strip().split()] print(solve(n, cols))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
n = int(input()) A = list(map(int, input().split())) l = [(-1) for i in range(n + 1)] r = [(-1) for i in range(n + 1)] for i in range(n): if l[A[i]] == -1: l[A[i]] = i r[A[i]] = i DP = [(0) for i in range(n + 1)] cnt = [(0) for i in range(n + 1)] for i in range(n - 1, -1, -1): cnt[A[i]] = cnt[A[i]] + 1 if l[A[i]] == i: DP[i] = cnt[A[i]] + DP[r[A[i]] + 1] DP[i] = max(DP[i], DP[i + 1]) else: DP[i] = cnt[A[i]] DP[i] = max(DP[i], DP[i + 1]) print(n - DP[0])
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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR 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 VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
import sys from sys import stdin n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) clis = [[] for i in range(n + 1)] for i in range(n): clis[a[i]].append(i) dp = [0] * (n + 1) app = [0] * (n + 1) for i in range(n - 1, -1, -1): app[a[i]] += 1 dp[i] = max(dp[i], dp[i + 1], app[a[i]]) if clis[a[i]][0] == i: dp[i] = max(dp[i], dp[clis[a[i]][-1] + 1] + len(clis[a[i]])) print(n - dp[0])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
import sys input = lambda: sys.stdin.readline().rstrip() n = int(input()) a = list(map(int, input().split())) a = [(a[i] - 1) for i in range(n)] R = [(-1) for i in range(n)] C = [[] for i in range(n)] for i in range(n): R[a[i]] = i C[a[i]].append(i) backet = [(-1) for i in range(n)] for i in range(n): if R[i] != -1: backet[R[i]] = i update = [b for b in backet if b != -1] dp = [(0) for i in range(n)] res = n for _ in range(len(update)): c = update[_] cn = len(C[c]) for i in range(cn): tmp = dp[C[c][i]] + cn - i res = min(res, n - tmp) start = C[c][0] dp[R[c]] = max(dp[R[c]], dp[start] + cn) if _ + 1 < len(update): nc = update[_ + 1] for i in range(R[c], R[nc] + 1): dp[i] = dp[R[c]] print(res)
IMPORT ASSIGN VAR FUNC_CALL 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 BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
n = int(input()) a = [(int(x) - 1) for x in input().split()] l = [(-1) for _ in range(n + 1)] r = [(-1) for _ in range(n + 1)] freq = [(0) for _ in range(n + 1)] dp = [(0) for _ in range(n + 1)] for i in range(n): if l[a[i]] == -1: l[a[i]] = i r[a[i]] = i for i in range(n - 1, -1, -1): dp[i] = dp[i + 1] freq[a[i]] += 1 if i == l[a[i]]: dp[i] = max(dp[i], dp[r[a[i]] + 1] + freq[a[i]]) else: dp[i] = max(dp[i], freq[a[i]]) print(n - dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
n = int(input()) a = [(int(x) - 1) for x in input().split()] cnt = [(0) for i in range(n)] l = [(-1) for i in range(n)] r = [(-1) for i in range(n)] for i in range(n): cnt[a[i]] += 1 if l[a[i]] == -1: l[a[i]] = i r[a[i]] = i dp = [(10**9) for i in range(n + 1)] ans = n dp[0] = 0 for i in range(n): dp[i + 1] = min(dp[i + 1], dp[i] + 1) if l[a[i]] == i: dp[r[a[i]] + 1] = min(dp[r[a[i]] + 1], dp[i] + r[a[i]] - i + 1 - cnt[a[i]]) ans = min(ans, dp[i] + n - i - cnt[a[i]]) cnt[a[i]] -= 1 print(min(ans, dp[n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR 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 VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10**16 md = 10**9 + 7 n = II() aa = LI1() ll = [-1] * n rr = [-1] * n cnt = [0] * n cs = [0] * (n + 1) for i in range(n - 1, -1, -1): a = aa[i] if rr[a] == -1: rr[a] = i ll[a] = i cnt[a] += 1 cs[i] = cnt[a] def chmax(i, val): if val > dp[i]: dp[i] = val dp = [-1] * (n + 1) dp[n] = 0 for i in range(n - 1, -1, -1): a = aa[i] dp[i] = dp[i + 1] if ll[a] == i: chmax(i, dp[rr[a] + 1] + cs[i]) else: chmax(i, cs[i]) print(n - dp[0])
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
n = int(input()) a = [int(i) for i in input().split()] left = {} right = {} freq = {} freq_so_far = [] for i, color in enumerate(a): if color not in left: left[color] = i freq[color] = 0 right[color] = i freq[color] += 1 freq_so_far.append(freq[color]) dp = [n] * (n + 1) dp[-1] = 0 for i in range(n - 1, -1, -1): color = a[i] if left[color] == i: best_unmoved = freq[color] + dp[right[color] + 1] else: best_unmoved = freq[color] - freq_so_far[i] + 1 dp[i] = max(dp[i + 1], best_unmoved) print(n - dp[0])
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 DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
n = int(input()) a = [(int(t) - 1) for t in input().split()] l, r = [-1] * n, [-1] * n for i, x in enumerate(a): if l[x] == -1: l[x] = i r[x] = i dp = [0] * (n + 1) cnt = [0] * n for i in reversed(range(n)): x = a[i] cnt[x] += 1 dp[i] = dp[i + 1] if i == l[x]: dp[i] = max(dp[i], dp[r[x] + 1] + cnt[x]) else: dp[i] = max(dp[i], cnt[x]) print(n - dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
class Stuct: def __init__(self): self.leftmost = 0 self.rightmost = 0 self.frequency = 0 def get_data(books): C = {} for i, b in enumerate(books): if not b in C: C[b] = Stuct() C[b].frequency += 1 C[b].rightmost = i for i, b in reversed(tuple(enumerate(books))): C[b].leftmost = i return C def solve(books): data = get_data(books) Cf = {} DP = [(0) for _ in range(len(books) + 1)] for i, ai in reversed(tuple(enumerate(books))): if ai not in Cf: Cf[ai] = 0 Cf[ai] += 1 DP[i] = DP[i + 1] if i == data[ai].leftmost: DP[i] = max(DP[i], data[ai].frequency + DP[data[ai].rightmost + 1]) else: DP[i] = max(DP[i], Cf[ai]) return len(books) - DP[0] N = int(input()) books = tuple(map(int, input().split(" "))) print(solve(books))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$. You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other. In one operation you can take one book from any position on the shelf and move it to the right end of the shelf. What is the minimum number of operations you need to make the shelf beautiful? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors. -----Output----- Output the minimum number of operations to make the shelf beautiful. -----Examples----- Input 5 1 2 2 1 3 Output 2 Input 5 1 2 2 1 1 Output 1 -----Note----- In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example: take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$; take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$. In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
def solve(n, arr): l = {} r = {} cnt = {} dp = [0] * (n + 1) for i in range(n): c = arr[i] if c not in l: l[c] = i for i in range(n - 1, -1, -1): c = arr[i] if c not in cnt: r[c] = i cnt[c] = 0 cnt[c] += 1 dp[i] = dp[i + 1] if l[c] == i: dp[i] = max(dp[i], cnt[c] + dp[r[c] + 1]) else: dp[i] = max(dp[i], cnt[c]) return n - dp[0] n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER 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 FUNC_CALL VAR VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
import sys input = sys.stdin.readline n = int(input()) m = list(map(int, input().split())) l = [-1] * n r = [n] * n stack = [] for i in range(n): while stack and m[stack[-1]] >= m[i]: stack.pop() if stack: l[i] = stack[-1] stack.append(i) stack = [] for i in range(n)[::-1]: while stack and m[stack[-1]] >= m[i]: stack.pop() if stack: r[i] = stack[-1] stack.append(i) pre = [0] * n suf = [0] * n for i in range(n): if l[i] == -1: pre[i] = (i + 1) * m[i] else: pre[i] = pre[l[i]] + (i - l[i]) * m[i] for i in range(n)[::-1]: if r[i] == n: suf[i] = (n - i) * m[i] else: suf[i] = suf[r[i]] + (r[i] - i) * m[i] ans = 0 idx = -1 for i in range(n): if ans < pre[i] + suf[i] - m[i]: ans = pre[i] + suf[i] - m[i] idx = i arr = [-1] * n arr[idx] = m[idx] for i in range(idx)[::-1]: arr[i] = min(arr[i + 1], m[i]) for i in range(idx + 1, n): arr[i] = min(arr[i - 1], m[i]) print(*arr)
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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
def min_right(a, n, reverse=False): if reverse: a.reverse() st = [] ans = [0] * n for i in range(n): while st and a[st[-1]] >= a[i]: st.pop() if st: ans[i] = (i - st[-1]) * a[i] + ans[st[-1]] else: ans[i] = (i + 1) * a[i] st.append(i) if reverse: a.reverse() ans.reverse() return ans n = int(input()) nums = list(map(int, input().split())) ll = min_right(nums, n) rr = min_right(nums, n, True) imax = 0 vmax = ll[0] + rr[0] - nums[0] for i in range(n): if ll[i] + rr[i] - nums[i] > vmax: imax = i vmax = ll[i] + rr[i] - nums[i] for i in range(imax - 1, -1, -1): nums[i] = min(nums[i], nums[i + 1]) for i in range(imax + 1, n): nums[i] = min(nums[i], nums[i - 1]) print(*nums)
FUNC_DEF NUMBER IF VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN 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 VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
inf = float("inf") n = int(input()) a = [-inf] + [*map(int, input().split())] + [-inf] for x in (0, 1): stack = [0] ns = [-1] * (n + 2) for i in range(n + 1, -1, -1): while a[i] < a[stack[-1]]: ns[stack.pop()] = i stack += [i] s = [0] * (n + 2) for i in range(1, len(a)): s[i] = s[ns[i]] + a[i] * (i - ns[i]) if ns[i] else i * a[i] a = a[::-1] if x: ri = s[:][::-1] else: le = s[:] s = [(i + j - k) for i, j, k in zip(le, ri, a)][1:-1] x = s.index(max(s)) a = a[1:-1] for i in range(x + 1, n): a[i] = min(a[i], a[i - 1]) for i in range(x - 1, -1, -1): a[i] = min(a[i], a[i + 1]) print(*a)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR FOR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
import sys input = sys.stdin.readline R = lambda: list(map(int, input().split())) (n,) = R() m = R() a = [m[0]] k = [0] for i in range(1, n): if m[i - 1] <= m[i]: a.append(a[i - 1] + m[i]) else: while k and m[k[-1]] > m[i]: k.pop() if k: j = k[-1] a.append((i - j) * m[i] + a[j]) else: a.append((i + 1) * m[i]) k.append(i) p = m[::-1] b = [p[0]] k = [0] for i in range(1, n): if p[i - 1] <= p[i]: b.append(b[i - 1] + p[i]) else: j = i - 1 while k and p[k[-1]] > p[i]: k.pop() if k: j = k[-1] b.append((i - j) * p[i] + b[j]) else: b.append((i + 1) * p[i]) k.append(i) ans = 0 index = 0 for i in range(n): temp = a[i] + b[n - i - 1] - m[i] if temp > ans: ans = temp index = i for i in range(index + 1, n): m[i] = min(m[i - 1], m[i]) for i in range(index - 1, -1, -1): m[i] = min(m[i + 1], m[i]) print(*m)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
num_inp = lambda: int(input()) arr_inp = lambda: list(map(int, input().split())) sp_inp = lambda: map(int, input().split()) str_inp = lambda: input() inf = float("inf") n = int(input()) a = [*map(int, input().split())] for x in (0, 1): stack = [-1] s = [0] * n for i in range(n - 1, -1, -1): while a[i] < a[stack[-1]] and stack[-1] != -1: stack.pop() s[i] = ( (n - i) * a[i] if stack[-1] == -1 else s[stack[-1]] + a[i] * (stack[-1] - i) ) stack += [i] a = a[::-1] if x: ri = s[:][::-1] else: le = s[:] s = [(i + j - k) for i, j, k in zip(le, ri, a)] x = s.index(max(s)) for i in range(x + 1, n): a[i] = min(a[i], a[i - 1]) for i in range(x - 1, -1, -1): a[i] = min(a[i], a[i + 1]) print(*a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR LIST VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
n = int(input()) a = list(map(int, input().split())) all = [a[i] for i in range(n)] peaks = [] p = [] l = [(0) for i in range(n)] r = [(0) for i in range(n)] st = [] for i in range(0, n): while st != [] and a[i] <= a[st[-1]]: st.pop() if st == []: l[i] = a[i] * (i + 1) else: l[i] = l[st[-1]] + a[i] * (i - st[-1]) st.append(i) while st != []: st.pop() for i in range(0, n): j = n - i - 1 while st != [] and a[j] <= a[st[-1]]: st.pop() if st == []: r[j] = a[j] * (i + 1) else: r[j] = r[st[-1]] + a[j] * (st[-1] - j) st.append(j) maxf = 0 I = 0 for i in range(n): if l[i] + r[i] - a[i] >= maxf: maxf = l[i] + r[i] - a[i] I = i for i in range(n): if i < I: a[I - (i + 1)] = min(a[I - i], a[I - (i + 1)]) if i > I: a[i] = min(a[i], a[i - 1]) print(*a)
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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR LIST ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR LIST ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
import sys n = int(sys.stdin.readline()) inf = int(2000000000.0) + 1 m = [-inf] + [int(x) for x in sys.stdin.readline().split()] + [-inf] r = [0] * (n + 2) st = [0] for i in range(1, n + 2): while m[st[-1]] > m[i]: r[st.pop()] = i st.append(i) l = [0] * (n + 2) st1 = [0] for i in range(n + 1, 0, -1): while m[st1[-1]] > m[i]: l[st1.pop()] = i st1.append(i) a = [0] * (n + 2) for i in range(1, n + 1): a[i] = a[l[i]] + m[i] * (i - l[i]) b = [0] * (n + 2) for i in range(n, 0, -1): b[i] = b[r[i]] + m[i] * (r[i] - i) p = -inf for i in range(n + 1): t = a[i] + b[i + 1] if t > p: q = i p = t for i in range(q - 1, 0, -1): if m[i + 1] < m[i]: m[i] = m[i + 1] q += 1 for i in range(1, q + 1): print(m[i]) for i in range(q + 1, n + 1): if m[i - 1] < m[i]: m[i] = m[i - 1] print(m[i])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR 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 VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.
def obhod(l, m, rev): if rev: m.reverse() locmin = [] number = [] l[0] = m[0] locmin.append(m[0]) number.append(0) for i in range(1, n): if m[i] >= m[i - 1]: l[i] = l[i - 1] + m[i] else: while len(locmin) > 0: if locmin[-1] > m[i]: locmin.pop() number.pop() else: break if len(locmin) == 0: l[i] = m[i] * (i + 1) else: l[i] = (i - number[-1]) * m[i] + l[number[-1]] locmin.append(m[i]) number.append(i) if rev: m.reverse() l.reverse() n = int(input()) m = list(map(int, input().split())) l = [0] * n r = [0] * n obhod(l, m, False) obhod(r, m, True) maxi = 0 maxd = 0 for i in range(n): if l[i] + r[i] - m[i] > maxd: maxd = l[i] + r[i] - m[i] maxi = i for i in range(maxi - 1, -1, -1): m[i] = min(m[i], m[i + 1]) for i in range(maxi + 1, n): m[i] = min(m[i], m[i - 1]) for i in range(n): print(m[i], end=" ")
FUNC_DEF IF VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR EXPR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) — the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys def I(): return sys.stdin.readline().rstrip() class Heap: def __init__(self): self.l = [-1] self.n = 0 def n(self): return self.n def top(self): return self.l[1] def ins(self, x): self.l.append(x) n = len(self.l) - 1 i = n while i > 1: j = i // 2 if self.l[j] > self.l[i]: self.l[j], self.l[i] = self.l[i], self.l[j] i = j else: break def pop(self): r = self.l[1] l = self.l.pop() n = len(self.l) - 1 if n: self.l[1] = l i = 1 while True: j = i * 2 k = j + 1 if k < len(self.l) and self.l[i] > max(self.l[j], self.l[k]): if self.l[j] == min(self.l[j], self.l[k]): self.l[i], self.l[j] = self.l[j], self.l[i] i = j else: self.l[i], self.l[k] = self.l[k], self.l[i] i = k elif k < len(self.l) and self.l[i] > self.l[k]: self.l[i], self.l[k] = self.l[k], self.l[i] i = k elif j < len(self.l) and self.l[i] > self.l[j]: self.l[i], self.l[j] = self.l[j], self.l[i] i = j else: break return r t = int(I()) for _ in range(t): n = int(I()) voter = [list(map(int, I().split())) for _ in range(n)] h = Heap() d = {} for m, p in voter: if m not in d: d[m] = [] d[m].append(p) need = {} c = 0 sk = sorted(d.keys()) for m in sk: need[m] = max(0, m - c) c += len(d[m]) c = 0 ans = 0 for m in sk[::-1]: for p in d[m]: h.ins(p) while c < need[m]: c += 1 ans += h.pop() print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) — the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
q = int(input()) for _ in range(q): n = int(input()) lr = [] for i in range(n): lr.append(list(map(int, input().split(" ")))) lr.sort(key=lambda x: x[1], reverse=True) lr.sort(key=lambda x: x[0]) cnt = [0] * n for i in range(n): if lr[i][0] > i: if lr[i][0] - i > cnt[lr[i][0]]: cnt[lr[i][0]] = lr[i][0] - i i = n - 1 tmp = 0 ans = 0 lst = [] while i >= 0: if i > 0 and lr[i][0] == lr[i - 1][0]: lst.append(lr[i][1]) i = i - 1 else: lst.append(lr[i][1]) if cnt[lr[i][0]] > tmp: lst.sort() for _ in range(tmp, cnt[lr[i][0]]): ans = ans + lst.pop(0) tmp = cnt[lr[i][0]] i = i - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys input = lambda: sys.stdin.readline().rstrip() ANS = [] T = int(input()) for _ in range(T): N, Q = list(map(int, input().split())) A = [int(a) for a in input().split()] ans = sum(max(b - a, 0) for a, b in zip([0] + A, A)) ANS.append(ans) for _ in range(Q): l, r = list(map(int, input().split())) l, r = l - 1, r - 1 if l == r: ANS.append(ans) continue ans -= max(A[l] - A[l - 1], 0) if l else A[l] if l < N - 1: ans -= max(A[l + 1] - A[l], 0) if r > l + 1: ans -= max(A[r] - A[r - 1], 0) if r < N - 1: ans -= max(A[r + 1] - A[r], 0) A[l], A[r] = A[r], A[l] ans += max(A[l] - A[l - 1], 0) if l else A[l] if l < N - 1: ans += max(A[l + 1] - A[l], 0) if r > l + 1: ans += max(A[r] - A[r - 1], 0) if r < N - 1: ans += max(A[r + 1] - A[r], 0) ANS.append(ans) print("\n".join(map(str, ANS)))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
from sys import stdin, stdout input = stdin.readline c = 0 a = [] b = [] def f(i): if a[i] == 0: return 0 if a[i - 1] < a[i] > a[i + 1]: return 1 if a[i - 1] > a[i] < a[i + 1]: return -1 return 0 def relax(i): global c c += (f(i) - b[i]) * a[i] b[i] = f(i) def relax1(i): global c c -= b[i] * a[i] b[i] = 0 for _ in range(int(input())): c = 0 n, q = map(int, input().split()) (*a,) = map(int, input().split()) a = [0] + a + [0] b = [0] * (n + 2) for i in range(1, n + 1): relax(i) print(c) for i in range(q): l, r = map(int, input().split()) relax1(l) relax1(l - 1) relax1(l + 1) relax1(r) relax1(r - 1) relax1(r + 1) a[l], a[r] = a[r], a[l] relax(l) relax(l - 1) relax(l + 1) relax(r) relax(r - 1) relax(r + 1) print(c)
ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys def big(i): global a return a[i] > a[i - 1] and a[i] >= a[i + 1] def little(i): global a return a[i] <= a[i - 1] and a[i] < a[i + 1] def make(a, i, x): global s if big(i): s -= a[i] if little(i): s += a[i] if big(i - 1): s -= a[i - 1] if little(i - 1): s += a[i - 1] if big(i + 1): s -= a[i + 1] if little(i + 1): s += a[i + 1] a[i] = x if big(i): s += a[i] if little(i): s -= a[i] if big(i - 1): s += a[i - 1] if little(i - 1): s -= a[i - 1] if big(i + 1): s += a[i + 1] if little(i + 1): s -= a[i + 1] t = int(input()) for I in range(t): n, q = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) a.append(0) a.append(0) up = True s = 0 for i in range(n): if up: if a[i] > a[i - 1]: s += a[i] - a[i - 1] else: up = False elif a[i] > a[i - 1]: up = True s += a[i] - a[i - 1] print(s) for i in range(q): l, r = map(int, sys.stdin.readline().split()) l -= 1 r -= 1 ll = a[l] rr = a[r] make(a, l, rr) make(a, r, ll) print(s)
IMPORT FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys def solve(n, q, a, u): z = [] def chk(i): if a[i - 1] < a[i] > a[i + 1]: return a[i] if a[i - 1] > a[i] < a[i + 1]: return -a[i] return 0 if n == 1: z.append(a[0]) for i in u: z.append(a[0]) else: a = [-1] + a + [-1] c = 0 for i in range(1, n + 1): c += chk(i) z.append(c) for l, r in u: if l == r: z.append(c) else: b = {l, max(1, l - 1), min(n, l + 1), r, max(1, r - 1), min(n, r + 1)} for j in b: c -= chk(j) a[l], a[r] = a[r], a[l] for j in b: c += chk(j) z.append(c) return z def stdin(): while True: yield sys.stdin.readline() inputs = stdin() t = int(next(inputs)) z = [] for _ in range(t): n, q = map(int, next(inputs).split()) a = list(map(int, next(inputs).split())) u = [map(int, next(inputs).split()) for i in range(q)] z += solve(n, q, a, u) print("\n".join(map(str, z)))
IMPORT FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF WHILE NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys input = sys.stdin.readline def main(): n, q = map(int, input().split()) alst = [0, 0] + list(map(int, input().split())) + [0, 0] pm = [(0) for _ in range(n + 4)] ans = 0 for i in range(2, n + 2): if alst[i - 1] < alst[i] and alst[i + 1] <= alst[i]: pm[i] = 1 ans += alst[i] elif alst[i - 1] > alst[i] and alst[i + 1] >= alst[i]: pm[i] = -1 ans -= alst[i] print(ans) for _ in range(q): l, r = map(int, input().split()) l += 1 r += 1 lr_s = set() for lr in [l, r]: for i in range(lr - 1, lr + 2): lr_s.add(i) for i in lr_s: ans -= alst[i] * pm[i] alst[l], alst[r] = alst[r], alst[l] for i in lr_s: if alst[i - 1] < alst[i] and alst[i + 1] <= alst[i]: pm[i] = 1 ans += alst[i] elif alst[i - 1] > alst[i] and alst[i + 1] >= alst[i]: pm[i] = -1 ans -= alst[i] else: pm[i] = 0 print(ans) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys def main(): iter_data = map(int, sys.stdin.read().split()) t = next(iter_data) for t_it in range(t): n, q = next(iter_data), next(iter_data) a = [0] + [next(iter_data) for _ in range(n)] + [0] all_ans = [0] * (q + 1) val = [0] * (n + 1) for i in range(n + 1): val[i] = max(a[i], a[i + 1]) cur_ans = sum(val) - sum(a) all_ans[0] = cur_ans for q_it in range(1, q + 1): l, r = next(iter_data), next(iter_data) if l != r: a[l], a[r] = a[r], a[l] cur_ans -= val[l - 1] + val[l] + val[r] val[l - 1] = max(a[l - 1], a[l]) val[l] = max(a[l], a[l + 1]) val[r] = max(a[r], a[r + 1]) cur_ans += val[l - 1] + val[l] + val[r] if r - l > 1: cur_ans -= val[r - 1] val[r - 1] = max(a[r - 1], a[r]) cur_ans += val[r - 1] all_ans[q_it] = cur_ans print(*all_ans, sep="\n") main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys z = sys.stdin.readline o = [] y = lambda a, b: a - b if a - b > 0 else 0 for _ in range(int(z())): n, q = map(int, z().split()) p = [0] + [*map(int, z().split())] s = m = 0 for i in p: if i > m: s += i - m m = i o.append(s) for i in range(q): a, b = map(int, z().split()) if a == b: o.append(s) continue va, vb = p[a], p[b] va1 = p[a - 1] c = b < n if c: vb1 = p[b + 1] p[a], p[b] = vb, va lv = nv = 0 if b - a < 2: if vb > va: lv += vb - va if va > va1: lv += va - va1 if c and vb1 > vb: lv += vb1 - vb if va > vb: nv += va - vb if vb > va1: nv += vb - va1 if c and vb1 > va: nv += vb1 - va else: va2, vb2 = p[a + 1], p[b - 1] if va2 > va: lv += va2 - va if va > va1: lv += va - va1 if vb > vb2: lv += vb - vb2 if c and vb1 > vb: lv += vb1 - vb if va2 > vb: nv += va2 - vb if vb > va1: nv += vb - va1 if va > vb2: nv += va - vb2 if c and vb1 > va: nv += vb1 - va s += nv - lv o.append(s) print("\n".join(map(str, o)))
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
from sys import stdin tt = int(stdin.readline()) pls = [] for loop in range(tt): n, q = list(map(int, stdin.readline().split())) a = list(map(int, stdin.readline().split())) a = [0] + a + [0] lis = [0] * len(a) ans = 0 for i in range(1, len(a) - 1): if a[i - 1] < a[i] and a[i] > a[i + 1]: lis[i] = 1 ans += a[i] elif a[i - 1] > a[i] and a[i] < a[i + 1]: lis[i] = -1 ans -= a[i] pls.append(ans) for loop in range(q): l, r = list(map(int, stdin.readline().split())) if l == r: pls.append(ans) continue ch = {} ch[l - 1] = 0 ch[l] = 0 ch[l + 1] = 0 ch[r - 1] = 0 ch[r] = 0 ch[r + 1] = 0 for i in ch: ans -= a[i] * lis[i] a[l], a[r] = a[r], a[l] for i in ch: if i == 0 or i == len(a) - 1: pass elif a[i - 1] < a[i] and a[i] > a[i + 1]: lis[i] = 1 ans += a[i] elif a[i - 1] > a[i] and a[i] < a[i + 1]: lis[i] = -1 ans -= a[i] else: lis[i] = 0 pls.append(ans) print("\n".join(map(str, pls)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys input = sys.stdin.buffer.readline INF = 10**18 t = int(input()) for _ in range(t): n, q = map(int, input().split()) a = [-INF] + list(map(int, input().split())) + [-INF] queries = [list(map(int, input().split())) for i in range(q)] ans = 0 for i in range(1, n + 1): if a[i - 1] < a[i] and a[i + 1] < a[i]: ans += a[i] if a[i] < a[i - 1] and a[i] < a[i + 1]: ans -= a[i] print(ans) for ll, rr in queries: for l in range(ll - 2, ll + 3): if not (0 <= l - 1 and l + 1 <= n + 1): continue elif a[l - 1] < a[l] and a[l + 1] < a[l]: ans -= a[l] elif a[l] < a[l - 1] and a[l] < a[l + 1]: ans += a[l] for r in range(max(rr - 2, ll + 3), rr + 3): if not (0 <= r - 1 and r + 1 <= n + 1): continue elif a[r - 1] < a[r] and a[r + 1] < a[r]: ans -= a[r] elif a[r] < a[r - 1] and a[r] < a[r + 1]: ans += a[r] a[ll], a[rr] = a[rr], a[ll] for l in range(ll - 2, ll + 3): if not (0 <= l - 1 and l + 1 <= n + 1): continue elif a[l - 1] < a[l] and a[l + 1] < a[l]: ans += a[l] elif a[l] < a[l - 1] and a[l] < a[l + 1]: ans -= a[l] for r in range(max(rr - 2, ll + 3), rr + 3): if not (0 <= r - 1 and r + 1 <= n + 1): continue elif a[r - 1] < a[r] and a[r + 1] < a[r]: ans += a[r] elif a[r] < a[r - 1] and a[r] < a[r + 1]: ans -= a[r] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
import sys def isLocalMin(i, a, n): if a[i - 1] > a[i] < a[i + 1]: return True else: return False def isLocalMax(i, a, n): if a[i - 1] < a[i] > a[i + 1]: return True else: return False def main(): t = int(input()) ans = [] for _ in range(t): n, q = readIntArr() a = readIntArr() a = [-1] + a + [-1] swaps = [] for __ in range(q): swaps.append(readIntArr()) total = 0 for i in range(1, n + 1): if isLocalMin(i, a, n): total -= a[i] elif isLocalMax(i, a, n): total += a[i] ans.append(total) for l, r in swaps: idxes = set() for ll in range(l - 1, l + 2): if 1 <= ll < n + 1: idxes.add(ll) for rr in range(r - 1, r + 2): if 1 <= rr < n + 1: idxes.add(rr) for idx in idxes: if isLocalMin(idx, a, n): total += a[idx] elif isLocalMax(idx, a, n): total -= a[idx] a[l], a[r] = a[r], a[l] for idx in idxes: if isLocalMin(idx, a, n): total -= a[idx] elif isLocalMax(idx, a, n): total += a[idx] ans.append(total) multiLineArrayPrint(ans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] main()
IMPORT FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5$) denoting the number of pokémon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 -----Note----- Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5-3+7=9$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $2-1+5-3+7=10$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $2-1+5-3+7=10$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $2-1+5-3+7=10$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $5-3+7=9$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $4-2+5-3+7=11$.
input = __import__("sys").stdin.readline def check(*inds): global localmax, localmin, maxs, mins for ind in inds: if not ok(ind): continue if localmax[ind] == 0 and s[ind - 1] < s[ind] > s[ind + 1]: localmax[ind] = 1 maxs += s[ind] if localmin[ind] == 0 and s[ind - 1] > s[ind] < s[ind + 1]: localmin[ind] = 1 mins += s[ind] def upd0(*inds): global localmax, localmin, maxs, mins for ind in inds: if not ok(ind): continue if localmax[ind]: localmax[ind] = 0 maxs -= s[ind] if localmin[ind]: localmin[ind] = 0 mins -= s[ind] def ok(ind): return 0 <= ind - 1 <= ind + 1 <= n ans = [] for _ in range(int(input())): n, q = map(int, input().split()) n += 1 s = [0] + list(map(int, input().split())) + [0] localmax = [0] * n localmin = [0] * n maxs = mins = 0 for i in range(n): check(i) ans.append(maxs - mins) for _ in range(q): l, r = map(int, input().split()) upd0(l, l - 1, l + 1, r, r - 1, r + 1) s[l], s[r] = s[r], s[l] check(l, l - 1, l + 1, r, r - 1, r + 1) ans.append(maxs - mins) print(*ans, sep="\n")
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_DEF RETURN NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] dp = [float("inf") for i in range(n)] all = [0] last = a[0] for i in range(1, n): if a[i] < last: all.append(i) last = a[i] if all[-1] != n - 1: all.append(n - 1) ans = 0 p = len(all) for i in range(p - 1): first = all[i] second = all[i + 1] ans += (second - first + 1) * a[first] - a[second] print(max(0, ans))
ASSIGN VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
t = int(input()) while t > 0: n = int(input()) lst = list(map(int, input().split())) energy = 0 i = 0 for j in range(n - 1): if lst[i] > lst[j]: energy += (j - i + 1) * lst[i] - lst[j] i = j energy += (n - 1 - i + 1) * lst[i] - lst[n - 1] print(max(energy, 0)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) prev, ans = 0, 0 for i in range(1, n): if l[i] < l[prev] or i == n - 1: ans = ans + (i - prev + 1) * l[prev] - l[i] prev = i ans = max(0, ans) print(ans)
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 VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
for _ in range(int(input())): n = int(input()) s = list(map(int, input().split())) ans = 0 minn = s[0] for i in range(n): ans += minn minn = min(minn, s[i]) print(max(ans - s[-1], 0))
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 NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
def subArrayJump(n, arr): e = 0 st = 1 for idx in range(1, n): if arr[st - 1] > arr[idx] or idx == n - 1: x = (idx - st + 2) * arr[st - 1] - arr[idx] e = e + x st = idx + 1 return max(0, e) t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().strip().split(" "))) print(subArrayJump(n, arr))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL 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 FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
t = int(input()) for k in range(t): n = int(input()) a = list(map(int, input().split())) e = 0 if n != 1: d = 1 i = 1 p = a[0] while i < n: if p > a[i]: d += 1 e += d * p - a[i] d = 1 p = a[i] else: d += 1 if i == n - 1: e += d * p - a[i] i += 1 if e < 0: e = 0 print(e)
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 IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) if N == 1: print(0) else: ans = 2 * A[0] - A[-1] temp = A[0] for i in range(1, N - 1): if A[i] >= temp: ans += temp else: ans += A[i] temp = A[i] print(max(0, ans))
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
get_int = lambda: int(input()) get_list_int = lambda: list(map(int, input().split())) t = get_int() for t_itr in range(t): n = get_int() arr = get_list_int() ans = 0 prev = 0 for i in range(1, n): if i == n - 1 or arr[i] <= arr[prev]: ans += (i - prev + 1) * arr[prev] - arr[i] prev = i print(max(0, ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
from sys import stdin, stdout write = stdout.write input = stdin.readline for t in range(int(input())): n = int(input()) A = list(map(int, input().strip().split())) dp = [0] mindp, minx = 0, 0 for i in range(1, n): temp = float("inf") for j in (mindp, minx): temp = min(temp, dp[j] + ((i - j + 1) * A[j] - A[i])) if dp[i - 1] <= dp[mindp]: mindp = i if A[i] <= A[minx]: minx = i dp.append(temp) print(max(0, dp[-1]))
ASSIGN VAR VAR ASSIGN 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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
def solve(arr, n): energy = 0 curr_cost = arr[0] last_step = 1 last_completed = False for i in range(n): last_completed = False if curr_cost > arr[i]: energy += curr_cost * (i + 1 - last_step + 1) - arr[i] curr_cost = arr[i] last_step = i + 1 last_completed = True if not last_completed: energy += curr_cost * (n + 1 - last_step) - arr[-1] return max(energy, 0) t = int(input()) for _ in range(t): n = int(input()) stones = [int(x) for x in input().split()] print(solve(stones, n))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
for _ in range(int(input())): n = int(input()) x = list(map(int, input().split())) if n == 1: print(0) elif n == 2: ans = 2 * x[0] - x[1] elif n > 2: ans = 2 * x[0] - x[-1] temp = x[0] for i in range(1, n - 1): if temp <= x[i]: ans += temp elif temp > x[i]: ans += x[i] temp = x[i] print(max(0, ans))
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
t = int(input()) for _ in range(t): n = int(input()) stones = list(map(int, input().split())) ans = n * stones[0] - stones[-1] jumps = [0] val = 0 for i in range(1, n): if stones[i] <= stones[jumps[-1]]: val += (i - jumps[-1] + 1) * stones[jumps[-1]] - stones[i] jumps.append(i) if jumps[-1] != n - 1: val += (n - jumps[-1]) * stones[jumps[-1]] - stones[-1] print(max(0, min(val, ans)))
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 BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
There are N stones in a pond, each having a value A_{i} written on it. A frog is at stone 1 and wants to reach stone N. The frog can jump from a stone i to any stone j (j>i). Let d be the length of subarray (i.e. j-i+1), then the energy required for the jump is (d \cdot A_{i})-A_{j}. Find the minimum non-negative amount of energy required by the frog to reach the N-th stone. Note: It is possible that the total amount of energy required is negative, in that case, you should print the minimum non-negative value (i.e. 0). ------ Input Format ------ - The first line contains an integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the number of stones. - The second line contains N integers denoting the numbers written on the stones. ------ Output Format ------ For each test case output a single integer - the minimum non-negative energy required by the frog. ------ Constraints ------ $ 1 ≤T ≤2500 $ - the number of test cases $ 1 ≤N ≤10^{5} $ - the number of stones. $ 1 ≤A_{i} ≤10^{9} $ - the number written on stones. - Sum of $N$ over all test cases does not exceed $5 \cdot 10^{5} $ ------ subtasks ------ Subtask 1 (5 points): $1 ≤N ≤10, 1 ≤A_{i} ≤100, \sum N ≤50$ Subtask 2 (10 points): $1 ≤N ≤100, 1 ≤A_{i} ≤100, \sum N ≤500$ Subtask 3 (15 points): $1 ≤N ≤1000, \sum N ≤5000$ Subtask 4 (70 points): original constraints ----- Sample Input 1 ------ 4 3 6 1 3 4 3 1 10 4 3 7 9 1 2 1 5 ----- Sample Output 1 ------ 10 4 20 0 ----- explanation 1 ------ - Test Case $1$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone, and then to the $3^{rd}$ stone from the $2^{nd}$ stone. - Test Case $3$: The frog can jump to the $3^{rd}$ stone directly from the $1^{st}$ stone. - Test Case $4$: The frog can jump to the $2^{nd}$ stone from the $1^{st}$ stone. However, the energy required in this case is negative (i.e. $-3$), So we will print $0$.
T = int(input()) for i in range(T): N = int(input()) A = list(map(int, input().split(" "))) energy = 0 lp = 0 rp = 0 while rp < N: if A[rp] >= A[lp]: rp += 1 else: energy += (rp - lp + 1) * A[lp] - A[rp] lp = rp energy += (N - lp) * A[lp] - A[N - 1] if energy <= 0: print(0) else: print(energy)
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 STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) // 3 def get_maxsubseq(pieces): m = len(pieces) dp = {} for i in range(1, n + 1): tdp = {} for j in range(m): tdp[j] = max(dp.get(j - 2, 0) + pieces[j], tdp.get(j - 1, 0)) dp = tdp return dp[m - 1] return max(get_maxsubseq(slices[1:]), get_maxsubseq(slices[:-1]))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, s: List[int]) -> int: ct = len(s) // 3 @lru_cache(None) def f(l, r, n): if n == 0: return 0 if l > r: return -1e18 if n == 1: return max(s[l : r + 1]) return max(f(l + 1, r, n), s[l] + f(l + 2, r, n - 1)) return max(f(1, len(s) - 1, ct), s[0] + f(2, len(s) - 2, ct - 1))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: @lru_cache(None) def dp(i, remaining, version): if i > len(slices) - version: return 0 if remaining == 0: return 0 else: return max( dp(i + 1, remaining, version), slices[i] + dp(i + 2, remaining - 1, version), ) return max(dp(1, len(slices) // 3, 1), dp(0, len(slices) // 3, 2))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: N = len(slices) // 3 dpOn = [[(-1) for i in range(3 * N)] for j in range(N + 1)] dpOff = [[(-1) for i in range(3 * N)] for j in range(N + 1)] dpOn[1][0] = slices[0] for i in range(1, 3 * N): dpOff[0][i] = 0 for i in range(1, 3 * N): for j in range(1, N + 1): dpOff[j][i] = max(dpOn[j][i - 1], dpOff[j][i - 1]) if dpOff[j - 1][i - 1] == -1: dpOn[j][i] = -1 else: dpOn[j][i] = dpOff[j - 1][i - 1] + slices[i] maxVal = dpOff[N][3 * N - 1] dpOn = [[(-1) for i in range(3 * N)] for j in range(N + 1)] dpOff = [[(-1) for i in range(3 * N)] for j in range(N + 1)] for i in range(0, 3 * N): dpOff[0][i] = 0 for i in range(1, 3 * N): for j in range(1, N + 1): dpOff[j][i] = max(dpOn[j][i - 1], dpOff[j][i - 1]) if dpOff[j - 1][i - 1] == -1: dpOn[j][i] = -1 else: dpOn[j][i] = dpOff[j - 1][i - 1] + slices[i] maxVal = max(maxVal, dpOn[N][3 * N - 1], dpOff[N][3 * N - 1]) return maxVal
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, w: List[int]) -> int: k = len(w) // 3 dp1 = [([0] * len(w)) for _ in range(k + 1)] dp2 = [([0] * len(w)) for _ in range(k + 1)] for n in range(1, k + 1): for i in range(1, len(w)): if i == 1: dp1[n][i] = w[i - 1] dp2[n][i] = w[i] continue dp1[n][i] = max(dp1[n - 1][i - 2] + w[i - 1], dp1[n][i - 1]) dp2[n][i] = max(dp2[n - 1][i - 2] + w[i], dp2[n][i - 1]) return max(dp1[-1][-1], dp2[-1][-1])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) nPick = n // 3 total = [[([0] * (n + 1)) for _ in range(nPick + 1)] for __ in range(2)] total[1][1][1] = slices[0] for i in range(1, nPick + 1): for j in range(2, n + 1): total[0][i][j] = max( slices[j - 1] + total[0][i - 1][j - 2], total[0][i][j - 1] ) total[1][i][j] = max( slices[j - 1] + total[1][i - 1][j - 2], total[1][i][j - 1] ) return max(max(total[0][nPick]), max(total[1][nPick][:-1]))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) rounds = n // 3 def num_s(slices): n = len(slices) dp = [i for i in slices] for i in range(rounds): maxx = 0 dp2 = [0] * (i * 2) for j in range(i * 2, n): if i == 0: dp2.append(max(maxx, slices[j])) maxx = dp2[j] else: dp2.append(max(maxx, slices[j] + dp[j - 2])) maxx = dp2[j] dp = dp2 return dp[len(dp) - 1] return max(num_s(slices[1:]), num_s(slices[: n - 1]))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: slicesN = len(slices) target = slicesN // 3 table1 = [[(0) for i in range(slicesN)] for ti in range(target + 1)] table2 = [[(0) for i in range(slicesN)] for ti in range(target + 1)] for ti in range(1, target + 1): for i in range(1, slicesN): if i == 1: table1[ti][i] = slices[i] table2[ti][i] = slices[i - 1] continue table1[ti][i] = max( table1[ti][i - 1], table1[ti - 1][i - 2] + slices[i] ) table2[ti][i] = max( table2[ti][i - 1], table2[ti - 1][i - 2] + slices[i - 1] ) return max(table1[-1][-1], table2[-1][-1])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def __init__(self): self.dp = {} def solve_linear(self, slices, ind, to_choose): if (ind, to_choose) not in self.dp: N = len(slices) if N <= ind or to_choose == 0: return 0 result_choose = self.solve_linear(slices, ind + 2, to_choose - 1) result_not_choose = self.solve_linear(slices, ind + 1, to_choose) self.dp[ind, to_choose] = max( result_choose + slices[ind], result_not_choose ) return self.dp[ind, to_choose] def maxSizeSlices(self, slices: List[int]) -> int: min_elem = min(slices) min_ind = 0 while slices[min_ind] != min_elem: min_ind += 1 N = len(slices) min_ind += 1 arr = [(0) for _ in range(N)] for i in range(N): arr[i] = slices[(min_ind + i) % N] return self.solve_linear(arr, 0, int(N / 3))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: a, b, n = [slices[0]], [0], len(slices) for i in range(1, n): a.append(max(a[-1], slices[i])) b.append(max(b[-1], slices[i])) for i in range(2, 2 * n // 3, 2): aa, bb = [0] * (n - 1), [0] * n for j in range(i, n - 1): aa[j] = max(aa[j - 1], a[j - 2] + slices[j]) for j in range(i + 1, n): bb[j] = max(bb[j - 1], b[j - 2] + slices[j]) a, b = aa, bb return max(a[-1], b[-1])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: l = len(slices) s1 = slices[0 : l - 1] s2 = slices[1:l] n = l // 3 def summax(arr): dp = [([0] * (n + 1)) for i in range(l + 1)] p = len(arr) for i in range(1, p + 1): for j in range(1, n + 1): if j > i: break if i == 1: dp[i][j] = arr[0] else: dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + arr[i - 1]) return dp[p][n] return max(summax(s1), summax(s2))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def dp(self, i, j, k, l): if (i, j, k, l) not in self.memo: if k == 1: self.memo[i, j, k, l] = max(self.s[i:j]) elif j - i < 2 * k - 1: self.memo[i, j, k, l] = float("-inf") else: self.memo[i, j, k, l] = max( self.dp(i + 2, j - l, k - 1, 0) + self.s[i], self.dp(i + 1, j, k, 0) ) return self.memo[i, j, k, l] def maxSizeSlices(self, slices: List[int]) -> int: self.memo, self.s, n = {}, slices, len(slices) return self.dp(0, n, n // 3, 1)
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR DICT VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: m = len(slices) n = m // 3 slice1 = slices[: m - 1] slice2 = slices[1:] return max(self.max_sum(slice1, n), self.max_sum(slice2, n)) def max_sum(self, slices, n): m = len(slices) dp = [([0] * (n + 1)) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): if i == 1: dp[i][j] = slices[0] else: dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i - 1]) return dp[m][n]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: m = len(slices) n = m // 3 dp = [([0] * (n + 1)) for i in range(m + 1)] for i in range(m - 2, -1, -1): for j in range(1, n + 1): dp[i][j] = max(dp[i + 1][j], slices[i] + dp[i + 2][j - 1]) dp2 = [([0] * (n + 1)) for i in range(m + 2)] for i in range(m - 1, 0, -1): for j in range(1, n + 1): dp2[i][j] = max(dp2[i + 1][j], slices[i] + dp2[i + 2][j - 1]) return max(dp[0][n], dp2[1][n])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: N = len(slices) target = N // 3 dp = [[(-1) for x in range(target)] for y in range(N)] def dfs(idx: int, cnt: int) -> int: if cnt >= target or idx >= N: return 0 if dp[idx][cnt] == -1: dp[idx][cnt] = max( dfs(idx + 1, cnt), slices[idx] + dfs(idx + 2, cnt + 1) ) return dp[idx][cnt] result = dfs(1, 0) dp = [[(-1) for x in range(target)] for y in range(N)] N -= 1 result = max(result, dfs(0, 0)) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: N = len(slices) @lru_cache(None) def rec(cur, taken, avoid_last): if taken * 3 > N: return -math.inf if cur >= N: return 0 if taken * 3 == N else -math.inf ans = rec(cur + 1, taken, avoid_last) if avoid_last and cur == N - 1: return ans ans = max( ans, rec(cur + 2, taken + 1, avoid_last | (cur == 0)) + slices[cur] ) return ans return rec(0, 0, False)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN VAR IF VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: N = len(slices) @lru_cache(None) def dp(i, k, d): if 3 * k == N: return 0 if i >= N - d: return -math.inf return max(slices[i] + dp(i + 2, k + 1, d), dp(i + 1, k, d)) return max(slices[0] + dp(2, 1, 1), dp(1, 0, 0))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: @lru_cache(None) def dp(i, j, k): if k == 0: return 0 elif i >= j: return -math.inf else: return max(slices[i] + dp(i + 2, j, k - 1), dp(i + 1, j, k)) n = len(slices) s = n // 3 return max(dp(1, n, s), dp(0, n - 1, s))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) // 3 def max_adj_sum(a): l = len(a) if l == 0: return 0 if l == 1: return a[0] dp = [([0] * l) for i in range(n + 1)] for i in range(1, n + 1): dp[i][0] = a[0] for j in range(1, l): t = dp[i - 1][j - 2] if j >= 2 else 0 dp[i][j] = max(dp[i][j - 1], a[j] + t) return dp[n][-1] return max(max_adj_sum(slices[1:]), max_adj_sum(slices[:-1]))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: def helper(start, end): dp = [([0] * n) for _ in range(end - start + 1)] dp[0] = [slices[start]] * n dp[1] = [max(slices[start], slices[start + 1])] * n for i in range(start + 2, end + 1): dp[i - start][0] = max(slices[i], dp[i - start - 1][0]) for j in range(1, n): dp[i - start][j] = max( dp[i - start - 1][j], slices[i] + dp[i - start - 2][j - 1] ) return dp[-1][-1] n = len(slices) // 3 return max(helper(0, len(slices) - 2), helper(1, len(slices) - 1))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST VAR VAR VAR ASSIGN VAR NUMBER BIN_OP LIST FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) k = n // 3 dp1 = collections.deque([([0] * (k + 1)) for i in range(2)]) dp2 = collections.deque([([0] * (k + 1)) for i in range(2)]) for i in range(n - 2, -1, -1): new_dp1 = [0] * (k + 1) new_dp2 = [0] * (k + 1) for j in range(1, k + 1): new_dp1[j] = max(dp1[0][j], slices[i] + dp1[1][j - 1]) new_dp2[j] = max(dp2[0][j], slices[i + 1] + dp2[1][j - 1]) dp1.pop() dp1.appendleft(new_dp1) dp2.pop() dp2.appendleft(new_dp2) return max(dp1[0][k], dp2[0][k])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP 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 FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: @lru_cache(None) def dp(i, k): if k == 0 or i > j: return 0 return max(dp(i + 1, j, k), slices[i] + dp(i + 2, j, k - 1)) n = len(slices) k = n // 3 dp = collections.deque([([0] * (k + 1)) for i in range(2)]) for i in range(n - 2, -1, -1): new_dp = [0] * (k + 1) for j in range(1, k + 1): new_dp[j] = max(dp[0][j], slices[i] + dp[1][j - 1]) dp.pop() dp.appendleft(new_dp) a = dp[0][k] dp.clear() dp.extend([([0] * (k + 1)) for i in range(2)]) for i in range(n - 1, 0, -1): new_dp = [0] * (k + 1) for j in range(1, k + 1): new_dp[j] = max(dp[0][j], slices[i] + dp[1][j - 1]) dp.pop() dp.appendleft(new_dp) b = dp[0][k] return max(a, b)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) idx = min(range(n), key=lambda x: slices[x]) slices = slices[idx + 1 :] + slices[:idx] @lru_cache(None) def dp(i, k): if i >= n - 1 or k == 0: return 0 return max(slices[i] + dp(i + 2, k - 1), dp(i + 1, k)) return dp(0, n // 3)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) // 3 if n == 1: return max(slices) @lru_cache(None) def process(f, p, i, k): if i == 0: return slices[0] if f == 1 else 0 if p == 1: return process(f, 0, i - 1, k) elif i == 1 and f == 1 or k == 0: return process(f, 0, i - 1, k) else: return max( process(f, 0, i - 1, k), slices[i] + process(f, 1, i - 1, k - 1) ) return max(process(0, 0, 3 * n - 1, n), process(1, 1, 3 * n - 1, n - 1))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) // 3 def linear(arr): a = [0] * (n + 1) b = [0] * (n + 1) for x in arr: a, b = b, [(i and max(x + a[i - 1], b[i])) for i in range(n + 1)] return b[-1] return max(linear(slices[1:]), linear(slices[:-1]))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution(object): def maxSizeSlices(self, A): N = len(A) A.extend(A) NINF = float("-inf") @lru_cache(None) def dp(i, j, rem): if rem == 0: return 0 elif i > j: return NINF else: return max(dp(i + 2, j, rem - 1) + A[i], dp(i + 1, j, rem)) return max(A[0] + dp(2, N - 2, N // 3 - 1), dp(1, N - 1, N // 3))
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) dp = [[(0) for j in range(n // 3)] for i in range(n)] dp[0][0] = slices[0] for i in range(1, n): dp[i][0] = max(dp[i - 1][0], slices[i]) for i in range(2, n): for j in range(1, min(i // 2 + 1, n // 3)): dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i]) mx = 0 for i in range(n - 1): mx = max(mx, dp[i][-1]) for i in range(n): for j in range(n // 3): dp[i][j] = 0 for i in range(1, n): dp[i][0] = max(dp[i - 1][0], slices[i]) for i in range(2, n): for j in range(1, min(i // 2 + 1, n // 3)): dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i]) mx = max(mx, dp[-1][-1]) return mx
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: self.slices = slices n = len(self.slices) self.mem = dict() rv1 = self.dfs(1, n - 1, n // 3) rv2 = self.dfs(0, n - 2, n // 3) return max(rv1, rv2) def dfs(self, i, j, k): if k <= 0: return 0 if j - i + 1 < 2 * k - 1: return -float("inf") key = i, j, k if key in self.mem: return self.mem[key] rv = max(self.dfs(i + 1, j, k), self.slices[i] + self.dfs(i + 2, j, k - 1)) self.mem[key] = rv return rv
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: original = slices slices = slices[:-1] dp = [ [(0) for j in range(len(original) // 3 + 1)] for i in range(len(slices) + 1) ] for j in range(1, len(original) // 3 + 1): dp[1][j] = slices[0] for j in range(1, len(dp[0])): for i in range(2, len(dp)): dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i - 1], dp[i][j]) ans = dp[-1][-1] slices = original[1:] dp = [ [(0) for j in range(len(original) // 3 + 1)] for i in range(len(slices) + 1) ] for j in range(1, len(original) // 3 + 1): dp[1][j] = slices[0] for j in range(1, len(dp[0])): for i in range(2, len(dp)): dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i - 1], dp[i][j]) ans = max(ans, dp[-1][-1]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: def find(arr, n): dp1 = [(0) for i in range(n + 1)] dp2 = [(0) for i in range(n + 1)] dp2[1] = arr[0] for i in range(1, len(arr)): new_dp1 = [max(dp1[j], dp2[j]) for j in range(n + 1)] new_dp2 = [0] + [(arr[i] + dp1[j - 1]) for j in range(1, n + 1)] dp1 = new_dp1 dp2 = new_dp2 return max(dp2[-1], dp1[-1]) n = len(slices) // 3 return max(find(slices[:-1], n), find(slices[1:], n))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF 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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick next slice in anti clockwise direction of your pick.  Your friend Bob will pick next slice in clockwise direction of your pick. Repeat until there are no more slices of pizzas. Sizes of Pizza slices is represented by circular array slices in clockwise direction. Return the maximum possible sum of slice sizes which you can have.   Example 1: Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6. Example 2: Input: slices = [8,9,8,6,1,1] Output: 16 Output: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8. Example 3: Input: slices = [4,1,2,5,8,3,1,9,7] Output: 21 Example 4: Input: slices = [3,1,2] Output: 3   Constraints: 1 <= slices.length <= 500 slices.length % 3 == 0 1 <= slices[i] <= 1000
class Solution: def maxSizeSlices(self, slices: List[int]) -> int: n = len(slices) memo = {} def solve(i, j, k): if k == 0: return 0 elif i > j: return 0 elif (i, j, k) in memo: return memo[i, j, k] memo[i, j, k] = max(slices[i] + solve(i + 2, j, k - 1), solve(i + 1, j, k)) return memo[i, j, k] return max(solve(0, n - 2, n // 3), solve(1, n - 1, n // 3))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
def main(): while True: try: n, m = map(int, input().strip().split()) s = str(input()) print(getAns(n, m, s)) except EOFError: break def getAns(n, k, s): ans = [0] * (n + 10) s = "0" + s ans[0] = 0 lrt = 0 for i in range(1, n + 1, 1): while lrt < i and (lrt < i - k or s[lrt] == "0"): lrt += 1 ans[i] = ans[max(0, lrt - k - 1)] + lrt if s[lrt] == "1" else ans[i - 1] + i if s[i] == "1": for j in range(i - 1, -1, -1): if s[j] == "1": break ans[j] = min(ans[j], ans[i]) return ans[n] main()
FUNC_DEF WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR STRING BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
def find(roomcount, radius, string): zero_roomcount = [0] * (roomcount + 1) binary_move = 1 << 1000 for i in range(roomcount, 0, -1): if string[i - 1] == "1": binary_move = i zero_roomcount[i] = binary_move dp = [0] for i in range(1, roomcount + 1): dp.append(dp[-1] + i) c = zero_roomcount[max(i - radius, 1)] if c <= i + radius: dp[i] = min(dp[i], dp[max(1, c - radius) - 1] + c) return dp[roomcount] roomcount, radius = map(int, input().split()) string = input() print(find(roomcount, radius, string))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
n, k = (int(i) for i in input().split()) string = input() INF = 10**100 cache = [INF] * n def getVal(idx): if idx < 0 or cache[idx] == INF: return 0 return cache[idx] last_idx = 0 reached_end = False for i in range(n): if string[i] == "0": new = getVal(i - 1) + i + 1 cache[i] = min(cache[i], new) else: new = getVal(i - 1 - k) + i + 1 if new < cache[i]: cache[i] = new last_idx = 0 if reached_end: continue end = i + k + 1 if end >= n: end = n reached_end = True for j in range(max(last_idx, i - k), end): cache[j] = min(new, cache[j]) last_idx = end print(cache[n - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
n, k = list(map(int, input().split())) mask = list(map(int, input())) dp = [0] * (n + 2) nxt = [1 << 31] * (n + 2) for i in range(n, 0, -1): nxt[i] = i if mask[i - 1] is 1 else nxt[i + 1] for i in range(1, n + 1): dp[i] = dp[i - 1] + i idx = nxt[max(1, i - k)] if idx <= i + k: dp[i] = min(dp[i], dp[max(0, idx - k - 1)] + idx) print(dp[n])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
n, k = tuple(map(int, input().rstrip().split())) s = input().rstrip() cur = int(2 * pow(10, 5) * (2 * pow(10, 5) + 1) / 2) + 1 cost = [0] * (n + 1) dp = [0] for i in range(n, 0, -1): if s[i - 1] == "1": cur = i cost[i] = cur for i in range(1, n + 1): dp.append(dp[-1] + i) cs = cost[max(i - k, 1)] if cs <= i + k: dp[i] = min(dp[i], dp[max(1, cs - k) - 1] + cs) print(dp[n])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
n, dist = map(int, input().split()) light = input().strip() right_cover = 0 left_most = [-1] * n for idx, ele in enumerate(light): if ele == "1": for i in range(max(right_cover, idx - dist), min(idx + dist + 1, n)): left_most[i] = idx right_cover = idx + dist + 1 dp = [0] * (2 * n + 1) for i in range(n): dp[i] = 1000000000000000000 for idx in range(n - 1, -1, -1): dp[idx] = min(dp[idx + 1] + idx + 1, dp[idx]) if left_most[idx] != -1: left = left_most[idx] dp[max(left - dist, 0)] = min(dp[idx + 1] + left + 1, dp[max(left - dist, 0)]) dp[idx] = min(dp[max(left - dist, 0)], dp[idx]) print(dp[0])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) s = input().strip() seg_el = 1 << (n + k + 1).bit_length() SEG = [1 << 40] * (2 * seg_el) def getvalue(n, seg_el): i = n + seg_el ANS = 1 << 40 ANS = min(SEG[i], ANS) i >>= 1 while i != 0: ANS = min(SEG[i], ANS) i >>= 1 return ANS def updates(l, r, x): L = l + seg_el R = r + seg_el while L < R: if L & 1: SEG[L] = min(x, SEG[L]) L += 1 if R & 1: R -= 1 SEG[R] = min(x, SEG[R]) L >>= 1 R >>= 1 updates(n, n + k + 1, 0) for i in range(n - 1, -1, -1): if s[i] == "0": x = getvalue(i + 1, seg_el) updates(i, i + 1, x + i + 1) else: x = getvalue(i + k + 1, seg_el) updates(max(0, i - k), i + k + 1, x + i + 1) print(getvalue(0, seg_el))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
You work as a system administrator in a dormitory, which has $n$ rooms one after another along a straight hallway. Rooms are numbered from $1$ to $n$. You have to connect all $n$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $i$-th room is $i$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $i$-th room is also $i$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $i$, you connect all rooms with the numbers from $max(1,~i - k)$ to $min(n,~i + k)$ inclusive to the Internet, where $k$ is the range of router. The value of $k$ is the same for all routers. Calculate the minimum total cost of connecting all $n$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of rooms and the range of each router. The second line of the input contains one string $s$ of length $n$, consisting only of zeros and ones. If the $i$-th character of the string equals to '1' then there is a spot for a router in the $i$-th room. If the $i$-th character of the string equals to '0' then you cannot place a router in the $i$-th room. -----Output----- Print one integer — the minimum total cost of connecting all $n$ rooms to the Internet. -----Examples----- Input 5 2 00100 Output 3 Input 6 1 000000 Output 21 Input 4 1 0011 Output 4 Input 12 6 000010000100 Output 15 -----Note----- In the first example it is enough to place the router in the room $3$, then all rooms will be connected to the Internet. The total cost of connection is $3$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $1 + 2 + 3 + 4 + 5 + 6 = 21$. In the third example you need to connect the room $1$ directly and place the router in the room $3$. Thus, the total cost of connection of all rooms is $1 + 3 = 4$. In the fourth example you need to place routers in rooms $5$ and $10$. Then all rooms will be connected to the Internet. The total cost of connection is $5 + 10 = 15$.
n, k = map(int, input().split()) s = input() inf = 10**18 who = [-1] * n for i in range(n): if s[i] == "1": for j in range(min(n - 1, i + k), i - 1, -1): if who[j] == -1: who[j] = i else: break for i in range(n): if s[i] == "1": for j in range(i - 1, max(-1, i - k - 1), -1): if who[j] == -1: who[j] = i else: break dp = [inf] * (n + 1) dp[n] = 0 for i in range(n, 0, -1): dp[i - 1] = min(dp[i - 1], dp[i] + i) if who[i - 1] != -1: dp[max(0, who[i - 1] - k)] = min( dp[max(0, who[i - 1] - k)], dp[i] + who[i - 1] + 1 ) print(dp[0])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum. But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array. For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9. Can you help Mostafa solve this problem? Input The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. Output Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). Examples Input 3 4 3 1 6 -2 2 3 3 2 -5 1 2 3 1 3 Output 9 Input 6 1 4 0 8 -3 -10 8 3 -2 -5 10 8 -9 -5 -4 1 0 1 -3 3 -8 5 6 2 9 6 1 Output 8
def get_best(arr): ans, now = -(1 << 64), 0 for i in arr: now += i ans = max(ans, now) if now < 0: now = 0 return ans def compute(arr): ans, now = -(1 << 64), 0 for i in arr: now += i ans = max(ans, now) return ans n, m = map(int, input().split()) vals = [] suffix, prefix, summation, best = [0] * n, [0] * n, [0] * n, [0] * n for i in range(n): arr = list(map(int, input().split()))[1:] summation[i] = sum(arr) suffix[i] = compute(list(reversed(arr))) prefix[i] = compute(arr) best[i] = get_best(arr) vals.append(arr) idx = list(map(lambda x: int(x) - 1, input().split())) f = [[(0) for x in range(m + 1)] for p in range(2)] f[0][m] = -(1 << 64) i = m - 1 while i >= 0: cur = idx[i] f[0][i] = max(max(f[0][i + 1], best[cur]), suffix[cur] + f[1][i + 1]) f[1][i] = max(prefix[cur], summation[cur] + f[1][i + 1]) i -= 1 print(f[0][0])
FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum. But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array. For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9. Can you help Mostafa solve this problem? Input The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. Output Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). Examples Input 3 4 3 1 6 -2 2 3 3 2 -5 1 2 3 1 3 Output 9 Input 6 1 4 0 8 -3 -10 8 3 -2 -5 10 8 -9 -5 -4 1 0 1 -3 3 -8 5 6 2 9 6 1 Output 8
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = 1 uu = t while t > 0: t -= 1 n, m = mi() pm = [0] * n sm = [0] * n s = [0] * n ans = [0] * n for i in range(n): a = li() p = a[0] a = a[1:] maxi = -(10**18) c = 0 for j in range(p - 1, -1, -1): c += a[j] maxi = max(maxi, c) sm[i] = maxi s[i] = c maxi = -(10**18) c = 0 for j in range(p): c += a[j] maxi = max(maxi, c) pm[i] = maxi c = 0 maxi = -(10**18) for j in a: if j > c + j: c = j else: c += j maxi = max(maxi, c) ans[i] = maxi b = li() maxi = -(10**18) c = 0 for i in b: maxi = max([maxi, c + pm[i - 1], ans[i - 1]]) if sm[i - 1] > c + s[i - 1]: c = sm[i - 1] else: c += s[i - 1] maxi = max(maxi, c) if c < 0: c = 0 print(maxi)
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum. But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array. For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9. Can you help Mostafa solve this problem? Input The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. Output Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). Examples Input 3 4 3 1 6 -2 2 3 3 2 -5 1 2 3 1 3 Output 9 Input 6 1 4 0 8 -3 -10 8 3 -2 -5 10 8 -9 -5 -4 1 0 1 -3 3 -8 5 6 2 9 6 1 Output 8
n, m = map(int, input().split()) l = [] d = {} e = {} for i in range(n): l = list(map(int, input().split())) a = l[1] b = sum(l) - l[0] c = l[-1] x = l[1] for j in range(2, len(l)): x += l[j] a = max(a, x) x = l[-1] for j in range(len(l) - 2, 0, -1): x += l[j] c = max(c, x) d[i + 1] = [a, b, c] a = l[1] b = 0 for j in range(1, len(l)): b += l[j] a = max(a, b) if b < 0: b = 0 e[i + 1] = a dp = [[0, 0, 0] for i in range(m)] l = list(map(int, input().split())) ans = 0 for i in range(m - 1, -1, -1): if i == m - 1: dp[i][0] = d[l[i]][0] dp[i][1] = d[l[i]][1] dp[i][2] = d[l[i]][2] ans = max(max(dp[i][1], dp[i][0]), dp[i][2]) ans = max(ans, e[l[i]]) else: dp[i][0] = d[l[i]][0] dp[i][1] = d[l[i]][1] + max(max(0, dp[i + 1][0]), dp[i + 1][1]) dp[i][2] = d[l[i]][2] + max(max(0, dp[i + 1][0]), dp[i + 1][1]) ans = max(ans, dp[i][0]) ans = max(ans, dp[i][1]) ans = max(ans, dp[i][2]) ans = max(ans, e[l[i]]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum. But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array. For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9. Can you help Mostafa solve this problem? Input The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. Output Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). Examples Input 3 4 3 1 6 -2 2 3 3 2 -5 1 2 3 1 3 Output 9 Input 6 1 4 0 8 -3 -10 8 3 -2 -5 10 8 -9 -5 -4 1 0 1 -3 3 -8 5 6 2 9 6 1 Output 8
i = input().split() N = int(i[0]) M = int(i[1]) arrList = [] leftMax = [] rightMax = [] listTotal = [] rightSum = [] for i in range(N): inputList = [int(x) for x in input().split()] list = inputList[1 : inputList[0] + 1] listTotal.append(0) leftMax.append(-999999) rightMax.append(-999999) rightSum.append(-999999) rightIter = 0 for j in list: listTotal[i] += j rightIter += j leftMax[i] = max(leftMax[i], listTotal[i]) rightSum[i] = max(rightSum[i], rightIter) rightIter = 0 if rightIter < 0 else rightIter rightMax[i] = rightIter arrList.append(list) idxOrder = [int(x) for x in input().split()][0:M] maxSum = -99999999 currMax = 0 for i in idxOrder: bestTot = max(maxSum, rightSum[i - 1]) bestSum = max(currMax + leftMax[i - 1], currMax + listTotal[i - 1]) maxSum = max(bestSum, bestTot) currMax = max(currMax + listTotal[i - 1], rightMax[i - 1]) print(maxSum)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: mod_one = [-1, -1] mod_two = [-1, -1] total_sum = 0 for x in nums: total_sum += x if x % 3 == 1: if mod_one[0] == -1 or x < mod_one[0]: mod_one[1] = mod_one[0] mod_one[0] = x elif mod_one[1] == -1 or x < mod_one[1]: mod_one[1] = x elif x % 3 == 2: if mod_two[0] == -1 or x < mod_two[0]: mod_two[1] = mod_two[0] mod_two[0] = x elif mod_two[1] == -1 or x < mod_two[1]: mod_two[1] = x if total_sum % 3 == 0: return total_sum elif total_sum % 3 == 1: if mod_one[0] != -1 and ( mod_two[0] == -1 or mod_two[1] == -1 or mod_one[0] < sum(mod_two) ): return total_sum - mod_one[0] elif mod_two[0] != -1 and mod_two[1] != -1: return total_sum - sum(mod_two) else: return 0 elif mod_two[0] != -1 and ( mod_one[0] == -1 or mod_one[1] == -1 or mod_two[0] < sum(mod_one) ): return total_sum - mod_two[0] elif mod_one[0] != -1 and mod_one[1] != -1: return total_sum - sum(mod_one) else: return 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: counts = [0, 0, 0] small_two = [] small_one = [] result = 0 for i in nums: result += i rem = i % 3 counts[i % 3] += 1 if rem == 0: continue elif rem == 1: small_one = sorted(small_one + [i])[:3] else: small_two = sorted(small_two + [i])[:3] print(small_two, small_one, result) if result % 3 == 2: return result - ( small_two[0] if len(small_one) < 2 or small_two[0] < small_one[0] + small_one[1] else small_one[0] + small_one[1] ) if result % 3 == 1: return result - ( small_one[0] if len(small_two) < 2 or small_one[0] < small_two[0] + small_two[1] else small_two[0] + small_two[1] ) return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: total = sum(nums) n = len(nums) if total % 3 == 0: return total nums_set = set(nums) div, mod = divmod(total, 3) nums.sort() while mod <= total: if mod in nums_set: return div * 3 for i in range(n - 2): sum_val = 0 if nums[i] > mod: break for j in range(i + 1, n - 1): if nums[i] + nums[j] == mod: return div * 3 for k in range(j + 1, n): sum_val += nums[i] + sum(nums[j:k]) if sum_val == mod: return div * 3 if sum_val > mod: break div -= 1 mod += 3
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
def s(l1, l1_start_idx, l2, l2_start_idx): if len(l1) < l1_start_idx or len(l2) < l2_start_idx: return 0 res = sum(l1[0:l1_start_idx]) + sum(l2[0:l2_start_idx]) l1_end = l1_start_idx + 3 * ((len(l1) - l1_start_idx) // 3) res += sum(l1[l1_start_idx:l1_end]) l2_end = l2_start_idx + 3 * ((len(l2) - l2_start_idx) // 3) res += sum(l2[l2_start_idx:l2_end]) return res class Solution: def maxSumDivThree(self, nums: List[int]) -> int: res = 0 ones = [] twos = [] for n in nums: mod = n % 3 if mod == 0: res += n elif mod == 1: ones.append(n) elif mod == 2: twos.append(n) ones.sort(reverse=True) twos.sort(reverse=True) counter = 0 ones_sum = [0] for x in ones: counter += x ones_sum.append(counter) counter = 0 twos_sum = [0] for x in twos: counter += x twos_sum.append(counter) m = 0 for i in range(0, min(len(ones), len(twos)) + 1): one_end = i + (len(ones) - i) // 3 * 3 two_end = i + (len(twos) - i) // 3 * 3 x = ones_sum[one_end] + twos_sum[two_end] m = max(m, x) res += m return res
FUNC_DEF IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: x = sum(nums) if x % 3 == 0: return x nums.sort() opt1 = 10**4 + 1 opt2 = count = 0 for n in nums: if n % 3 == x % 3: opt1 = min(opt1, n) if count == 2: break elif n % 3 != 0 and n % 3 != x % 3: if count <= 1: opt2 += n count += 1 if count == 2 and opt1 < 10**4 + 1: break if count == 2: return x - min(opt1, opt2) return x - opt1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR