description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 " " * int(input()): n, a = int(input()), [*map(int, input().split())] print("YNEOS"[sum(max(a[i] - a[i + 1], 0) for i in range(n - 1)) > a[0] :: 2])
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER
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**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.readline().rstrip() def SI(): return sys.stdin.readline().rstrip().decode() inf = 10**9 def solve(): l = inf r = 0 for a in aa: l = min(l, a - r) r = a - l if l < 0 or r < 0: return "NO" return "YES" for _ in range(II()): n = II() aa = LI() print(solve())
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR 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
def answer(): dec, m = 0, a[0] for i in range(1, n): if a[i - 1] < a[i]: dec += a[i] - a[i - 1] m = min(m, a[i] - dec) if m < 0: return "NO" return "YES" for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(answer())
FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN STRING RETURN 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 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
t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) x = 0 low = a[0] works = True for i in range(1, n): if a[i] < x: print("NO") works = False break next_low = low if a[i] - x < low: next_low = a[i] - x if a[i] - low > x: x = a[i] - low low = next_low if works: print("YES")
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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF 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
def solve(arr, n): diffarr = [0] * n count = 0 for i in range(n - 2, -1, -1): arr[i] -= count if arr[i] < 0: return 0 if arr[i] > arr[i + 1]: count += arr[i] - arr[i + 1] arr[i] = arr[i + 1] return 1 t = int(input()) for _ in range(t): n = int(input()) oriarr = [int(j) for j in input().split()] arr = oriarr[:] revarr = oriarr[::-1] if solve(arr, n) == 1 or solve(revarr, n) == 1: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR 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
def solve(): n = int(input()) a = list(map(int, input().split())) l = a[0] r = 0 for i in range(1, n): x = a[i] x -= r if x < 0: return "NO" if x > l: r += x - l else: l = x return "YES" t = int(input()) i = 0 while i < t: print(solve()) i += 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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER RETURN STRING IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
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.buffer.readline def solution(): n = int(input()) l = list(map(int, input().split())) pre = [0] * (n + 1) for i in range(1, n): if l[i] < l[i - 1]: pre[0] -= abs(l[i] - l[i - 1]) pre[i + 1] += abs(l[i] - l[i - 1]) for i in range(1, n + 1): pre[i] += pre[i - 1] l[i - 1] += pre[i] if l[0] < 0: print("NO") return for i in range(1, n): if l[i] < 0 or l[i] < l[i - 1]: print("NO") return print("YES") t = int(input()) for _ in range(t): solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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
def solve(n, arr): x = arr[0] - sum(max(arr[i - 1] - arr[i], 0) for i in range(1, n)) if x >= 0: return "YES" else: return "NO" t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 _ in range(t): n = int(input()) fl = 0 a = [int(x) for x in input().split()] mi = 0 ma = 10000000 for i in range(n): if a[i] < mi: fl = 1 break else: mi = max(mi, a[i] - ma) ma = min(ma, a[i] - mi) if fl: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR 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
t = int(input()) for t1 in range(0, t): n = int(input()) a = list(map(int, input().split())) last = 0 flag = 1 diff = [] for i in range(1, n): diff.append(a[i - 1] - a[i]) s = 0 for i in range(0, n - 1): s = s + max(0, a[i] - a[i + 1]) if s <= a[0]: print("YES") else: print("NO")
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER 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
import sys 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() c = 0 flag = 0 for i in range(n - 2, -1, -1): c = max(c, a[i] - a[i + 1]) a[i] -= c if a[i] < 0: flag = 1 print("YES" if flag == 0 else "NO")
IMPORT 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER 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
def rangeUpdate(t, node, tl, tr, l, r, val): if tl == l and tr == r: t[node][1] += val return mid = (tl + tr) // 2 if r <= mid: rangeUpdate(t, 2 * node, tl, mid, l, r, val) elif l > mid: rangeUpdate(t, 2 * node + 1, mid + 1, tr, l, r, val) else: rangeUpdate(t, 2 * node, tl, mid, l, mid, val) rangeUpdate(t, 2 * node + 1, mid + 1, tr, mid + 1, r, val) def pointQuery(t, node, tl, tr, index): if tl == tr: return t[node][0] + t[node][1] mid = (tl + tr) // 2 if index <= mid: return t[node][1] + pointQuery(t, 2 * node, tl, mid, index) return t[node][1] + pointQuery(t, 2 * node + 1, mid + 1, tr, index) def solve(arr, n, ans): t = [[(0) for j in range(2)] for i in range(4 * n + 1)] for i in range(n): rangeUpdate(t, 1, 0, n - 1, i, i, arr[i]) for i in range(1, n): curr = pointQuery(t, 1, 0, n - 1, i) left = pointQuery(t, 1, 0, n - 1, i - 1) if curr > left: diff = curr - left rangeUpdate(t, 1, 0, n - 1, i, n - 1, -diff) elif left > curr: diff = left - curr rangeUpdate(t, 1, 0, n - 1, 0, i - 1, -diff) for i in range(n): if pointQuery(t, 1, 0, n - 1, i) < 0: ans.append("NO") return ans.append("YES") def main(): t = int(input()) ans = [] for i in range(t): n = int(input()) arr = list(map(int, input().split())) solve(arr, n, ans) print("\n".join(ans)) main()
FUNC_DEF IF VAR VAR VAR VAR VAR VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING 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 EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR 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
def satyam(a, n): i = 0 sum = 0 t = a[0] while i < n - 1 and sum <= t: s = a[i] - a[i + 1] if s > 0: sum += s i += 1 if sum <= t: return "YES" return "NO" t = int(input()) for _ in range(t): n = int(input()) a = [int(p) for p in input().split()] print(satyam(a, n))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
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 _ in range(T): n = int(input()) a = [int(i) for i in input().split()] r = a[0] for i in range(1, n): if a[i - 1] > a[i]: r -= a[i - 1] - a[i] print("YES") if r >= 0 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR VAR NUMBER FUNC_CALL VAR STRING 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 fA(n, A): flag = 0 B = [(0) for i in range(n)] v = A[n - 1] for i in range(n - 2, -1, -1): v = min(A[i], v) B[i] = A[i] - v v = A[0] flag = 0 f1 = 0 vm = 0 for i in range(1, n): v1, v2 = A[i], B[i] if v1 <= v and f1 == 0: v = v1 elif v2 <= v: f1 = 1 v = min(v, v1 - vm) vm = max(vm, v1 - v) else: flag = 1 break return flag T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) flag = fA(n, A) if flag == 0: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR 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
for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] left = arr[0] right = 0 ans = True for i in range(1, len(arr)): if arr[i] < right: ans = False break rem = arr[i] - right left = min(left, rem) add = rem - left right += add if ans: 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR 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
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = "YES" if n <= 2: print("YES") else: l1 = [0] l2 = [l[0]] for j in range(1, n): if l[j] <= l[j - 1]: l1.append(l1[-1]) l2.append(l2[-1] - (l[j - 1] - l[j])) else: l1.append(l1[-1] + l[j] - l[j - 1]) l2.append(l2[-1]) for j in l2: if j < 0: ans = "NO" 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 STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL 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
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) ans = True i = n - 2 total = 0 while i > 0: if arr[i] <= arr[i + 1]: i -= 1 else: total += arr[i] - arr[i + 1] i -= 1 if arr[i] - total < 0: ans = False break if ans: 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER 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
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) mina = 10000000 for i in range(n - 1): if l[i] > l[i + 1]: diff = l[i] - l[i + 1] l[0] -= diff if min(l) >= 0: 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF FUNC_CALL 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
import sys (*data,) = map(int, sys.stdin.read().split()[::-1]) def inp(): return data.pop() def solve(arr): mn = arr[0] mx = 0 for i in range(n): mn = min(mn, arr[i] - mx) arr[i] -= mn mx = max(mx, arr[i]) return mn >= 0 ans = [] for _ in range(inp()): n = inp() arr = [inp() for _ in range(n)] ans.append("YES" if solve(arr) else "NO") print("\n".join(ans))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL STRING 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(" "))) x = 0 while x < n - 1 and a[x] >= a[x + 1]: x += 1 if x == n - 1: print("YES") continue else: onhold = a[x] chk = 0 x += 1 a[x] -= onhold for y in range(x + 1, n): if a[y - 1] > a[y]: chk = 1 break if a[y] - onhold < a[y - 1]: onhold = a[y] - a[y - 1] a[y] = a[y - 1] else: a[y] -= onhold if chk == 1: 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 STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR 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
T = int(input()) for _ in range(T): n, ls = int(input()), list(map(int, input().split())) left, right = [-1] * n, [-1] * n left[0] = 0 for u in range(1, n): if ls[u - 1] >= ls[u]: if ls[u] < left[u - 1]: break else: left[u] = left[u - 1] else: left[u] = left[u - 1] + (ls[u] - ls[u - 1]) right[n - 1] = ls[n - 1] for u in reversed(range(n - 1)): if ls[u] <= ls[u + 1]: right[u] = ls[u] else: break ok = 0 for u in range(1, n): if left[u - 1] == -1 or right[u] == -1: continue if left[u - 1] <= right[u]: ok = 1 break print("YES" if n == 1 or ok else "NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER 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
import sys input = sys.stdin.readline t = int(input()) for test in range(t): n = int(input()) a = list(map(int, input().split(" "))) temp = [] temp.append(a[0]) d = 0 for i in range(1, n): if a[i] > a[i - 1]: d = d + a[i] - a[i - 1] temp.append(a[i] - d) d = 0 for i in reversed(range(n - 1)): if a[i] > a[i + 1]: d = d + a[i] - a[i + 1] temp[i] = temp[i] - d if temp[0] < 0: print("NO") else: print("YES")
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER 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())) i = 1 while i < n: if A[i] > A[i - 1]: break else: i += 1 j = n - 1 while j > 0: if A[j] < A[j - 1]: break else: j -= 1 x = A[i - 1] y = 0 for k in range(i, j): if A[k] > x + y: y = A[k] - x else: x = A[k] - y if x >= 0 and y <= A[j]: 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 WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR 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
def solve(): n = int(input()) a = [0] + list(map(int, input().split())) v = -1 for i in range(1, n + 1): if i == 1: v = 0 else: v = max(a[i] - a[i - 1] + v, v) if a[i] < v: print("NO") return print("YES") return def main(): t = int(input()) for i in range(t): solve() return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN 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
def find_smallest_index(a): min_value = min(a) max_value = max(a) if min_value == max_value: return 0, True n = len(a) for i, v in enumerate(a): if i > 0 and a[i - 1] != min_value and a[i] == min_value: return i, False if i < n - 1 and a[i + 1] != min_value and a[i] == min_value: return i, False def can_make_zero(a): n = len(a) l = [0] * n r = [0] * n s = 0 for i in range(1, n): d = a[i] - a[i - 1] if d > 0: s += d l[i] = s s = 0 for i in range(n - 2, -1, -1): d = a[i + 1] - a[i] if d < 0: s -= d r[i] = s for i in range(n): if l[i] + r[i] > a[i]: return False return True def can_make_zero_old(a): n = len(a) while True: i, is_finished = find_smallest_index(a) if a[i] < 0: return False if is_finished: return True if i > 0: delta = a[i - 1] - a[i] for x in range(i): a[x] -= delta if i < n - 1: delta = a[i + 1] - a[i] for x in range(i + 1, n): a[x] -= delta n_samples = int(input()) for t in range(n_samples): n = int(input()) a = list(map(int, input().split(" "))) print("YES" if can_make_zero(a) else "NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR 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 STRING EXPR FUNC_CALL VAR 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
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [(0) for i in range(n)] c = [(0) for i in range(n)] b[0] = a[0] flag = 0 for i in range(1, n): if a[i] > a[i - 1]: c[i] = c[i - 1] + a[i] - a[i - 1] b[i] = b[i - 1] elif a[i] < a[i - 1]: b[i] = b[i - 1] + a[i] - a[i - 1] c[i] = c[i - 1] else: b[i] = b[i - 1] c[i] = c[i - 1] if b[i] < 0 or c[i] < 0: flag = -1 break if flag == -1: 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER 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.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] b = [(-1) for _ in range(n)] minSoFar = float("inf") maxLeftOver = 0 for i in range(n): b[i] = max(0, a[i] - minSoFar, maxLeftOver) b[i] = min(a[i], b[i]) minSoFar = min(minSoFar, a[i] - b[i]) maxLeftOver = max(maxLeftOver, b[i]) c = [(-1) for _ in range(n)] minSoFar = float("inf") for i in range(n - 1, -1, -1): minSoFar = min(minSoFar, b[i]) c[i] = max(0, b[i] - minSoFar) if set(c) == {0}: 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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF FUNC_CALL 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
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] s = sum([max(0, arr[i] - arr[i + 1]) for i in range(n - 1)]) if s <= arr[0]: print("YES") else: print("NO")
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 FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL 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()) arr = list(map(int, input().split())) m = arr[0] i = 1 broke = False while i < n: if arr[i] < arr[i - 1]: if arr[i - 1] - arr[i] <= m: m -= arr[i - 1] - arr[i] m = min(m, arr[i]) else: broke = True break i += 1 if broke: 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER 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
from sys import * for _ in range(int(input())): n = int(stdin.readline()) m = list(map(int, iter(stdin.readline().split()))) A = m[0] - sum(max(0, m[i - 1] - m[i]) for i in range(1, n)) if A >= 0: stdout.write(str("YES") + "\n") else: stdout.write(str("NO") + "\n")
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL 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()) for _ in range(0, T): n = int(input()) s = [int(x) for x in input().split()] l = [1000000000] r = [0] ans = "YES" for i in range(0, len(s)): if r[-1] > s[i]: ans = "NO" break mn = r[-1] rem = s[i] - mn mx = l[-1] if rem <= l[-1]: r.append(mn) l.append(rem) else: rq = rem - l[-1] r.append(mn + rq) l.append(l[-1]) if r[-1] < 0 or l[-1] < 0: ans = "NO" break print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR STRING EXPR FUNC_CALL 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
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) t = int(sys.stdin.readline()) for _ in range(t): n = int(sys.stdin.readline()) a = get_ints() if n == 1: print("YES") else: f = 0 if a[-2] >= a[-1]: m = a[-1] j = a[-2] else: m = a[-2] j = a[-2] for i in range(n - 3, -1, -1): if a[i] < j: if j - a[i] <= m: m = m - (j - a[i]) j = a[i] else: f = 1 break else: j = a[i] if f: print("NO") else: print("YES")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL 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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR 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
for nt in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n == 1: print("YES") continue p, s = [a[0]], [a[-1]] for i in range(1, n): p.append(min(a[i], p[-1])) for i in range(n - 2, -1, -1): s.append(min(a[i], s[-1])) s = s[::-1] ans = "YES" for i in range(1, n - 1): if a[i] > p[i - 1] + s[i + 1]: ans = "NO" break if ans == "NO": print(ans) continue new = [a[0]] for i in range(1, n): if a[i] == a[i - 1]: continue new.append(a[i]) if len(new) == 1: print("YES") continue high, low = 0, 0 if new[0] > new[1]: high += 0 else: low += new[0] if new[-1] > new[-2]: high += 0 else: low += new[-1] n = len(new) for i in range(1, n - 1): if new[i] > new[i - 1] and new[i] > new[i + 1]: high += new[i] if new[i] < new[i - 1] and new[i] < new[i + 1]: low += new[i] if high > low: 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 IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR LIST VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR 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
def solve(n, a): s = sum([max(0, a[i] - a[i + 1]) for i in range(n - 1)]) print("YES") if s <= a[0] else print("NO") for j in range(int(input())): solve(int(input()), list(map(int, input().split())))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) flag = 1 for i in range(1, n): if l[i] > l[i - 1]: if i == n - 1: pass else: d = l[i] - l[i - 1] for j in range(i, n): if l[j] >= d: l[j] -= d else: flag = 0 break if flag: 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 FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty. Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of modifications. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N≤106$1 \le N \le 10^6$ - 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N≤1,000$1 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 1 4 1 2 2 4 2 3 2 3 -----Example Output----- 1 2
T = int(input()) for _ in range(T): n = int(input()) l = [int(e) for e in input().split()] difNum = dict() for e in l: difNum[e] = difNum.get(e, 0) + 1 minAcum = 0 difOrd = [[e, difNum[e]] for e in sorted(difNum.keys())] while len(difOrd) > 0: ant = 0 addAct = 0 actLen = len(difOrd) minAct = actLen era = [] for i, k in enumerate(difOrd): addAct = k[0] - (ant + 1) + addAct ant = k[0] difOrd[i][1] -= 1 if difOrd[i][1] == 0: era.append(i) minAct = min(minAct, addAct + actLen - (i + 1)) difOrd = [[e[0], e[1]] for e in difOrd if e[1] > 0] minAcum += minAct print(min(minAcum, n))
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 FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty. Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of modifications. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N≤106$1 \le N \le 10^6$ - 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N≤1,000$1 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 1 4 1 2 2 4 2 3 2 3 -----Example Output----- 1 2
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) cost = 0 dp = [[] for val in range(2 * n + 1)] cnt = [0] * (n * 2 + 1) dp[0].extend([(0) for val in range(n * 2 + 1)]) for val in a: if val <= 2 * n: cnt[val] += 1 else: cost += 1 for i in range(1, n * 2 + 1): rn = n * 2 // i for j in range(0, rn + 1): dp[i].extend([dp[i - 1][j] + abs(cnt[i] - j)]) for j in range(rn - 1, -1, -1): dp[i][j] = min(dp[i][j], dp[i][j + 1]) print(cost + min(dp[2 * n][0], dp[2 * n][1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER
You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty. Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of modifications. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N≤106$1 \le N \le 10^6$ - 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N≤1,000$1 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 1 4 1 2 2 4 2 3 2 3 -----Example Output----- 1 2
t = int(input()) while t: t -= 1 n = int(input()) a = list(map(int, input().split())) set = [[]] a.sort() i = 0 prev = 0 for c in a: if c == prev: i += 1 if len(set) <= i: set.append([]) set[i].append(c) else: i = 0 set[i].append(c) prev = c ans = 0 for s in set: if s[-1] == len(s): continue j = 0 remove = len(s) add = 0 min = len(s) i = 0 last = s[-1] while i < last: i += 1 if s[j] != i: add += s[j] - i remove -= 1 i = s[j] j += 1 else: j += 1 add += 0 remove -= 1 modifications = add + remove if modifications < min: min = modifications if min == 0: break ans += min 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 LIST LIST EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of integers [A1,A2,…,AN]$[A_1, A_2, \ldots, A_N]$. Let's call adding an element to this array at any position (including the beginning and the end) or removing an arbitrary element from it a modification. It is not allowed to remove an element from the array if it is empty. Find the minimum number of modifications which must be performed so that the resulting array can be partitioned into permutations. Formally, it must be possible to partition elements of the resulting array into zero or more groups (multisets; not necessarily identical) in such a way that each element belongs to exactly one group and for each group, if it contains L$L$ elements, then it must contain only integers 1$1$ through L$L$, each of them exactly once. -----Input----- - The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows. - The first line of each test case contains a single integer N$N$. - The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$. -----Output----- For each test case, print a single line containing one integer ― the minimum required number of modifications. -----Constraints----- - 1≤T≤1,000$1 \le T \le 1,000$ - 1≤N≤106$1 \le N \le 10^6$ - 1≤Ai≤109$1 \le A_i \le 10^9$ for each valid i$i$ - the sum of N$N$ over all test cases does not exceed 106$10^6$ -----Subtasks----- Subtask #1 (50 points): - 1≤N≤1,000$1 \le N \le 1,000$ - the sum of N$N$ over all test cases does not exceed 10,000$10,000$ Subtask #2 (50 points): original constraints -----Example Input----- 2 5 1 4 1 2 2 4 2 3 2 3 -----Example Output----- 1 2
for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) li.sort() dli = dict() modi = 0 for i in li: if i not in dli: dli[i] = li.count(i) if len(dli) != 0: while 1: tmp = [] for i in dli: if dli[i] == 0: continue tmp.append(i) dli[i] -= 1 l = len(tmp) mn = l for i in range(l): mn = min(mn, tmp[i] - 1 - i + l - 1 - i) modi += mn if l == 0: break print(modi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes_algo(arr): max_sum = 0 curr_sum = 0 n = len(arr) if max(arr) < 0: max_sum = max(arr) return max_sum i = 0 while i < n: if curr_sum + arr[i] > 0: curr_sum += arr[i] else: curr_sum = 0 i += 1 max_sum = max(max_sum, curr_sum) return max_sum t = int(input()) for j in range(0, t): l1 = [int(x) for x in input().split()] n = l1[0] k = l1[1] a = [int(x) for x in input().split()] val1 = kadanes_algo(a) if k == 1: print(val1) else: sum_a = 0 for i in a: sum_a += i max_right = a[n - 1] val = a[n - 1] for i in range(n - 2, -1, -1): val += a[i] max_right = max(val, max_right) max_left = a[0] val = a[0] for i in range(1, n): val += a[i] max_left = max(val, max_left) val2 = max_right + sum_a * (k - 2) + max_left val3 = max_left + max_right ans = max(val1, val2, val3) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def kadane(a): cur = 0 ans = -sys.maxsize for i in a: cur = max(cur + i, i) ans = max(cur, ans) return ans for _ in range(int(input())): n, k = map(int, input().split()) a = [int(x) for x in input().split()] if k == 1: t = kadane(a) print(t) continue s = 0 for i in a: s += i b = [] for i in range(2): for i in a: b.append(i) t = kadane(b) if s >= 0: t += (k - 2) * s print(t)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def getMax(arr): max_elem = arr[0] for elem in arr: if elem > max_elem: max_elem = elem return max_elem def getBestPrefixSum(arr, n): prefix_sum = [elem for elem in arr] for i in range(1, n): prefix_sum[i] += prefix_sum[i - 1] return getMax(prefix_sum) def getBestSuffixSum(arr, n): suffix_sum = [elem for elem in arr] for i in range(n - 2, -1, -1): suffix_sum[i] += suffix_sum[i + 1] return getMax(suffix_sum) def getKadaneMaxSum(arr, n): current_sum = 0 best_sum = arr[0] for i in range(n): current_sum += arr[i] if best_sum < current_sum: best_sum = current_sum if current_sum < 0: current_sum = 0 return best_sum def getEntireArrSum(arr): sum = 0 for elem in arr: sum += elem return sum def solve(arr, n, k): answer = 0 if k == 1: answer = getKadaneMaxSum(arr, n) elif k == 2: answer = max( getKadaneMaxSum(arr, n), getBestPrefixSum(arr, n) + getBestSuffixSum(arr, n) ) else: array_sum = getEntireArrSum(arr) if array_sum > 0: answer = max( getKadaneMaxSum(arr, n), getBestPrefixSum(arr, n) + (k - 2) * array_sum + getBestSuffixSum(arr, n), ) else: answer = max( getKadaneMaxSum(arr, n), getBestPrefixSum(arr, n) + getBestSuffixSum(arr, n), ) return answer def main(): t = int(input()) while t: n, k = (int(x) for x in input().split()) arr = [int(elem) for elem in input().split()] print(solve(arr, n, k)) t -= 1 return main()
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
x = int(input()) for i in range(x): n, k = map(int, input().split()) arr = list(map(int, input().split())) ar = [i for i in arr] * k l = [(0) for i in range(n * k)] l[0] = ar[0] for i in range(n * k): l[i] = max([l[i - 1] + ar[i], ar[i]]) print(max(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes(arr): cursum = arr[0] maxsum = arr[0] for i in range(1, len(arr)): if cursum >= 0: cursum += arr[i] else: cursum = arr[i] maxsum = max(maxsum, cursum) return maxsum def kadanesOftwo(arr): narr = arr[:] + arr[:] return kadanes(narr) t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) arr_sum = sum(arr) if k == 1: print(kadanes(arr)) elif arr_sum < 0: print(kadanesOftwo(arr)) else: print(kadanesOftwo(arr) + (k - 2) * arr_sum)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes(seq): max_global, max_current = seq[0], seq[0] for i in range(1, len(seq)): max_current = max(seq[i] + max_current, seq[i]) if max_current > max_global: max_global = max_current return max_global for _ in range(int(input())): N, K = map(int, input().split()) arr = list(map(int, input().split())) if K == 1: m = kadanes(arr) print(m) continue else: m = kadanes(arr + arr) sum_arr = sum(arr) if sum_arr < 0: print(m) else: print(m + sum_arr * (K - 2))
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadane(l): current_sum, best_so_far = 0, -9999999999999 for i in l: current_sum += i if best_so_far < current_sum: best_so_far = current_sum if current_sum < 0: current_sum = 0 return best_so_far def max_sum(l, k): if k == 1: ans = kadane(l) return ans current_prefix_sum, best_prefix_sum = 0, -99999999999999 current_postfix_sum, best_postfix_sum = 0, -9999999999999 total_sum = 0 for i in l: current_prefix_sum += i best_prefix_sum = max(current_prefix_sum, best_prefix_sum) total_sum = current_prefix_sum for i in range(len(l) - 1, -1, -1): current_postfix_sum += l[i] best_postfix_sum = max(current_postfix_sum, best_postfix_sum) if total_sum < 0: ans = max(best_prefix_sum + best_postfix_sum, kadane(l)) else: ans = max(best_prefix_sum + best_postfix_sum + (k - 2) * total_sum, kadane(l)) return ans t = int(input()) while t > 0: n, k = map(int, input().split()) l = list(map(int, input().split())) print(max_sum(l, k)) t -= 1
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def maxSubArraySum(a, n, k): max_so_far = -sys.maxsize - 1 max_ending_here = 0 for i in range(n * k): max_ending_here = max_ending_here + a[i % n] if max_so_far < max_ending_here: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far for _ in range(int(input())): n, k = map(int, input().split()) ar = [int(x) for x in input().split()] s = sum(ar) if s >= 0: if k == 1 or k == 2: print(maxSubArraySum(ar, n, k)) else: print(maxSubArraySum(ar, n, 2) + (k - 2) * s) elif k == 1: print(maxSubArraySum(ar, n, k)) else: print(maxSubArraySum(ar, n, 2))
IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
tcase = int(input()) for i in range(tcase): ninput = input() nlist = list(ninput.split(" ")) A = [] B = [] numinput = input() A = list(numinput.split(" ")) B = A * int(nlist[1]) size = int(nlist[0]) * int(nlist[1]) B = [int(i) for i in B] max_so_far = B[0] curr_max = B[0] for i in range(1, size): curr_max = max(B[i], curr_max + B[i]) max_so_far = max(max_so_far, curr_max) print(max_so_far)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
for i in range(int(input())): n, k = map(int, input().split()) l1 = list(map(int, input().split())) g_max = l1[0] c_max = l1[0] total = sum(l1) if k == 1: for i in range(1, len(l1)): c_max = max(l1[i], c_max + l1[i]) if c_max > g_max: g_max = c_max print(g_max) elif k > 1 and sum(l1) <= 0: l1 = l1 * 2 for i in range(1, len(l1)): c_max = max(l1[i], c_max + l1[i]) if c_max > g_max: g_max = c_max print(g_max) elif sum(l1) > 0: l1 = l1 * 2 for i in range(1, len(l1)): c_max = max(l1[i], c_max + l1[i]) if c_max > g_max: g_max = c_max print(g_max + (k - 2) * total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kcon(n, k, arr): if k < 3: return maxSubArray(arr * k) sum_arr = sum(arr) max_sum_2_kcon = maxSubArray(arr * 2) if sum_arr < 0: return max_sum_2_kcon return max_sum_2_kcon + sum_arr * (k - 2) def maxSubArray(nums): if not nums: return 0 curr_sum = max_sum = nums[0] for i in range(1, len(nums)): curr_sum = max(nums[i], curr_sum + nums[i]) max_sum = max(max_sum, curr_sum) return max_sum t = int(input()) for i in range(t): N, K = map(int, input().split()) Arr = list(map(int, input().split())) res = kcon(N, K, Arr) print(res)
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR 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 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 VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] l = [int(x) for x in input().split()] aa = sum(l) if k == 1: a = l[:] maxi = -999999999 sumn = 0 for i in a: sumn += i if maxi < sumn: maxi = sumn if sumn < 0: sumn = 0 print(maxi) continue if aa > 0: a = l[:] * 2 maxi = -999999999 sumn = 0 for i in a: sumn += i if maxi < sumn: maxi = sumn if sumn < 0: sumn = 0 print(maxi + (k - 2) * aa) continue else: a = l[:] * 2 maxi = -999999999 sumn = 0 for i in a: sumn += i if maxi < sumn: maxi = sumn if sumn < 0: sumn = 0 print(maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
for _ in range(int(input())): nk = input() nk = list(map(int, nk.split())) n = nk[0] k = nk[1] arr = input() arr = list(map(int, arr.split())) S = sum(arr) maxSum = -(2**31) tempsum = 0 for i in range(len(arr)): tempsum += arr[i] tempsum = max(tempsum, arr[i]) maxSum = max(tempsum, maxSum) baseSum = maxSum if maxSum == S: print(k * S) elif k == 1: print(maxSum) else: karr = 2 * arr maxSum = -(2**31) tempsum = 0 for i in range(len(karr)): tempsum += karr[i] tempsum = max(tempsum, karr[i]) maxSum = max(tempsum, maxSum) if S > 0: print((k - 2) * S + maxSum) else: print(maxSum)
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 VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def maxSubArraySum(array): currentSum = array[0] answer = array[0] for x in array[1:]: currentSum = max(currentSum + x, x) answer = max(answer, currentSum) return answer t = int(input()) while t != 0: t -= 1 n, k = list(map(int, input().split(" "))) array = list(map(int, input().split(" "))) s = sum(array) if k == 1: print(maxSubArraySum(array)) else: temp = array + array if s >= 0: print(s * (k - 2) + maxSubArraySum(temp)) else: print(maxSubArraySum(temp))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes_algo(arr): dparr = [0] * len(arr) ans = dparr[0] = arr[0] for i in range(1, len(arr)): dparr[i] = max(arr[i], arr[i] + dparr[i - 1]) ans = max(ans, dparr[i]) return ans def solve(arr, k): if k == 1: print(kadanes_algo(arr)) else: summ = sum(arr) newarr = arr * 2 print(max(kadanes_algo(newarr), kadanes_algo(newarr) + summ * (k - 2))) def main(): t = int(input()) for i in range(t): n, k = map(int, input().split(" ")) arr = list(map(int, input().split(" "))) solve(arr, k) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) l = list(map(int, input().split())) curMax = maxSum = total = l[0] for i in range(1, n * min(k, 2)): curMax = max(curMax + l[i % n], l[i % n]) maxSum = max(maxSum, curMax) if i < n: total += l[i] if total > 0 and k > 2: maxSum += total * (k - 2) print(maxSum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [int(i) for i in input().split()] pre = [0] * n suf = [0] * n bs = suf[-1] = a[-1] bp = pre[0] = a[0] for i in range(1, n): pre[i] = pre[i - 1] + a[i] suf[-(i + 1)] = suf[-i] + a[-(i + 1)] if bp < pre[i]: bp = pre[i] if bs < suf[-(i + 1)]: bs = suf[-(i + 1)] kad = a[0] cs = a[0] for i in range(1, n): cs += a[i] if cs > kad: kad = cs if cs <= 0: cs = 0 res = 0 if pre[-1] >= 0 and k >= 2: res = (k - 2) * pre[-1] + bp + bs elif pre[-1] < 0 and k >= 2: res = max(bp + bs, kad) else: res = kad print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def Kadanes(A, n): current_sum = 0 best_sum = -(2**31) for i in range(n): current_sum = current_sum + A[i] if best_sum < current_sum: best_sum = current_sum if current_sum < 0: current_sum = 0 return best_sum def K_concatenation(A, n, k): Sum = Kadanes(A, n) if k == 1: return Sum curr_prefix_sum = 0 curr_suffix_sum = 0 max_prefix_sum = -(2**31) max_suffix_sum = -(2**31) for i in range(n): curr_prefix_sum = curr_prefix_sum + A[i] max_prefix_sum = max(max_prefix_sum, curr_prefix_sum) totalSum = curr_prefix_sum for i in range(n - 1, -1, -1): curr_suffix_sum = curr_suffix_sum + A[i] max_suffix_sum = max(max_suffix_sum, curr_suffix_sum) if totalSum < 0: ans = max(max_suffix_sum + max_prefix_sum, Sum) else: ans = max(max_suffix_sum + max_prefix_sum + totalSum * (k - 2), Sum) return ans def TakeInput(): arr1 = [int(x) for x in input().split()] n = arr1[0] k = arr1[1] A = [int(y) for y in input().split()] return n, k, A t = int(input()) for i in range(t): n, k, A = TakeInput() ans = K_concatenation(A, n, k) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys t = int(input()) for _ in range(t): N, k = map(int, input().split()) A = list(map(int, input().split())) B = A * k max_ = -sys.maxsize - 1 value = 0 for i in range(len(B)): value = value + B[i] if value > max_: max_ = value if value < 0: value = 0 print(max_)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadane(arr): c_m = arr[0] o_m = arr[0] s = arr[0] for i in range(1, len(arr)): s += arr[i] if arr[i] >= s: c_m = arr[i] s = arr[i] o_m = max(c_m, o_m) else: c_m = s o_m = max(c_m, o_m) return o_m for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) sumofarr = sum(arr) if k == 1: print(kadane(arr)) elif sumofarr < 0: print(kadane(arr * 2)) else: temp = kadane(arr * 2) + (k - 2) * sumofarr print(temp)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys for i in range(int(input())): import sys x = input().split(" ") n = int(x[0]) k = int(x[1]) data = [int(num) for num in input().split(" ")] datas = data * 2 if k > 1 else data cs = 0 ms = -sys.maxsize for num in datas: cs += num ms = max(ms, cs) if cs < 0: cs = 0 print(ms + (k - 2) * sum(data) if sum(data) > 0 and k > 1 else ms)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def kadane(arr, n): curr_sum, max_sum = 0, max(arr) for i in range(n): if curr_sum + arr[i] >= 0: curr_sum += arr[i] max_sum = max(max_sum, curr_sum) else: curr_sum = 0 return max_sum for __ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) ans = kadane(a + a, 2 * n) if k > 1 else kadane(a, n) if k > 2: ans = max(ans, ans + (k - 2) * sum(a)) print(ans)
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def solve(arr, size): maxans = -(10**9) pref = 0 for i in range(size): pref += arr[i] maxans = max(maxans, pref) if pref < 0: pref = 0 return maxans for _ in range(int(input())): n, k = map(int, input().split()) arr = [int(x) for x in input().split()] if k == 1: print(solve(arr, n)) else: summ = sum(arr) if summ <= 0: print(solve(arr + arr, n * 2)) else: print(solve(arr + arr, n * 2) + (k - 2) * summ)
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def subarr(arr): m = float("-inf") s = 0 for i in arr: s = s + i m = max(m, s) if s < 0: s = 0 return m t = int(input()) out = [] for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) z = subarr(arr) s = sum(arr) arr = arr + arr c = subarr(arr) if k == 1: out.append(z) elif s >= 0: out.append(c + s * (k - 2)) else: out.append(c) for i in out: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def findMaxSubArray(arr, mx): MSA = mx sm = 0 for x in arr: if sm + x > 0: sm += x MSA = max(MSA, sm) else: sm = 0 return MSA for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) mx = max(arr) mx = max(mx, findMaxSubArray(arr, mx)) if k > 1: mx = max(mx, findMaxSubArray(arr + arr, mx)) if k > 2: mx = max(mx, mx + sum(arr) * (k - 2)) print(mx)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def maxSumSubarray(B): import sys currSum, maxSum = 0, -(sys.maxsize - 1) for b in B: currSum += b if currSum > maxSum: maxSum = currSum if currSum < 0: currSum = 0 return maxSum def formB(A, K): B = [(0) for _ in range(len(A) * K)] for i in range(len(A)): B[i] = A[i] ti, j = i, 1 while j < K: B[ti + len(A)] = A[i] ti += len(A) j += 1 return maxSumSubarray(B) try: T = int(input()) except Exception as e: pass else: while T >= 1: N, K = map(int, input().split()) A = [int(x) for x in input().split()] print(formB(A, K)) T -= 1
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes(nums): currentSum = float("-inf") overallSum = float("-inf") for x in range(len(nums)): tempSum = currentSum + nums[x] if nums[x] < tempSum: currentSum = tempSum else: currentSum = nums[x] if currentSum > overallSum: overallSum = currentSum return overallSum T = int(input()) while T > 0: T -= 1 N, K = [int(x) for x in input().split()] A = [int(x) for x in input().split()] if K == 1: print(kadanes(A)) else: arrSum = sum(A) A = 2 * A if arrSum < 0: print(kadanes(A)) else: print(kadanes(A) + (K - 2) * arrSum)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadane(k): max_so_far = k[0] curr_so_far = k[0] for j in range(1, len(k)): curr_so_far = max(k[j], curr_so_far + k[j]) max_so_far = max(max_so_far, curr_so_far) return max_so_far x = int(input()) for i in range(x): y, z = map(int, input().split()) ar = list(map(int, input().split())) if z == 1: print(kadane(ar)) elif sum(ar) < 0: print(kadane(ar * 2)) else: temp = kadane(ar * 2) + (z - 2) * sum(ar) print(temp)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadne_sum(arr, n): currSum = 0 maxSum = float("-inf") for i in range(n): currSum += arr[i] if maxSum < currSum: maxSum = currSum if currSum < 0: currSum = 0 return maxSum def max_subarray_sum(arr, n, k): kadne_sum_value = kadne_sum(arr, n) if k == 1: return kadne_sum_value curr_prefix_sum = 0 curr_suffix_sum = 0 prefix_sum = float("-inf") suffix_sum = float("-inf") for i in range(n): curr_prefix_sum += arr[i] prefix_sum = max(curr_prefix_sum, prefix_sum) li_sum = curr_prefix_sum for i in range(n - 1, -1, -1): curr_suffix_sum += arr[i] suffix_sum = max(suffix_sum, curr_suffix_sum) ans = 0 if li_sum < 0: ans = max(suffix_sum + prefix_sum, kadne_sum_value) else: ans = max(suffix_sum + prefix_sum + (k - 2) * li_sum, kadne_sum_value) return ans t = int(input()) for i in range(t): n, k = map(int, input().split()) li = list(map(int, input().split())) print(max_subarray_sum(li, n, k))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def kadane(arr): max_so_far = arr[0] curr_sum = arr[0] for i in range(1, len(arr)): curr_sum = max(arr[i], curr_sum + arr[i]) max_so_far = max(curr_sum, max_so_far) return max_so_far def max_sum(arr, kadane_sum, k): if k <= 0: return 0 if k == 1: return kadane_sum curr_pre_sum = 0 curr_suf_sum = 0 max_pre_sum = sys.maxsize * -1 max_suf_sum = sys.maxsize * -1 total_sum = 0 for i in range(len(arr)): curr_pre_sum += arr[i] max_pre_sum = max(curr_pre_sum, max_pre_sum) total_sum = curr_pre_sum for i in range(len(arr) - 1, -1, -1): curr_suf_sum += arr[i] max_suf_sum = max(curr_suf_sum, max_suf_sum) if total_sum < 0: return max(max_pre_sum + max_suf_sum, kadane_sum) else: return max(max_pre_sum + max_suf_sum + total_sum * (k - 2), kadane_sum) for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) kadane_sum = kadane(arr) ans = max_sum(arr, kadane_sum, k) print(ans)
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def maximum_subarray_sum(arr, n, k): if k == 1: return kadane(arr) one_sum = sum(arr) a = arr + arr if one_sum > 0: return kadane(a) + (k - 2) * one_sum return kadane(a) def kadane(a): best_sum = float("-inf") curr_sum = 0 for i in a: curr_sum = curr_sum + i if curr_sum < i: curr_sum = i best_sum = max(best_sum, curr_sum) return best_sum def __starting_point(): input = sys.stdin.read() data = list(map(int, input.split())) ans = [] j = 1 t = data[0] for i in range(t): n = data[j] k = data[j + 1] arr = data[j + 2 : j + 2 + n] ans.append(maximum_subarray_sum(arr, n, k)) j = j + 2 + n for x in ans: print(x) __starting_point()
IMPORT FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def maxSubarraySum(arr): maxSum, currSum = float("-inf"), 0 for i in arr: if currSum < 0: currSum = i else: currSum += i maxSum = max(maxSum, currSum) return maxSum t = int(input()) for i in range(t): n, k = map(int, input().split()) l = list(map(int, input().split())) arr = l[:] for i in range(1, 2): arr.extend(l) s1 = maxSubarraySum(arr) s2 = sum(l) if k == 1: print(maxSubarraySum(l)) elif s2 < 0: print(s1) else: print(s1 + (k - 2) * s2)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR 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 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 FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys t = int(input()) while t > 0: list1 = list(map(int, input().split())) list2 = list(map(int, input().split())) n = list1[0] k = list1[1] def kadane(li): max_so_far = 0 max_ending_here = -sys.maxsize for x in li: max_so_far += x if max_so_far > max_ending_here: max_ending_here = max_so_far if max_so_far < 0: max_so_far = 0 return max_ending_here li = [] sum = 0 for x in list2: li.append(x) for x in list2: sum += x li.append(x) if k == 1: ans = kadane(list2) elif sum >= 0: ans = kadane(li) ans += (k - 2) * sum elif sum < 0: ans = kadane(li) print(ans) t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
mod = 1000000007 for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) om = a[0] cm = a[0] if k == 1: for i in range(1, n, 1): cm = max(a[i % n], cm + a[i % n]) om = max(om, cm) print(om) else: c = 0 for i in range(n): c = c + a[i] if c <= 0: a = a * 2 for i in range(1, n * 2, 1): cm = max(a[i % n], cm + a[i % n]) om = max(om, cm) print(om) else: a = a * 2 for i in range(1, n * 2, 1): cm = max(a[i % n], cm + a[i % n]) om = max(om, cm) print(om + (k - 2) * c)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def maximum_subarray_sum(a): N = len(a) max_so_far = -float("inf") max_ending_here = 0 start = 0 end = 0 s = 0 for i in range(N): max_ending_here += a[i] if max_ending_here > max_so_far: max_so_far = max_ending_here start = s end = i if max_ending_here < 0: max_ending_here = 0 s = i + 1 return start, end T = int(input()) ans = [] for _ in range(T): N, K = [int(i) for i in input().split()] A = [int(i) for i in input().split()] if K == 1: s, e = maximum_subarray_sum(A) ans.append(sum(A[s : e + 1])) else: s, e = maximum_subarray_sum(A + A) if e < N: ans.append(sum(A[s : e + 1])) else: e -= N ans.append( max( sum(A[s:]) + sum(A) * (K - 2) + sum(A[: e + 1]), sum((A + A)[s : e + N + 1]), ) ) for i in ans: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def sum_of_arr(l2): res = 0 for i in l2: res += i return res t = int(input()) for j in range(0, t): l1 = [int(x) for x in input().split()] n = l1[0] k = l1[1] l2 = [int(x) for x in input().split()] l3 = [] for i in l2: l3.append(i) if k == 1: max_sum = 0 curr_sum = 0 for i in range(0, n): if curr_sum + l2[i] > 0: curr_sum += l2[i] else: curr_sum = 0 max_sum = max(max_sum, curr_sum) max_sum = max(max_sum, curr_sum) if max(l2) < 0: max_sum = max(l2) ans = max_sum else: l2 += l2 max_sum = 0 curr_sum = 0 for i in range(0, 2 * n): if curr_sum + l2[i] > 0: curr_sum += l2[i] else: curr_sum = 0 max_sum = max(max_sum, curr_sum) max_sum = max(max_sum, curr_sum) if max(l2) < 0: max_sum = max(l2) ans = max_sum + max(0, sum_of_arr(l3)) * (k - 2) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadane_solution(A): curr_sum = 0 max_sum = float("-inf") for i in range(len(A)): if curr_sum < 0: curr_sum = 0 curr_sum += A[i] max_sum = max(curr_sum, max_sum) return max_sum t = int(input()) while t > 0: n, k = map(int, input().split()) a = list(map(int, input().split()))[:n] res = 0 if k < 2: print(kadane_solution(a), end="\n") else: k_res = kadane_solution(a + a) s = sum(a) if s >= 0: res = k_res + (k - 2) * s else: res = k_res print(res, end="\n") t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def maxsubarraysum(arr): max_till = arr[0] max_cur = arr[0] n = len(arr) for i in range(1, n): max_cur = max(arr[i], max_cur + arr[i]) max_till = max(max_cur, max_till) return max_till for _ in range(int(input())): n, m = map(int, input().split()) arr = [int(i) for i in input().split()] if m == 1: print(maxsubarraysum(arr)) else: arr2 = arr + arr if m == 2: print(maxsubarraysum(arr2)) else: maxsum2 = maxsubarraysum(arr2) if sum(arr) >= 0: print(maxsum2 + (m - 2) * sum(arr)) else: print(maxsum2)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
t = int(input()) for i in range(t): l = list(map(int, input().split())) l2 = list(map(int, input().split())) if sum(l2) > 0: x = 0 if l[1] == 1: h = l2 y = 0 for j in range(len(h)): y = max(0, y + h[j]) x = max(y, x) print(x) else: h = l2 + l2 y = 0 for j in range(len(h)): y = max(0, y + h[j]) x = max(y, x) print(x + (l[1] - 2) * sum(l2)) elif max(l2) < 0: print(max(l2)) else: x = 0 if l[1] == 1: h = l2 y = 0 for j in range(len(h)): y = max(0, y + h[j]) x = max(y, x) print(x) else: h = l2 + l2 y = 0 for j in range(len(h)): y = max(0, y + h[j]) x = max(y, x) print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def KADANE_s(a): curr_sum = 0 best_sum = float("-inf") for i in a: curr_sum += i if best_sum < curr_sum: best_sum = curr_sum if curr_sum < 0: curr_sum = 0 return best_sum def maxSubarrSum(a, n, k): k_sum = KADANE_s(a) if k == 1: return k_sum suffix_sum, max_suffix_sum = 0, float("-inf") prefix_sum, max_prefix_sum = 0, float("-inf") for i in range(n - 1, -1, -1): suffix_sum += a[i] if max_suffix_sum < suffix_sum: max_suffix_sum = suffix_sum for i in a: prefix_sum += i if max_prefix_sum < prefix_sum: max_prefix_sum = prefix_sum total_sum = prefix_sum if total_sum < 0: ans = max(max_prefix_sum + max_suffix_sum, k_sum) else: ans = max(max_prefix_sum + max_suffix_sum + total_sum * (k - 2), k_sum) return ans t = int(input()) while t: t -= 1 n, k = map(int, input().split()) a = list(map(int, input().split())) ans = maxSubarrSum(a, n, k) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def maxsum(l): ans = -sys.maxsize - 1 cur = 0 for i in l: cur = max(cur + i, i) ans = max(ans, cur) return ans t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [int(x) for x in input().split()] s = 0 for i in a: s += i if k == 1: ans = maxsum(a) print(ans) elif s < 0: b = [] for i in range(2): for j in a: b.append(j) print(maxsum(b)) else: b = [] for i in range(2): for j in a: b.append(j) t = maxsum(b) print(t + (k - 2) * s)
IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys t = int(input()) while t > 0: t = t - 1 n, k = map(int, input().split()) a = list(map(int, input().split())) if k == 1: globmax = sys.maxsize * -1 curmax = 0 for i in range(0, len(a)): curmax = curmax + a[i] if curmax > globmax: globmax = curmax if curmax < 0: curmax = 0 print(globmax) else: summ = sum(a) if summ < 0: b = [] for i in range(0, 2): for j in range(0, len(a)): b.append(a[j]) globmax = sys.maxsize * -1 curmax = 0 for i in range(0, len(b)): curmax = curmax + b[i] if curmax > globmax: globmax = curmax if curmax < 0: curmax = 0 print(globmax) else: b = [] for i in range(0, 2): for j in range(0, len(a)): b.append(a[j]) globmax = sys.maxsize * -1 curmax = 0 for i in range(0, len(b)): curmax = curmax + b[i] if curmax > globmax: globmax = curmax if curmax < 0: curmax = 0 ans = globmax + (k - 2) * summ print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def mgc(arr, n, k): s = sum(arr) b = [] if k == 1: b.extend(arr) else: b.extend(arr) if s * (k - 2) > 0: b.append(s * (k - 2)) b.extend(arr) s = b[0] gs = b[0] for i in b[1:]: s = max(i, s + i) gs = max(s, gs) return gs for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) print(mgc(arr, n, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
for t in range(0, int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split()))[:n] t = 0 total = 0 dp = [] if k == 1: new_arr = arr else: new_arr = arr * 2 for i in range(0, len(new_arr)): if total < 0: total = 0 total += new_arr[i] dp.append(total) t = 1 if k == 1: print(max(dp)) elif sum(arr) > 0: print(max(dp) + sum(arr) * (k - 2)) else: print(max(dp))
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
t = int(input()) for test in range(t): n, k = list(map(int, input().split())) l = list(map(int, input().split())) s = sum(l) maxSum = l[0] curSum = l[0] i = 1 while i < len(l): curSum = max(curSum + l[i], l[i]) if curSum > maxSum: maxSum = curSum i += 1 if maxSum == sum(l): print(maxSum * k) elif k == 1: print(maxSum) else: ms = maxSum maxSum = l[0] curSum = l[0] i = 1 l *= 2 while i < len(l): curSum = max(curSum + l[i], l[i]) if curSum > maxSum: maxSum = curSum i += 1 if s > 0: print((k - 2) * s + maxSum) else: print(maxSum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys inpu = sys.stdin.readline for _ in range(int(inpu())): n, k = map(int, inpu().split()) nums = list(map(int, inpu().split(" "))) ms = nums[0] cs = nums[0] s = nums[0] check = nums[0] for i in range(1, n): s += nums[i] check = max(check, s) if cs < 0: cs = 0 cs += nums[i] ms = max(cs, ms) q = nums[::-1] ch = q[0] ss = q[0] for i in range(1, n): ss += q[i] ch = max(ch, ss) if k == 1: print(ms) continue a = nums * 2 mp = ms for i in range(n, len(a)): if cs < 0: cs = 0 cs += a[i] mp = max(cs, mp) ch += s * (k - 2) + check print(max(mp, ms, ch))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
from sys import stdin, stdout input = stdin.readline def func(arr): n = len(arr) maxsum, total = 0, 0 neg_arr = [arr[i] for i in range(n) if arr[i] < 0] neg = len(neg_arr) if neg == n: return max(arr) for i in range(n): total += arr[i] if total < 0: total = 0 else: maxsum = max(maxsum, total) return maxsum t = int(input().strip()) for _ in range(t): n, k = list(map(int, input().strip().split())) arr = list(map(int, input().strip().split())) if k == 1: print(func(arr)) elif k == 2: a = func(arr) b = func(arr + arr) print(max(a, b)) else: a = func(arr) b = func(arr + arr) c = func(arr + arr + arr) diff1 = b - a diff2 = c - b ans = b + (k - 2) * diff2 print(ans)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def solve(arr, k): def kadanes(arr): curr, msf = arr[0], arr[0] for i in range(1, len(arr)): curr = max(curr + arr[i], arr[i]) msf = max(msf, curr) return msf S = sum(arr) if k == 1: return kadanes(arr) sumConcat = kadanes(2 * arr) if S > 0: return sumConcat + (k - 2) * S else: return sumConcat tc = int(input()) while tc: n, k = list(map(int, input().split())) arr = list(map(int, input().split())) print(solve(arr, k)) tc -= 1
FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def Kandans(arr): m = arr[0] m1 = arr[0] for x in arr[1:]: m = max(x, m + x) m1 = max(m, m1) return m1 for x in range(int(input())): N, k = map(int, input().split()) arr = list(map(int, input().split())) s = sum(arr) if k == 1: print(Kandans(arr)) elif s <= 0: print(Kandans(arr + arr)) else: print(Kandans(arr + arr) + (k - 2) * s)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys def kadane(arr): if len(arr) == 0: return 0 ans = -sys.maxsize cur = 0 for i in arr: cur = max(cur + i, i) ans = max(ans, cur) return ans for _ in range(int(input())): n, k = map(int, input().split()) a = [int(x) for x in input().split()] s = sum(a) if k == 1: ans = kadane(a) print(ans) continue b = [] for i in range(2): for j in a: b.append(j) ans = kadane(b) if s < 0: print(ans) else: ans = ans + (k - 2) * s print(ans)
IMPORT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def heler_kadane(arr, n): max1 = arr[0] max_point = arr[0] for i in range(1, n): max1 = max(arr[i], max1 + arr[i]) max_point = max(max1, max_point) return max_point t = int(input()) for i in range(0, t): l = list(map(int, input().split())) n = l[0] k = l[1] arr = list(map(int, input().split())) arrb = [] if k == 1: maxsum = heler_kadane(arr, n) elif k == 2: for j in range(0, 2 * n): arrb.append(arr[j % n]) maxsum = heler_kadane(arrb, 2 * n) elif k == 3: for j in range(0, 3 * n): arrb.append(arr[j % n]) maxsum = heler_kadane(arrb, 3 * n) else: sum = 0 for j in range(0, n): sum = sum + arr[j] for j in range(0, 2 * n): arrb.append(arr[j % n]) for j in range(0, k - 3): arrb.append(sum) for j in range(0, n): arrb.append(arr[j]) maxsum = heler_kadane(arrb, k - 3 + 3 * n) print(maxsum)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def mxarr(arr): max1 = arr[0] maxpo = arr[0] for i in range(1, len(arr)): max1 = max(arr[i], arr[i] + max1) maxpo = max(max1, maxpo) i += 1 return maxpo t = int(input()) for i in range(t): l = list(map(int, input().split())) n = l[0] k = l[1] arr = list(map(int, input().split())) if k == 1: maxpo = mxarr(arr) else: sum = 0 for i in range(len(arr)): sum += arr[i] newarr = arr for i in range(len(arr)): newarr.append(arr[i]) maxpo = max(mxarr(newarr), mxarr(newarr) + sum * (k - 2)) print(maxpo)
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys tt = int(input()) for _ in range(0, tt): l = list(map(int, input().split())) k = l[1] darr = list(map(int, input().split())) n = len(darr) maxsofar = -999999 maxuphere = 0 for i in range(0, n * min(k, 2)): maxuphere += darr[i % n] if maxsofar < maxuphere: maxsofar = maxuphere if maxuphere < 0: maxuphere = 0 if sum(darr) > 0: if k == 1: print(maxsofar) else: print(maxsofar + sum(darr) * (k - 2)) else: print(maxsofar)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def max_sum_array(a): max_so_far = 0 max_ending_here = 0 for i in range(len(a)): max_ending_here = max_ending_here + a[i] if max_ending_here > max_so_far: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far for _ in range(int(input())): N, K = list(map(int, input().split())) array = list(map(int, input().split())) max_array = max_sum_array(array) max_double_array = max_sum_array(array + array) sum_array = sum(array) max_starting_0 = 0 max_ending_here = 0 for i in range(len(array)): max_ending_here = max_ending_here + array[i] if max_ending_here > max_starting_0: max_starting_0 = max_ending_here max_ending_j = 0 max_ending_here = 0 j = len(array) - 1 while j >= 0: max_ending_here = max_ending_here + array[j] if max_ending_here > max_ending_j: max_ending_j = max_ending_here j = j - 1 max_sum = max_array if max_sum < max_double_array: max_sum = max_double_array if max_sum < K * sum_array: max_sum = K * sum_array if max_sum < max_ending_j + (K - 2) * sum_array + max_starting_0: max_sum = max_ending_j + (K - 2) * sum_array + max_starting_0 flag = 0 max_element = array[0] for i in range(len(array)): if array[i] > 0: flag = 1 if max_element < array[i]: max_element = array[i] if flag == 0: max_sum = max_element max_array = max_element if K > 1: print(max_sum) else: print(max_array)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def solve(a, n, m): max_so_far = a[0] curr_max = a[0] for i in range(1, m): curr_max = max(a[i % n], curr_max + a[i % n]) max_so_far = max(max_so_far, curr_max) return max_so_far for _ in range(int(input())): n, k = map(int, input().split()) l = [int(x) for x in input().split()] if k == 1: print(solve(l, n, n)) else: subSum = solve(l, n, 2 * n) s = 0 for i in l: s += i print(max(subSum, subSum + s * (k - 2)))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def max_sum(arr): max_till_now = -1000000 current_sum = 0 for i in range(len(arr)): if current_sum < 0: current_sum = 0 current_sum += arr[i] if max_till_now < current_sum: max_till_now = current_sum return max_till_now def solve(A, k): if k == 1: return max_sum(A) sum_A = 0 for i in range(len(A)): sum_A += A[i] Max_Suffix_Sum = -1000000 current = 0 for i in range(len(A)): current += A[-i - 1] if current > Max_Suffix_Sum: Max_Suffix_Sum = current Max_Prefix_Sum = -1000000 current = 0 for i in range(len(A)): current += A[i] if current > Max_Prefix_Sum: Max_Prefix_Sum = current if sum_A <= 0: case_1_max_sum = max_sum(A) case_2_max_sum = Max_Suffix_Sum + Max_Prefix_Sum return max([case_1_max_sum, case_2_max_sum]) else: case_1_max_sum = max_sum(A) case_2_max_sum = Max_Suffix_Sum + (k - 2) * sum_A + Max_Prefix_Sum return max([case_1_max_sum, case_2_max_sum]) T = int(input()) for i in range(T): [N, k] = list(map(int, input().split(" "))) A = list(map(int, input().split(" "))) answer = solve(A, k) print(answer)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
for i in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) if k == 1: c_sum = l[0] m_sum = l[0] for i in range(1, n): c_sum = max(l[i], c_sum + l[i]) m_sum = max(m_sum, c_sum) print(m_sum) else: s = sum(l) l *= 2 j = 1 c_sum = l[0] m_sum = l[0] while j < 2 * n: if l[j] > c_sum + l[j]: c_sum = l[j] else: c_sum += l[j] if c_sum > m_sum: m_sum = c_sum j += 1 if s <= 0: print(m_sum) else: print(m_sum + (k - 2) * s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
import sys minn = -sys.maxsize - 1 T = int(input()) def kadane(arr, n): cur_sum, min_sum = 0, minn for i in range(n): cur_sum += arr[i] if min_sum < cur_sum: min_sum = cur_sum if cur_sum < 0: cur_sum = 0 return min_sum for _ in range(T): n, k = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] cur_sum = sum(arr) kad = kadane(arr, n) if k == 1: print(kad) else: cur_pre, cur_suf = 0, 0 max_pre, max_suf = minn, minn for i in range(n): cur_suf += arr[i] max_suf = max(cur_suf, max_suf) suf = max_suf for i in range(n - 1, -1, -1): cur_pre += arr[i] max_pre = max(cur_pre, max_pre) pre = max_pre if cur_sum < 0: print(max(suf + pre, kad)) elif cur_sum >= 0: a1 = pre + suf + cur_sum * (k - 2) print(max(a1, kad))
IMPORT ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def li(): return list(map(int, input().split())) def si(): return input().split() def ii(): return int(input()) def ip(): return input() def maxum(a, n): max_so_far = -1e19 curr_max = 0 for i in range(n): curr_max = max(a[i], curr_max + a[i]) max_so_far = max(max_so_far, curr_max) return max_so_far for tastcas in range(int(input())): n, k = li() a = li() if k == 1: print(maxum(a, n)) continue s = sum(a) ans = maxum(a + a, 2 * n) if s >= 0: ans += (k - 2) * s print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A with size N (indexed from 0) and an integer K. Let's define another array B with size N · K as the array that's formed by concatenating K copies of array A. For example, if A = {1, 2} and K = 3, then B = {1, 2, 1, 2, 1, 2}. You have to find the maximum subarray sum of the array B. Fomally, you should compute the maximum value of Bi + Bi+1 + Bi+2 + ... + Bj, where 0 ≤ i ≤ j < N · K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and K. - The second line contains N space-separated integers A0, A1, ..., AN-1. -----Output----- For each test case, print a single line containing the maximum subarray sum of B. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 105 - 1 ≤ K ≤ 105 - -106 ≤ Ai ≤ 106 for each valid i -----Subtasks----- Subtask #1 (18 points): N · K ≤ 105 Subtask #2 (82 points): original constraints -----Example----- Input: 2 2 3 1 2 3 2 1 -2 1 Output: 9 2 -----Explanation----- Example case 1: B = {1, 2, 1, 2, 1, 2} and the subarray with maximum sum is the whole {1, 2, 1, 2, 1, 2}. Hence, the answer is 9. Example case 2: B = {1, -2, 1, 1, -2, 1} and the subarray with maximum sum is {1, 1}. Hence, the answer is 2.
def kadanes(arr): c_m = arr[0] o_m = arr[0] s = arr[0] for i in range(1, len(arr)): s += arr[i] if arr[i] >= s: c_m = arr[i] s = arr[i] else: c_m = s o_m = max(c_m, o_m) return o_m t = int(input()) for i in range(t): n, k = map(int, input().split()) arr = [int(i) for i in input().split()] if k == 1: print(kadanes(arr)) continue s = sum(arr) if s < 0: print(kadanes(arr + arr)) continue else: print(kadanes(arr + arr) + (k - 2) * s) continue
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR