description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if str[0] == 0 or str.__contains__("00"): return 0 str_len = len(str) if str_len == 0 or str_len == 1: return 1 dp = [-1] * (str_len + 1) return self.validate_message(str, str_len, dp) % 1000000007 def validate_message(self, str, location, dp): if location == 0 or location == 1: return 1 if dp[location] != -1: return dp[location] valid_message_count = 0 char_check = int(str[location - 1]) prev_char_check = int(str[location - 2]) if char_check != 0: valid_message_count += self.validate_message(str, location - 1, dp) if prev_char_check == 1 or prev_char_check == 2 and char_check <= 6: valid_message_count += self.validate_message(str, location - 2, dp) dp[location] = valid_message_count return dp[location]
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): n = len(str) if n == 0: return 1 if str[0] == "0": return 0 dp = [0] * (n + 1) dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): oneDigit = int(str[i - 1]) twoDigit = int(str[i - 2 : i]) if oneDigit >= 1: dp[i] += dp[i - 1] if twoDigit >= 10 and twoDigit <= 26: dp[i] += dp[i - 2] return dp[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def helper(self, str, dp): if str in dp: return dp[str] if len(str) <= 1: return 1 mod = 1000000007 dp[str] = 0 if ( str[0] == "1" or str[0] == "2" and len(str) > 1 and str[1] >= "0" and str[1] <= "6" ): if len(str) == 2: dp[str] += 1 elif str[2] != "0": dp[str] += self.helper(str[2:], dp) % mod if len(str) > 1 and str[1] != "0" or len(str) == 1: dp[str] = (dp[str] + self.helper(str[1:], dp)) % mod return dp[str] def CountWays(self, str): for i in range(len(str)): if str[i] == "0" and (i > 0 and str[i - 1] == "0" or str[i - 1] > "2"): return 0 dp = {} return self.helper(str, dp)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if not str: return 0 dp = [(0) for i in range(len(str) + 1)] dp[0] = 1 if str[0] == "0": dp[1] = 0 else: dp[1] = 1 for i in range(2, len(str) + 1): if 0 < int(str[i - 1 : i]) <= 9: dp[i] = dp[i] + dp[i - 1] if 10 <= int(str[i - 2 : i]) <= 26: dp[i] = dp[i] + dp[i - 2] return dp[len(str)] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def solve(self, data, n, dp): if n == 0: return 1 if n == 1: return 1 if dp[n] != -1: return dp[n] a = 0 if n >= 2: if data[n - 2] == "1" or data[n - 2] == "2" and data[n - 1] <= "6": a += self.solve(data, n - 2, dp) if n >= 1 and int(data[n - 1]) != 0: a += self.solve(data, n - 1, dp) dp[n] = a return dp[n] def CountWays(self, str): dp = [(-1) for i in range(len(str) + 1)] res = self.solve(str, len(str), dp) return res % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): d = [0] * len(s) if s[0] != "0": d[0] = 1 else: return 0 for i in range(1, len(s)): if s[i] != "0": d[i] += d[i - 1] g = s[i - 1] + s[i] h = int(g) if s[i - 1] != "0" and h <= 26 and h > 0: if i - 2 >= 0: d[i] += d[i - 2] else: d[i] += 1 return d[-1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, strx): n = len(strx) if n == 0: return 1 if strx.find("00") >= 0 or strx[0] == "0": return 0 for i in range(3, 10): if strx.find(i.__str__() + "0") >= 0: return 0 strx = strx.replace("10", "A") strx = strx.replace("20", "A") n = len(strx) dp = [(-1) for i in range(n + 1)] def mask(i): if dp[i] != -1: return dp[i] if i == n: return 1 c = 0 if i < n - 1 and strx[i + 1] == "0": c = mask(i + 2) else: c = mask(i + 1) if i < n - 1 and ( strx[i] == "1" and ord(strx[i + 1]) < ord("A") or strx[i] == "2" and ord(strx[i + 1]) <= ord("6") ): c = (c + mask(i + 2)) % 1000000007 dp[i] = c return c return mask(0)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR STRING NUMBER VAR NUMBER STRING RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
mod = 1000000000.0 + 7 class Solution: def CountWays(self, digits): n = len(digits) count = [0] * (n + 1) count[0] = 1 count[1] = 1 for i in range(2, n + 1): count[i] = 0 if digits[i - 1] > "0": count[i] = count[i - 1] if digits[i - 2] == "1" or digits[i - 2] == "2" and digits[i - 1] < "7": count[i] = int((count[i] + count[i - 2]) % mod) return count[n]
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): def isValid(n): if n == "" or n[0] == "0": return False n = int(n) if n == 0 or n > 26: return False return True n = len(s) def f(i): if i == n: return 1 if dp[i] != -1: return dp[i] count = 0 if i + 1 <= n and isValid(s[i : i + 1]): count += f(i + 1) if i + 2 <= n and isValid(s[i : i + 2]): count += f(i + 2) dp[i] = count return dp[i] dp = [-1] * n return f(0) % 1000000007
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR STRING VAR NUMBER STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, stg): dp = [(-1) for i in range(len(stg) + 1)] n = len(stg) def recursive(dp, stg, n): if n == 0: return 1 if dp[n] != -1: return dp[n] char = stg[n - 1] if char == "0" and (n == 1 or int(stg[n - 2]) < 1 or int(stg[n - 2]) > 2): return float("inf") if char != "0": dp[n] = recursive(dp, stg, n - 1) if n > 1 and ( char <= "9" and stg[n - 2] == "1" or char <= "6" and stg[n - 2] == "2" ): dp[n] += recursive(dp, stg, n - 2) else: dp[n] = recursive(dp, stg, n - 2) return dp[n] ans = recursive(dp, stg, n) if ans == float("inf"): return 0 return ans % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER STRING VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN BIN_OP VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, st): di = {x: (1) for x in range(1, 27)} m = 1000000007 li = [1] if st[0] == "0": return 0 li.append(1) for i in range(1, len(st)): if st[i] == "0": if st[i - 1] == "1" or st[i - 1] == "2": x = li[-2] % m li.append(x) else: return 0 else: x = int(str[i]) y = int(str[i - 1 : i + 1]) if y == x or y not in di.keys(): n = li[-1] % m li.append(n) else: li.append((li[-1] % m + li[-2] % m) % m) return li[-1] % m
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER STRING RETURN NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): memo = {} def dfs(s): if s in memo: return memo[s] if not s: return 1 if int(s[0]) == 0: return 0 l = dfs(s[1:]) r = 0 if len(s) >= 2 and int(s[:2]) < 27: r = dfs(s[2:]) memo[s] = l + r return l + r mod = 1000000007 return dfs(str) % mod
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
import sys class Combination: def __init__(self, n, MOD): self.f = [1] for i in range(1, n + 1): self.f.append(self.f[-1] * i % MOD) self.inv_f = [0] * (n + 1) self.inv_f[n] = pow(self.f[n], MOD - 2, MOD) for i in reversed(range(n)): self.inv_f[i] = self.inv_f[i + 1] * (i + 1) % MOD self.MOD = MOD def inv(self, k): return self.inv_f[k] * self.f[k - 1] % self.MOD def fact(self, k): return self.f[k] def inv_fact(self, k): return self.inv_f[k] def perm(self, k, r): if k < r: return 0 return self.f[k] * self.inv_f[k - r] % self.MOD def comb(self, k, r): if k < r: return 0 return self.f[k] * self.inv_f[k - r] * self.inv_f[r] % self.MOD def combination(k, r, MOD): if k < r: return 0 r = min(r, k - r) numer, denom = 1, 1 for l in range(r): numer *= k - l numer %= MOD denom *= l + 1 denom %= MOD return numer * pow(denom, MOD - 2, MOD) % MOD input = sys.stdin.buffer.readline n, MOD = map(int, input().split()) comb = Combination(10**5, MOD) dp = [([0] * (n + 1)) for i in range(n + 1)] dp[0][0] = 1 for i in range(1, n + 1): dp[i][i] = pow(2, i - 1, MOD) pows = [pow(2, i, MOD) for i in range(n + 10)] for i in range(n + 1): for times in range(max(i // 2, 1), i + 1): for length in range(1, times + 1): nokori = times - length ptn = pows[length - 1] ptn *= comb.fact(times) * comb.inv_fact(nokori) * comb.inv_fact(length) if i - length == 1: continue dp[i][times] += ptn * dp[i - length - 1][nokori] dp[i][times] %= MOD print(sum(dp[-1]) % MOD)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
fac = [1] * 500 finv = [1] * 500 p2 = [1] * 500 n, MOD = map(int, input().split()) for i in range(len(fac) - 1): fac[i + 1] = fac[i] * (i + 1) % MOD finv[i + 1] = pow(fac[i + 1], MOD - 2, MOD) p2[i + 1] = p2[i] * 2 % MOD ans = 0 dp = [([0] * (n // 2 + 2)) for _ in range(n + 2)] dp[0][0] = 1 for i in range(n): for j in range(i + 2, n + 2): for k in range(n // 2 + 1): dp[j][k + 1] += dp[i][k] % MOD * finv[j - i - 1] * p2[j - i - 2] ans = 0 for i in range(1, n // 2 + 2): ans += dp[n + 1][i] * fac[n - i + 1] print(ans % MOD)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
inp = input().split() totNums, mod = int(inp[0]), int(inp[1]) def Exp(b, exp): if exp == 0: return 1 temp = Exp(b, exp >> 1) ** 2 if exp % 2 == 1: temp *= b return temp % mod n = 410 fact, inv = [(0) for i in range(n)], [(0) for i in range(n)] fact[0] = inv[0] = 1 for i in range(1, totNums + 1): fact[i] = fact[i - 1] * i % mod inv[i] = Exp(fact[i], mod - 2) dp, choose = [[(0) for i in range(n)] for j in range(n)], [ [(0) for i in range(n)] for j in range(n) ] for i in range(0, totNums + 1): for j in range(0, i + 1): choose[i][j] = fact[i] * inv[j] * inv[i - j] % mod pow2 = [Exp(2, i) for i in range(n)] dp[0][0] = 1 for i in range(totNums): for j in range(i + 1): for k in range(1, totNums - i + 1): dp[i + k + 1][j + k] += dp[i][j] * pow2[k - 1] * choose[j + k][k] dp[i + k + 1][j + k] %= mod ans = 0 for i in range(0, totNums + 1): ans = (ans + dp[totNums + 1][i]) % mod print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
N, MOD = map(int, input().split()) dp = [([0] * (N + 2)) for i in range(N + 2)] dp[0][0] = 1 limit = 1000 frac = [1] * limit for i in range(2, limit): frac[i] = i * frac[i - 1] % MOD fraci = [None] * limit fraci[-1] = pow(frac[-1], MOD - 2, MOD) for i in range(-2, -limit - 1, -1): fraci[i] = fraci[i + 1] * (limit + i + 1) % MOD bb = [1, 2] for i in range(1000): bb.append(bb[-1] * 2 % MOD) for ln in range(N + 1): for cnt in range(ln // 2, ln + 1): for k in range(1, N - ln + 1): cmb = frac[cnt + k] * (fraci[cnt] * fraci[k] % MOD) % MOD dp[ln + k + 1][cnt + k] += dp[ln][cnt] * (bb[k - 1] * cmb % MOD) % MOD dp[ln + k + 1][cnt + k] %= MOD R = 0 for x in dp[N + 1][: N + 1]: R = (R + x) % MOD print(R)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
N, M = map(int, input().split()) fac = [1] + [0] * N for i in range(1, N + 1): fac[i] = fac[i - 1] * i % M fac_inv = [0] * N + [pow(fac[N], M - 2, M)] for i in range(N, 0, -1): fac_inv[i - 1] = fac_inv[i] * i % M pow2 = [1] + [0] * N for i in range(N): pow2[i + 1] = pow2[i] * 2 % M DP = [([0] * N) for _ in range(N + 2)] DP[0][0] = 1 for i in range(N): for j in range(N): DP[i][j] %= M if DP[i][j]: for k in range(i + 2, N + 2): DP[k][j + 1] += DP[i][j] * fac_inv[k - i - 1] % M * pow2[k - i - 2] % M ans = 0 for j in range(N): DP[N + 1][j] %= M if DP[N + 1][j]: ans += DP[N + 1][j] * fac[N - j + 1] % M print(ans % M)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
import sys input = sys.stdin.readline n, m = map(int, input().split()) MOD = m MAX_N = 10**3 fac = [1] + [0] * MAX_N for i in range(1, MAX_N + 1): fac[i] = fac[i - 1] * i % MOD fac_inv = [1] + [0] * MAX_N fac_inv[MAX_N] = pow(fac[MAX_N], MOD - 2, MOD) for i in range(MAX_N, 1, -1): fac_inv[i - 1] = fac_inv[i] * i % MOD def mod_nCr(n, r): if n < r or n < 0 or r < 0: return 0 tmp = fac_inv[n - r] * fac_inv[r] % MOD return tmp * fac[n] % MOD pow2 = [0] * (n + 1) pow2[0] = 1 for i in range(1, n + 1): pow2[i] = pow2[i - 1] * 2 % MOD table = [([0] * 500) for _ in range(500)] for i in range(500): for j in range(i + 1): table[i][j] = mod_nCr(i, j) dp = [([0] * (n + 1)) for _ in range(n)] for i in range(n): dp[i][i + 1] = pow2[i] for i in range(n - 1): for j in range(i // 2 + 1, n - 1): if dp[i][j] == 0: continue dp[i][j] %= MOD for k in range(1, n - j): if i + k + 1 >= n: break dp[i + k + 1][j + k] += dp[i][j] * pow2[k - 1] * table[k + j][k] ans = sum(dp[-1]) % MOD print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
n, M = map(int, input().split()) combdic = {} def fastfrac(a, b, M): numb = pow(b, M - 2, M) return a % M * (numb % M) % M def comb(p, q): if p == 1: return q if (p, q) in combdic: return combdic[p, q] output = comb(p - 1, q - 1) * q % M output = fastfrac(output, p, M) combdic[p, q] = output return output def getnext(i, j, dic): if 2 * j + 1 > i: return 0 if (i, j) in dic: return dic[i, j] if j == 0: dic[i, j] = (1 << i - 1) % M return dic[i, j] output = 0 for k in range(2, i): if 2 * j - 1 > i - k: break output += ( getnext(i - k, j - 1, dic) * getnext(k - 1, 0, dic) % M * comb(k - 1, i - j) ) output = output % M dic[i, j] = output return output dic = {} ans = 0 dp = [[(0) for j in range(n // 2 + 3)] for i in range(n + 1)] for i in range(1, n + 1): dp[i][0] = (1 << i - 1) % M for j in range(1, n + 1): if 2 * j + 1 > i: break for k in range(2, i): if 2 * j - 1 > i - k: break dp[i][j] += dp[i - k][j - 1] * dp[k - 1][0] % M * comb(k - 1, i - j) % M dp[i][j] = dp[i][j] % M ans = 0 for j in range(n): if 2 * j + 1 > i: break ans += dp[n][j] ans = ans % M print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
N, MOD = map(int, input().split()) dp = [] comps = [0] * (N + 1) ncr = [[1]] for i in range(420): tmp = [1] for j in range(i): tmp.append((ncr[i][j] + ncr[i][j + 1]) % MOD) tmp.append(1) ncr.append(tmp) for i in range(N): curr = list(comps) curr[1] = pow(2, i, MOD) for j in range(i - 1): m = pow(2, i - j - 2) for k in range(N): num = j - k + 2 if num < 0: continue mr = m * ncr[i - j - 1 + num][num] % MOD curr[k + 1] += mr * dp[j][k] curr[k + 1] %= MOD dp.append(curr) print(sum(dp[-1]) % MOD)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
import sys f = sys.stdin def line(): return f.readline().strip().split() def powers(limit): size = limit + 1 p = [1] * size for n in range(1, size): p[n] = 2 * p[n - 1] % M return p def binomials(limit): size = limit + 1 bc = [[(0) for k in range(size)] for n in range(size)] for n in range(size): bc[n][0] = 1 for n in range(1, size): for k in range(1, n + 1): bc[n][k] = bc[n - 1][k - 1] + bc[n - 1][k] bc[n][k] %= M return bc def solve(): size = N + 1 dp = [[(0) for _ in range(size)] for _ in range(size)] dp[1][0] = 1 for i in range(2, size): for k in range(1, i): for j in range(1, k): dp[i][j] += BC[i - j][k - j] * dp[k - 1][j - 1] * POW[i - k - 1] dp[i][j] %= M dp[i][0] = POW[i - 1] res = 0 for j in range(0, N - 1): res = (res + dp[N][j]) % M return str(res) T = 1 for test in range(1, T + 1): N, M = map(int, line()) BC = binomials(N) POW = powers(N) print(solve()) f.close()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,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 LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI1(rows_number): return [LI1() 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 n, md = LI() def nHr(hn, hr): return nCr(hn + hr - 1, hr - 1) def nPr(com_n, com_r): if com_r < 0: return 0 if com_n < com_r: return 0 return fac[com_n] * ifac[com_n - com_r] % md def nCr(com_n, com_r): if com_r < 0: return 0 if com_n < com_r: return 0 return fac[com_n] * ifac[com_r] % md * ifac[com_n - com_r] % md n_max = 405 fac = [1] for i in range(1, n_max + 1): fac.append(fac[-1] * i % md) ifac = [1] * (n_max + 1) ifac[n_max] = pow(fac[n_max], md - 2, md) for i in range(n_max - 1, 1, -1): ifac[i] = ifac[i + 1] * (i + 1) % md pw = [1] for i in range(400): pw.append(pw[-1] * 2 % md) dp = [([0] * (n // 2 + 2)) for _ in range(n + 2)] dp[0][0] = 1 for i in range(1, n + 2): for j in range(1, n // 2 + 2): v = 0 for k in range(i - 2, -1, -1): v += dp[k][j - 1] * pw[i - k - 2] * nCr(i - j, i - k - 1) % md dp[i][j] = v % md ans = sum(dp[-1]) % md print(ans)
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 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 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 VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
import sys def input(): return sys.stdin.readline().strip() n, mod = map(int, input().split()) le = 405 def pow(x, y): ans = 1 while y > 0: if y % 2 == 1: ans = ans * x % mod x = x**2 % mod y //= 2 return ans def inv(x): return pow(x, mod - 2) M = [1] mul = 1 for i in range(1, le): mul = mul * i % mod M.append(mul) MI = [0] * (le - 1) + [inv(M[le - 1])] for i in range(le - 2, -1, -1): MI[i] = MI[i + 1] * (i + 1) % mod def C(x, y): if y < 0 or y > x: return 0 elif x > le: y = min(y, x - y) ans = 1 for i in range(x, x - y, -1): ans = ans * i % mod return ans * MI[y] % mod else: ans = M[x] ans = ans * MI[y] % mod return ans * MI[x - y] % mod M2 = [1] for i in range(n + 5): M2.append(M2[-1] * 2 % mod) CO = [([0] * (n + 5)) for i in range(n + 5)] for i in range(n + 5): for j in range(n + 5): CO[i][j] = C(i, j) D = [([0] * (n + 1)) for i in range(n + 2)] D[0][0] = 1 for i in range(n + 2): for j in range(i // 2, min(n + 1, i + 1)): for k in range(1, min(n + 1, n - i + 1, n - j + 1)): ind0 = i + k + 1 ind1 = j + k if ind0 <= n + 1 and ind1 <= n: D[ind0][ind1] += D[i][j] * CO[j + k][k] * M2[k - 1] D[ind0][ind1] %= mod print(sum(D[-1]) % mod)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
N, mod = map(int, input().split()) two = [1] * (N + 1) fact = [1] * (N + 1) inv = [1] * (N + 1) for i in range(1, N + 1): two[i] = two[i - 1] * 2 % mod for i in range(2, N + 1): fact[i] = fact[i - 1] * i % mod inv[N] = pow(fact[N], mod - 2, mod) for i in range(N, 0, -1): inv[i - 1] = inv[i] * i % mod dp = [([0] * (N + 2)) for _ in range(N + 2)] dp[0][0] = 1 for i in range(N): for j in range(i + 1): for k in range(1, N + 1): if i + k > N: break dp[i + k + 1][j + 1] += dp[i][j] * two[k - 1] * inv[k] % mod dp[i + k + 1][j + 1] %= mod ans = 0 for j in range(1, N + 1): ans += dp[N + 1][j] * fact[N - j + 1] % mod ans %= mod print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR 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 BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
def getc(): f = [([0] * 500) for i in range(500)] for i in range(500): f[i][0] = 1 f[1][0] = 1 f[1][1] = 1 for i in range(2, 411): for j in range(1, i + 1): f[i][j] = (f[i - 1][j - 1] + f[i - 1][j]) % mod return f n, mod = map(int, input().split()) f = [([0] * 500) for i in range(500)] c = getc() mi_2 = [0] * 500 mi_2[0] = 1 for i in range(1, 500): mi_2[i] = mi_2[i - 1] * 2 % mod for i in range(1, n + 1): for j in range(0, i // 2 + 1): if j == 0: f[i][j] = mi_2[i - 1] else: for k in range(2, i): f[i][j] = ( f[i][j] + mi_2[k - 2] * f[i - k][j - 1] % mod * c[i - j][k - 1] % mod ) % mod ans = 0 for i in range(0, n + 1): ans = (ans + f[n][i]) % mod print(ans)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
N = 405 n, m = map(int, input().split()) dp = [([0] * N) for _ in range(N)] c = [([1] * N) for _ in range(N)] p = [0] * N p[0] = 1 for i in range(1, N): p[i] = p[i - 1] * 2 % m for i in range(1, N): for j in range(1, i): c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % m dp[0][0] = 1 for i in range(2, n + 2): for x in range(1, (n - 1) // 2 + 2): for k in range(1, i): dp[i][x] = ( dp[i][x] + dp[i - k - 1][x - 1] * p[k - 1] % m * c[i - x][k] ) % m ans = 0 for i in range(1, (n - 1) // 2 + 2): ans = (ans + dp[n + 1][i]) % m print(ans)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ computers in a row, all originally off, and Phoenix wants to turn all of them on. He will manually turn on computers one at a time. At any point, if computer $i-1$ and computer $i+1$ are both on, computer $i$ $(2 \le i \le n-1)$ will turn on automatically if it is not already on. Note that Phoenix cannot manually turn on a computer that already turned on automatically. If we only consider the sequence of computers that Phoenix turns on manually, how many ways can he turn on all the computers? Two sequences are distinct if either the set of computers turned on manually is distinct, or the order of computers turned on manually is distinct. Since this number may be large, please print it modulo $M$. -----Input----- The first line contains two integers $n$ and $M$ ($3 \le n \le 400$; $10^8 \le M \le 10^9$) — the number of computers and the modulo. It is guaranteed that $M$ is prime. -----Output----- Print one integer — the number of ways to turn on the computers modulo $M$. -----Examples----- Input 3 100000007 Output 6 Input 4 100000007 Output 20 Input 400 234567899 Output 20914007 -----Note----- In the first example, these are the $6$ orders in which Phoenix can turn on all computers: $[1,3]$. Turn on computer $1$, then $3$. Note that computer $2$ turns on automatically after computer $3$ is turned on manually, but we only consider the sequence of computers that are turned on manually. $[3,1]$. Turn on computer $3$, then $1$. $[1,2,3]$. Turn on computer $1$, $2$, then $3$. $[2,1,3]$ $[2,3,1]$ $[3,2,1]$
n, mod = map(int, input().split()) le = 500 def pow(x, y): ans = 1 while y > 0: if y % 2 == 1: ans = ans * x % mod x = x**2 % mod y //= 2 return ans def inv(x): return pow(x, mod - 2) M = [1] mul = 1 for i in range(1, le): mul = mul * i % mod M.append(mul) L0 = n // 2 + 3 L1 = n + 1 D = [[(0) for i in range(L1)] for j in range(L0)] ND = [[(0) for i in range(L1)] for j in range(L0)] INVS = [0] + [inv(i) for i in range(1, n + 1)] D[1][1] = 1 for z in range(2, n + 1): l0 = z // 2 + 3 l1 = z + 1 for i in range(l0): for j in range(l1): ND[i][j] = 0 for i in range(l0): if i >= 1: ND[i][1] += D[i - 1][0] * (z - (i - 1)) ND[i][1] %= mod for i in range(l0): for j in range(1, n + 1): ND[i][0] += D[i][j] ND[i][0] %= mod for i in range(l0): for j in range(l1): if j >= 2: p = D[i][j - 1] p *= z - (i - 1) p %= mod p *= INVS[j] * 2 p %= mod ND[i][j] += p ND[i][j] %= mod for i in range(l0): for j in range(l1): D[i][j] = ND[i][j] ans = 0 for i in range(L0): for j in range(1, L1): ans += D[i][j] ans %= mod print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
def generate_table(max_n, max_fn): a = [] a.append([[(0) for __ in range(_ + 1)] for _ in range(max_fn + 1)]) for j in range(max_fn + 1): for k in range(j + 1): if j + k <= max_fn: a[0][j][k] = 1 i = 1 while 2**i + 1 <= max_n: a.append([[(0) for __ in range(_ + 1)] for _ in range(max_fn + 1)]) for j in range(max_fn + 1): for k in range(j + 1): a[i][j][k] = sum( [ ( a[i - 1][max(j, c)][min(j, c)] * a[i - 1][max(k, c)][min(k, c)] % 12345787 ) for c in range(max_fn + 1) ] ) i += 1 return a def linear(s1, s2, indices, table, max_fn): index = indices[-1] if len(indices) == 1: return table[index][max(s1, s2)][min(s1, s2)] else: return sum( [ ( table[index][max(s1, s)][min(s1, s)] * linear(s, s2, indices[:-1], table, max_fn) ) for s in range(0, max_fn + 1) ] ) def circular_limited_sums(max_n, max_fn): table = generate_table(max_n + 1, max_fn) lengths = [(2**i + 1) for i in range(8)] lengths = lengths[::-1] m = max_n indices = [] for index, length in enumerate(lengths): while length - 1 <= m: indices.append(7 - index) m -= length - 1 if len(indices) == 1: return sum([table[indices[0]][s][s] for s in range(max_fn + 1)]) % 12345787 if len(indices) == 2: return ( sum( [ ( table[indices[0]][max(s, t)][min(s, t)] * table[indices[1]][max(s, t)][min(s, t)] ) for s in range(max_fn + 1) for t in range(max_fn + 1) ] ) % 12345787 ) if len(indices) >= 3: return ( sum( [ ( table[indices[0]][max(s, s1)][min(s, s1)] * table[indices[1]][max(s, s2)][min(s, s2)] * linear(s1, s2, indices[2:], table, max_fn) ) for s in range(max_fn + 1) for s1 in range(max_fn + 1) for s2 in range(max_fn + 1) ] ) % 12345787 )
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
def circular_limited_sums(max_n, max_fn): p = 12345787 m = max_fn + 1 v = [[int(f1 == fn) for fn in range(m)] for f1 in range(m)] for n in range(max_n - 1): v = [[(sum(w[: m - fn]) % p) for fn in range(m)] for w in v] return sum(sum(w[: m - f1]) % p for f1, w in enumerate(v)) % p
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
def circular_limited_sums(max_n, max_fn): FL = [([0] * (max_fn + 1)) for i in range(max_fn + 1)] for i in range(max_fn + 1): for j in range(max_fn + 1): if i == j: FL[i][j] = 1 for x in range(max_n - 1): nextFL = [([0] * (max_fn + 1)) for i in range(max_fn + 1)] for i in range(max_fn + 1): for j in range(max_fn + 1): for k in range(max_fn + 1): if j + k <= max_fn: nextFL[i][k] += FL[i][j] FL = nextFL tot = 0 for i in range(max_fn + 1): for j in range(max_fn + 1): if i + j <= max_fn: tot += FL[i][j] return tot % 12345787
FUNC_DEF 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
MODULUS = 12345787 def circular_limited_sums(max_n, max_fn): total = 0 for initial in range(max_fn + 1): counts = {v: (0) for v in range(max_fn + 1)} counts[initial] = 1 for n in range(1, max_n): new_counts = {} for v in range(max_fn + 1): new_counts[v] = ( sum(counts[v_prev] for v_prev in range(max_fn - v + 1)) % MODULUS ) counts = new_counts total += sum(counts[v] for v in range(max_fn - initial + 1)) % MODULUS return total % MODULUS
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR VAR
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
class Memoize: def __init__(self, fn): self.fn = fn self.memo = {} def __call__(self, *args): if args not in self.memo: self.memo[args] = self.fn(*args) return self.memo[args] @Memoize def linear_limited_sums(X, Y, head, tail): if X == 1: return 1 if head == tail else 0 if X == 2: return 1 if head + tail <= Y else 0 return ( sum(linear_limited_sums(X - 1, Y, head, T) for T in range(Y - tail + 1)) % 12345787 ) def circular_limited_sums(X, Y): return ( sum( linear_limited_sums(X, Y, head, tail) for head in range(Y + 1) for tail in range(Y - head + 1) ) % 12345787 )
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
mod = 12345787 mat = [ ([1, 1], [0, 1, 3]), ([2, 1, -1], [0, 2, 6, 11]), ([2, 3, -1, -1], [0, 2, 10, 23, 70]), ([3, 3, -4, -1, 1], [0, 3, 15, 42, 155, 533]), ([3, 6, -4, -5, 1, 1], [0, 3, 21, 69, 301, 1223, 5103]), ] for i in range(100): [m.append(sum(k * m[-1 - i] for i, k in enumerate(c)) % mod) for c, m in mat] def circular_limited_sums(max_n, max_fn): return mat[max_fn - 1][1][max_n]
ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR NUMBER NUMBER VAR
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
dp = {} def circular_limited_sums(max_n, max_fn): if max_n == 1: return max_fn // 2 + 1 else: s = 0 for i in range(max_fn + 1): s += solve(max_n - 1, max_fn, i, i) s %= 12345787 return s def solve(n, m, first, last): if (n, m, first, last) in dp: return dp[n, m, first, last] if n == 1: dp[n, m, first, last] = m - max(first, last) + 1 return dp[n, m, first, last] s = 0 for i in range(m - last + 1): if first is None: s += solve(n - 1, m, i, i) else: s += solve(n - 1, m, first, i) s %= 12345787 dp[n, m, first, last] = s return dp[n, m, first, last]
ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR
# Problem Description Let's imagine a function `F(n)`, which is defined over the integers in the range of `1 <= n <= max_n`, and `0 <= F(n) <= max_fn` for every `n`. There are `(1 + max_fn) ** max_n` possible definitions of `F` in total. Out of those definitions, how many `F`s satisfy the following equations? Since your answer will be very large, please give your answer **modulo 12345787**. * `F(n) + F(n + 1) <= max_fn` for `1 <= n < max_n` * `F(max_n) + F(1) <= max_fn` # Constraints `1 <= max_n <= 100` `1 <= max_fn <= 5` The inputs will be always valid integers. # Examples ```python # F(1) + F(1) <= 1, F(1) = 0 circular_limited_sums(1, 1) == 1 # F = (0, 0), (0, 1), (1, 0) circular_limited_sums(2, 1) == 3 # F = (0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0) circular_limited_sums(3, 1) == 4 # F = (0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), # (0, 1, 0, 1), (1, 0, 0, 0), (1, 0, 1, 0) circular_limited_sums(4, 1) == 7 # F = (0), (1) circular_limited_sums(1, 2) == 2 # F = (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0) circular_limited_sums(2, 2) == 6 # F = (0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), # (0, 2, 0), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0) circular_limited_sums(3, 2) == 11 circular_limited_sums(4, 2) == 26 ``` # Acknowledgement This problem was designed as a hybrid of [Project Euler #209: Circular Logic](https://projecteuler.net/problem=209) and [Project Euler #164: Numbers for which no three consecutive digits have a sum greater than a given value](https://projecteuler.net/problem=164). If you enjoyed this Kata, please also have a look at [my other Katas](https://www.codewars.com/users/Bubbler/authored)!
def circular_limited_sums(max_n, max_fn): if max_n == 1: return sum([(1) for i in range(max_fn + 1) if i + i <= max_fn]) % 12345787 nexts = [ list(filter(lambda x: i + x <= max_fn, range(max_fn + 1))) for i in range(max_fn + 1) ] result = 0 for ii in range(max_fn + 1): v = [[(1 if e in nexts[ii] else 0) for e in range(max_fn + 1)]] for _ in range(max_n - 2): v.append([(0) for i in range(max_fn + 1)]) for i, e in enumerate(v[-2]): for j in nexts[i]: v[-1][j] += e result += sum([e for i, e in enumerate(v[-1]) if i + ii <= max_fn]) return result % 12345787
FUNC_DEF IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): N = int(input()) arr = list(map(int, input().split())) (HardLeftMin, HardLeftMax, SoftLeftMax, SoftLeftMin, HardRightMax, HardRightMin) = ( [0] * N, [0] * N, [0] * N, [0] * N, [0] * N, [0] * N, ) for i in range(N): HardLeftMin[i] = min((HardLeftMin[i - 1] if i - 1 >= 0 else 0) + arr[i], arr[i]) HardLeftMax[i] = max((HardLeftMax[i - 1] if i - 1 >= 0 else 0) + arr[i], arr[i]) SoftLeftMax[i] = max( HardLeftMax[i], SoftLeftMax[i - 1] if i - 1 >= 0 else HardLeftMax[i] ) SoftLeftMin[i] = min( HardLeftMin[i], SoftLeftMin[i - 1] if i - 1 >= 0 else HardLeftMin[i] ) for i in range(N - 1, -1, -1): HardRightMin[i] = min( (HardRightMin[i + 1] if i + 1 < N else 0) + arr[i], arr[i] ) HardRightMax[i] = max( (HardRightMax[i + 1] if i + 1 < N else 0) + arr[i], arr[i] ) ans = 0 for i in range(N): if i != N - 1: ans = max(ans, SoftLeftMax[i] - HardRightMin[i + 1]) if i != 0: ans = max(ans, HardRightMax[i] - SoftLeftMin[i - 1]) 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 VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER 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 VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) for rep in range(t): n = int(input()) d = list(map(int, input().split())) lmax = [0] * n lmax[0] = d[0] rmax = [0] * n rmax[n - 1] = d[n - 1] lmin = [0] * n lmin[0] = d[0] rmin = [0] * n rmin[n - 1] = d[n - 1] for i in range(1, n): lmin[i] = min(lmin[i - 1] + d[i], d[i]) lmax[i] = max(lmax[i - 1] + d[i], d[i]) for i in range(n - 2, -1, -1): rmin[i] = min(rmin[i + 1] + d[i], d[i]) rmax[i] = max(rmax[i + 1] + d[i], d[i]) maxi1 = 0 maxi2 = 0 for i in range(n - 1): maxi1 = max(maxi1, abs(lmin[i] - rmax[i + 1])) maxi2 = max(maxi2, abs(lmax[i] - rmin[i + 1])) print(max(maxi1, maxi2))
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 LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) i = [0] * n i[0] = a[0] j = [0] * n j[0] = a[0] k = [0] * n k[-1] = a[-1] l = [0] * n l[-1] = a[-1] for x in range(1, n): i[x] = min(i[x - 1] + a[x], a[x]) j[x] = max(j[x - 1] + a[x], a[x]) k[-1 - x] = min(k[-x] + a[-1 - x], a[-1 - x]) l[-1 - x] = max(l[-x] + a[-1 - x], a[-1 - x]) ans = max(j[0] - k[1], l[-1] - i[-2]) for x in range(1, n - 1): ans = max(ans, j[x] - k[x + 1], l[x] - i[x - 1]) 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
from sys import stdin def fix_one(max1_e, min1_e, max1_s, min1_s): next_best = -(10**9) - 1 for x in range(0, len(max1_e) - 1): next_best = max( abs(max1_e[x] - min1_s[x + 1]), abs(max1_s[x + 1] - min1_e[x]), next_best ) return next_best tc = int(stdin.readline()) for i in range(tc): n = int(stdin.readline()) list1 = list(map(int, stdin.readline().split())) max1_e = [0] * n min1_e = [0] * n max_e = [0] * n min_e = [0] * n max1_e[0] = min1_e[0] = max_e[0] = max_until_now = min_e[0] = min_until_now = list1[ 0 ] for j in range(1, n): max1_e[j] = max(list1[j], max1_e[j - 1] + list1[j]) if max1_e[j] > max_until_now: max_until_now = max1_e[j] max_e[j] = max_until_now min1_e[j] = min(list1[j], min1_e[j - 1] + list1[j]) if min1_e[j] < min_until_now: min_until_now = min1_e[j] min_e[j] = min_until_now max1_s = [0] * n min1_s = [0] * n max_s = [0] * n min_s = [0] * n max1_s[n - 1] = min1_s[n - 1] = max_s[n - 1] = max_until_now = min_s[n - 1] = ( min_until_now ) = list1[n - 1] for k in range(n - 2, -1, -1): max1_s[k] = max(list1[k], max1_s[k + 1] + list1[k]) if max1_s[k] > max_until_now: max_until_now = max1_s[k] max_s[k] = max_until_now min1_s[k] = min(list1[k], min1_s[k + 1] + list1[k]) if min1_s[k] < min_until_now: min_until_now = min1_s[k] min_s[k] = min_until_now print(fix_one(max_e, min_e, max_s, min_s))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR 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 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 ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR 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 ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER 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 BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
T = int(input()) for tc in range(T): N = int(input()) D = list(map(int, input().split())) Omaxlist = [] Ominlist = [] Rmaxlist = [] Rminlist = [] Omax = D[0] Omin = D[0] Rmax = D[-1] Rmin = D[-1] Olocalmax = 0 Olocalmin = 0 Rlocalmax = 0 Rlocalmin = 0 for i in range(N): Olocalmax += D[i] Olocalmin += D[i] Rlocalmax += D[-i - 1] Rlocalmin += D[-i - 1] if Olocalmax > Omax: Omax = Olocalmax if Olocalmin < Omin: Omin = Olocalmin if Rlocalmax > Rmax: Rmax = Rlocalmax if Rlocalmin < Rmin: Rmin = Rlocalmin Omaxlist.append(Omax) Ominlist.append(Omin) Rmaxlist.append(Rmax) Rminlist.append(Rmin) if Olocalmax < 0: Olocalmax = 0 if Olocalmin > 0: Olocalmin = 0 if Rlocalmax < 0: Rlocalmax = 0 if Rlocalmin > 0: Rlocalmin = 0 dif = 0 for j in range(N - 1): dif = max( abs(Omaxlist[j] - Rminlist[N - 2 - j]), abs(Ominlist[j] - Rmaxlist[N - 2 - j]), dif, ) print(dif)
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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): num_ingredients = int(input()) delish_values = list(map(int, input().split())) prefix_max = [0] * num_ingredients prefix_max[0] = delish_values[0] prefix_min = [0] * num_ingredients prefix_min[0] = delish_values[0] suffix_max = [0] * num_ingredients suffix_max[-1] = delish_values[-1] suffix_min = [0] * num_ingredients suffix_min[-1] = delish_values[-1] for i in range(1, num_ingredients): prefix_max[i] = max(prefix_max[i - 1] + delish_values[i], delish_values[i]) prefix_min[i] = min(prefix_min[i - 1] + delish_values[i], delish_values[i]) suffix_max[num_ingredients - 1 - i] = max( suffix_max[num_ingredients - i] + delish_values[num_ingredients - 1 - i], delish_values[num_ingredients - 1 - i], ) suffix_min[num_ingredients - 1 - i] = min( suffix_min[num_ingredients - i] + delish_values[num_ingredients - 1 - i], delish_values[num_ingredients - 1 - i], ) for i in range(1, num_ingredients): prefix_max[i] = max(prefix_max[i], prefix_max[i - 1]) prefix_min[i] = min(prefix_min[i], prefix_min[i - 1]) suffix_max[num_ingredients - 1 - i] = max( suffix_max[num_ingredients - 1 - i], suffix_max[num_ingredients - i] ) suffix_min[num_ingredients - 1 - i] = min( suffix_min[num_ingredients - 1 - i], suffix_min[num_ingredients - i] ) max_delish = max( prefix_max[0] - suffix_min[1], suffix_max[num_ingredients - 1] - prefix_min[num_ingredients - 2], ) for i in range(1, num_ingredients - 1): max_delish = max( max_delish, abs(prefix_max[i] - suffix_min[i + 1]), abs(suffix_max[i] - prefix_min[i - 1]), ) print(max_delish)
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER 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 ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def cal(): def max_delish2(idx): nonlocal maxdp2 if idx == n - 1: maxdp2[idx] = d[idx] return maxdp2[idx] temp = max_delish2(idx + 1) if temp + d[idx] > d[idx]: maxdp2[idx] = temp + d[idx] else: maxdp2[idx] = d[idx] return maxdp2[idx] def max_delish1(idx): nonlocal maxdp1 if idx == 0: maxdp1[idx] = d[idx] return maxdp1[idx] temp = max_delish1(idx - 1) if temp + d[idx] > d[idx]: maxdp1[idx] = temp + d[idx] else: maxdp1[idx] = d[idx] return maxdp1[idx] def min_delish2(idx): nonlocal mindp2 if idx == n - 1: mindp2[idx] = d[idx] return mindp2[idx] temp = min_delish2(idx + 1) if temp + d[idx] < d[idx]: mindp2[idx] = temp + d[idx] else: mindp2[idx] = d[idx] return mindp2[idx] def min_delish1(idx): nonlocal mindp1 if idx == 0: mindp1[idx] = d[idx] return mindp1[idx] temp = min_delish1(idx - 1) if temp + d[idx] < d[idx]: mindp1[idx] = temp + d[idx] else: mindp1[idx] = d[idx] return mindp1[idx] maxdp1, maxdp1[0] = [0] * n, d[0] maxdp2, maxdp2[-1] = [0] * n, d[-1] mindp1, mindp1[0] = [0] * n, d[0] mindp2, mindp2[-1] = [0] * n, d[-1] for i in range(n - 2, -1, -1): if maxdp2[i + 1] + d[i] > d[i]: maxdp2[i] = maxdp2[i + 1] + d[i] else: maxdp2[i] = d[i] if mindp2[i + 1] + d[i] < d[i]: mindp2[i] = mindp2[i + 1] + d[i] else: mindp2[i] = d[i] for i in range(1, n): if maxdp1[i - 1] + d[i] > d[i]: maxdp1[i] = maxdp1[i - 1] + d[i] else: maxdp1[i] = d[i] if mindp1[i - 1] + d[i] < d[i]: mindp1[i] = mindp1[i - 1] + d[i] else: mindp1[i] = d[i] ans = 0 for i in range(1, n): ans = max(ans, abs(maxdp2[i] - mindp1[i - 1]), abs(mindp2[i] - maxdp1[i - 1])) return ans for _ in range(int(input())): n = int(input()) d = list(map(int, input().split())) print(cal())
FUNC_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def mp(): return map(int, input().split(" ")) def lst(): return list(mp()) def subsm(el, e): ms = el[0] me = el[0] nw = [el[0] * e] for i in range(1, len(el)): ms = max(el[i], ms + el[i]) me = max(me, ms) nw.append(me * e) return nw for _ in range(int(input())): n = int(input()) ar = lst() nar = [(-1 * ar[i]) for i in range(n)] pmxf = subsm(ar, 1) pmxl = subsm(ar[::-1], 1) pmnf = subsm(nar, -1) pmnl = subsm(nar[::-1], -1) pmxl = pmxl[::-1] pmnl = pmnl[::-1] print( max( [ max(abs(pmxf[i] - pmnl[i + 1]), abs(pmnf[i] - pmxl[i + 1])) for i in range(n - 1) ] ) )
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) lmax, lmin, rmax, rmin = [0] * n, [0] * n, [0] * n, [0] * n lmax[0] = l[0] lmin[0] = l[0] rmax[-1] = l[-1] rmin[-1] = l[-1] for i in range(1, len(l)): lmax[i] = max(lmax[i - 1] + l[i], l[i]) lmin[i] = min(lmin[i - 1] + l[i], l[i]) for i in range(n - 2, -1, -1): rmax[i] = max(rmax[i + 1] + l[i], l[i]) rmin[i] = min(rmin[i + 1] + l[i], l[i]) ans = 0 for i in range(n - 1): ans = max(ans, max(abs(rmax[i + 1] - lmin[i]), abs(rmin[i + 1] - lmax[i]))) 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def solve(arr, n): ans = -(10**20) dp = [0] * n dp[0] = arr[0] dp2 = [10**20] * n dp2[n - 1] = arr[n - 1] for i in range(1, n): dp[i] = max(arr[i], dp[i - 1] + arr[i]) for i in range(n - 2, -1, -1): dp2[i] = min(arr[i], dp2[i + 1] + arr[i]) maxpref = [0] * n minsuff = [0] * n maxpref[0] = dp[0] minsuff[n - 1] = dp2[n - 1] for i in range(1, n): maxpref[i] = max(maxpref[i - 1], dp[i]) for i in range(n - 2, -1, -1): minsuff[i] = min(minsuff[i + 1], dp2[i]) for i in range(n - 1): ans = max(ans, abs(maxpref[i] - minsuff[i + 1])) return ans t = int(input()) for _ in range(t): n = int(input()) arr = [int(j) for j in input().split()] print(max(solve(arr, n), solve(arr[::-1], n)))
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP 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 VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
import sys for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) lmax = [(0) for i in range(n)] lmin = [(0) for i in range(n)] rmax = [(0) for i in range(n)] rmin = [(0) for i in range(n)] c1, c2 = l[0], l[0] m1, m2 = l[0], l[0] lmax[0], lmin[0] = l[0], l[0] rmax[-1], rmin[-1] = l[-1], l[-1] for i in range(1, n): c1 += l[i] c2 += l[i] c1 = max(c1, l[i]) c2 = min(c2, l[i]) m1 = max(m1, c1) m2 = min(m2, c2) lmax[i] = m1 lmin[i] = m2 c1, c2 = l[-1], l[-1] m1, m2 = l[-1], l[-1] for i in range(n - 2, -1, -1): c1 += l[i] c2 += l[i] c1 = max(c1, l[i]) c2 = min(c2, l[i]) m1 = max(m1, c1) m2 = min(m2, c2) rmax[i] = m1 rmin[i] = m2 m = -sys.maxsize for i in range(1, n): m = max(m, abs(rmax[i] - lmin[i - 1]), abs(rmin[i] - lmax[i - 1])) print(m)
IMPORT 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 VAR FUNC_CALL VAR 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 ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def findMax(x): c = x[0] for i in range(1, len(x)): if c < 0: c = 0 c += x[i] if c < x[i - 1]: x[i] = x[i - 1] else: x[i] = c for _ in range(int(input())): N = int(input()) d = list(map(int, input().split())) min1 = d.copy() min2 = d.copy() min2.reverse() for i in range(N): min1[i] = -min1[i] min2[i] = -min2[i] max1 = d.copy() max2 = d.copy() max2.reverse() findMax(min1) findMax(min2) findMax(max1) findMax(max2) ans = 0 for i in range(N - 1): d1 = -min1[i] - max2[N - i - 2] if d1 < 0: d1 = -d1 if ans < d1: ans = d1 d1 = max1[i] + min2[N - i - 2] if d1 < 0: d1 = -d1 if ans < d1: ans = d1 print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) for i in range(t): n = int(input()) ar = [int(x) for x in input().split()] pos = [0] * n neg = [0] * n rpos = [0] * n rneg = [0] * n sm = 0 ng = 0 rsm = 0 rng = 0 pos[0] = ar[0] neg[n - 1] = ar[n - 1] rpos[n - 1] = ar[n - 1] rneg[0] = ar[0] for j in range(n): sm += ar[j] if sm <= 0: sm = 0 if j > 0: pos[j] = ar[j] else: pos[j] = sm rng += ar[j] if rng >= 0: rng = 0 if j > 0: rneg[j] = ar[j] else: rneg[j] = rng for j in range(n - 1, -1, -1): ng += ar[j] if ng >= 0: ng = 0 if j < n - 1: neg[j] = ar[j] else: neg[j] = ng rsm += ar[j] if rsm <= 0: rsm = 0 if j < n - 1: rpos[j] = ar[j] else: rpos[j] = rsm p = n - 1 mx = 0 mp = 0 for j in range(1, n): pos[j] = max(pos[j - 1], pos[j]) neg[p - j] = min(neg[p - j], neg[p - j + 1]) rpos[p - j] = max(rpos[p - j], rpos[p - j + 1]) rneg[j] = min(rneg[j - 1], rneg[j]) for j in range(n - 1): mx = max(mx, abs(pos[j] - neg[j + 1])) mp = max(mp, abs(rneg[j] - rpos[j + 1])) print(max(mp, mx)) j
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 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP 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 ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
n = int(input()) for _ in range(n): total = int(input()) arr = list(map(int, input().split())) rightmax = [0] * total leftmax = [0] * total rightmin = [0] * total leftmin = [0] * total rightmax[0] = arr[0] leftmax[-1] = arr[-1] rightmin[0] = arr[0] leftmin[-1] = arr[-1] for i in range(1, total): rightmax[i] = max(arr[i], rightmax[i - 1] + arr[i]) rightmin[i] = min(arr[i], rightmin[i - 1] + arr[i]) for i in range(total - 2, -1, -1): leftmax[i] = max(arr[i], leftmax[i + 1] + arr[i]) leftmin[i] = min(arr[i], leftmin[i + 1] + arr[i]) max1 = [] i = 0 while i + 1 < total: max1.append(abs(rightmax[i] - leftmin[i + 1])) max1.append(abs(leftmax[i + 1] - rightmin[i])) i += 1 print(max(max1))
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 LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP 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 VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def kadane(a): b = [a[0]] b1 = [a[0]] cs = a[0] cs1 = a[0] for i in range(1, len(a)): cs = max(a[i], cs + a[i]) cs1 = min(a[i], cs1 + a[i]) b.append(max(cs, b[-1])) b1.append(min(cs1, b1[-1])) return b, b1 def kadane1(a): b, b1 = [0] * len(a), [0] * len(a) b[-1] = b1[-1] = a[-1] cs = cs1 = a[-1] for i in range(len(a) - 2, -1, -1): cs = min(a[i], cs + a[i]) cs1 = max(a[i], cs1 + a[i]) b[i] = min(cs, b[i + 1]) b1[i] = max(cs1, b[i + 1]) return b, b1 for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] x, y = kadane(a) z, w = kadane1(a) mx = 0 for i in range(n - 1): mx = max(mx, abs(x[i] - z[i + 1]), abs(y[i] - w[i + 1])) print(mx)
FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) delish = list(map(int, input().split())) r_max, r_min = [0] * n, [0] * n l_max, l_min = [0] * n, [0] * n all_max, all_min, cur_max, cur_min = (-10000000000, 10000000000, 0, 10000000000) all_maxb, all_minb, cur_maxb, cur_minb = (-10000000000, 10000000000, 0, 10000000000) for i in range(n): cur_max += delish[i] all_max = max(all_max, cur_max) if cur_max < 0: cur_max = 0 r_max[i] = all_max if cur_min > 0: cur_min = delish[i] else: cur_min += delish[i] all_min = min(cur_min, all_min) r_min[i] = all_min cur_maxb += delish[n - i - 1] all_maxb = max(all_maxb, cur_maxb) if cur_maxb < 0: cur_maxb = 0 l_max[n - i - 1] = all_maxb if cur_minb > 0: cur_minb = delish[n - i - 1] else: cur_minb += delish[n - i - 1] all_minb = min(cur_minb, all_minb) l_min[n - i - 1] = all_minb print( max( max(abs(r_max[j] - l_min[j + 1]), abs(r_min[j] - l_max[j + 1])) for j in range(n - 1) ) )
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 BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def solve(): n = int(input()) arr = list(map(int, input().split())) hardLeftMin = [] for i in range(n): if i == 0: hardLeftMin.append(arr[i]) else: hardLeftMin.append(min(hardLeftMin[-1] + arr[i], arr[i])) hardLeftMax = [] for i in range(n): if i == 0: hardLeftMax.append(arr[i]) else: hardLeftMax.append(max(hardLeftMax[-1] + arr[i], arr[i])) softLeftMin = [] for i in range(n): if i == 0: softLeftMin.append(hardLeftMin[i]) else: softLeftMin.append(min(softLeftMin[-1], hardLeftMin[i])) softLeftMax = [] for i in range(n): if i == 0: softLeftMax.append(hardLeftMax[i]) else: softLeftMax.append(max(softLeftMax[-1], hardLeftMax[i])) hardRightMin = [] for i in range(n - 1, -1, -1): if i == n - 1: hardRightMin.append(arr[i]) else: hardRightMin.append(min(hardRightMin[-1] + arr[i], arr[i])) hardRightMin = list(reversed(hardRightMin)) hardRightMax = [] for i in range(n - 1, -1, -1): if i == n - 1: hardRightMax.append(arr[i]) else: hardRightMax.append(max(hardRightMax[-1] + arr[i], arr[i])) hardRightMax = list(reversed(hardRightMax)) ans = 0 for i in range(n - 1): ans = max(ans, softLeftMax[i] - hardRightMin[i + 1]) ans = max(ans, hardRightMax[i + 1] - softLeftMin[i]) print(ans) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def solve(a, n): prefix_max = [0] * n prefix_min = [0] * n suffix_max = [0] * n suffix_min = [0] * n for i in range(n): if i == 0: prefix_max[i] = a[i] prefix_min[i] = a[i] suffix_max[n - i - 1] = a[n - i - 1] suffix_min[n - i - 1] = a[n - i - 1] else: prefix_max[i] = max(prefix_max[i - 1], 0) + a[i] prefix_min[i] = min(prefix_min[i - 1], 0) + a[i] suffix_max[n - i - 1] = max(suffix_max[n - i], 0) + a[n - i - 1] suffix_min[n - i - 1] = min(suffix_min[n - i], 0) + a[n - i - 1] ans = 0 for i in range(n - 1): ans = max( ans, max( abs(prefix_max[i] - suffix_min[i + 1]), abs(prefix_min[i] - suffix_max[i + 1]), ), ) return ans for _ in range(int(input())): n = int(input()) a = tuple(map(int, input().split())) result = solve(a, n) print(result)
FUNC_DEF 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 IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) min_left = [0] * n max_left = [0] * n min_right = [0] * n max_right = [0] * n min_left[0] = max_left[0] = l[0] min_right[n - 1] = max_right[n - 1] = l[n - 1] for i in range(1, n): min_left[i] = min(l[i], l[i] + min_left[i - 1]) max_left[i] = max(l[i], l[i] + max_left[i - 1]) for i in range(n - 2, -1, -1): min_right[i] = min(l[i], l[i] + min_right[i + 1]) max_right[i] = max(l[i], l[i] + max_right[i + 1]) ans = 0 for i in range(n - 1): ans = max(ans, abs(min_left[i] - max_right[i + 1])) ans = max(ans, abs(max_left[i] - min_right[i + 1])) 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 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 ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) dp1 = [0] * n dp2 = [0] * n dp3 = [0] * n dp4 = [0] * n cs = a[0] gs = a[0] dp1[0] = cs for i in range(1, n): cs = max(a[i], cs + a[i]) gs = max(cs, gs) dp1[i] = gs cs = gs = a[-1] dp2[-1] = cs for i in range(n - 2, -1, -1): cs = max(a[i], cs + a[i]) gs = max(cs, gs) dp2[i] = gs cs = gs = a[0] dp3[0] = cs for i in range(1, n): cs = min(a[i], cs + a[i]) gs = min(cs, gs) dp3[i] = gs cs = gs = a[-1] dp4[-1] = cs for i in range(n - 2, -1, -1): cs = min(a[i], cs + a[i]) gs = min(cs, gs) dp4[i] = gs mx = 0 for i in range(n - 1): mx = max(mx, abs(dp1[i] - dp4[i + 1]), abs(dp2[i + 1] - dp3[i])) print(mx)
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 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 ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def cal(): maxdp1, maxdp1[0] = [0] * n, d[0] maxdp2, maxdp2[-1] = [0] * n, d[-1] mindp1, mindp1[0] = [0] * n, d[0] mindp2, mindp2[-1] = [0] * n, d[-1] for i in range(n - 2, -1, -1): if maxdp2[i + 1] + d[i] > d[i]: maxdp2[i] = maxdp2[i + 1] + d[i] else: maxdp2[i] = d[i] if mindp2[i + 1] + d[i] < d[i]: mindp2[i] = mindp2[i + 1] + d[i] else: mindp2[i] = d[i] for i in range(1, n): if maxdp1[i - 1] + d[i] > d[i]: maxdp1[i] = maxdp1[i - 1] + d[i] else: maxdp1[i] = d[i] if mindp1[i - 1] + d[i] < d[i]: mindp1[i] = mindp1[i - 1] + d[i] else: mindp1[i] = d[i] ans = 0 for i in range(1, n): ans = max(ans, abs(maxdp2[i] - mindp1[i - 1]), abs(mindp2[i] - maxdp1[i - 1])) return ans for _ in range(int(input())): n = int(input()) d = list(map(int, input().split())) print(cal())
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) Ar = A[::-1] dpmax = [A[i] for i in range(n)] dpmin = [Ar[i] for i in range(n)] for i in range(1, n): dpmax[i] = max(A[i], dpmax[i - 1] + A[i]) for i in range(1, n): dpmin[i] = min(Ar[i], dpmin[i - 1] + Ar[i]) dpmin = dpmin[::-1] for i in range(1, n): dpmax[i] = max(dpmax[i - 1], dpmax[i]) ans = dpmax[0] - dpmin[1] for i in range(n - 1): ans = max(ans, dpmax[i] - dpmin[i + 1]) dpmax = [Ar[i] for i in range(n)] dpmin = [A[i] for i in range(n)] for i in range(1, n): dpmax[i] = max(Ar[i], dpmax[i - 1] + Ar[i]) dpmax = dpmax[::-1] for i in range(1, n): dpmin[i] = min(A[i], dpmin[i - 1] + A[i]) for i in range(1, n): dpmin[i] = min(dpmin[i - 1], dpmin[i]) for i in range(n - 1): ans = max(ans, dpmax[i + 1] - dpmin[i]) 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 ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def maxSubArray(nums): n = len(nums) dp = [float("-inf")] * n for i in range(n): dp[i] = max((dp[i - 1] if i > 0 else 0) + nums[i], nums[i]) return dp def minSubArray(nums): n = len(nums) dp = [float("inf")] * n for i in range(n): dp[i] = min((dp[i - 1] if i > 0 else 0) + nums[i], nums[i]) return dp def solve(arr): n = len(arr) max_from_left = maxSubArray(arr) max_from_right = maxSubArray(arr[::-1])[::-1] min_from_left = minSubArray(arr) min_from_right = minSubArray(arr[::-1])[::-1] max_diff = float("-inf") for split in range(n - 1): max_diff = max(max_diff, max_from_left[split] - min_from_right[split + 1]) max_diff = max(max_diff, max_from_right[split + 1] - min_from_left[split]) return max_diff for T in range(int(input())): N = int(input()) arr = list(map(int, input().split())) print(solve(arr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) while t != 0: n = int(input()) a = list(map(int, input().split())) max_left = a[:] min_left = a[:] min_right = a[:] max_right = a[:] max_diff = 0 for i in range(1, n): max_left[i] = max(max_left[i - 1] + a[i], a[i]) min_left[i] = min(min_left[i - 1] + a[i], a[i]) for i in range(n - 2, -1, -1): max_right[i] = max(max_right[i + 1] + a[i], a[i]) min_right[i] = min(min_right[i + 1] + a[i], a[i]) for i in range(n - 1): max_diff = max(max_diff, abs(min_left[i] - max_right[i + 1])) max_diff = max(max_diff, abs(max_left[i] - min_right[i + 1])) print(max_diff) 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 VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for tests in range(int(input())): N = int(input()) D = list(map(int, input().split())) minL = [] minR = [] maxL = [] maxR = [] currentMax = currentMin = 0 for i in range(N - 1): if i == 0: if D[i] < 0: currentMin = D[i] else: currentMax = D[i] minL.append(D[i]) maxL.append(D[i]) continue currentMax = max(currentMax + D[i], D[i]) currentMin = min(currentMin + D[i], D[i]) minL.append(min(currentMin, minL[-1])) maxL.append(max(currentMax, maxL[-1])) currentMax = currentMin = 0 for i in range(N - 1, 0, -1): if i == N - 1: if D[i] < 0: currentMin = D[i] else: currentMax = D[i] minR.append(D[i]) maxR.append(D[i]) continue currentMax = max(currentMax + D[i], D[i]) currentMin = min(currentMin + D[i], D[i]) maxR.append(max(currentMax, maxR[-1])) minR.append(min(currentMin, minR[-1])) minR.reverse() maxR.reverse() maxAbs = 0 for i in range(N - 1): maxAbs = max(max(abs(minL[i] - maxR[i]), abs(maxL[i] - minR[i])), maxAbs) print(maxAbs)
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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) startmax = [0] * n startmin = [0] * n endmax = [0] * n endmin = [0] * n startmax[n - 1], startmin[n - 1] = A[n - 1], A[n - 1] endmax[0], endmin[0] = A[0], A[0] for i in range(n - 2, -1, -1): startmax[i] = max(startmax[i + 1] + A[i], A[i]) startmin[i] = min(startmin[i + 1] + A[i], A[i]) j = n - 1 - i endmax[j] = max(endmax[j - 1] + A[j], A[j]) endmin[j] = min(endmin[j - 1] + A[j], A[j]) for i in range(n - 2, -1, -1): startmax[i] = max(startmax[i + 1], startmax[i]) startmin[i] = min(startmin[i + 1], startmin[i]) j = n - 1 - i endmax[j] = max(endmax[j - 1], endmax[j]) endmin[j] = min(endmin[j - 1], endmin[j]) ans = -1 for i in range(n - 1): ans = max( abs(startmax[i + 1] - endmin[i]), abs(startmin[i + 1] - endmax[i]), 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 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 ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
T = int(input()) ans = [] for _ in range(T): N = int(input()) D = [int(i) for i in input().split()] lmax = [D[0]] * N lmin = [D[0]] * N rmax = [D[-1]] * N rmin = [D[-1]] * N for i in range(1, N): lmin[i] = min(lmin[i - 1] + D[i], D[i]) lmax[i] = max(lmax[i - 1] + D[i], D[i]) for i in range(N - 2, -1, -1): rmin[i] = min(rmin[i + 1] + D[i], D[i]) rmax[i] = max(rmax[i + 1] + D[i], D[i]) s = 0 for i in range(N - 1): s = max(lmax[i] - rmin[i + 1], rmax[i + 1] - lmin[i], s) ans.append(s) for i in ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input().strip()) for _ in range(t): n = int(input().strip()) a = list(map(int, input().strip().split())) max_lr, min_lr = [a[0]], [a[0]] for i in range(1, n): max_lr.append(max(max_lr[i - 1] + a[i], a[i])) min_lr.append(min(min_lr[i - 1] + a[i], a[i])) max_rl = [(0) for i in range(n)] min_rl = [(0) for i in range(n)] max_inc_rl = [(0) for i in range(n)] min_inc_rl = [(0) for i in range(n)] max_rl[n - 1] = a[n - 1] min_rl[n - 1] = a[n - 1] max_inc_rl[n - 1] = a[n - 1] min_inc_rl[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): max_inc_rl[i] = max(max_inc_rl[i + 1] + a[i], a[i]) max_rl[i] = max(max_inc_rl[i], max_rl[i + 1]) min_inc_rl[i] = min(min_inc_rl[i + 1] + a[i], a[i]) min_rl[i] = min(min_inc_rl[i], min_rl[i + 1]) dishes = [ max(abs(max_lr[i] - min_rl[i + 1]), abs(min_lr[i] - max_rl[i + 1])) for i in range(n - 1) ] print(max(dishes))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) minleft = n * [0] maxleft = n * [0] minleft[0] = maxleft[0] = ar[0] for i in range(1, n): minleft[i] = min(ar[i], minleft[i - 1] + ar[i]) maxleft[i] = max(ar[i], maxleft[i - 1] + ar[i]) minright = n * [0] maxright = n * [0] minright[n - 1] = maxright[n - 1] = ar[n - 1] for i in range(n - 2, -1, -1): minright[i] = min(ar[i], minright[i + 1] + ar[i]) maxright[i] = max(ar[i], maxright[i + 1] + ar[i]) maxdiff = 0 for i in range(n - 1): maxdiff = max(maxdiff, abs(minleft[i] - maxright[i + 1])) maxdiff = max(maxdiff, abs(maxleft[i] - minright[i + 1])) print(maxdiff)
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 BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] leftdp = [[(0) for i in range(n)] for j in range(2)] rightdp = [[(0) for i in range(n)] for j in range(2)] leftdp[0][0] = a[0] leftdp[1][0] = a[0] for i in range(1, n): leftdp[0][i] = max(leftdp[0][i - 1], 0) + a[i] leftdp[1][i] = min(leftdp[1][i - 1], 0) + a[i] rightdp[0][n - 1] = a[n - 1] rightdp[1][n - 1] = a[n - 1] for i in range(n - 2, -1, -1): rightdp[0][i] = max(rightdp[0][i + 1], 0) + a[i] rightdp[1][i] = min(rightdp[1][i + 1], 0) + a[i] ans = 0 for i in range(n - 1): ans = max(ans, abs(leftdp[0][i] - rightdp[1][i + 1])) ans = max(ans, abs(leftdp[1][i] - rightdp[0][i + 1])) 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
class node: def __init__(self, size, elements): self.size = size self.elements = elements def solve(inputs, size): maxr = [0] * size maxl = [0] * size minr = [0] * size minl = [0] * size maxl[0] = minl[0] = inputs[0] maxr[size - 1] = minr[size - 1] = inputs[size - 1] for i in range(1, size): maxl[i] = max(maxl[i - 1] + inputs[i], inputs[i]) minl[i] = min(minl[i - 1] + inputs[i], inputs[i]) for i in range(size - 2, -1, -1): maxr[i] = max(maxr[i + 1] + inputs[i], inputs[i]) minr[i] = min(minr[i + 1] + inputs[i], inputs[i]) result = [] for i in range(size - 1): result.append(abs(maxl[i] - minr[i + 1])) result.append(abs(minl[i] - maxr[i + 1])) return max(result) t = int(input()) inputs = [] for i in range(t): inputs.append(node(int(input()), list(map(int, input().split())))) for i in inputs: print(solve(i.elements, i.size))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF 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 ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def delish(arr, n): minpref = [(0) for _ in range(n)] maxpref = [(0) for _ in range(n)] minsuf = [(0) for _ in range(n)] maxsuf = [(0) for _ in range(n)] minpref[0] = maxpref[0] = arr[0] curmax, curmin = arr[0], arr[0] for i in range(1, n): curmax = max(curmax + arr[i], arr[i]) curmin = min(curmin + arr[i], arr[i]) maxpref[i] = max(maxpref[i - 1], curmax) minpref[i] = min(minpref[i - 1], curmin) minsuf[n - 1] = maxsuf[n - 1] = arr[n - 1] curmax, curmin = arr[n - 1], arr[n - 1] for i in range(n - 2, -1, -1): curmax = max(curmax + arr[i], arr[i]) curmin = min(curmin + arr[i], arr[i]) maxsuf[i] = max(maxsuf[i + 1], curmax) minsuf[i] = min(minsuf[i + 1], curmin) m = float("-inf") for i in range(n - 1): r = max(abs(maxpref[i] - minsuf[i + 1]), abs(minpref[i] - maxsuf[i + 1])) if r > m: m = r return m t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(delish(arr, n))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def leftMin(a, n): r = [] minSum = a[0] curSum = 0 i = 0 while i < n - 1: if curSum >= 0: curSum = a[i] else: curSum += a[i] if minSum > curSum: minSum = curSum r.append(minSum) i += 1 return r def rightMin(a, n): r = [] minSum = a[-1] curSum = 0 i = n - 1 while i > 0: if curSum >= 0: curSum = a[i] else: curSum += a[i] if minSum > curSum: minSum = curSum r.insert(0, minSum) i -= 1 return r def leftMax(a, n): r = [] maxSum = a[0] curSum = 0 i = 0 while i < n - 1: if curSum <= 0: curSum = 0 curSum += a[i] if maxSum < curSum: maxSum = curSum r.append(maxSum) i += 1 return r def rightMax(a, n): r = [] maxSum = a[-1] curSum = 0 i = n - 1 while i > 0: if curSum <= 0: curSum = 0 curSum += a[i] if maxSum < curSum: maxSum = curSum r.insert(0, maxSum) i -= 1 return r for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) leftMinSeq = leftMin(a, n) leftMaxSeq = leftMax(a, n) rightMinSeq = rightMin(a, n) rightMaxSeq = rightMax(a, n) ans = [abs(leftMaxSeq[i] - rightMinSeq[i]) for i in range(n - 1)] ans.extend([abs(leftMinSeq[i] - rightMaxSeq[i]) for i in range(n - 1)]) print(max(ans))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) min_p = [a[0]] * n max_p = [a[0]] * n for i in range(1, n): min_p[i] = min(min_p[i - 1] + a[i], a[i]) max_p[i] = max(max_p[i - 1] + a[i], a[i]) min_s = [a[-1]] * n max_s = [a[-1]] * n for i in range(n - 2, -1, -1): min_s[i] = min(min_s[i + 1] + a[i], a[i]) max_s[i] = max(max_s[i + 1] + a[i], a[i]) ans = 0 for i in range(n - 1): ans = max(ans, max_s[i + 1] - min_p[i], max_p[i] - min_s[i + 1]) 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 BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): N = int(input()) a = list(map(int, input().split()))[::-1] mxsfr = [0] * N mv = float("-inf") tmv = 0 for i in range(N): tmv = tmv + a[i] if mv < tmv: mv = tmv if tmv < 0: tmv = 0 mxsfr[-i - 1] = mv mn = float("inf") tmn = 0 mnsfr = [0] * N for i in range(N): tmn = tmn + a[i] if mn > tmn: mn = tmn if tmn > 0: tmn = 0 mnsfr[-i - 1] = mn a = a[::-1] mn = float("inf") tmn = 0 mnsf = [0] * N for i in range(N): tmn = tmn + a[i] if mn > tmn: mn = tmn if tmn > 0: tmn = 0 mnsf[i] = mn mxsf = [0] * N mv = float("-inf") tmv = 0 for i in range(N): tmv = tmv + a[i] if mv < tmv: mv = tmv if tmv < 0: tmv = 0 mxsf[i] = mv ans = 0 for i in range(N - 1): if abs(mnsf[i] - mxsfr[i + 1]) > ans: ans = abs(mnsf[i] - mxsfr[i + 1]) if abs(mxsf[i] - mnsfr[i + 1]) > ans: ans = abs(mxsf[i] - mnsfr[i + 1]) 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for case in range(int(input())): N = int(input()) L = list(map(int, input().split())) leftMaSum = [None] * N leftMiSum = [None] * N leftMa = [None] * N leftMi = [None] * N leftMaSum[0] = L[0] leftMiSum[0] = L[0] leftMa[0] = L[0] leftMi[0] = L[0] for i in range(1, N): num = L[i] leftMaSum[i] = max(num + leftMaSum[i - 1], num) leftMiSum[i] = min(num + leftMiSum[i - 1], num) leftMa[i] = max(leftMaSum[i], leftMa[i - 1]) leftMi[i] = min(leftMiSum[i], leftMi[i - 1]) rightMaSum = [None] * N rightMiSum = [None] * N rightMa = [None] * N rightMi = [None] * N rightMaSum[-1] = L[-1] rightMiSum[-1] = L[-1] rightMa[-1] = L[-1] rightMi[-1] = L[-1] for i in range(N - 2, -1, -1): num = L[i] rightMaSum[i] = max(num + rightMaSum[i + 1], num) rightMiSum[i] = min(num + rightMiSum[i + 1], num) rightMa[i] = max(rightMaSum[i], rightMa[i + 1]) rightMi[i] = min(rightMiSum[i], rightMi[i + 1]) v1 = abs(leftMa[0] - rightMa[1]) v2 = abs(leftMa[0] - rightMi[1]) v3 = abs(leftMi[0] - rightMa[1]) v4 = abs(leftMi[0] - rightMi[1]) ans = max(v1, v2, v3, v4) for i in range(1, N - 1): v1 = abs(leftMa[i] - rightMa[i + 1]) v2 = abs(leftMa[i] - rightMi[i + 1]) v3 = abs(leftMi[i] - rightMa[i + 1]) v4 = abs(leftMi[i] - rightMi[i + 1]) ans = max(ans, v1, v2, v3, v4) 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 BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER 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 ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
T = int(input()) for i in range(T): n = int(input()) l = list(map(int, input().split())) premin = [0] * n premax = [0] * n sufmin = [0] * n sufmax = [0] * n premin[0] = premax[0] = l[0] sufmin[-1] = sufmax[-1] = l[-1] for i in range(1, n): premin[i] = min(premin[i - 1], 0) + l[i] premax[i] = max(premax[i - 1], 0) + l[i] for i in range(n - 2, -1, -1): sufmin[i] = min(sufmin[i + 1], 0) + l[i] sufmax[i] = max(sufmax[i + 1], 0) + l[i] ans = 0 for i in range(n - 1): ans = max(ans, abs(premax[i] - sufmin[i + 1])) ans = max(ans, abs(premin[i] - sufmax[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 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) a = [[0, 0] for i in range(n)] b = [[0, 0] for i in range(n)] x = arr[0] y = arr[0] for i in range(1, n): a[i][0] = x a[i][1] = y if x < 0: x = arr[i] else: x += arr[i] if y > 0: y = arr[i] else: y += arr[i] x = 0 y = 0 for i in range(n - 1, -1, -1): if x < 0: x = arr[i] else: x += arr[i] if y > 0: y = arr[i] else: y += arr[i] b[i][0] = x b[i][1] = y ans = 0 for i in range(1, n): c = abs(a[i][0] - b[i][1]) d = abs(a[i][1] - b[i][0]) c = max(c, d) ans = max(ans, c) 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) minleft = [0] * n maxleft = [0] * n minright = [0] * n maxright = [0] * n minleft[0] = maxleft[0] = a[0] for i in range(1, n): minleft[i] = min(minleft[i - 1] + a[i], a[i]) maxleft[i] = max(maxleft[i - 1] + a[i], a[i]) minright[n - 1] = a[n - 1] maxright[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): minright[i] = min(minright[i + 1] + a[i], a[i]) maxright[i] = max(maxright[i + 1] + a[i], a[i]) max_diff = 0 for i in range(n - 1): max_diff = max(max_diff, abs(minleft[i] - maxright[i + 1])) max_diff = max(max_diff, abs(minleft[i] - minright[i + 1])) max_diff = max(max_diff, abs(maxleft[i] - maxright[i + 1])) max_diff = max(max_diff, abs(maxleft[i] - minright[i + 1])) print(max_diff)
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 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 ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
t = int(input()) for x in range(t): n = int(input()) m = input().strip().split() a = [] for val in m: a.append(int(val)) f = list() b = list() for i in range(n): f.append([0, 0]) b.append([0, 0]) f[0][0] = a[0] f[0][1] = a[0] for i in range(1, n): m1 = max(f[i - 1][0], 0) + a[i] m2 = min(f[i - 1][1], 0) + a[i] f[i][0] = m1 f[i][1] = m2 b[n - 1][0] = a[n - 1] b[n - 1][1] = a[n - 1] for i in range(n - 2, -1, -1): b1 = max(b[i + 1][0], 0) + a[i] b2 = min(b[i + 1][1], 0) + a[i] b[i][0] = b1 b[i][1] = b2 ans = float("-inf") for i in range(0, n - 1): ans = max(ans, abs(f[i][0] - b[i + 1][1])) ans = max(ans, abs(f[i][1] - b[i + 1][0])) 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 FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
T = int(input()) output = [] srmax, hlmin, hrmax = {}, {}, {} slmax, hrmin, hlmax = {}, {}, {} def hard_left_min(array, j): if j == 0: return array[0] elif j in hlmin: return hlmin[j] else: tmp = min(array[j], hard_left_min(array, j - 1) + array[j]) hlmin[j] = tmp return tmp def soft_right_max(array, j): N = len(array) if j == N - 1: return array[N - 1] elif j in srmax: return srmax[j] else: tmp = max(hard_right_max(array, j), soft_right_max(array, j + 1)) srmax[j] = tmp return tmp def hard_right_max(array, j): N = len(array) if j == N - 1: return array[N - 1] elif j in hrmax: return hrmax[j] else: tmp = max(array[j], array[j] + hard_right_max(array, j + 1)) hrmax[j] = tmp return tmp def hard_right_min(array, j): N = len(array) if j == N - 1: return array[j] elif j in hrmin: return hrmin[j] else: tmp = min(array[j], array[j] + hard_right_min(array, j + 1)) hrmin[j] = tmp return tmp def soft_left_max(array, j): if j == 0: return array[0] elif j in slmax: return slmax[j] else: tmp = max(hard_left_max(array, j), soft_left_max(array, j - 1)) slmax[j] = tmp return tmp def hard_left_max(array, j): if j == 0: return array[0] elif j in hlmax: return hlmax[j] else: tmp = max(array[j], hard_left_max(array, j - 1) + array[j]) hlmax[j] = tmp return tmp for _ in range(T): N = int(input()) D = list(map(int, input().split())) for j in range(N): hard_left_min(D, j) for j in range(N - 1, -1, -1): hard_right_max(D, j) for j in range(N - 1, -1, -1): soft_right_max(D, j) for j in range(N - 1, -1, -1): hard_right_min(D, j) for j in range(N): hard_left_max(D, j) for j in range(N): soft_left_max(D, j) max_delish = -1 for j in range(N): if j < N - 1: max_delish = max( max_delish, soft_right_max(D, j + 1) - hard_left_min(D, j), soft_left_max(D, j) - hard_right_min(D, j + 1), ) output.append(max_delish) srmax, hlmin, hrmax = {}, {}, {} slmax, hrmin, hlmax = {}, {}, {} for o in output: print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR DICT DICT DICT ASSIGN VAR VAR VAR DICT DICT DICT FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR DICT DICT DICT ASSIGN VAR VAR VAR DICT DICT DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
import sys for t in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] lma = [(0) for i in range(n)] lmi = [(0) for i in range(n)] rma = [(0) for i in range(n)] rmi = [(0) for i in range(n)] s1 = a[0] s2 = a[-1] ma = ma1 = -sys.maxsize mi = mi1 = sys.maxsize s = 0 o = 0 s1 = 0 o1 = 0 for i in range(0, n - 1): j = n - i - 1 s += a[i] o += a[i] s1 += a[j] o1 += a[j] ma = max(ma, s) mi = min(mi, o) ma1 = max(ma1, s1) mi1 = min(mi1, o1) if s < 0: s = 0 if o > 0: o = 0 if s1 < 0: s1 = 0 if o1 > 0: o1 = 0 lmi[i] = mi lma[i] = ma rmi[j] = mi1 rma[j] = ma1 ans = max(abs(lmi[0] - rma[1]), abs(lma[0] - rmi[1])) for i in range(n - 1): y = max(abs(lmi[i] - rma[i + 1]), abs(lma[i] - rmi[i + 1])) ans = ans if ans > y else y print(ans)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
def lrimax(n, l): dplri = [0] * n dplri[0] = l[0] for i in range(1, n): dplri[i] = max(dplri[i - 1] + l[i], l[i]) for i in range(1, n): dplri[i] = max(dplri[i - 1], dplri[i]) return dplri def lrimin(n, l): dp = [999999999999999999999999999] * n dp[0] = l[0] for i in range(1, n): dp[i] = min(l[i], l[i] + dp[i - 1]) for i in range(1, n): dp[i] = min(dp[i - 1], dp[i]) return dp def rlimax(n, l): dp = [0] * n dp[n - 1] = l[n - 1] for i in range(n - 2, -1, -1): dp[i] = max(dp[i + 1] + l[i], l[i]) for i in range(n - 2, -1, -1): dp[i] = max(dp[i], dp[i + 1]) return dp def rlimin(n, l): dp = [9999999999999999999999999999] * n dp[n - 1] = l[n - 1] for i in range(n - 2, -1, -1): dp[i] = min(dp[i + 1] + l[i], l[i]) for i in range(n - 2, -1, -1): dp[i] = min(dp[i], dp[i + 1]) return dp t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) lrmax = lrimax(n, l) rlmin = rlimin(n, l) lrmin = lrimin(n, l) rlmax = rlimax(n, l) maxi = -9999999999999999999999999999999999999999 for i in range(n - 1): maxi = max(abs(lrmax[i] - rlmin[i + 1]), abs(rlmax[i + 1] - lrmin[i]), maxi) print(maxi)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER 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 RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER 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 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 ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish. The wizard provides the chef with a sequence of N ingredients where the i^{th} ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases. Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j]. Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l]. Note that 1 ≤ i ≤ j < k ≤ l ≤ N. The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him. ------ Input ------ First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the i^{th} integer represents the delish value D[i] of the i^{th} ingredient. ------ Output ------ Print the maximum delish value of the dish that the chef can get. ------ Constraints ------ $ 1 ≤ T ≤ 50 $ $ 2 ≤ N ≤ 10000 $ $ -1000000000 (−10^{9}) ≤ D[i] ≤ 1000000000 (10^{9}) ----- Sample Input 1 ------ 2 5 1 2 3 4 5 4 1 1 -1 -1 ----- Sample Output 1 ------ 13 4 ----- explanation 1 ------ Example case 1. Chef can choose i = j = 1, k = 2, l = 5. The delish value hence obtained is | (2+3+4+5) ? (1) | = 13 . Example case 2. Chef can choose i = 1, j = 2, k = 3, l = 4. The delish value hence obtained is | ( ( ?1 ) + ( ?1 ) ) ? ( 1 + 1 ) | = 4 .
for _ in range(int(input())): n = int(input()) my = [int(i) for i in input().split()] leftmax = [my[0]] rightmax = [1000000] rightmin = [1000000] leftmin = [my[0]] total = my[0] totmin = my[0] for i in range(1, n): leftmax.append(0) rightmax.append(0) leftmin.append(1000000) rightmin.append(1000000) for i in range(1, len(my)): leftmax[i] = max(total + my[i], max(my[i], leftmax[i - 1])) leftmin[i] = min(totmin + my[i], min(my[i], leftmin[i - 1])) total = max(my[i], total + my[i]) totmin = min(my[i], totmin + my[i]) total = my[n - 1] totmin = my[n - 1] rightmin[n - 1] = my[n - 1] rightmax[n - 1] = my[n - 1] for i in range(n - 2, -1, -1): rightmax[i] = max(total + my[i], max(my[i], rightmax[i + 1])) rightmin[i] = min(totmin + my[i], min(my[i], rightmin[i + 1])) total = max(my[i], total + my[i]) totmin = min(my[i], totmin + my[i]) ans = 0 for i in range(1, n): ans = max(leftmax[i - 1] - rightmin[i], max(rightmax[i] - leftmin[i - 1], 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
s = input() n = len(s) palindromic = [[(False) for i in range(n)] for y in range(n)] start = [[(0) for i in range(n)] for y in range(n)] end = [[(0) for i in range(n)] for y in range(n)] for i in range(n): for j in range(i + 1): if i == j: palindromic[j][i] = True elif s[j] == s[i] and j == i - 1: palindromic[j][i] = True elif palindromic[j + 1][i - 1] and s[j] == s[i]: palindromic[j][i] = True for i in range(n): start[i][i] = 1 for j in range(i + 1, n): if palindromic[i][j]: start[i][j] = start[i][j - 1] + 1 else: start[i][j] = start[i][j - 1] for i in range(n): end[i][i] = 1 for j in range(i - 1, 0, -1): if palindromic[j][i]: end[j][i] = end[j + 1][i] + 1 else: end[j][i] = end[j + 1][i] cur = [0] * n prev = [0] * n for i in range(1, n): cur[i] = cur[i - 1] for j in range(i): if s[i] == s[j]: prev[j] = start[j + 1][i - 1] + end[j + 1][i - 1] + prev[j + 1] + 1 cur[i] += prev[j] else: prev[j] = 0 print(cur[n - 1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
def oddPalindrome(task): answer = [0] * len(task) for i in range(len(task)): for k in range(min(len(task) - i, i + 1)): if task[i - k] != task[i + k]: break else: answer[i] += 1 return answer def evenPalindrome(task): answer = [0] * (len(task) - 1) for i in range(len(task) - 1): for k in range(min(i + 1, len(task) - 1 - i)): if task[i - k] != task[i + k + 1]: break else: answer[i] += 1 return answer def makeLeftPalindrome(oddPalindromes, evenPalindromes): n = len(oddPalindromes) leftPalindrome = [([0] * (n - x)) for x in range(n)] for a in range(n): leftPalindrome[a][0] = 1 for k in range(1, n - a): leftPalindrome[a][k] = ( leftPalindrome[a][k - 1] + (1 if k % 2 == 0 and oddPalindromes[a + k // 2] > k // 2 else 0) + (1 if k % 2 != 0 and evenPalindromes[a + k // 2] > k // 2 else 0) ) return leftPalindrome def makeRightPalindrome(oddPalindromes, evenPalindromes): n = len(oddPalindromes) rightPalindrome = [([0] * (x + 1)) for x in range(n - 1)] for b in range(n - 1): rightPalindrome[b][0] = 1 for k in range(1, b + 1): rightPalindrome[b][k] = ( rightPalindrome[b][k - 1] + (1 if k % 2 == 0 and oddPalindromes[b - k // 2] > k // 2 else 0) + (1 if k % 2 != 0 and evenPalindromes[b - k // 2 - 1] > k // 2 else 0) ) return rightPalindrome def makeSubstringPalindrome(leftPalindrome, rightPalindrome, task): n = len(leftPalindrome) answer = [([0] * n) for _ in range(n)] for k in range(n - 1): for a in range(n - 1 - k): if task[a] == task[k + a + 1]: answer[a][k + a + 1] = ( (answer[a + 1][k + a] if k > 1 else 0) + (leftPalindrome[a + 1][k - 1] if k > 0 else 0) + (rightPalindrome[k + a][k - 1] if k > 0 else 0) + 1 ) return answer task = input() oddP = oddPalindrome(task) evenP = evenPalindrome(task) leftP = makeLeftPalindrome(oddP, evenP) rightP = makeRightPalindrome(oddP, evenP) A = makeSubstringPalindrome(leftP, rightP, task) print(sum([sum(x) for x in A]))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN 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 BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
s = str(input()) n = len(s) DP = [([0] * n) for _ in range(n)] P = [([0] * n) for _ in range(n)] LR = [([0] * n) for _ in range(n)] RL = [([0] * n) for _ in range(n)] for i in range(n): for j in range(n): s2 = s[i : j + 1] if s[i : j + 1] == s2[::-1]: P[i][j] = 1 if i > j: P[i][j] = 0 for i in range(n): for j in range(n): if j == 0: LR[i][j] = P[i][j] else: LR[i][j] = P[i][j] + LR[i][j - 1] for j in range(n): for i in range(n - 1, -1, -1): if i == n - 1: RL[i][j] = P[i][j] else: RL[i][j] = P[i][j] + RL[i + 1][j] for j in range(n): for i in range(j, -1, -1): if i == j: DP[i][j] = 0 continue if i > j: DP[i][j] = 0 continue if s[i] == s[j]: DP[i][j] = 1 + RL[i + 1][j - 1] + LR[i + 1][j - 1] + DP[i + 1][j - 1] else: DP[i][j] = 0 s = 0 for i in range(n): s += sum(DP[i]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
st = str(input()) def checkpal(i, j, k, l): a = i b = l while a < b: if st[a] != st[b]: return -1 if a == j: a = k - 1 if b == k: b = j + 1 a += 1 b -= 1 return 1 l = len(st) count = 0 for i in range(l): for j in range(i, l): for k in range(j + 1, l): for m in range(k, l): if checkpal(i, j, k, m) == 1: count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
DP = [] for i in range(1002): temp = [] for j in range(1002): temp.append(False) DP.append(temp) def Pre_process(s): n = len(s) for i in range(n): for j in range(n): DP[i][j] = False for j in range(1, n + 1): for i in range(0, n - j + 1): if j <= 2: if s[i] == s[i + j - 1]: DP[i][i + j - 1] = True elif s[i] == s[i + j - 1]: DP[i][i + j - 1] = DP[i + 1][i + j - 2] for i in range(0, n): for j in range(0, n): DP[j][i] = DP[i][j] def main(): s = input() Pre_process(s) n = len(s) left = [] for i in range(n): temp = [] for j in range(n): temp.append(0) left.append(temp) right = [] for i in range(n): temp = [] for j in range(n): temp.append(0) right.append(temp) for i in range(n): left[i][i] = 1 for j in range(i + 1, n): left[i][j] = left[i][j - 1] if DP[i][j]: left[i][j] += 1 for i in range(n - 1, -1, -1): right[i][i] = 1 for j in range(i - 1, -1, -1): right[i][j] = right[i][j + 1] if DP[i][j]: right[i][j] += 1 ans = [] for i in range(n): temp = [] for j in range(n): temp.append(0) ans.append(temp) for i in range(0, n - 1): if s[i] == s[i + 1]: ans[i][i + 1] = 1 for j in range(2, n): for i in range(n - j): if s[i] == s[i + j]: ans[i][i + j] = ( ans[i + 1][i + j - 1] + 1 + left[i + 1][i + j - 1] + right[i + j - 1][i + 1] ) Sum = 0 for i in range(n): for j in range(n): Sum += ans[i][j] print(Sum) main()
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
def binarySearch(arr, l, r, x): mid = 0 while l <= r: mid = l + (r - l) // 2 if arr[mid] == x: return mid + 1 elif arr[mid] < x: l = mid + 1 else: r = mid - 1 if mid != len(arr): if arr[mid] < x: return mid + 1 return mid s = input() strt = [] end = [] plc = [] landr = [] l2r = [] lr = [] ans = 0 n = len(s) if n != 1: for i in range(n): strt.append([]) end.append([]) landr.append([0] * n) l2r.append([0] * n) for i in range(n): for j in range(n): if i - j < 0 or i + j >= n: break if s[i - j] == s[i + j]: if i - j - 1 >= 0: strt[i - j - 1].append(2 * j + 1) if i + j + 1 < n: end[i + j + 1].append(2 * j + 1) else: break for i in range(n): for j in range(n): if i - j < 0 or i + j + 1 >= n: break if s[i - j] == s[i + j + 1]: if i - j - 1 >= 0: strt[i - j - 1].append(2 * j + 2) if i + j + 2 < n: end[i + j + 2].append(2 * j + 2) else: break for i in range(n): end[i].sort() strt[i].sort() for i in range(n - 1): for j in range(i + 1, n): if s[i] == s[j]: lr.append([i, j]) if i > 0 and j < n - 1: landr[i][j] = landr[i - 1][j + 1] + 1 else: landr[i][j] = 1 for i in lr: tempans = 1 l = i[0] r = i[1] length = r - l - 1 tempans += binarySearch(strt[l], 0, len(strt[l]) - 1, length) tempans += binarySearch(end[r], 0, len(end[r]) - 1, length) l2r[l][r] = tempans for i in range(n): for j in range(n): ans += l2r[i][j] * landr[i][j] print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
s = input() N = len(s) Nkj = [([0] * N) for _ in range(N)] for k in range(N): for j in range(N - 1, k, -1): if s[j] == s[k]: Nkj[k][j] = 1 + (Nkj[k - 1][j + 1] if k > 0 and j < N - 1 else 0) else: Nkj[k][j] = 0 Nkj[j][k] = Nkj[k][j] Pab = [([0] * N) for _ in range(N)] for m in range(N): for w in range(0, min(N - m, m + 1)): if s[m - w] == s[m + w]: Pab[m - w][m + w] = 1 Pab[m + w][m - w] = 1 else: break for w in range(0, min(N - m - 1, m + 1)): if s[m - w] == s[m + w + 1]: Pab[m - w][m + w + 1] = 1 Pab[m + w + 1][m - w] = 1 else: break SPab = [list(Pab[a]) for a in range(N)] for a in range(N): for b in range(a + 1, N): SPab[a][b] += SPab[a][b - 1] for b in range(a - 1, -1, -1): SPab[a][b] += SPab[a][b + 1] C = 0 for i in range(-1, N - 1): for j in range(i + 1, N + 1): center_width = j - (i + 1) c = 0 if center_width >= 2: if Pab[i + 1][j - 1]: c += center_width - 1 if center_width >= 1: if i >= 0 and j < N: if center_width >= 2: Pi = SPab[i + 1][j - 2] Ni = Nkj[i][j] c += Pi * Ni Pj = SPab[j - 1][i + 2] Nj = Nkj[j][i] c += Pj * Nj c += Nkj[i][j] C += c print(C)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
def func(S): N = len(S) visit = [[None for _ in range(N)] for _ in range(N)] for i in range(N): visit[i][i] = True for i in range(N - 1): if S[i] == S[i + 1]: visit[i][i + 1] = True else: visit[i][i + 1] = False for k in range(3, N + 1): for i in range(0, N - k + 1): j = i + k - 1 if S[i] == S[j] and visit[i + 1][j - 1] == True: visit[i][j] = True else: visit[i][j] = False x = [[(0) for _ in range(N)] for _ in range(N)] for i in range(N): x[i][i] = 1 for i in range(N - 1): if S[i] == S[i + 1]: x[i][i + 1] = 2 else: x[i][i + 1] = 1 for i in range(N - 2): for j in range(i + 2, N): if S[i] == S[j] and visit[i + 1][j - 1] == True: x[i][j] = x[i][j - 1] + 1 else: x[i][j] = x[i][j - 1] y = [[(0) for _ in range(N)] for _ in range(N)] for j in range(N): y[j][j] = 1 for j in range(1, N): if S[j] == S[j - 1]: y[j - 1][j] = 2 else: y[j - 1][j] = 1 for j in range(2, N): for i in reversed(range(0, j - 1)): if S[i] == S[j] and visit[i + 1][j - 1] == True: y[i][j] = y[i + 1][j] + 1 else: y[i][j] = y[i + 1][j] return x, y S = input() result = 0 N = len(S) if N == 1: result = 0 elif N == 2: if S[0] == S[1]: result = 1 else: result = 0 else: x, y = func(S) qw = [[[-1, -1] for _ in range(N)] for _ in range(N)] for i in range(N): qw[i][i][0] = 1 qw[i][i][1] = 0 for i in range(N - 1): if S[i] == S[i + 1]: qw[i][i + 1][0] = 1 result += 1 else: qw[i][i + 1][0] = 0 qw[i][i + 1][1] = 0 for z in range(3, N + 1): for i in range(0, N - z + 1): j = i + z - 1 if S[i] != S[j]: qw[i][j][0] = 0 qw[i][j][1] = 0 else: if qw[i + 1][j - 1][0] == 0: qw[i][j][0] = 0 else: qw[i][j][0] = j - i qw[i][j][1] = ( 1 + qw[i + 1][j - 1][1] + x[i + 1][j - 2] + y[i + 2][j - 1] ) result += qw[i][j][0] + qw[i][j][1] print(result)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $S$. Find the number of ways to choose an unordered pair of non-overlapping non-empty substrings of this string (let's denote them by $s_1$ and $s_2$ in such a way that $s_2$ starts after $s_1$ ends) such that their concatenation $s_1 + s_2$ is a palindrome. Two pairs $(s_1, s_2)$ and $(s_1', s_2')$ are different if $s_1$ is chosen at a different position from $s_1'$ or $s_2$ is chosen at a different position from $s_2'$. -----Input----- The first and only line of the input contains a single string $S$. -----Output----- Print a single line containing one integer — the number of ways to choose a valid pair of substrings. -----Constraints----- - $1 \le |S| \le 1,000$ - $S$ contains only lowercase English letters -----Subtasks----- Subtask #1 (25 points): $|S| \le 100$ Subtask #2 (75 points): original constraints -----Example Input----- abba -----Example Output----- 7 -----Explanation----- The following pairs of substrings can be chosen: ("a", "a"), ("a", "ba"), ("a", "bba"), ("ab", "a"), ("ab", "ba"), ("abb", "a"), ("b", "b").
def palSub(s, n, isPal): for gap in range(n): for i in range(n - gap): j = i + gap if gap == 0: isPal[i][j] = 1 elif gap == 1: isPal[i][j] = 1 if s[i] == s[j] else 0 else: isPal[i][j] = 1 if s[i] == s[j] and isPal[i + 1][j - 1] else 0 return isPal def case1(n, isPal, c1): for i in range(n): for j in range(i, n): if i == j: c1[i][j] = 1 else: c1[i][j] = c1[i][j - 1] + isPal[i][j] return c1 def case2(n, isPal, c2): for j in range(n - 1, -1, -1): for i in range(j, -1, -1): if i == j: c2[i][j] = 1 else: c2[i][j] = c2[i + 1][j] + isPal[i][j] return c2 def getSub(s, n, c1, c2, res): for gap in range(n): for i in range(n - gap): j = i + gap if gap == 0: res[i][j] = 0 elif gap == 1: res[i][j] = 1 if s[i] == s[j] else 0 elif s[i] == s[j]: res[i][j] = 1 + c1[i + 1][j - 1] + c2[i + 1][j - 1] + res[i + 1][j - 1] return res def __starting_point(): s = input() n = len(s) isPal = [[(0) for x in range(n)] for y in range(n)] isPal = palSub(s, n, isPal) c1 = [[(-1) for x in range(n)] for y in range(n)] c2 = [[(-1) for x in range(n)] for y in range(n)] c1 = case1(n, isPal, c1) c2 = case2(n, isPal, c2) res = [[(0) for x in range(n)] for y in range(n)] res = getSub(s, n, c1, c2, res) mycount = 0 for i in range(n): for j in range(n): mycount += res[i][j] print(mycount) __starting_point()
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
M = 10**9 + 7 N = 1000 ways = [1] + [0] * N for k in range(N): for b in (1, 2, 3, 4): if k + b <= N: ways[k + b] += ways[k] ways[k] %= M def solve(height, width): combs = [pow(w, height, M) for w in ways] no_gaps = [0] * (width + 1) for w in range(1, width + 1): no_gaps[w] = combs[w] - sum(combs[w - i] * no_gaps[i] % M for i in range(1, w)) no_gaps[w] %= M return no_gaps[width] def main(): t = int(input()) for _ in range(t): height, width = map(int, input().split()) print(solve(height, width)) main()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
import sys MOD = 1000000007 T = int(input()) for testcases in range(T): N, M = [int(z) for z in input().split()] rows = [1, 1, 2, 4] while len(rows) <= M: rows.append(sum(rows[-4:]) % MOD) total = [pow(c, N, MOD) for c in rows] unstable = [0, 0, 1] for i in range(3, M + 1): unstable.append( sum((total[j] - unstable[j]) * total[i - j] for j in range(1, i)) % MOD ) print((total[M] - unstable[M]) % MOD)
IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
P = 10**9 + 7 MAX_M = 1000 nr_ways_ending = [0] * (MAX_M + 1) nr_ways_ending[0] = 1 for i in range(MAX_M): for s in (1, 2, 3, 4): if i + s < MAX_M + 1: nr_ways_ending[i + s] += nr_ways_ending[i] def solve(N, M): A = [0] * (M + 1) solution = [0] * (M + 1) solution[0] = 1 for k in range(1, M + 1): A[k] = pow(nr_ways_ending[k], N, P) solution[k] = A[k] for j in range(1, k): solution[k] -= solution[j] * A[k - j] solution[k] %= P return solution[M] % P T = int(input()) for _ in range(T): N, M = map(int, input().split()) print(solve(N, M))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
def expand_row_config_arr(width): curr_len = len(row_config_arr) while curr_len <= width: a = row_config_arr[-1] b = row_config_arr[-2] c = row_config_arr[-3] d = row_config_arr[-4] next_num = (a + b + c + d) % 1000000007 row_config_arr.append(next_num) curr_len += 1 num_tests = int(input()) row_config_arr = [0, 1, 2, 4, 8] for i in range(num_tests): N_M = input().split() N = int(N_M[0]) M = int(N_M[1]) if len(row_config_arr) <= M: expand_row_config_arr(M) T = [0, 1] S = [0, 1] U = [0, 0] for k in range(2, M + 1): T.append(pow(row_config_arr[k], N, 1000000007)) U.append(0) for l in range(1, k): U[k] = U[k] + S[l] * T[k - l] U[k] = U[k] % 1000000007 S.append((T[k] - U[k]) % 1000000007) print(S[-1])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
T = int(input()) mu = 10**9 + 7 def mod_pow(x, p): b = [int(d) for d in format(p, "b")[::-1]] product = 1 for i in range(0, len(b)): if not b[i]: continue factor = x % mu for j in range(i): factor = factor**2 % mu product = product * factor % mu return product for t in range(T): N, M = [int(a) for a in input().split(" ")] row_ways = [0, 1] for m in range(2, M + 1): new = 1 if m <= 4 else 0 row_ways.append((new + sum(row_ways[-4:])) % mu) all_walls = [mod_pow(x, N) for x in row_ways] solid = [None for __ in range(M + 1)] solid[1] = 1 not_solid = [None for __ in range(M + 1)] not_solid[1] = 0 for m in range(2, M + 1): accum = 0 for m_prime in range(1, m): accum = (accum + solid[m_prime] * all_walls[m - m_prime] % mu) % mu not_solid[m] = accum solid[m] = (all_walls[m] - not_solid[m]) % mu print((all_walls[M] - not_solid[M]) % mu)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
MAX = 1000000007 def calcLevels(levels): N = len(levels) M = len(levels[1]) levels[1][0] = 1 p = 1 while p < M: i = 1 while i <= 4 and p - i >= 0: levels[1][p] = (levels[1][p] + levels[1][p - i]) % MAX i += 1 p += 1 n = 2 while n < N: p = 1 while p < M: levels[n][p] = levels[n - 1][p] * levels[1][p] % MAX p += 1 n += 1 def getDP(dp, levels, N, M): if dp[N][M] != 0: return dp[N][M] curTmp = levels[N][M] p = 1 while p < M: if dp[N][p] == 0: curTmpLow = levels[N][p] q = 1 while q < p: curTmpLow = (curTmpLow - dp[N][q] * levels[N][p - q]) % MAX q += 1 dp[N][p] = curTmpLow curTmp = (curTmp - dp[N][p] * levels[N][M - p]) % MAX p += 1 dp[N][M] = curTmp return dp[N][M] T = int(input()) inputs = [[0, 0] for i in range(T)] maxN, maxM = 0, 0 p = 0 while p < T: N, M = [int(i) for i in input().split(" ")] if N > maxN: maxN = N if M > maxM: maxM = M inputs[p][:] = N, M p += 1 levels = [[(0) for i in range(maxM + 1)] for j in range(maxN + 1)] dp = [[(0) for i in range(maxM + 1)] for j in range(maxN + 1)] calcLevels(levels) p = 0 while p <= maxN: dp[p][1] = 1 p += 1 p = 1 while p <= maxM: dp[1][p] = levels[1][p] p += 1 p = 0 while p < T: N, M = inputs[p] if N == 1 and M >= 1 and M <= 4: print(1) elif N == 1: print(0) else: print(getDP(dp, levels, N, M)) p += 1
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
memos = dict({(0): 1, (-1): 0, (-2): 0, (-3): 0}) def calc_one_high_wall(width): if width in memos: return memos[width] ans = sum(calc_one_high_wall(x) for x in range(width - 4, width)) % 1000000007 memos[width] = ans return ans one_high_wall = [calc_one_high_wall(x) for x in range(1100)] memos2 = dict() for h in range(2000): memos2[1, h] = 1 pow_ohw = dict() def solve(width, height): if (width, height) in memos2: return memos2[width, height] modval = 1000000007 if (width, height) not in pow_ohw: for w in range(width + 1): pow_ohw[w, height] = pow(one_high_wall[w], height, modval) if width % 100 == 1: solve(width - 100, height) solve(width - 1, height) one_high = one_high_wall[width] all_combos = pow(one_high, height, modval) err = 0 for w in range(1, width): err += pow_ohw[w, height] * memos2[width - w, height] err %= modval ans = (all_combos + modval - err) % modval memos2[width, height] = ans return ans for _ in range(int(input())): height, width = [int(x) for x in input().split()] ans = solve(width, height) print(ans)
ASSIGN VAR FUNC_CALL VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
import sys MOD = 1000000007 bufArr = [1, 2, 4, 8] powArr = [1] solArr = [1] def makeBufArr(width): for i in range(len(bufArr), width): bufArr.append( (bufArr[i - 1] + bufArr[i - 2] + bufArr[i - 3] + bufArr[i - 4]) % MOD ) def makePowArr(height, width): for i in range(1, width): powArr.append(pow(bufArr[i], height, MOD)) def getSum(width): sum = 0 for i in range(width): sum = (sum + powArr[i] * solArr[width - i - 1]) % MOD return sum def getSolid(width): for i in range(1, width): solArr.append(powArr[i] - getSum(i) % MOD) return solArr[width - 1] n = int(input().strip()) for q in range(n): h, w = input().strip().split(" ") h, w = [int(h), int(w)] if w == 1: print(1) continue if h == 1 and w <= 4: print(1) continue if h == 1 and w > 4: print(0) continue powArr = [1] solArr = [1] makeBufArr(w) makePowArr(h, w) print((getSolid(w) + MOD) % MOD)
IMPORT ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
MOD = 1000000007 def ways_to_build_solid_block(N, M, block_sizes): M_TO_SOLVE = M M = max(M, 4) total = [0] * (M + 1) for block_size in block_sizes: total[block_size] = 1 for i in range(1, M + 1): sum_total_i = 0 for block_size in block_sizes: if i - block_size >= 0: sum_total_i += total[i - block_size] total[i] = total[i] + sum_total_i total[i] = total[i] % MOD for i in range(1, M + 1): total[i] = pow(total[i], N, MOD) solid = [0] * (M + 1) separable = [0] * (M + 1) solid[1] = 1 separable[1] = 0 for m in range(2, M + 1): sum_separable_m = 0 for separate_index in range(1, m): sum_separable_m += total[separate_index] * solid[m - separate_index] sum_separable_m = sum_separable_m % MOD separable[m] = sum_separable_m solid[m] = total[m] - separable[m] solid[m] = solid[m] % MOD return solid[M_TO_SOLVE] block_sizes = [1, 2, 3, 4] t = int(input()) for _ in range(t): N, M = map(int, input().split()) answer = ways_to_build_solid_block(N, M, block_sizes) print(answer)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR LIST NUMBER NUMBER 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 FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
maxb = 4 maxl = 1000 M = 10**9 + 7 arrange = [1] for i in range(1, maxl + 1): ways = 0 for j in range(1, maxb + 1): if i - j >= 0: ways += arrange[i - j] arrange.append(ways % M) t = int(input()) for _ in range(t): n, m = map(int, input().split()) valid = [1] total = [0] for i in range(1, m + 1): total.append(pow(arrange[i], n, M)) for i in range(1, m + 1): invalid = 0 for j in range(1, i): invalid += valid[j] * total[i - j] % M valid.append((total[i] - invalid) % M) print(valid[m])
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR 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 LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
K = 1000000007 MAXM = 1001 T = int(input().strip()) W = [-1] * MAXM for m, c in enumerate((0, 1, 2, 4, 8, 15, 29, 56, 108)): W[m] = c for m in range(9, MAXM): W[m] = ((W[m - 1] << 1) - W[m - 5]) % K for t in range(T): N, M = map(int, input().strip().split()) if N == 1: print(1 if 0 < M < 5 else 0) continue WNM = [-1] * (M + 1) WNM[0] = 0 WNM[1] = 1 POW = [-1] * (M + 1) for m in range(M + 1): POW[m] = pow(W[m], N, K) for m in range(2, M + 1): notgood = 0 for i in range(1, m): notgood += WNM[i] * POW[m - i] % K res = POW[m] - notgood % K if res < 0: res += K WNM[m] = res print(WNM[M])
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER 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 VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width): d h w 1 1 1 1 1 2 1 1 3 1 1 4 Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are: - The wall should not have any holes in it. - The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks. - The bricks must be laid horizontally. How many ways can the wall be built? Example $n=2$ $m=3$ The height is $2$ and the width is $3$. Here are some configurations: These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all. Function Description Complete the legoBlocks function in the editor below. legoBlocks has the following parameter(s): int n: the height of the wall int m: the width of the wall Returns - int: the number of valid wall formations modulo $(10^9+7)$ Input Format The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$. Constraints $1\leq t\leq100$ $1\leq n,m\leq1000$ Sample Input STDIN Function ----- -------- 4 t = 4 2 2 n = 2, m = 2 3 2 n = 3, m = 2 2 3 n = 2, m = 3 4 4 n = 4, m = 4 Sample Output 3 7 9 3375 Explanation For the first case, we can have: $3\:\:\:\text{mod}\:(10^9+7)=3$ For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$.
P = 10**9 + 7 MMAX = 1001 def main(): L = [0] * MMAX L[0] = 1 for i in range(1, MMAX): for j in range(1, min(4, i) + 1): L[i] = (L[i] + L[i - j]) % P T = int(input()) for _ in range(T): N, M = map(int, input().split()) NonSolid = [pow(L[m], N, P) for m in range(M + 1)] Solid = [0] * (M + 1) for m in range(M + 1): Solid[m] = NonSolid[m] for j in range(1, m): Solid[m] = (Solid[m] - Solid[j] * NonSolid[m - j]) % P print(Solid[M]) main()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR 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 VAR VAR VAR VAR VAR FUNC_CALL VAR 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 VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR