description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
isPalindrome = []
def palindromicPartition(self, string):
self.isPalindrome = [
[(False) for i in range(len(string))] for j in range(len(string))
]
def populateIsPalindromeDP():
for i in range(0, len(string)):
for j in range(0, len(string) - i):
if i == 0:
self.isPalindrome[j][j] = True
elif i == 1:
if string[j] == string[j + 1]:
self.isPalindrome[j][j + 1] = True
elif (
string[j] == string[j + i]
and self.isPalindrome[j + 1][j + i - 1]
):
self.isPalindrome[j][j + i] = True
populateIsPalindromeDP()
dp = [(0) for i in range(len(string) + 1)]
for i in range(1, len(string) + 1):
dp[i] = dp[i - 1] + 1
if self.isPalindrome[0][i - 1]:
dp[i] = 0
continue
for j in range(0, i):
if self.isPalindrome[j + 1 - 1][i - 1]:
dp[i] = min(dp[i], 1 + dp[j])
return dp[-1] | CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, s):
dp = [([-1] * (len(s) + 1)) for _ in range(len(s) + 1)]
def dfs(s, i, j):
if dp[i][j] != -1:
return dp[i][j]
if i > j:
return 0
if self.isPal(s, i, j):
return 0
mx = len(s)
for k in range(i, j):
if self.isPal(s, i, k):
t = dfs(s, k + 1, j) + 1
mx = min(mx, t)
dp[i][j] = mx
return dp[i][j]
return dfs(s, 0, len(s) - 1)
def isPal(self, s, start, end):
while start <= end:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, str):
n = len(str)
dp = [([False] * n) for i in range(n)]
for g in range(0, n):
for i, j in zip(range(0, n), range(g, n)):
if g == 0:
dp[i][j] = True
elif g == 1:
if str[i] == str[j]:
dp[i][j] = True
else:
dp[i][j] = False
elif str[i] == str[j] and dp[i + 1][j - 1] == True:
dp[i][j] = True
else:
dp[i][j] = False
if dp[0][n - 1] == True:
return 0
ans = [int(x) for x in range(n + 1)]
ans[0] = 0
ans[1] = 0
ans[2] = 0
if str[0] != str[1]:
ans[2] += 1
for i in range(3, n + 1):
for j in range(i, 0, -1):
if dp[j - 1][i - 1] == True:
ans[i] = min(ans[i], ans[j - 1] + 1)
return ans[n] | CLASS_DEF 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 NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR 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 IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def p(self, s):
l = 0
r = len(s) - 1
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True
def palindromicPartition(self, string):
dp = [(len(string) + 1) for i in range(len(string) + 1)]
dp[0] = -1
for i in range(1, len(string) + 1):
for j in range(i):
if self.p(string[j:i]):
dp[i] = min(dp[i], 1 + dp[j])
return dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, string):
def pal(i, j):
if i >= j or string[i : j + 1] == string[i : j + 1][::-1]:
dp[i] = 0
return 0
if dp[i] != -1:
return dp[i]
count = 10**10
for k in range(i, j):
count = min(count, pal(i, k) + pal(k + 1, j) + 1)
dp[i] = count
return count
i = 0
j = len(string)
dp = [(-1) for k in range(j + 1)]
return pal(i, j) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def ispallindrome(self, s, i, j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
def palindromicPartition(self, s):
n = len(s)
dp = [[(-1) for i in range(501)] for j in range(501)]
def helper(i, j):
if i >= j:
dp[i][j] = 0
return dp[i][j]
if dp[i][j] != -1:
return dp[i][j]
if self.ispallindrome(s, i, j):
dp[i][j] = 0
return 0
ans = 510
for k in range(i, j):
if self.ispallindrome(s, i, k):
temp = 1 + helper(i, k) + helper(k + 1, j)
ans = min(ans, temp)
dp[i][j] = ans
return dp[i][j]
return helper(0, n - 1) | CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, string):
S = string
n = len(S)
hmap = {}
def is_palindrome(s, i, j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
def palin_partition(arr, i, j):
if i >= j or is_palindrome(arr, i, j):
hmap[i, j] = 0
return 0
mini = 1000000000.0
if (i, j) not in hmap:
for k in range(i, j):
if is_palindrome(arr, i, k):
if (k + 1, j) not in hmap:
hmap[k + 1, j] = palin_partition(arr, k + 1, j)
temp_ans = 1 + hmap[k + 1, j]
mini = min(mini, temp_ans)
hmap[i, j] = mini
return hmap[i, j]
return palin_partition(S, 0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, string):
s = string
def solve(s, i, j):
if temp[i][j] != -1:
return temp[i][j]
if i >= j or s[i : j + 1] == s[i : j + 1][::-1]:
return 0
ans = float("infinity")
for k in range(i, j):
if s[i : k + 1] == s[i : k + 1][::-1]:
left = solve(s, k + 1, j)
tmp = 1 + left
ans = min(ans, tmp)
temp[i][j] = ans
return temp[i][j]
temp = [([-1] * 502) for i in range(502)]
return solve(s, 0, len(s) - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, s):
n = len(s)
dp = [[(-1) for i in range(0, n + 2)] for j in range(0, n + 2)]
def getAns(i, j):
if i >= j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if isPal(s, i, j):
return 0
mn = int(1000000000.0)
for k in range(i, j):
if isPal(s, i, k):
temp = getAns(k + 1, j) + 1
mn = min(mn, temp)
dp[i][j] = mn
return mn
def isPal(s, start, end):
while start <= end:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True
return getAns(0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | def ispallindrome(string, i, j):
if string == "":
return False
while i < j:
if string[i] != string[j]:
return False
i += 1
j += -1
return True
class Solution:
def solve(self, arr, ind, dp):
if ind == len(arr):
return 0
partition = 0
MIN = float("inf")
if dp[ind] != -1:
return dp[ind]
for i in range(ind, len(arr)):
if ispallindrome(arr, ind, i):
partition = 1 + self.solve(arr, i + 1, dp)
MIN = min(MIN, partition)
dp[ind] = MIN
return MIN
def palindromicPartition(self, string):
dp = [(-1) for _ in range(len(string))]
return self.solve(string, 0, dp) - 1 | FUNC_DEF IF VAR STRING RETURN NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | class Solution:
def palindromicPartition(self, string):
i = 0
j = len(string) - 1
dp = [[(-1) for i in range(len(string) + 1)] for j in range(len(string) + 1)]
ans = self.solve(string, i, j, dp)
return ans
def isPalindrome(self, string):
return string == string[::-1]
def solve(self, string, i, j, dp):
if i >= j or self.isPalindrome(string[i : j + 1]):
return 0
if dp[i][j] != -1:
return dp[i][j]
if dp[i][j] != -1:
return dp[i][j]
mini = float("inf")
for k in range(i, j):
if self.isPalindrome(string[i : k + 1]):
temp_ans = 1 + self.solve(string, k + 1, j, dp)
if temp_ans < mini:
mini = temp_ans
dp[i][j] = mini
return dp[i][j] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR |
Given a string str, a partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome. Determine the fewest cuts needed for palindrome partitioning of the given string.
Example 1:
Input: str = "ababbbabbababa"
Output: 3
Explaination: After 3 partitioning substrings
are "a", "babbbab", "b", "ababa".
Example 2:
Input: str = "aaabba"
Output: 1
Explaination: The substrings after 1
partitioning are "aa" and "abba".
Your Task:
You do not need to read input or print anything, Your task is to complete the function palindromicPartition() which takes the string str as the input parameter and returns the minimum number of partitions required.
Expected Time Complexity: O(n*n) [n is the length of the string str]
Expected Auxiliary Space: O(n*n)
Constraints:
1 β€ length of str β€ 500 | import sys
class Solution:
def is_palindrome(self, strt, end, st):
while strt < end:
if st[strt] != st[end]:
return False
strt += 1
end -= 1
return True
def get_cuts(self, i, j, string, dp):
if i >= j:
return 0
if self.is_palindrome(i, j, string):
return 0
if dp[i][j] != -1:
return dp[i][j]
ans = sys.maxsize
curr_min = sys.maxsize
for k in range(i, j):
if self.is_palindrome(i, k, string):
curr_min = 1 + self.get_cuts(k + 1, j, string, dp)
ans = min(curr_min, ans)
dp[i][j] = ans
return ans
def palindromicPartition(self, string):
dp = [[(-1) for x in range(len(string) + 1)] for y in range(len(string) + 1)]
return self.get_cuts(0, len(string) - 1, string, dp) | IMPORT CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | T = int(input())
for t in range(T):
n = int(input())
cc = [int(x) for x in input().split()]
aa = [int(x) for x in input().split()]
bb = [int(x) for x in input().split()]
state = "OPEN"
cur_size = 0
max_size = 0
for i in range(n - 1):
c = cc[i]
a = aa[i + 1]
b = bb[i + 1]
if state == "OPEN":
cur_size += abs(a - b) + 2
state = "CONTINUE"
elif state == "CONTINUE":
force_end_size = cur_size + c - 1
if force_end_size > max_size:
max_size = force_end_size
cur_size += c - abs(a - b) - 1
if a == b:
if max_size < cur_size:
max_size = cur_size
cur_size = 2
else:
if cur_size < abs(a - b):
cur_size = abs(a - b)
cur_size += 2
cur_size += cc[-1] - 1
if max_size < cur_size:
max_size = cur_size
print(max_size) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for i in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
max_support = c[n - 1] + 1
max_length = []
for i in range(1, n)[::-1]:
small, big = [a[i], b[i]] if a[i] < b[i] else [b[i], a[i]]
if small == big:
max_length.append(max_support)
max_support = c[i - 1] + 1
else:
max_length.append(max_support + big - small)
max_support = max(c[i - 1] + 1 - big + small + max_support, c[i - 1] + 1)
print(max(max_length)) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR LIST VAR VAR VAR VAR LIST VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def length_cycle(n, a, b, c):
longest_len = 0
portion_of_prev_cycle_len = 0
for i in range(1, n):
curr_chain_length = c[i] - 1
upper_node = min(a[i], b[i])
lower_node = max(a[i], b[i])
portion_of_prev_chain_len = lower_node - upper_node
cycle_formed = curr_chain_length + portion_of_prev_chain_len + 2
if a[i] != b[i]:
cycle_formed_with_prev_part = (
portion_of_prev_cycle_len
- portion_of_prev_chain_len
+ curr_chain_length
+ 2
)
cycle_formed = max(cycle_formed, cycle_formed_with_prev_part)
portion_of_prev_cycle_len = cycle_formed
longest_len = max(longest_len, cycle_formed)
return longest_len
t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(length_cycle(n, a, b, c)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def main():
T = eval(input())
for _ in range(T):
N = eval(input())
L = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0] * N
for i in range(1, N):
if a[i] > b[i]:
a[i], b[i] = b[i], a[i]
if b[i] == a[i]:
dp[i] = L[i] + b[i] - a[i] + 1
else:
dp[i] = L[i] + b[i] - a[i] + 1
dp[i] = max(dp[i], dp[i - 1] + L[i] - (b[i] - a[i] - 1))
ans = max(dp)
print(ans)
main() | FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for i in range(t):
n = int(input())
C = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
longestChainLength = 0
currentChainLength = 0
for j in range(1, n):
if j == 1 or A[j] == B[j]:
currentChainLength = abs(A[j] - B[j]) + 1
else:
currentChainLength = max(
abs(A[j] - B[j]) + 1,
currentChainLength + C[j - 1] - abs(A[j] - B[j]) + 1,
)
longestChainLength = max(C[j] + currentChainLength, longestChainLength)
print(longestChainLength) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | k = int(input())
while k:
k -= 1
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans, t = 0, 0
for i in range(1, n):
x = abs(a[i] - b[i])
if x == 0:
t = c[i] + 1
else:
t = max(x, t - x) + c[i] + 1
ans = max(ans, t)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0] * n
dp[0] = abs(b[1] - a[1])
ans = 0
for i in range(1, n):
if i < n - 1:
ai = a[i + 1]
bi = b[i + 1]
ans = max(ans, dp[i - 1] + 1 + c[i])
if i == n - 1:
break
dp[i] = abs(bi - ai)
if ai != bi:
dp[i] = max(dp[i], dp[i - 1] + 2 + min(ai, bi) - 1 + c[i] - max(ai, bi))
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
initialSpan = c[1] + abs(b[1] - a[1]) + 1
maxSpan = initialSpan
for i in range(2, n):
if b[i] != a[i]:
initialSpan += c[i]
initialSpan -= abs(b[i] - a[i]) - 1
secSpan = c[i] + abs(b[i] - a[i]) + 1
if secSpan > initialSpan:
maxSpan = max(maxSpan, initialSpan)
maxSpan = max(maxSpan, secSpan)
initialSpan = secSpan
else:
initialSpan = c[i] + 1
maxSpan = max(maxSpan, initialSpan)
maxSpan = max(maxSpan, initialSpan)
print(maxSpan) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for t in range(int(input())):
n = int(input())
c = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
prev = [(0) for i in range(n)]
best2 = 0
i = 1
x = b[i] - a[i]
if x > 0:
temp = x + 1
prev[i] = temp
elif x == 0:
prev[i] = 1
else:
temp = -x + 1
prev[i] = temp
best2 = max(prev[i] + c[i], best2)
for i in range(2, n):
x = b[i] - a[i]
if x > 0:
temp = max(x + 1, prev[i - 1] + c[i - 1] - x + 1)
prev[i] = temp
elif x == 0:
prev[i] = 1
else:
temp = max(-x + 1, prev[i - 1] + c[i - 1] + x + 1)
prev[i] = temp
best2 = max(prev[i] + c[i], best2)
print(best2) | 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
lengthOfChains = [int(x) for x in input().split()]
prevA = [int(x) for x in input().split()]
prevB = [int(x) for x in input().split()]
result = 0
longestChainLength = 0
answer = 0
for i in range(1, n):
diff = abs(prevA[i] - prevB[i])
if diff == 0:
longestChainLength = lengthOfChains[i] - 1 + diff + 2
else:
longestChainLength = (
lengthOfChains[i] - 1 + 2 + max(diff, longestChainLength - diff)
)
answer = max(answer, longestChainLength)
print(answer) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for t in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
s = 0
for i in range(1, n):
x = c[i] + abs(b[i] - a[i]) + 1
if a[i] != b[i] and i != 1:
s += c[i] - abs(b[i] - a[i]) + 1
if a[i] == b[i]:
s = 1
s = max(x, s)
if s > ans:
ans = s
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
inf = sys.stdin
input = inf.readline
def read_one_int():
return int(input().rstrip("\n"))
def read_list_of_ints():
res = [int(val) for val in input().rstrip("\n").split(" ")]
return res
def check_seq(cnt, lengths, edges1, edges2):
f_i = []
res = 0
for i in range(1, cnt):
v1 = min(edges1[i], edges2[i])
v2 = max(edges1[i], edges2[i])
if not f_i:
f_i.append(v2 - v1 + 1)
continue
if v1 == v2:
f_i.append(1)
continue
f_i.append(max(v2 - v1 + 1, v1 + lengths[i - 1] + -v2 + 1 + f_i[-1]))
for j in range(cnt - 1):
res = max(res, lengths[j + 1] + f_i[j])
return res
def main():
samples = read_one_int()
res = ""
for _ in range(samples):
chains_cnt = read_one_int()
chains_l = read_list_of_ints()
edges1 = read_list_of_ints()
edges2 = read_list_of_ints()
res = check_seq(chains_cnt, chains_l, edges1, edges2)
print(res)
main() | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER 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 RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.buffer.readline
ans = []
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
tmp = c[-1] - 1
pos_x, pos_y = 1, c[-1]
for i in range(n - 1, 0, -1):
tmp += 2
pos_x, pos_y = min(a[i], b[i]), max(a[i], b[i])
res = max(res, tmp + pos_y - pos_x)
if pos_x == pos_y:
tmp = c[i - 1] - 1
pos_x, pos_y = 1, c[i - 1]
else:
tmp += pos_x - 1 + c[i - 1] - pos_y
if c[i - 1] - 1 >= tmp:
tmp = c[i - 1] - 1
pos_x, pos_y = 1, c[i - 1]
ans.append(res)
print(*ans, sep="\n") | IMPORT ASSIGN VAR VAR ASSIGN VAR LIST 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = 0
cnt = [0] * n
curr = 0
for i in range(1, n):
if a[i] == b[i]:
curr = 2
ans = max(ans, curr + c[i] - 1)
elif curr == 0:
curr += abs(a[i] - b[i]) + 2
ans = max(ans, curr + c[i] - 1)
else:
curr += min(a[i], b[i]) - 1 + c[i - 1] - max(a[i], b[i]) + 2
curr = max(curr, abs(a[i] - b[i]) + 2)
ans = max(ans, curr + c[i] - 1)
ans = max(ans, curr + c[-1] - 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | a = int(input())
for i in range(a):
s = int(input())
z = list(map(int, input().split()))
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
maxa = 0
leng = 0
for i in range(1, len(a1)):
if i == 1:
maxa = max(maxa, z[i] - 1 + abs(a1[i] - a2[i]) + 2)
if i < len(z) - 1:
redun = abs(a1[i + 1] - a2[i + 1])
else:
redun = 0
if i < len(z) - 1 and a1[i + 1] == a2[i + 1]:
leng = 0
continue
else:
leng = z[i] - 1 - redun + abs(a1[i] - a2[i]) + 2
if i < len(z) - 1:
leng = max(leng, abs(a1[i + 1] - a2[i + 1]))
else:
if i < len(z) - 1:
redun = abs(a1[i + 1] - a2[i + 1])
else:
redun = 0
maxa = max(maxa, leng + z[i] - 1 + 2)
if i < len(z) - 1 and a1[i + 1] == a2[i + 1]:
leng = 0
continue
elif i < len(z) - 1 and a1[i + 1] != a2[i + 1]:
leng += 1 + z[i] - abs(a1[i + 1] - a2[i + 1])
leng = max(leng, abs(a1[i + 1] - a2[i + 1]))
else:
break
print(maxa) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, l, a, b, best, last, curr = (
int(input()),
[int(i) for i in input().split()],
[int(i) for i in input().split()],
[int(i) for i in input().split()],
0,
0,
0,
)
for i in range(1, n):
curr = 1 + l[i] + abs(a[i] - b[i])
if a[i] != b[i]:
curr = max(curr, 1 + l[i] + last - abs(a[i] - b[i]))
best = max(best, curr)
last = curr
print(best) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
r = sys.stdin.readline
for _ in range(int(r())):
N = int(r())
C = list(map(int, r().split()))
A = list(map(int, r().split()))
B = list(map(int, r().split()))
S = 0
ans = 0
for i in range(1, N):
if i == 1:
S = abs(A[i] - B[i]) + 2 + (C[i] - 1)
elif A[i] == B[i]:
S = 2 + (C[i] - 1)
elif S < 2 * abs(A[i] - B[i]):
S = abs(A[i] - B[i]) + 2 + (C[i] - 1)
else:
S += 2 - abs(A[i] - B[i]) + (C[i] - 1)
if S > ans:
ans = S
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def read_input():
n = int(input())
chains = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
return chains, a, b
def solve(chains, a, b):
best_dp = [-1]
if a[1] == b[1]:
best_dp.append(chains[1] - 1 + 2)
else:
best_dp.append(chains[1] - 1 + 2 + abs(a[1] - b[1]))
best = best_dp[-1]
for i in range(2, len(chains)):
if a[i] == b[i]:
best_dp.append(chains[i] - 1 + 2)
else:
diff = abs(a[i] - b[i])
best_dp.append(chains[i] - 1 + 2 + max(diff, best_dp[i - 1] - diff))
if best_dp[-1] > best:
best = best_dp[-1]
return best
ans = []
t = int(input())
for _ in range(t):
chains, a, b = read_input()
ans.append(solve(chains, a, b))
print(solve(chains, a, b)) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
MOD = 10**9 + 7
INF = float("inf")
T = int(input())
Ns = []
Cs = []
As = []
Bs = []
for _ in range(T):
N = int(input())
C = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Ns.append(N)
Cs.append(C)
As.append(A)
Bs.append(B)
for i in range(T):
N = Ns[i]
C = Cs[i]
A = As[i]
B = Bs[i]
res = 0
t = abs(A[1] - B[1])
for j in range(1, N):
tend = t + 2 + C[j] - 1
res = max(res, tend)
if j < N - 1:
M = max(A[j + 1], B[j + 1])
m = min(A[j + 1], B[j + 1])
if M == m:
t = 0
else:
t = max(t + 2 + C[j] - M + m - 1, M - m)
print("{}".format(res)) | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
lengths = [0]
for ind in range(1, n):
if a[ind] == b[ind]:
lengths.append(c[ind] + 1)
else:
lengths.append(
c[ind]
+ 1
+ max(lengths[-1] - abs(a[ind] - b[ind]), abs(a[ind] - b[ind]))
)
print(max(lengths)) | 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
temp = 0
maxa = 0
for i in range(1, n):
if i == 1:
temp += abs(b[i] - a[i]) + 2
elif a[i] == b[i]:
temp += c[i - 1] - 1
if temp > maxa:
maxa = temp
temp = 2
else:
if temp + c[i - 1] - 1 > maxa:
maxa = temp + c[i - 1] - 1
if b[i] > a[i]:
temp += a[i] - 1 + c[i - 1] - b[i] + 2
else:
temp += b[i] - 1 + c[i - 1] - a[i] + 2
if temp < abs(b[i] - a[i]) + 2:
temp = abs(b[i] - a[i]) + 2
if i == n - 1:
temp += c[i] - 1
if temp > maxa:
maxa = temp
print(maxa) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = max(c[1:]) + 1
C = 2 + c[-1] - 1
for i in range(n - 1, 1, -1):
if a[i] == b[i]:
if ans < C:
ans = C
C = 2
C += a[i] - 1 + (c[i - 1] - a[i])
else:
if ans < C + abs(a[i] - b[i]):
ans = C + abs(a[i] - b[i])
C += min(a[i], b[i]) - 1 + (c[i - 1] - max(a[i], b[i]))
C = max(C, c[i - 1] - 1)
C += 2
C += abs(a[1] - b[1])
if C > ans:
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def find(chains, first, last, n, ans):
max_wt = 0
add = chains[-1] - 1
for i in range(n - 2, -1, -1):
a, b, c = first[i + 1], last[i + 1], chains[i + 1]
add += 2
max_wt = max(max_wt, add + abs(a - b))
if a == b:
add = 0
else:
a, b = min(a, b), max(a, b)
add += a - 1 + chains[i] - b
if chains[i] - 1 > add:
add = chains[i] - 1
return max_wt
def find2(chains, first, last, n, ans):
max_wt = 0
add = 0
for i in range(1, n):
a, b, c = first[i], last[i], chains[i]
if i == 1:
add += abs(a - b)
if a == b:
add = 0
else:
a, b = min(a, b), max(a, b)
add += a - 1 + chains[i - 1] - b
add += 2
max_wt = max(max_wt, add + c - 1)
if abs(a - b) > add:
add = abs(a - b)
return max_wt
def solve(chains, first, last, n, ans):
max_wt = find(chains, first, last, n, ans)
ans.append(str(max_wt))
def main():
t = int(input())
ans = []
for i in range(t):
n = int(input())
chains = list(map(int, input().split()))
first = list(map(int, input().split()))
last = list(map(int, input().split()))
solve(chains, first, last, n, ans)
print("\n".join(ans))
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
def longestSimpleCycle(n, c, a, b):
ans, tmp = 0, abs(a[1] - b[1])
for i in range(1, n):
if a[i] == b[i]:
tmp = 0
elif i != 1:
tmp -= c[i - 1] - 1
tmp += min(a[i], b[i]) - 1 + c[i - 1] - max(a[i], b[i])
if abs(a[i] - b[i]) > tmp:
tmp = abs(a[i] - b[i])
tmp += c[i] + 1
ans = max(ans, tmp)
return ans
def main():
for t in range(int(input().strip())):
n, c, a, b = (
int(input().strip()),
[int(i) for i in input().strip().split()],
[int(i) for i in input().strip().split()],
[int(i) for i in input().strip().split()],
)
print(longestSimpleCycle(n, c, a, b))
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
array = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
under = [(0) for i in range(n)]
over = [(0) for i in range(n)]
for i in range(1, n):
under[i - 1] = max(a[i], b[i]) - min(a[i], b[i]) + 1
over[i - 1] = min(a[i], b[i]) + array[i - 1] - max(a[i], b[i]) + 1
dp = [(0) for i in range(n + 1)]
i = n - 2
dp[n - 1] = array[-1]
maxi = 0
while i >= 0:
maxi = max(maxi, dp[i + 1] + under[i])
if a[i + 1] == b[i + 1]:
dp[i] = array[i]
else:
dp[i] = max(array[i], dp[i + 1] + over[i])
i -= 1
print(maxi) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
crr = list(map(int, input().split()))
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
for i in range(n):
if arr[i] > brr[i]:
arr[i], brr[i] = brr[i], arr[i]
ans = 0
maxi = 0
for i in range(1, n):
if i == 1:
maxi = brr[i] - arr[i] + 2
else:
maxi = max(maxi + arr[i] - 1 + crr[i - 1] - brr[i] + 2, brr[i] - arr[i] + 2)
if brr[i] - arr[i] == 0:
maxi = 2
ans = max(ans, maxi + crr[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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
n = int(input())
c = input().split()
ci = [int(i) for i in c]
a = input().split()
a = a[1:]
ai = [int(i) for i in a]
b = input().split()
b = b[1:]
bi = [int(i) for i in b]
dp = [[0, 0] for i in range(n)]
dp[n - 1][1] = ci[-1] - 1
for i in range(n - 2, -1, -1):
dp[i][0] = 2 + dp[i + 1][1]
if ai[i] != bi[i]:
dp[i][1] = max(ci[i] - 1, ci[i] - 1 - abs(ai[i] - bi[i]) + 2 + dp[i + 1][1])
else:
dp[i][1] = ci[i] - 1
maxa = 0
for i in range(n - 1):
maxa = max(maxa, dp[i][0] + abs(ai[i] - bi[i]))
print(maxa) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
C = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dp = [0] * n
dp[-1] = C[-1]
ans = 0
for i in range(n - 2, -1, -1):
ans = max(ans, abs(A[i + 1] - B[i + 1]) + 1 + dp[i + 1])
a, b = min(A[i + 1], B[i + 1]), max(A[i + 1], B[i + 1])
dp[i] = max(C[i], dp[i])
if a != b:
dp[i] = max(dp[i], a + C[i] - b + 1 + dp[i + 1])
return ans
for _ in range(int(input())):
print(solve()) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
def print(x, end="\n"):
sys.stdout.write(str(x) + end)
def get_int():
return int(input())
def list_in():
return list(map(int, input().split()))
def get_char_list():
s = input()
return list(s[: len(s) - 1])
def get_tuple_ints():
return tuple(map(int, input().split()))
def print_iterable(p):
print(" ".join(map(str, p)))
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def main():
t = get_int()
for i in range(t):
n = get_int()
c = list_in()
a = list_in()
b = list_in()
len = [(0) for i in range(n)]
for i in range(1, n):
d = abs(b[i] - a[i])
len[i] = c[i] + 1 + int(d != 0) * max(d, len[i - 1] - d)
print(max(len))
pass
main() | IMPORT ASSIGN VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def sol(c, a, b):
ans = abs(a[1] - b[1])
ans_ = []
ans_1 = [abs(a[1] - b[1])]
for i in range(1, len(c)):
if i < n - 1 and a[i + 1] != b[i + 1]:
ans_1.append(ans + 2 + c[i] - 1)
ans = max(
ans + 2 + c[i] - 1 - abs(a[i + 1] - b[i + 1]), abs(a[i + 1] - b[i + 1])
)
elif i < n - 1:
ans_.append(ans + 2 + c[i] - 1)
ans_1.append(ans + 2 + c[i] - 1)
ans = 0
else:
ans += c[n - 1] + 1
m = 0
if len(ans_) > 0:
m = max(ans_)
return max(ans, m, max(ans_1))
for _ in range(int(input())):
n = int(input())
c = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
k = sol(c, a, b)
print(k) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for z in range(t):
n = int(input())
l = input().split(" ")
c = []
for val in l:
c.append(int(val))
l = input().split(" ")
a = []
for val in l:
a.append(int(val))
l = input().split(" ")
b = []
for val in l:
b.append(int(val))
for i in range(n):
if a[i] > b[i]:
x = a[i]
a[i] = b[i]
b[i] = x
dp = []
for i in range(n):
dp.append(0)
dp[n - 1] = c[n - 1]
best = 0
l = []
for i in range(n - 1):
l.append(i)
l.reverse()
for i in l:
dp[i] = c[i]
if a[i + 1] != b[i + 1]:
dp[i] = max(dp[i], dp[i + 1] + a[i + 1] + c[i] - b[i + 1] + 1)
best = max(best, dp[i + 1] + b[i + 1] - a[i + 1] + 1)
print(best) | 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 VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
b1 = 0
for i in range(len(c) - 1):
if i == 0:
b1 = max(b1, abs(b[i + 1] - a[i + 1]))
else:
b1 += c[i] + 1
ans = max(b1, ans)
a1 = abs(b[i + 1] - a[i + 1])
b1 -= a1
b1 = max(b1, a1)
if a1 == 0:
b1 = 0
b1 += c[-1] + 1
ans = max(ans, b1)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for t in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()] + [1]
b = [int(i) for i in input().split()] + [1]
current = abs(a[1] - b[1])
best = 0
for i in range(1, n):
current += 2 + c[i] - 1 - abs(a[i + 1] - b[i + 1])
best = max(best, current + abs(a[i + 1] - b[i + 1]))
current = max(current, abs(a[i + 1] - b[i + 1]))
if a[i + 1] == b[i + 1]:
current = 0
print(best) | 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 BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for i in range(t):
(n,) = map(int, input().strip().split(" "))
q = list(map(int, input().strip().split(" ")))
a = list(map(int, input().strip().split(" ")))
b = list(map(int, input().strip().split(" ")))
ans = 0
pre_ans = 0
for i in range(1, len(q)):
cyc1 = abs(a[i] - b[i]) + 1 + q[i]
if a[i] != b[i] and i != 1:
cyc2 = pre_ans - abs(a[i] - b[i]) + 1 + q[i]
else:
cyc2 = 0
pre_ans = max(cyc1, cyc2)
ans = max(ans, pre_ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
t = int(input())
input = sys.stdin.readline
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
lstlen = 0
ans = 0
for i in range(1, n):
curllen = c[i] + 1 + abs(a[i] - b[i])
if a[i] != b[i]:
curllen = max(curllen, c[i] + 1 + lstlen - abs(a[i] - b[i]))
ans = max(ans, curllen)
lstlen = curllen
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
lis = []
lis.append(0)
lis.append(1 + c[1] + abs(b[1] - a[1]))
for i in range(n - 2):
if a[i + 2] != b[i + 2]:
aa = abs(a[i + 2] - b[i + 2]) + c[i + 2] + 1
ab = c[i + 2] + lis[i + 1] - abs(a[i + 2] - b[i + 2]) + 1
lis.append(max(aa, ab))
else:
aa = abs(a[i + 2] - b[i + 2]) + c[i + 2] + 1
lis.append(aa)
print(max(lis)) | 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | a = int(input())
for x in range(a):
b = int(input())
c = list(map(int, input().split()))
d = list(map(int, input().split()))
e = list(map(int, input().split()))
dp = [c[1] + 1 + abs(d[1] - e[1])]
for y in range(1, b - 1):
if abs(d[y + 1] - e[y + 1]) == 0:
dp.append(c[y + 1] + 1)
else:
dp.append(
max(
c[y + 1] + 1 + dp[y - 1] - abs(d[y + 1] - e[y + 1]),
c[y + 1] + 1 + abs(d[y + 1] - e[y + 1]),
)
)
print(max(dp)) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
maxl = 0
prc = 0
prevc = 0
for i in range(1, n):
cc, ca, cb = c[i], a[i], b[i]
if ca == cb:
prc = 1
elif prc > 0:
prc += min(ca, cb) - 1 + (prevc - max(ca, cb))
pv = abs(ca - cb) + 1
if pv > prc:
prc = pv
cl = prc + cc
prc += 2
maxl = max(maxl, cl)
prevc = cc
print(maxl) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
lst_ans = [0]
for i in range(1, n):
x = c[i] + 1
if a[i] == b[i]:
lst_ans.append(x)
continue
x += max(abs(b[i] - a[i]), lst_ans[i - 1] - abs(a[i] - b[i]))
lst_ans.append(x)
print(max(lst_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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def simple_cycle(n, a, b, c):
maxi = 0
if a[1] == b[1]:
s = 1 + c[1]
maxi = s
else:
s = abs(b[1] - a[1]) + 1 + c[1]
maxi = s
for i in range(2, n):
if b[i] == a[i]:
s = 1 + c[i]
else:
s = max(s - abs(b[i] - a[i]) + 1 + c[i], abs(b[i] - a[i]) + 1 + c[i])
if maxi < s:
maxi = s
return maxi
t = int(input())
for i in range(0, t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(simple_cycle(n, a, b, c)) | FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
C = [int(t) for t in input().split()]
A = [int(t) for t in input().split()]
B = [int(t) for t in input().split()]
ans = 0
cur = 0
for i in range(1, n):
if A[i] != B[i]:
cur = max(1 + C[i] + abs(A[i] - B[i]), cur + 1 + C[i] - abs(A[i] - B[i]))
else:
cur = 1 + C[i]
ans = max(ans, cur)
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
c = l[n - 1]
for i in range(n - 1, 0, -1):
if a[i] > b[i]:
a[i], b[i] = b[i], a[i]
if i == 1:
c += b[i] - a[i] + 1
ans = max(ans, c, c - b[i] + a[i] - 1 + b[i] - a[i] + 1)
elif a[i] == b[i]:
c += 1
ans = max(ans, c)
else:
c += a[i] + l[i - 1] - b[i] + 1
ans = max(ans, c, c - a[i] - l[i - 1] + b[i] - 1 + b[i] - a[i] + 1)
if a[i] == b[i]:
c = l[i - 1]
else:
c = max(l[i - 1], c)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for y in range(int(input())):
n = int(input())
lst = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
maxx = lst[-1]
cur = lst[-1]
for i in range(n):
if b[i] < a[i]:
a[i], b[i] = b[i], a[i]
for i in range(n - 1, 0, -1):
if a[i] == b[i]:
maxx = max(cur + 1, maxx)
cur = lst[i - 1]
else:
maxx = max(maxx, cur + b[i] - a[i] + 1)
cur = max(cur + a[i] + lst[i - 1] - b[i] + 1, lst[i - 1])
print(maxx) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | T = int(input())
for t in range(T):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dp = [(0) for i in range(n)]
for i in range(1, n):
if a[i] == b[i]:
dp[i] = c[i] + 1 + abs(a[i] - b[i])
else:
dp[i] = max(
dp[i - 1] + c[i] + 1 - abs(a[i] - b[i]), c[i] + 1 + abs(a[i] - b[i])
)
print(max(dp)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for t in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = []
for i in range(1, len(b)):
if i == 1:
ans.append(2 + abs(b[i] - a[i]) + (c[i] - 1))
elif b[i] == a[i]:
ans.append(2 + c[i] - 1)
else:
ans.append(
max(
2 + c[i] - 1 + ans[-1] - abs(b[i] - a[i]),
2 + abs(b[i] - a[i]) + (c[i] - 1),
)
)
print(max(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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n = int(input())
C = list(map(int, input().split()))[::-1]
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
DP = [0] * n
DP[0] = C[0] + 1
for i in range(1, n - 1):
x = abs(A[i - 1] - B[i - 1])
if not x:
DP[i] = C[i] + 1
else:
DP[i] = max(DP[i - 1] + C[i] + 1 - x, C[i] + 1)
for i in range(n - 1):
DP[i] += abs(A[i] - B[i])
ans = max(DP)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
len = [(0) for i in range(n)]
for i in range(1, n):
d = abs(b[i] - a[i])
len[i] = c[i] + 1 + int(d != 0) * max(d, len[i - 1] - d)
print(max(len)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
def main():
t = int(input())
allAns = []
for _ in range(t):
n = int(input())
c = readIntArr()
a = readIntArr()[1:]
b = readIntArr()[1:]
maxOpenLen = [(0) for _ in range(n - 1)]
maxOpenLen[0] = abs(a[0] - b[0])
ans = 0
for i in range(1, n - 1):
ans = max(ans, c[i] - 1 + maxOpenLen[i - 1] + 2)
aa, bb = min(a[i], b[i]), max(a[i], b[i])
if aa < bb:
maxOpenLen[i] = maxOpenLen[i - 1] + (c[i] - bb) + (aa - 1) + 2
maxOpenLen[i] = max(maxOpenLen[i], bb - aa)
ans = max(ans, maxOpenLen[-1] + c[-1] - 1 + 2)
allAns.append(ans)
multiLineArrayPrint(allAns)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main() | IMPORT FUNC_DEF 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 ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input().strip())
for i in range(t):
n = int(input().strip())
c = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
ans = c[1] + abs(a[1] - b[1]) + 1
cur = ans
for i in range(2, n):
if a[i] == b[i]:
cur = c[i] + 1
else:
cur += c[i] - abs(a[i] - b[i]) + 1
cur = max(cur, c[i] + abs(a[i] - b[i]) + 1)
ans = max(ans, cur)
print(ans) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
def solution(n, c, a, b):
intervals = [[min(ai, bi), max(ai, bi)] for ai, bi in zip(a, b)]
branches = [(0) for _ in range(n)]
sol = 0
for i in range(1, n):
if intervals[i][1] == intervals[i][0]:
branches[i] = 1
elif i == 1:
branches[i] = intervals[i][1] - intervals[i][0] + 1
else:
val1 = intervals[i][1] - intervals[i][0] + 1
val2 = intervals[i][0] + c[i - 1] - intervals[i][1] + 1 + branches[i - 1]
branches[i] = max(val1, val2)
sol = max(branches[i] + c[i] for i in range(1, n))
print(sol)
T = int(input())
for t in range(T):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
solution(n, c, a, b) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def solution():
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
currentLen = 0
maxLen = 0
for i in range(n):
if i == 0:
currentLen += abs(max(a[i + 1], b[i + 1]) - min(a[i + 1], b[i + 1]))
if i > 0 and i < n - 1:
currentLen += 2
if a[i + 1] == b[i + 1]:
currentLen += c[i] - 1
maxLen = max(currentLen, maxLen)
currentLen = 0
else:
currentLen1 = currentLen + (c[i] - 1)
currentLen2 = max(a[i + 1], b[i + 1]) - min(a[i + 1], b[i + 1])
currentLen = (
currentLen
+ min(a[i + 1], b[i + 1])
- 1
+ c[i]
- max(a[i + 1], b[i + 1])
)
currentLen = max(currentLen, currentLen2)
maxLen = max(currentLen1, maxLen)
if i == n - 1:
currentLen += 2
currentLen += c[i] - 1
maxLen = max(currentLen, maxLen)
print(maxLen)
T = int(input())
while T:
solution()
T -= 1 | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def main():
t = int(input())
for i in range(t):
solve()
def solve():
n = int(input())
chains = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = []
for i in range(1, len(chains)):
cycle = abs(b[i] - a[i]) + chains[i] + 1
if i != 1 and b[i] != a[i]:
dp.append(max(dp[i - 2] + cycle - abs(b[i] - a[i]) * 2, cycle))
else:
dp.append(cycle)
print(max(dp))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def solve():
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
prev = c[1] + abs(b[1] - a[1]) + 1
ans = prev
for i in range(2, n):
if a[i] == b[i]:
prev = c[i] + 1
else:
diff = max(0, abs(b[i] - a[i]) - 1)
prev = c[i] + max(diff + 2, prev - diff)
ans = max(prev, ans)
print(ans)
for _ 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = sys.stdin.readline
for nt in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(c[-1] // 2)
b.append(c[-1] // 2)
maxx = 0
curr2 = 0
for i in range(1, n):
curr = c[i] + 1 + abs(a[i] - b[i])
if a[i] != b[i]:
curr = max(curr, c[i] + 1 + curr2 - abs(a[i] - b[i]))
maxx = max(maxx, curr)
curr2 = curr
print(maxx) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
while t:
t -= 1
n = int(input())
arr = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
maxi = 0
prev = 0
for i in range(1, n):
if a[i] == b[i]:
prev = abs(b[i] - a[i]) + 2 + arr[i] - 1
elif i == 1:
prev = 2 + abs(b[i] - a[i]) + arr[i] - 1
else:
a1 = prev + 2 - abs(b[i] - a[i]) + arr[i] - 1
a2 = abs(b[i] - a[i]) + 2 + arr[i] - 1
prev = max(a1, a2)
maxi = max(maxi, prev)
print(maxi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR 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 |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def solve(n, c, a, b):
dpr = [0] * n
dpr[-1] = c[-1] - 1
for i in range(n - 2, -1, -1):
stay = c[i] - 1
if a[i + 1] > b[i + 1]:
a[i + 1], b[i + 1] = b[i + 1], a[i + 1]
if a[i + 1] == b[i + 1]:
nxt = 0
else:
nxt = a[i + 1] - 1 + c[i] - b[i + 1] + 2 + dpr[i + 1]
dpr[i] = max(nxt, stay)
M = 0
for i in range(n - 1):
len_cycle = b[i + 1] - a[i + 1] + 2 + dpr[i + 1]
M = max(M, len_cycle)
return M
def main():
t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(solve(n, c, a, b))
main() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [(0) for _ in range(n)]
for i in range(1, n):
if a[i] != b[i]:
dp[i] = max(
dp[i - 1] - abs(a[i] - b[i]) + 2 + l[i] - 1,
abs(a[i] - b[i]) + 2 + l[i] - 1,
)
else:
dp[i] = 2 + l[i] - 1
print(max(dp)) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for tc in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = [0]
for i in range(1, n):
if a[i] == b[i]:
k.append(2 + c[i] - 1)
else:
n1 = abs(a[i] - b[i]) + 2 + c[i] - 1
n2 = k[i - 1] - abs(a[i] - b[i]) + 2 + c[i] - 1
k.append(max(n1, n2))
print(max(k)) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | number_of_testcases = int(input())
for _ in range(number_of_testcases):
chain_length = int(input())
q = [0] * chain_length
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(chain_length):
if i > 0:
q[i] = max(
c[i] + 1 + abs(a[i] - b[i]),
c[i] + 1 + q[i - 1] - abs(a[i] - b[i]) if a[i] != b[i] else 0,
)
print(max(q)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
maxi = 0
cnt = c[n - 1]
for i in range(n - 1, 0, -1):
if a[i] > b[i]:
a[i], b[i] = b[i], a[i]
if a[i] == b[i]:
cnt += 1
maxi = max(maxi, cnt)
cnt = c[i - 1]
elif i == 1:
cnt += b[i] - a[i] + 1
maxi = max(maxi, cnt)
else:
cnt1 = cnt
cnt1 += b[i] - a[i] + 1
cnt += a[i] + (c[i - 1] - b[i] + 1)
maxi = max(maxi, cnt)
maxi = max(maxi, cnt1)
if cnt < c[i - 1]:
cnt = c[i - 1]
print(maxi) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def main():
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if n == 1:
print(0)
continue
out = 0
l = r = 0
len_l = abs(a[1] - b[1])
side = 0
while r < n - 1:
r += 1
if r < n - 1:
len_r = abs(a[r + 1] - b[r + 1])
side += c[r] - len_r + 1
now = side + len_r + len_l
out = max(out, now)
if len_r == 0 or len_r > side + len_l:
len_l = len_r
side = 0
else:
len_r = c[r] - 1
now = side + 2 + len_r + len_l
out = max(out, now)
print(out)
main() | FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | T = int(input())
r = 1
def getnext(index, diff, chain, dp, ans):
if dp[index] >= 0:
return dp[index]
if diff[index] > 0:
num1 = chain[index] - diff[index] + 1 + getnext(index + 1, diff, chain, dp, ans)
num2 = chain[index]
dp[index] = max(num1, num2)
else:
dp[index] = chain[index]
newnum = diff[index] + 1 + getnext(index + 1, diff, chain, dp, ans)
ans[0] = max(ans[0], newnum)
return dp[index]
while r <= T:
n = int(input())
chain = list(map(int, input().split()))
front = list(map(int, input().split()))
rear = list(map(int, input().split()))
diff = []
for i in range(1, n):
diff.append(abs(front[i] - rear[i]))
dp = [-1] * n
dp[-1] = chain[-1]
ans = 0
for index in range(n - 2, -1, -1):
if diff[index] > 0:
num1 = (
chain[index]
- diff[index]
+ 1
+ getnext(index + 1, diff, chain, dp, ans)
)
num2 = chain[index]
dp[index] = max(num1, num2)
else:
dp[index] = chain[index]
newnum = diff[index] + 1 + dp[index + 1]
ans = max(ans, newnum)
print(ans)
r += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR WHILE 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | from sys import stdin, stdout
input = stdin.readline
def output(answer):
stdout.write(str(answer) + "\n")
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
curr = None
mx = 0
for index in range(1, n):
if a[index] == b[index]:
curr = 2
elif curr == None:
dist = abs(a[index] - b[index])
curr = dist + 2
else:
dist1 = abs(max(a[index], b[index]) - c[index - 1])
dist2 = abs(min(a[index], b[index]) - 1)
spc = abs(a[index] - b[index]) + 2
dist = dist1 + dist2
curr += dist + 2
curr = max(curr, spc)
mx = max(mx, curr + c[index] - 1)
output(mx) | ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ul = list(map(int, input().split()))
ll = list(map(int, input().split()))
ck = abs(ul[1] - ll[1]) + 2
ans = ck + a[1] - 1
for i in range(2, n):
if ul[i] == ll[i]:
ans = max(ans, ck + a[i - 1] - 1, a[i] + 1)
ck = 2
else:
ck = max(abs(ul[i] - ll[i]), ck + a[i - 1] - 1 - abs(ul[i] - ll[i])) + 2
ans = max(ans, ck + a[i] - 1)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def longest(size, up, low):
ans = abs(low[1] - up[1]) + 1 + size[1]
curr = abs(up[1] - low[1])
for i in range(1, len(size) - 1):
ans = max(ans, curr + size[i] + 1)
curr += size[i] + 1 - abs(up[i + 1] - low[i + 1])
if up[i + 1] == low[i + 1]:
ans = max(ans, curr)
curr = 0
elif abs(up[i + 1] - low[i + 1]) > curr:
curr = abs(up[i + 1] - low[i + 1])
curr += 1 + size[-1]
return max(ans, curr)
for i in range(int(input())):
a = input()
lst1 = list(map(int, input().strip().split()))
lst2 = list(map(int, input().strip().split()))
lst3 = list(map(int, input().strip().split()))
print(longest(lst1, lst2, lst3)) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input().strip())
for i in range(t):
n = int(input().strip())
c = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
c[0] = 0
s = 0
m = 0
z = 0
for k in range(1, n):
if b[k] < a[k]:
a[k], b[k] = b[k], a[k]
for k in range(1, n):
if a[k] != b[k]:
m = max(m + a[k] + c[k - 1] + 1 - b[k], b[k] - a[k] + 1)
s = max(c[k] + m, s)
else:
z = max(z, s)
s = 1 + c[k]
m = 1
print(max(z, s)) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | test = int(input())
for testcase in range(test):
n = int(input())
arr = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
area = arr[n - 1] - 1 + 2 + abs(a[n - 1] - b[n - 1])
ans = [area]
for i in range(n - 2, 0, -1):
if a[i + 1] == b[i + 1]:
ans.append(arr[i] - 1 + 2 + abs(a[i] - b[i]))
else:
ans.append(
max(
ans[-1]
+ arr[i]
- 1
+ 2
+ abs(a[i] - b[i])
- 2 * abs(a[i + 1] - b[i + 1]),
arr[i] - 1 + 2 + abs(a[i] - b[i]),
)
)
print(max(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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
if a[i] > b[i]:
a[i], b[i] = b[i], a[i]
ans = 0
cur = 0
for i in range(1, n):
if a[i] == b[i]:
cur = 0
cur = max(c[i] + abs(a[i] - b[i]) + 1, cur + c[i] - abs(a[i] - b[i]) + 1)
ans = max(ans, cur)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for w in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
ans = 0
for i in range(1, n):
if x != 0:
ans = max(x, ans, x + c[i - 1] - 1)
if x == 0:
x = 2 + abs(a[i] - b[i])
elif a[i] == b[i]:
x = 2
else:
x = max(x + c[i - 1] - 1 - abs(b[i] - a[i]) + 2, abs(b[i] - a[i]) + 2)
ans = max(ans, x)
x += c[-1] - 1
ans = max(ans, x)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | for tt in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
cnt = 0
cnt = c[n - 1] - 1
for i in range(n - 2, -1, -1):
if a[i + 1] != b[i + 1]:
cnt += 2
ans = max(ans, cnt + abs(a[i + 1] - b[i + 1]))
if a[i + 1] < b[i + 1]:
cnt += a[i + 1] - 1 + c[i] - b[i + 1]
else:
cnt += c[i] - a[i + 1] + b[i + 1] - 1
cnt = max(cnt, c[i] - 1)
else:
ans = max(cnt + 2, ans)
cnt = c[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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def solve():
n = int(input())
c = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
current = 0
best = 0
for i in range(1, n):
if a[i] == b[i]:
current = 1 + c[i]
else:
diff = abs(a[i] - b[i])
current = max(diff, current - diff) + 1 + c[i]
best = max(current, best)
print(best)
t = int(input())
for test in range(t):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | t = int(input())
for _ in range(t):
n = int(input())
c = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
curr = abs(a[1] - b[1])
ans = curr
for i in range(1, n - 1):
ans = max(ans, curr + 1 + c[i])
curr += c[i] + 1 - abs(a[i + 1] - b[i + 1])
if a[i + 1] == b[i + 1]:
ans = max(ans, curr)
curr = 0
elif abs(a[i + 1] - b[i + 1]) > curr:
curr = abs(a[i + 1] - b[i + 1])
curr += 1 + c[-1]
ans = max(curr, ans)
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have $n$ chains, the $i$-th chain consists of $c_i$ vertices. Vertices in each chain are numbered independently from $1$ to $c_i$ along the chain. In other words, the $i$-th chain is the undirected graph with $c_i$ vertices and $(c_i - 1)$ edges connecting the $j$-th and the $(j + 1)$-th vertices for each $1 \le j < c_i$.
Now you decided to unite chains in one graph in the following way:
the first chain is skipped;
the $1$-st vertex of the $i$-th chain is connected by an edge with the $a_i$-th vertex of the $(i - 1)$-th chain;
the last ($c_i$-th) vertex of the $i$-th chain is connected by an edge with the $b_i$-th vertex of the $(i - 1)$-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$) β the number of chains you have.
The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($2 \le c_i \le 10^9$) β the number of vertices in the corresponding chains.
The third line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($a_1 = -1$; $1 \le a_i \le c_{i - 1}$).
The fourth line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($b_1 = -1$; $1 \le b_i \le c_{i - 1}$).
Both $a_1$ and $b_1$ are equal to $-1$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the length of the longest simple cycle.
-----Examples-----
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
-----Note-----
In the first test case, the longest simple cycle is shown below:
We can't increase it with the first chain, since in such case it won't be simple β the vertex $2$ on the second chain will break simplicity. | def cycle(n, vert, a, b):
idx = len(a) - 1
max_so_far = 0
chain_len = int(vert[idx]) - 1
while idx > 1:
chain_len += 2
max_so_far = max(max_so_far, chain_len + abs(int(a[idx]) - int(b[idx])))
if int(a[idx]) == int(b[idx]):
max_so_far = max(max_so_far, chain_len)
chain_len = int(vert[idx - 1]) - 1
else:
chain_len += int(vert[idx - 1]) - 1 - abs(int(a[idx]) - int(b[idx]))
if int(vert[idx - 1]) - 1 > chain_len:
chain_len = int(vert[idx - 1]) - 1
idx -= 1
chain_len += 2
chain_len += abs(int(a[idx]) - int(b[idx]))
max_so_far = max(max_so_far, chain_len)
return max_so_far
num_test_cases = int(input())
for i in range(num_test_cases):
n = int(input())
vert = input().split()
a = input().split()
b = input().split()
print(cycle(n, vert, a, b)) | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for i in range(t):
n = int(input())
b = list(map(int, input().split()))
j = 1
s = 0
while j < n:
s += max(b[j] - b[j - 1], 0)
j += 1
if b[-1] >= s:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
f_inf = float("inf")
mod = 10**9 + 7
def resolve():
t = int(input())
for _ in range(t):
n = int(input())
A = list(map(int, input().split()))
B = [(A[i + 1] - A[i]) for i in range(n - 1)]
plus = A[-1]
need_plus = 0
for i in range(n - 1):
if B[i] >= 0:
need_plus += B[i]
print("YES" if plus >= need_plus else "NO")
resolve() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF 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 VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for w in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
x = -1
p = -1
ans = 1
k = n + 1
for i in range(n - 1):
if a[i] >= a[i + 1]:
continue
else:
x = a[i]
p = i + 1
a[i + 1] -= x
break
for k in range(p + 1, n):
if a[k] >= a[k - 1] and a[k] - a[k - 1] <= x:
x = a[k] - a[k - 1]
a[k] = a[k - 1]
elif a[k] >= a[k - 1]:
a[k] -= x
elif a[k] < a[k - 1]:
break
for j in range(k - 1, n - 1, 1):
if a[j] <= a[j + 1]:
continue
else:
ans = 0
break
if ans == 1 or x == -1:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
R = lambda: map(int, input().split())
(t,) = R()
for _ in [0] * t:
R()
pre, sur = float("inf"), 0
for a in R():
if sur > a:
print("NO")
break
if pre + sur < a:
sur = a - pre
else:
pre = a - sur
else:
print("YES") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = a[0]
for i in range(1, n):
diff = a[i] - a[i - 1]
if diff < 0 and d >= 0:
d += diff
if d < 0:
print("NO")
else:
print("YES") | 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 FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
a, b = arr[0], 0
for i in range(1, n):
a = min(a, arr[i] - b)
b = arr[i] - a
if arr[n - 1] >= a >= 0:
print("YES")
else:
print("NO") | 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | def solve():
n = int(input().strip())
arr = [int(x) for x in input().strip().split()]
incr = [0] * n
decr = [0] * n
decr[0] = arr[0]
for i in range(1, n):
decr[i] = min(arr[i] - incr[i - 1], decr[i - 1])
incr[i] = arr[i] - decr[i]
for i in range(1, n):
if incr[i] < incr[i - 1]:
return "NO"
if incr[i] < 0:
return "NO"
for i in range(1, n):
if decr[i] > decr[i - 1]:
return "NO"
if decr[i] < 0:
return "NO"
return "YES"
for _ in range(int(input().strip())):
print(solve()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR 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 VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING IF VAR VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING IF VAR VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
def eprint(*args):
print(*args, file=sys.stderr)
zz = 1
if zz:
input = sys.stdin.readline
def li():
return [int(xx) for xx in input().split()]
def fi():
return int(input())
def mi():
return map(int, input().split())
t = fi()
while t > 0:
t -= 1
n = fi()
a = li()
pre = [10**18] * (n + 1)
suf = [10**18] * (n + 1)
for i in range(n):
pre[i] = min(pre[i - 1], a[i])
for i in range(n - 1, -1, -1):
suf[i] = min(suf[i + 1], a[i])
flag = 0
r = [0] * (n + 1)
l = [0] * (n + 1)
for i in range(n):
if pre[i - 1] + suf[i + 1] < a[i]:
flag = 1
break
for i in range(n):
c = max(0, a[i] - pre[i - 1])
l[i] = max(l[i - 1], c)
for i in range(n - 1, -1, -1):
d = max(0, a[i] - suf[i + 1])
r[i] = max(r[i + 1], d)
for i in range(n - 1, -1, -1):
suf[i] = min(suf[i + 1], a[i] - r[i])
for i in range(n):
pre[i] = min(pre[i - 1], a[i] - l[i])
if l[i] + r[i] > a[i] or pre[i - 1] + suf[i + 1] < a[i]:
flag = 1
break
print("YES" if flag == 0 else "NO") | IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR 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 ASSIGN 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 VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
ans = 0
a = list(map(int, input().split()))
for i in range(n - 1):
if a[i + 1] < a[i]:
ans += a[i] - a[i + 1]
if ans > a[0]:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
cur = a[0]
down = 0
for c in a[1:]:
if c - down < 0:
print("NO")
break
elif c - down <= cur:
cur = c - down
else:
down = c - cur
else:
print("YES") | 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 NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
n = int(input())
l = input().split()
li = [int(i) for i in l]
l = [0]
for i in range(1, n):
z = l[-1]
z = z + max(li[i] - li[i - 1], 0)
l.append(z)
poss = 1
for i in range(n):
if l[i] > li[i]:
poss = 0
break
if poss:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | n = int(input())
for _ in range(n):
num = input()
arr = [int(x) for x in input().split()]
count = 0
for index in range(1, len(arr)):
count += max(0, arr[index - 1] - arr[index])
res = arr[0] - count
if res >= 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
def prog():
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
decrease1 = 0
decrease2 = 0
for i in range(1, n):
decrease1 += max(0, a[i] - a[i - 1])
for i in range(n - 2, -1, -1):
decrease2 += max(0, a[i] - a[i + 1])
if a[n - 1] >= decrease1 or a[0] >= decrease2:
print("YES")
else:
print("NO")
prog() | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
dec, flag = 0, 1
for i in range(n - 1):
if a[i] < dec:
flag = 0
break
if a[i] < a[i + 1]:
dec += a[i + 1] - a[i]
if a[-1] < dec:
flag = 0
print("YES" if flag else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
You are given an array a of n positive integers.
You can use the following operation as many times as you like: select any integer 1 β€ k β€ n and do one of two things:
* decrement by one k of the first elements of the array.
* decrement by one k of the last elements of the array.
For example, if n=5 and a=[3,2,2,1,4], then you can apply one of the following operations to it (not all possible options are listed below):
* decrement from the first two elements of the array. After this operation a=[2, 1, 2, 1, 4];
* decrement from the last three elements of the array. After this operation a=[3, 2, 1, 0, 3];
* decrement from the first five elements of the array. After this operation a=[2, 1, 1, 0, 3];
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
Input
The first line contains one positive integer t (1 β€ t β€ 30000) β the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 β€ n β€ 30000) β the number of elements in the array.
The second line of each test case contains n integers a_1 β¦ a_n (1 β€ a_i β€ 10^6).
The sum of n over all test cases does not exceed 30000.
Output
For each test case, output on a separate line:
* YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations.
* NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
Example
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
fflg = 0
if t == 3000:
fflg = 1
cnt = 1
while t != 0:
t -= 1
n = int(input())
lst = input().split(" ")
for i in range(0, n, 1):
lst[i] = int(lst[i])
flg = 0
mini = 1000000000000000
psum = [(0) for i in range(0, n + 1, 1)]
for i in range(0, n - 1, 1):
mini = min(mini, lst[i])
if lst[i + 1] < lst[i]:
x = lst[i] - lst[i + 1]
psum[0] -= x
psum[i + 1] += x
cnt += 1
for i in range(1, n + 1, 1):
psum[i] += psum[i - 1]
for i in range(0, n, 1):
if lst[i] + psum[i] < 0:
flg = 1
if flg == 1:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.