description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
ina = lambda: [*map(int, input().split())] ins = lambda: input() ind = lambda: int(input()) inf = lambda: float(input()) def csum(a, n): r = a[:] for i in range(n - 1): r[i + 1] = r[i] + r[i + 1] return r def sump(b): return sum(i for i in b if i > 0) def main(): n, a = ind(), ina() _, b = ind(), ina() print(max(max(csum(a, n)), max(csum(list(reversed(a)), n))) + sump(b)) for _ in range(ind()): main()
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t = int(input()) for i in range(t): n = int(input()) a = [int(i) for i in input().split()] m = int(input()) b = [int(i) for i in input().split()] maxi = -(2**32) + 1 if n >= 3: dp = [(0) for i in range(n)] dp[1] = a[1] for i in range(2, n - 1): dp[i] = max(a[i], dp[i - 1] + a[i]) maxi = max(dp) st = 0 start = 0 for i in range(0, n): st += a[i] start = max(start, st) en = 0 end = 0 for i in range(n - 1, -1, -1): en += a[i] end = max(end, en) ma = max(start, end) for i in b: if i > 0: ma += i print(max(maxi, ma))
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split(" "))) m = int(input()) b = list(map(int, input().split(" "))) c1 = a.copy() c2 = a.copy() for x in range(m): if b[x] < 0: c1.append(b[x]) c2.insert(0, b[x]) else: c1.insert(0, b[x]) c2.append(b[x]) csum = 0 ans1 = 0 for i in range(len(c1)): csum += c1[i] if csum < 0: csum = 0 ans1 = max(ans1, csum) csum = 0 ans2 = 0 for i in range(len(c2)): csum += c2[i] if csum < 0: csum = 0 ans2 = max(ans2, csum) print(max(ans1, ans2))
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
for tc in range(int(input())): n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) c = [i for i in b if i > 0] maxi = a[0] ans = 0 for i in a + c: if ans < 0: ans = 0 ans += i maxi = max(maxi, ans) ans = 0 for i in c + a: if ans < 0: ans = 0 ans += i maxi = max(maxi, ans) print(maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def calc_max(A): max_, cur_sum = A[0], A[0] for i in range(1, N): cur_sum += A[i] if cur_sum > max_: max_ = cur_sum return max_ for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) M = int(input()) B = list(map(int, input().split())) sum_b = sum([el for el in B if el > 0]) print(max(calc_max(A), calc_max(A[::-1])) + sum_b)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) maxx = 0 maxe = 0 sume = 0 for i in range(n): sume += a[i] maxe = max(maxe, sume) maxx = maxe maxe = 0 sume = 0 for i in range(n - 1, -1, -1): sume += a[i] maxe = max(maxe, sume) maxx = max(maxx, maxe) for i in b: if i > 0: maxx += i print(maxx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) m = int(input()) s = 0 a1 = 0 a2 = 0 s1 = 0 e = 0 b = input().split() for i in range(m): b[i] = int(b[i]) if b[i] > 0: s1 += b[i] else: e += b[i] s = s1 for i in range(n): s += a[i] a1 = max(s, a1) if s < 0: s = 0 a1 = max(s + e, a1) s = e for i in range(n): s += a[i] a2 = max(s, a2) if s < 0: s = 0 a2 = max(s + s1, a2) print(max(a1, a2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def maxSubArraySum(a): max_so_far = a[0] max_ending_here = 0 for i in range(0, len(a)): max_ending_here = max_ending_here + a[i] if max_so_far < max_ending_here: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far for _ in range(int(input())): n = int(input()) nli = list(map(int, input().split(" "))) m = int(input()) mli = list(map(int, input().split(" "))) psli = list(filter(lambda x: x > 0, mli)) ali = nli + psli a = maxSubArraySum(ali) bli = psli + nli b = maxSubArraySum(bli) print(max(a, b))
FUNC_DEF ASSIGN VAR 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 RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING 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 FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def calc(a): ans = cur = 0 for x in a: cur = max(x, x + cur) ans = max(ans, cur) return ans t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) b.sort() pos = 0 while pos < m and b[pos] < 0: pos += 1 print(max(calc(b[pos:] + a), calc(a + b[pos:])))
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) m = int(input()) b = list(map(int, input().split(" "))) l = 0 x = 0 for i in range(n): x += a[i] l = max(l, x) r = 0 x = 0 for i in range(n)[::-1]: x += a[i] r = max(r, x) x = 0 for el in b: if el > 0: x += el print(max(l, r) + x)
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
from sys import maxsize def kadane(arr): n = len(arr) max_till_now = -maxsize - 1 max_ending = 0 for i in range(n): max_ending += arr[i] if max_ending > max_till_now: max_till_now = max_ending if max_ending < 0: max_ending = 0 return max_till_now T = int(input()) while T > 0: N = int(input()) A = list(map(int, input().split())) M = int(input()) B = list(map(int, input().split())) A1 = A.copy() A2 = A.copy() B1 = B.copy() B2 = B.copy() flag = True while len(B1) != 0: if flag: if B1[0] > 0: A1.append(B1[0]) else: A1.insert(0, B1[0]) B1.pop(0) flag = False else: if B1[-1] > 0: A1.append(B1[-1]) else: A1.insert(0, B1[-1]) B1.pop(-1) flag = True flag = True while len(B2) != 0: if flag: if B2[0] < 0: A2.append(B2[0]) else: A2.insert(0, B2[0]) B2.pop(0) flag = False else: if B2[-1] < 0: A2.append(B2[-1]) else: A2.insert(0, B2[-1]) B2.pop(-1) flag = True print(max(kadane(A1), kadane(A2))) T -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t = int(input()) while t: t = t - 1 n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) m = int(input()) b = input().split() sum = 0 for i in range(m): b[i] = int(b[i]) if b[i] > 0: sum = sum + b[i] max1 = -100000000 max2 = -100000000 sum1 = 0 for i in range(n): sum1 = sum1 + a[i] if sum1 > max1: max1 = sum1 sum1 = 0 for i in range(n): sum1 = sum1 + a[n - 1 - i] if sum1 > max2: max2 = sum1 if max1 > max2: print(max1 + sum) else: print(max2 + sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def max_sum_subarray(P): A = [] a = 0 M = [] m = 0 D = [] d = 0 N = [] n = 0 i = 0 while i < len(P): if P[i] < 0: if n + P[i] < 0: D += N + [P[i]] d += n + P[i] N = [] n = 0 i += 1 continue else: N += [P[i]] n += P[i] i += 1 continue else: N += [P[i]] n += P[i] if n > m: if m + d > 0: M = M + D + N m += d + n D = [] d = 0 N = [] n = 0 i += 1 continue else: A += M + D a += m + d M = N m = n D = [] d = 0 N = [] n = 0 i += 1 continue elif n + d > 0: M += D + N m += d + n D = [] d = 0 N = [] n = 0 i += 1 continue i += 1 continue D += N d += n return [[A, M, D], [a, m, d]] def main(): T = int(input()) while T > 0: N = int(input()) A = input().split() A = [int(item) for item in A] M = int(input()) B = input().split() B = [int(item) for item in B] C = max_sum_subarray(A) C = C[1] a = C[0] m = C[1] d = C[2] if d > a: t = m + d else: t = m + a for x in B: if x > 0: t += x if t > m: print(t) else: print(m) T -= 1 return main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER VAR LIST VAR VAR VAR VAR VAR VAR NUMBER VAR LIST VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN LIST LIST VAR VAR VAR LIST VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def calc_max(A, N): max_1 = -N * 10**8 max_2 = -N * 10**8 cur_sum_1 = 0 cur_sum_2 = 0 for i in range(N): cur_sum_1 += A[i] cur_sum_2 += A[N - i - 1] if cur_sum_1 > max_1: max_1 = cur_sum_1 if cur_sum_2 > max_2: max_2 = cur_sum_2 return max(max_1, max_2) for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) M = int(input()) B = list(map(int, input().split())) sum_b = sum([el for el in B if el > 0]) print(calc_max(A, N) + sum_b)
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def ar(): l = list(map(int, input().split())) return l def xy(): x, y = map(int, input().split()) return x, y t = int(input()) for i in range(t): n = int(input()) a = ar() m = int(input()) b = ar() k = sum([i for i in b if i > 0]) def kad(a): maxi = a[0] cur = 0 for i in a: cur += i maxi = max(maxi, cur) if cur < 0: cur = 0 return maxi print(kad(a) + k)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] m = int(input()) b = [*map(int, input().split())] y = sum(max(0, i) for i in b) l = st = 0 r = n ll = 0 bb = 0 ps = [0] for i in a: ps.append(ps[-1] + i) for i in range(n): if ll + a[i] < 0: st = i + 1 ll = 0 else: ll += a[i] if ll > bb: bb = ll r = i l = st print(y + max(ps[l] - ps[0], ps[-1] - ps[r + 1]) + bb)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t = int(input()) for _ in range(t): n1 = int(input()) a = list(map(int, input().split())) n2 = int(input()) b = list(map(int, input().split())) ans1, ans2, sm = 0, 0, 0 for i in range(n1): sm += a[i] ans1 = max(ans1, sm) sm = 0 for i in range(n1 - 1, -1, -1): sm += a[i] ans2 = max(ans2, sm) if ans2 > ans1: for i in b: if i > 0: a.append(i) ans1, sm = 0, 0 for i in range(len(a) - 1, -1, -1): sm += a[i] ans1 = max(ans1, sm) else: for i in b: if i > 0: a = [i] + a ans1, sm = 0, 0 for i in range(len(a)): sm += a[i] ans1 = max(ans1, sm) print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
for i in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] m = int(input()) b = [int(x) for x in input().split()] ans = -1000000000 s = 0 p1 = 0 for i in range(0, n): s += a[i] if s < 0: s = 0 p1 = i + 1 else: p2 = i pass if s > ans: l = [p1, i] ans = s q = a[: l[0]] r = a[l[1] + 1 :] s1 = sum(q) s2 = sum(r) bb = 0 for i in b: if i > 0: bb += i print(bb + ans + max(s1, s2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
t1 = int(input()) for i in range(t1): n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) c = a.copy() for i in range(m): if b[i] > 0: a.append(b[i]) c.insert(0, b[i]) def fun(list1): sum = 0 gsum = list1[0] for i in range(len(list1)): sum += list1[i] if gsum < sum: gsum = sum if sum <= 0: sum = 0 return gsum e = fun(a) f = fun(c) if e > f: print(e) else: print(f)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR 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 NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given two arrays A and B of sizes N and M respectively. You can apply the following operation until the array B is non-empty: Choose either the first or the last element of array B. Insert the chosen element to either the front or the back of array A. Delete the chosen element from array B. For example, let A = [9, 7] and B = [1, 3, 2]. In one operation, we can choose either X = 1 or X = 2 (first or last element of array B). We can insert X in array A and make it either A = [X, 9, 7] or A = [9, 7, X]. The chosen X is deleted from array B. Thus, it will become either B = [3, 2] (when chosen X is 1) or B = [1, 3] (when chosen X is 2). Find the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of 4 lines of input. - The first line of each test contains a single integer N, the size of array A. - The next line contains N space-separated integers, denoting elements of array A. - The third line of each test contains a single integer M, the size of array B. - The next line contains M space-separated integers, denoting elements of array B. ------ Output Format ------ For each test case, output on a new line the maximum sum of any subarray of the array A that you can achieve after performing exactly M operations. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ $-10^{8} ≀ A_{i}, B_{i} ≀ 10^{8}$ ----- Sample Input 1 ------ 3 5 3 26 -79 72 23 2 66 44 1 81 1 -97 5 10 -5 14 -20 4 3 -10 5 -2 ----- Sample Output 1 ------ 205 81 24 ----- explanation 1 ------ Test case $1$: - Operation $1$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66]$ and $B = [44]$. - Operation $2$: Add the first element of array $B$ to the back of array $A$. Thus, $A = [3, 26, -79, 72, 23, 66, 44]$ and $B = []$. The, maximum sum subarray of array $A$ is $[72, 23, 66, 44]$ having sum $72+23+66+44=205$. Test case $2$: - Operation $1$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-97, 81]$ and $B = []$. The, maximum sum subarray of array $A$ is $[81]$ having sum $81$. Test case $3$: - Operation $1$: Add the last element of array $B$ to the back of array $A$. Thus, $A = [10, -5, 14, -20, 4, -2]$ and $B = [-10, 5]$. - Operation $2$: Add the last element of array $B$ to the front of array $A$. Thus, $A = [5, 10, -5, 14, -20, 4, -2]$ and $B = [-10]$. - Operation $3$: Add the first element of array $B$ to the front of array $A$. Thus, $A = [-10, 5, 10, -5, 14, -20, 4, -2]$ and $B = []$. The, maximum sum subarray of array $A$ is $[5, 10, -5, 14]$ having sum $5+10-5+14 = 24$.
def maxsubarray(x): max_sum = max(x) max_tillnow = 0 for i in range(0, len(x)): max_tillnow += x[i] if max_tillnow > max_sum: max_sum = max_tillnow if max_tillnow < 0: max_tillnow = 0 return max_sum tc = int(input()) for _ in range(tc): n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) a1 = [] a2 = [] for i in range(n): a1.append(a[i]) a2.append(a[i]) for i in range(m): if b[i] >= 0: a1.insert(0, b[i]) a2.append(b[i]) else: a1.append(b[i]) a2.insert(0, b[i]) print(max(maxsubarray(a1), maxsubarray(a2)))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
import sys def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) last_pair = -1 res = 0 lhs = 0, a[0] for key, value in enumerate(a): if value != lhs[1]: if lhs[1] != last_pair and value != last_pair: last_pair = -1 if key - lhs[0] > 1: res += 1 if lhs[1] == last_pair: res -= 1 last_pair = lhs[1] res += 1 lhs = key, value res += 1 if n - lhs[0] > 1: res += 1 if lhs[1] == last_pair: res -= 1 print(res) return main()
IMPORT 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
import sys n = int(input()) a = list(map(int, input().split())) pre = [] last = a[0] count = 0 for i in range(n): if last == a[i]: count += 1 continue else: pre.append((last, count)) last = a[i] count = 1 pre.append((last, count)) b = -1 w = -1 ans = 0 for i in range(len(pre)): val = pre[i][0] count = pre[i][1] if count > 1: if b != val and w != val: ans += 2 b = val w = val elif b == val: w = val ans += 1 elif w == val: b = val ans += 1 elif b == val and w == val: pass elif b == val: w = val ans += 1 elif w == val: b = val ans += 1 elif i + 1 < len(pre): val2 = pre[i + 1][0] count2 = pre[i + 1][1] if b == val2: b = val ans += 1 elif w == val2: w = val ans += 1 else: b = val ans += 1 else: b = val ans += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
input() arr = [int(x) for x in input().split()] + [None] seg1 = None seg2 = None score = 0 for i, a in enumerate(arr): if arr[i] == None: break if arr[i + 1] != a: if arr[i + 1] == seg1 and seg1 != a: seg1 = a score += 1 continue elif arr[i + 1] == seg2 and seg2 != a: seg2 = a score += 1 continue if seg1 != a: score += 1 seg1 = a elif seg2 != a: score += 1 seg2 = a print(score)
EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NONE IF VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
import sys def rl(proc=None): if proc is not None: return proc(sys.stdin.readline()) else: return sys.stdin.readline().rstrip() def srl(proc=None): if proc is not None: return list(map(proc, rl().split())) else: return rl().split() def good(r, v): return any(r[i] != v for i in range(1, len(r), 2)) def main(): rl() A = srl(int) v = A[0] c = 0 ret = 0 r = [] last_bulk = -1 A.append(0) for x in A: if x == v: c += 1 else: if c == 1: r.append(v) else: if v != last_bulk: ret += len(r) + 2 last_bulk = v elif good(r, v): ret += len(r) + 2 else: ret += len(r) + 1 r = [] v = x c = 1 assert c == 1 assert v == 0 ret += len(r) print(ret) main()
IMPORT FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE IF VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
n = int(input()) a = list(map(int, input().split())) w = [0] b = [0] for i in range(n): if a[i] != w[-1] and a[i] != b[-1]: if i + 1 <= n - 1 and a[i + 1] == b[-1]: b.append(a[i]) else: w.append(a[i]) elif a[i] != w[-1]: w.append(a[i]) elif a[i] != b[-1]: b.append(a[i]) print(len(w) + len(b) - 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
x = int(input()) l = [int(i) for i in input().split()] c, e1, e2 = 0, 0, 0 for i in range(x): if l[i] != e1 and l[i] != e2: if i < x - 1 and l[i + 1] == e2: c += 1 e2 = l[i] else: c += 1 e1 = l[i] elif l[i] != e1: c += 1 e1 = l[i] elif l[i] != e2: c += 1 e2 = l[i] print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
from sys import stdin input = stdin.readline def main(): n = int(input()) ara = [int(x) for x in input().split()] positions = [[] for i in range(n + 1)] for idx in range(n): positions[ara[idx]].append(idx) next_ara = [n] * (n + 1) for idx in range(n + 1): size = len(positions[idx]) for jdx in range(size): if jdx < size - 1: next_ara[positions[idx][jdx]] = positions[idx][jdx + 1] else: next_ara[positions[idx][jdx]] = n ara.append(n + n) ones = [n] zeros = [n] ans = 0 for idx in range(n): if ara[idx] == ara[ones[-1]] and ara[idx] == ara[zeros[-1]]: ones.append(idx) elif ara[idx] == ara[ones[-1]]: zeros.append(idx) ans += 1 elif ara[idx] == ara[zeros[-1]]: ones.append(idx) ans += 1 elif next_ara[ones[-1]] < next_ara[zeros[-1]]: ones.append(idx) ans += 1 else: zeros.append(idx) ans += 1 print(ans) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
def compact(array): result = [] array.append(-1) start = 0 for i in range(len(array) - 1): if array[i] != array[i + 1]: result.append((array[i], i - start + 1)) start = i + 1 return result n = int(input()) a = list(map(int, input().split())) data = compact(a) result = 0 last_group = -1 between = [] for el, cnt in data: if cnt == 1: result += 1 between.append(el) else: if last_group != el: result += 2 elif ( len(between) > 0 and (len(between) % 2 == 1 and between.count(el) >= (len(between) - 1) // 2) or len(between) == 1 ): result += 1 else: result += 2 between = [] last_group = el print(result)
FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
n = int(input()) a1 = list(map(int, input().split())) tmp1, tmp2, ans1 = 0, 0, 0 for i in range(n): if a1[i] == tmp1 and a1[i] == tmp2: continue elif a1[i] != tmp1 and a1[i] != tmp2: if i + 1 < n and a1[i + 1] == tmp1: tmp1 = a1[i] else: tmp2 = a1[i] elif a1[i] != tmp1: tmp1 = a1[i] elif a1[i] != tmp2: tmp2 = a1[i] ans1 += 1 print(ans1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
n = int(input()) aa = [int(x) for x in input().split()] segcount = 0 choices = {(-1, -1)} for i in range(n): a = aa[i] new_choices = set() have_plus = False num_awhite = 0 num_ablack = 0 for choice in choices: last_white, last_black = choice if num_awhite <= 2 and a != last_white: have_plus = True new_choices.add((a, last_black)) num_awhite += 1 if num_ablack <= 2 and a != last_black: have_plus = True new_choices.add((last_white, a)) num_ablack += 1 if have_plus == True: segcount += 1 choices = new_choices print(segcount)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
import sys input = sys.stdin.readline getint = lambda: int(input()) getints = lambda: [int(a) for a in input().split()] def solve(): n = getint() values = getints() pos = [(-1) for i in range(n + 1)] next = [n for i in range(n)] for i in range(n): if pos[values[i]] != -1: next[pos[values[i]]] = i pos[values[i]] = i one = -1 two = -1 onei = -1 twoi = -1 segments = 0 for i in range(n): v = values[i] if v == two: segments += 1 if v != one else 0 one = v onei = i elif v == one: segments += 1 two = v twoi = i elif next[onei] > next[twoi]: segments += 1 two = v twoi = i else: segments += 1 one = v onei = i print(segments) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
def painting_the_array_i(array): count = 0 last1 = None last2 = None buffer = None buffer_count = 0 for i in array: if last1 is None or i != last1 and last2 is None: last1 = i count += 1 elif last2 is None: last2 = i count += 1 elif last1 == last2: if i != last1: count += 1 last1 = i elif i == last1 and buffer is None: last2 = i count += 1 elif i == last2 and buffer is None: last1 = i count += 1 elif i == buffer: last1 = last2 = i count += buffer_count count += 1 buffer = None buffer_count = 0 else: buffer = i buffer_count += 1 return count + buffer_count n = int(input()) array = [int(i) for i in input().split()] print(painting_the_array_i(array))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP 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
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
n = int(input()) arr = [int(s) for s in input().split(" ")] ans = 0 lastArray1 = 0 lastArray2 = 0 for i in range(n): if arr[i] != lastArray1 and arr[i] != lastArray2: if i + 1 < n and arr[i + 1] == lastArray2: lastArray2 = arr[i] else: lastArray1 = arr[i] ans += 1 elif arr[i] != lastArray1: lastArray1 = arr[i] ans += 1 elif arr[i] != lastArray2: lastArray2 = arr[i] ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
def segmenti(a, n): if n == 0: print("0") elif n == 1: print("1") elif n == 2: print("2") else: beli = ["brt"] nb = 1 crni = ["brt"] nc = 1 i = 0 seg = 0 while i < n - 1: if beli[nb - 1] == crni[nc - 1]: if beli[nb - 1] != a[i]: seg += 1 beli.append(a[i]) nb += 1 elif a[i] == a[i + 1]: if a[i] != beli[nb - 1]: beli.append(a[i]) nb += 1 seg += 1 else: seg += 1 crni.append(a[i]) nc += 1 elif a[i] == beli[nb - 1]: seg += 1 crni.append(a[i]) nc += 1 elif a[i] == crni[nc - 1]: seg += 1 beli.append(a[i]) nb += 1 elif a[i + 1] == beli[nb - 1]: seg += 1 beli.append(a[i]) nb += 1 else: seg += 1 crni.append(a[i]) nc += 1 i += 1 if a[i] != beli[nb - 1] or a[i] != crni[nc - 1]: seg += 1 print(seg) n = int(input()) a = input().split() a = [int(i) for i in a] segmenti(a, n)
FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
n = int(input()) a = [0] * 400000 seg = [0] * 400000 lenSeg = [0] * 400000 row = input() row = row.split(" ") cnt = 0 for i in range(0, n + 1): if i != n: a[i] = int(row[i]) else: a[i] = -1 if i != 0 and a[i] != a[i - 1]: seg[cnt] = a[i - 1] cnt += 1 lenSeg[cnt] += 1 res = 0 mid = -1 long = -1 for i in range(0, cnt): if lenSeg[i] == 1: res += 1 elif lenSeg[i] > 1: if long != -1 and seg[long] == seg[i]: if long < mid: res += 2 else: res += 1 if seg[long] != seg[i] or long == -1: res += 2 long = i if ( seg[i + 1] != seg[i - 1] and i < cnt - 1 and seg[i] != seg[long] and lenSeg[i] == 1 ): mid = i print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $a_1, a_2, \dots, a_n$ with two kinds of colors, white and black. A painting assignment for $a_1, a_2, \dots, a_n$ is described by an array $b_1, b_2, \dots, b_n$ that $b_i$ indicates the color of $a_i$ ($0$ for white and $1$ for black). According to a painting assignment $b_1, b_2, \dots, b_n$, the array $a$ is split into two new arrays $a^{(0)}$ and $a^{(1)}$, where $a^{(0)}$ is the sub-sequence of all white elements in $a$ and $a^{(1)}$ is the sub-sequence of all black elements in $a$. For example, if $a = [1,2,3,4,5,6]$ and $b = [0,1,0,1,0,0]$, then $a^{(0)} = [1,3,5,6]$ and $a^{(1)} = [2,4]$. The number of segments in an array $c_1, c_2, \dots, c_k$, denoted $\mathit{seg}(c)$, is the number of elements if we merge all adjacent elements with the same value in $c$. For example, the number of segments in $[1,1,2,2,3,3,3,2]$ is $4$, because the array will become $[1,2,3,2]$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $0$. Homer wants to find a painting assignment $b$, according to which the number of segments in both $a^{(0)}$ and $a^{(1)}$, i.e. $\mathit{seg}(a^{(0)})+\mathit{seg}(a^{(1)})$, is as large as possible. Find this number. -----Input----- The first line contains an integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq n$). -----Output----- Output a single integer, indicating the maximal possible total number of segments. -----Examples----- Input 7 1 1 2 2 3 3 3 Output 6 Input 7 1 2 3 4 5 6 7 Output 7 -----Note----- In the first example, we can choose $a^{(0)} = [1,2,3,3]$, $a^{(1)} = [1,2,3]$ and $\mathit{seg}(a^{(0)}) = \mathit{seg}(a^{(1)}) = 3$. So the answer is $3+3 = 6$. In the second example, we can choose $a^{(0)} = [1,2,3,4,5,6,7]$ and $a^{(1)}$ is empty. We can see that $\mathit{seg}(a^{(0)}) = 7$ and $\mathit{seg}(a^{(1)}) = 0$. So the answer is $7+0 = 7$.
import sys input = sys.stdin.readline flush = sys.stdout.flush def stupid(pos, p1, p2, c1, c2, a): if pos == len(a): return p1 + p2 return max( stupid(pos + 1, a[pos], p2, c1 + int(p1 != a[pos]), c2, a), stupid(pos + 1, p1, a[pos], c1, c2 + int(p2 != a[pos]), a), ) def solve(): n = int(input()) a = list(map(int, input().split())) p = -1 p1 = -1 pos1 = -1 c1 = 0 c2 = 0 j = 0 for i in a: if i != p: p = i c1 += 1 else: if i != p1: c2 += 1 else: for k in range(pos1 + 1, j): if a[k] != p1 and a[k - 1] != a[k + 1]: c2 += 1 break p1 = i pos1 = j j += 1 print(c1 + c2) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def r(a): s = [0] for i in range(len(a)): s.append(s[-1] + a[i]) return s[::-1] for _ in range(int(input())): n = int(input()) a = list(map(lambda x: [0, 1, -1][x], list(map(int, input().split())))) a, b = a[:n], a[n:][::-1] ra, rb = r(a), r(b) rb = sorted([(rb[i], i) for i in range(n + 1)]) d = 4 * n for i in range(n + 1): e = ra[i] L, R = 0, n t = 10000000000, 8 * n while L <= R: M = (L + R) // 2 if rb[M][0] >= -e: t = rb[M] R = M - 1 else: L = M + 1 if t[0] + e == 0 and t[1] < 4 * n: d = min(d, i + t[1]) print(d)
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER 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 LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) x = list(map(int, input().split())) d = {(0): 0} c = 0 for i in range(n, 2 * n): if x[i] == 1: c += 1 else: c -= 1 if c not in d: d[c] = i - n + 1 diff = x.count(1) - x.count(2) ans = 2 * n c = 0 for i in range(n - 1, -1, -1): if x[i] == 1: c += 1 else: c -= 1 if diff - c in d: ans = min(ans, d[diff - c] + n - i) if diff in d: ans = min(ans, d[diff]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def solve(): n = int(input()) nums = list(map(int, input().split(" "))) sum = [] for i in range(len(nums)): if nums[i] == 2: nums[i] = -1 tem = 0 pos = {} pos[0] = -1 for i in range(len(nums)): tem += nums[i] sum.append(tem) if i < n: pos[tem] = i ans = 2 * n for i in range(n - 1, len(nums)): if sum[i] - sum[-1] in pos: ans = min(ans, i - pos[sum[i] - sum[-1]]) print(ans) N = int(input()) for _ in range(N): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) count = 0 add = 0 i = 1 dict = {} dict[0] = 0 total = 0 for j in range(n, 2 * n): if a[j] == 1: count += 1 total += 1 else: add += 1 total -= 1 if total not in dict: dict[total] = i i += 1 b = [0] total = 0 for i in range(n - 1, -1, -1): if a[i] == 1: total += 1 count += 1 else: total -= 1 add += 1 b.append(total) d = add - count if d == 0: print(0) continue mini = 9999999999 for i in range(n + 1): if -(b[i] + d) in dict: mini = min(mini, i + dict[-(b[i] + d)]) print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
q = int(input()) for _ in range(q): n = int(input()) lst = list(map(int, input().split(" "))) cl = [[(0) for _ in range(2)] for _ in range(n + 1)] cr = [[(0) for _ in range(2)] for _ in range(n + 1)] for i in range(1, n + 1): if lst[n - 1 + i] == 1: cr[i][0] = cr[i - 1][0] - 1 else: cr[i][0] = cr[i - 1][0] + 1 if lst[n - i] == 1: cl[i][0] = cl[i - 1][0] - 1 else: cl[i][0] = cl[i - 1][0] + 1 cl[i][1] = i cr[i][1] = i c = cl[-1][0] + cr[-1][0] if c == 0: print(0) continue cl.sort() cr.sort() for i in range(n, 0, -1): if cl[i][0] == cl[i - 1][0]: if cl[i][1] > cl[i - 1][1]: cl.pop(i) else: cl.pop(i - 1) if cr[i][0] == cr[i - 1][0]: if cr[i][1] > cr[i - 1][1]: cr.pop(i) else: cr.pop(i - 1) x = 0 y = len(cr) - 1 m = 2 * n while y >= 0 and x < len(cl): if c == cl[x][0] + cr[y][0]: if m > cl[x][1] + cr[y][1]: m = cl[x][1] + cr[y][1] x = x + 1 y = y - 1 elif c < cl[x][0] + cr[y][0]: y = y - 1 else: x = x + 1 print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().strip().split())) left = a[:n] right = a[n:] red = 0 blue = 0 balance_left = [0] * (n + 1) balance_right = [0] * (n + 1) for j in range(n): if left[-j - 1] == 1: red += 1 balance_left[j + 1] = balance_left[j] + 1 else: blue += 1 balance_left[j + 1] = balance_left[j] - 1 if right[j] == 1: red += 1 balance_right[j + 1] = balance_right[j] + 1 else: blue += 1 balance_right[j + 1] = balance_right[j] - 1 difference = red - blue min_number_of_cans = 10**6 potential_answers_dict = {} potential_ans_set = set() for left_index in range(n + 1): if difference - balance_left[left_index] in potential_ans_set: pass else: potential_ans_set.add(difference - balance_left[left_index]) potential_answers_dict[difference - balance_left[left_index]] = left_index for right_index in range(n + 1): if balance_right[right_index] in potential_ans_set: tmp_min = right_index + potential_answers_dict[balance_right[right_index]] if tmp_min < min_number_of_cans: min_number_of_cans = tmp_min print(min_number_of_cans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import itertools def solve(): n = int(input()) a = list(map(int, input().split())) a = list(map({(1): 1, (2): -1}.__getitem__, a)) l = a[:n] l.reverse() r = a[n:] l = [0] + list(itertools.accumulate(l)) r = [0] + list(itertools.accumulate(r)) d = {} for i in range(n + 1): if l[i] not in d: d[l[i]] = i s = sum(a) ans = n * 2 for i in range(n + 1): if s - r[i] in d: ans = min(ans, i + d[s - r[i]]) print(ans) t = int(input()) for _ in range(t): solve()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR DICT NUMBER NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) jars = [int(x) for x in input().split()] left = [0] * (n + 1) right = [0] * (n + 1) for x in range(1, n + 1): if jars[x - 1] == 1: left[x] = left[x - 1] + 1 else: left[x] = left[x - 1] - 1 if jars[2 * n - x] == 1: right[x] = right[x - 1] + 1 else: right[x] = right[x - 1] - 1 a = {} b = {} for l in range(len(left)): a[left[l]] = l for r in range(len(right)): b[right[r]] = 2 * n - r + 1 ans = 2 * n for key in a: if -key in b: ans = min(ans, b[-key] - (a[key] + 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) dif = [0] for i in a: if i == 1: dif.append(dif[-1] + 1) else: dif.append(dif[-1] - 1) dif_init = dif[-1] dic = {} ans = 2 * n for i, v in enumerate(dif): if i < n + 1: dic[v] = i if i > n - 1: t = v - dif_init if t in dic: ans = min(ans, i - dic[t]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for _ in [0] * int(input()): n = int(input()) s = list(map(int, input().split())) Al = [0] * (n + 1) Ar = [0] * (n + 1) count = 0 for l in range(1, n + 1): if s[n - l] == 2: Al[l] = Al[l - 1] + 1 count += 1 else: Al[l] = Al[l - 1] - 1 count -= 1 for r in range(1, n + 1): if s[n + r - 1] == 2: Ar[r] = Ar[r - 1] + 1 count += 1 else: Ar[r] = Ar[r - 1] - 1 count -= 1 if count == 0: print(0) else: minstep = 2 * n p = {} for l in range(n, -1, -1): p[Al[l]] = l for r in range(n + 1): k = count - Ar[r] if k in p: minstep = min(minstep, p[k] + r) print(minstep)
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys readline = sys.stdin.readline T = int(readline()) Ans = [None] * T inf = 10**9 + 7 for qu in range(T): N = int(readline()) A = list(map(int, readline().split())) bj = A.count(1) sj = 2 * N - bj x = sj - bj A1 = [(3 - 2 * a) for a in A[:N][::-1]] A2 = [(3 - 2 * a) for a in A[N:]] for i in range(1, N): A1[i] += A1[i - 1] A2[i] += A2[i - 1] geta = -min(0, min(A2)) + 1 mA = max(0, max(A2)) idx = [inf] * (mA + geta + 1) idx[geta + 0] = 0 for i in range(N): a2 = A2[i] idx[geta + a2] = min(idx[geta + a2], i + 1) ans = inf A1 = [0] + A1 for i in range(N + 1): a1 = A1[i] if -geta <= -a1 - x <= mA: ans = min(ans, i + idx[geta - a1 - x]) Ans[qu] = ans print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
from itertools import accumulate t = int(input()) for _ in range(t): n = int(input()) jars = [(1 if i == "1" else -1) for i in input().split()] left = list(accumulate(jars[0:n])) right = list(accumulate(jars[2 * n - 1 : n - 1 : -1])) left_dict = {(0): 0} right_dict = {(0): 0} for i in range(n): left_dict[left[i]] = i + 1 right_dict[right[i]] = i + 1 ans = 0 for k in left_dict: if -k in right_dict: ans = max(ans, left_dict[k] + right_dict[-k]) print(2 * n - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys from itertools import accumulate q = int(input()) def process(a, interesting): result = [] current_count = 0 balance = 0 for x in a: current_count += 1 if x == interesting: if balance == 0: result.append(current_count) current_count = 0 else: balance += 1 else: balance -= 1 return result def cumsum(a): return list(accumulate(a)) for _ in range(q): n = int(input()) a = list(map(int, input().split(" "))) left = list(reversed(a[:n])) right = a[n:] balance = 0 for x in a: if x == 1: balance += 1 else: balance -= 1 if balance == 0: print(0) continue else: interesting = 1 if balance < 1: interesting = 2 lp = cumsum(process(left, interesting)) rp = cumsum(process(right, interesting)) best_result = 10**9 balance = abs(balance) i = balance if balance >= len(lp): i = len(lp) - 1 j = 0 while i >= 0: while i + j + 2 < balance and j < len(rp): j += 1 if j == len(rp): break best_result = min(best_result, lp[i] + rp[j]) i -= 1 while j < len(rp): if j + 1 >= balance: best_result = min(best_result, rp[j]) j += 1 for i in range(len(lp)): if i + 1 >= balance: best_result = min(best_result, lp[i]) print(best_result)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = a[:n][::-1] c = a[n:] dif_b = [0] * (n + 1) dif_c = [0] * (n + 1) min_c = {} for i in range(1, n + 1): if b[i - 1] == 2: dif_b[i] = 1 else: dif_b[i] = -1 if c[i - 1] == 2: dif_c[i] = 1 else: dif_c[i] = -1 dif_b[i] += dif_b[i - 1] dif_c[i] += dif_c[i - 1] for i in range(n + 1)[::-1]: min_c[dif_c[i]] = i dif_init = dif_b[-1] + dif_c[-1] ans = 10**18 for i in range(n + 1): x = dif_init - dif_b[i] if x in min_c: ans = min(ans, i + min_c[x]) print(ans)
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 VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for t in range(int(input())): n = 2 * int(input()) jars = list(map(int, input().split())) ones = jars.count(1) twos = jars.count(2) if ones > twos: for idx in range(len(jars)): jars[idx] = 3 - jars[idx] ones, twos = twos, ones diff = twos - ones halfway = n // 2 left_best = [-1] * (n + 2) right_best = [-1] * (n + 2) left_best[0] = 0 right_best[0] = 0 current_diff = 0 for idx in range(halfway - 1, -1, -1): if jars[idx] == 1: current_diff -= 1 else: current_diff += 1 if current_diff > 0 and left_best[current_diff] == -1: left_best[current_diff] = halfway - idx current_diff = 0 for idx in range(halfway, n): if jars[idx] == 1: current_diff -= 1 else: current_diff += 1 if current_diff > 0 and right_best[current_diff] == -1: right_best[current_diff] = idx - halfway + 1 best = 1000000 for left_diff in range(n): if left_best[left_diff] == -1 or left_diff > diff: break if right_best[diff - left_diff] == -1: continue best = min(best, left_best[left_diff] + right_best[diff - left_diff]) assert best < 1000000 print(best)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def f(m, arr, max): sum = 0 extra = 1 for i in range(len(arr)): if extra > max: break sum += arr[i] if sum > 0: sum = 0 m[extra] = i + 1 extra += 1 for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr = [(1 if x == 1 else -1) for x in arr] sum = 0 for x in arr: sum += x if sum == 0: print("0") continue if sum < 0: for i in range(len(arr)): arr[i] *= -1 sum *= -1 left = arr[:n] left.reverse() right = arr[n:] l_map = [0] + [float("inf")] * sum r_map = [0] + [float("inf")] * sum f(l_map, left, sum) f(r_map, right, sum) ans = float("inf") for i in range(0, sum + 1): temp = l_map[i] + r_map[sum - i] if temp < ans: ans = temp print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
n = int(input()) def calc(a): cs = 0 for v in a: cs += 1 if v == 1 else -1 rcs = cs for m, v in enumerate(a): cs -= 1 if v == 1 else -1 a[m] = cs return rcs for i in range(n): k = int(input()) a = list(map(int, input().split(" "))) b = 0 l = a[k - 1 :: -1] r = a[k:] lc = calc(l) rc = calc(r) l = [lc, *l] r = [rc, *r] ml = {} for p, v in enumerate(l): if v in ml: ml[v] = min(p, ml[v]) else: ml[v] = p ans = int(1000000000.0) for p, v in enumerate(r): if -v in ml: ans = min(ans, ml[-v] + p) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys t = int(sys.stdin.readline()) ans_arr = [] for x in range(t): n = int(sys.stdin.readline()) a = [int(i) for i in sys.stdin.readline().split()] d = {} r = 0 b = 0 for i in range(2 * n): if a[i] == 1: r += 1 else: b += 1 if r == b: ans_arr.append(str(0)) else: r1 = 0 b1 = 0 for e in range(n - 1, -1, -1): if a[e] == 1: r1 += 1 else: b1 += 1 if r1 - b1 not in d: d[r1 - b1] = e ans = 2 * n r2 = 0 b2 = 0 for f in range(n, 2 * n): if a[f] == 1: r2 += 1 else: b2 += 1 if r - b - (r2 - b2) in d: ans = min(ans, f - d[r - b - (r2 - b2)] + 1) rend = 0 bend = 0 for g in range(n - 1, -1, -1): if a[g] == 1: rend += 1 else: bend += 1 if r - rend == b - bend: ans = min(ans, n - g) rsta = 0 bsta = 0 for h in range(n, 2 * n): if a[h] == 1: rsta += 1 else: bsta += 1 if r - rsta == b - bsta: ans = min(ans, h - n + 1) ans_arr.append(str(ans)) print("\n".join(ans_arr))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a, b = a[n - 1 :: -1], a[n:] ma, mb = {(0): 0}, {(0): 0} pa = pb = 0 for i in range(n): pa += 1 if a[i] == 1 else -1 pb += 1 if b[i] == 1 else -1 if pa not in ma: ma[pa] = i + 1 if pb not in mb: mb[pb] = i + 1 total = pa + pb ans = 10**9 for da, va in list(ma.items()): req = total - da if req in mb: ans = min(ans, va + mb[req]) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR DICT NUMBER NUMBER DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys t = int(input()) for case in range(t): n = int(input()) full_list = [int(f) for f in input().split()] l_list = full_list[0:n] r_list = full_list[n : 2 * n] diff_init = 0 for v in full_list: diff_init = diff_init + (1 if v == 1 else -1) if diff_init == 0: print(0) continue diff_r = 0 dic1 = {} for index, v in enumerate(r_list): diff_r += 1 if v == 1 else -1 if diff_r not in dic1: dic1[diff_r] = index + 1 min_r_only = 1000000000 if diff_init in dic1: min_r_only = dic1[diff_init] diff_l = 0 min_l_r = 1000000000 min_l_only = 1000000000 for index, v in enumerate(reversed(l_list)): diff_l += 1 if v == 1 else -1 key = diff_init - diff_l if key == 0 and min_l_only == 1000000000: min_l_only = index + 1 if key in dic1: min_l_r = min(min_l_r, index + 1 + dic1[key]) print(min(min_r_only, min_l_only, min_l_r))
IMPORT 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 VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for nt in range(int(input())): n = int(input()) l = list(map(int, input().split())) two, one = 0, 0 for i in l: if i == 1: one += 1 else: two += 1 if one == 0 or two == 0: print(2 * n) continue if one == two: print(0) continue ans = [] if True: diff = one - two d1 = {} prev1, prev2 = 0, 0 for i in range(n - 1, -1, -1): if l[i] == 1: if str(prev1 + 1 - prev2) not in d1: d1[str(prev1 + 1 - prev2)] = n - i prev1 += 1 else: if str(prev1 - prev2 - 1) not in d1: d1[str(prev1 - prev2 - 1)] = n - i prev2 += 1 d2 = {} prev1, prev2 = 0, 0 for i in range(n, 2 * n): if l[i] == 1: if str(prev1 + 1 - prev2) not in d2: d2[str(prev1 + 1 - prev2)] = i - n + 1 prev1 += 1 else: if str(prev1 - prev2 - 1) not in d2: d2[str(prev1 - prev2 - 1)] = i - n + 1 prev2 += 1 if str(diff) in d1: ans.append(d1[str(diff)]) for i in range(n, 2 * n): if l[i] == 1: diff -= 1 else: diff += 1 if str(diff) in d1: ans.append(d1[str(diff)] + i - n + 1) diff = one - two if str(diff) in d2: ans.append(d2[str(diff)]) for i in range(n - 1, -1, -1): if l[i] == 1: diff -= 1 else: diff += 1 if str(diff) in d2: ans.append(d2[str(diff)] + n - i) print(min(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
M = 100000000000 for _ in range(int(input())): n = int(input()) A = [int(_) for _ in input().split()] A = [(+1 if _ == 1 else -1) for _ in A] bal = sum(A) B, C = A[:n][::-1], A[n:] b, c = {(0): 0}, {(0): 0} for i in range(n): if i > 0: B[i] += B[i - 1] C[i] += C[i - 1] b[B[i]] = min(b.get(B[i], M), i + 1) c[C[i]] = min(c.get(C[i], M), i + 1) bst = M for k, v in b.items(): bst = min(bst, c.get(bal - k, M) + v) print(bst)
ASSIGN VAR NUMBER 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 NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR DICT NUMBER NUMBER DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def solve(): n = int(input()) a = list(map(int, input().split())) red = a.count(1) blue = 2 * n - red s = red - blue if s == 0: print(0) return cur = 0 d = {(0): 0} for i in range(n, 2 * n): if a[i] == 2: cur -= 1 else: cur += 1 if cur not in d: d[cur] = i - n + 1 ans = float("inf") need = s cur = 0 if need in d: ans = min(ans, d[need]) for i in reversed(list(range(n))): if a[i] == 2: cur -= 1 else: cur += 1 if need - cur in d: ans = min(ans, d[need - cur] + n - i) print(ans) t = int(input()) for i in range(t): solve()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
q = int(input()) for rwier in range(q): n = int(input()) l = list(map(int, input().split())) j = l.count(1) d = l.count(2) pr = [0] * n le = [0] * n pr[0] = 1 if l[n] == 1 else -1 le[0] = 1 if l[n - 1] == 1 else -1 for i in range(1, n): pr[i] = pr[i - 1] + (1 if l[n + i] == 1 else -1) le[i] = le[i - 1] + (1 if l[n - i - 1] == 1 else -1) if j - d < 0: for i in range(n): pr[i] = -pr[i] le[i] = -le[i] ab = abs(j - d) if ab == 0: print(0) else: najwp = [123456789] * (2 * n + 1) najwl = [123456789] * (2 * n + 1) le = [0] + le pr = [0] + pr for i in range(n + 1): if pr[i] >= 0 and najwp[pr[i]] == 123456789: najwp[pr[i]] = i if le[i] >= 0 and najwl[le[i]] == 123456789: najwl[le[i]] = i wyn = 41343443143 for i in range(ab + 1): if najwp[i] + najwl[ab - i] < wyn: wyn = najwp[i] + najwl[ab - i] print(wyn)
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 NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def inp(ai): cur = sum(ai) * 2 - n * 3 yield cur for aii in ai: if aii == 1: cur += 1 else: cur -= 1 yield cur def inp2(ii): ans = {} for i, iii in enumerate(ii): if iii not in ans: ans[iii] = i return ans for _ in range(int(input())): n = int(input()) a = iter(map(int, input().split())) a1 = [next(a) for _ in range(n)] a1.reverse() a2 = list(a) i1 = inp2(inp(a1)) i2 = inp2(-i2i for i2i in inp(a2)) res = n * 2 for k in i1.keys(): if k in i2.keys(): res = min(res, i1[k] + i2[k]) print(res)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) while t > 0: n = int(input()) ones = 0 twos = 0 a = [int(i) for i in input().split()] d1 = {} d2 = {} for i in a: if i == 1: ones += 1 else: twos += 1 if ones == twos: print(0) t -= 1 continue else: pos = 0 if ones > twos: diff = ones - twos pos = 1 else: diff = twos - ones pos = 2 k = diff diffa = [(0) for i in range(2 * n + 5)] for i in range(n - 1, -1, -1): if a[i] == pos: k -= 1 else: k += 1 diffa[i] = k if d1.get(k, -1) == -1: d1[k] = n - i if k == 0: break k = diff for i in range(n, 2 * n): if a[i] == pos: k -= 1 else: k += 1 diffa[i] = k if d2.get(k, -1) == -1: d2[k] = i + 1 - n if k == 0: break ans = 10000000 for i in range(n - 1, -1, -1): if diffa[i] == 0: ans = min(ans, n - i) else: need = diff - diffa[i] cur = d2.get(need, None) if cur != None: ans = min(ans, cur + (n - i)) for i in range(n, 2 * n): if diffa[i] == 0: ans = min(ans, i + 1 - n) else: need = diff - diffa[i] cur = d1.get(need, None) if cur != None: ans = min(ans, cur + (i + 1 - n)) print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for test in range(t): n = int(input()) ans = list() blue = 0 straw = 0 mas = [int(i) for i in input().split()] for i in mas: if i == 2: blue += 1 else: straw += 1 state = (n * 2 + 1) * [0] bl = 0 st = 0 min_ = 10**6 if straw == blue: print(0) continue for i in range(n - 1, -1, -1): if mas[i] == 1: st += 1 else: bl += 1 if state[st - bl + n] == 0: state[st - bl + n] = n - i if straw - st == blue - bl: min_ = min(min_, n - i) for i in range(n, 2 * n): if mas[i] == 1: straw -= 1 else: blue -= 1 if 0 <= straw - blue + n <= 2 * n: if straw == blue: min_ = min(min_, i - n + 1) if state[straw - blue + n] != 0: min_ = min(min_, i - n + 1 + state[straw - blue + n]) print(min_)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
from sys import stdin di = {"1": 1, "2": -1} a = lambda: stdin.readline().split() def f(): n = int(stdin.readline()) lst = [di[x] for i, x in enumerate(a())] straw = lst[:n] blue = lst[-1 : -n - 1 : -1] d, count = {(0): 0}, 0 for i, x in enumerate(straw): count += x d[count] = i + 1 count, res = 0, d[0] for i, x in enumerate(blue): count += x if d.get(-count) != None: res = max(res, d[-count] + i + 1) print(2 * n - res) for _ in range(int(stdin.readline())): f()
ASSIGN VAR DICT STRING STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
fg = int(input()) for ghj in range(fg): input() l = input().split(" ") for k in range(0, len(l)): if l[k] == "1": l[k] = -1 if l[k] == "2": l[k] = 1 n = len(l) // 2 s = 0 h = ["" for i in range(0, len(l) + 1)] o = ["" for i in range(0, len(l) + 1)] h[n] = -1 o[n] = -1 for i in range(0, n): s += int(l[i]) h[s + n] = i su = 0 for j in range(0, n): su -= int(l[len(l) - j - 1]) o[su + n] = j mn = len(l) for p in range(0, len(l) + 1): if o[p] != "" and h[p] != "": if len(l) - o[p] - h[p] - 2 < mn: mn = len(l) - o[p] - h[p] - 2 print(mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(0, t): n = int(input()) a = [int(i) for i in input().split()] nos, nob = a.count(1), a.count(2) if nos == nob: ans = 0 else: s, b = 0, 0 ans = 2 * n diff = {} for i in range(0, n): if a[i] == 1: s += 1 else: b += 1 diff[s - b] = i if b == s: ans = min(ans, 2 * n - i - 1) s, b = 0, 0 for i in range(2 * n - 1, n - 1, -1): if a[i] == 1: s += 1 else: b += 1 if b - s in diff: ans = min(ans, i - diff[b - s] - 1) if b == s: ans = min(ans, i) 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 VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
T = int(input()) for iiiiiii in range(T): n = int(input()) A = list(map(int, input().strip().split())) L = A[:n] L = L[::-1] L1 = [0] * (n + 1) L2 = [0] * (n + 1) R = A[n:] R1 = [0] * (n + 1) R2 = [0] * (n + 1) A = 0 for i in range(n - 1, -1, -1): if L[i] == 1: L1[i] = L1[i + 1] + 1 L2[i] = L2[i + 1] else: L2[i] = L2[i + 1] + 1 L1[i] = L1[i + 1] if R[i] == 1: R1[i] = R1[i + 1] + 1 R2[i] = R2[i + 1] else: R2[i] = R2[i + 1] + 1 R1[i] = R1[i + 1] d_L = {} keys_L = set() keys_R = set() d_R = {} for i in range(n + 1): a = L1[i] - L2[i] if a in keys_L: continue else: keys_L.add(a) d_L[a] = i a = R1[i] - R2[i] if a in keys_R: continue else: keys_R.add(a) d_R[a] = i X = [] for i in range(n + 1): a = L1[i] - L2[i] if -1 * a in keys_R: X.append(i + d_R[-1 * a]) a = R1[i] - R2[i] if -1 * a in keys_L: X.append(i + d_L[-1 * a]) print(min(X))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) inp_arr = list(map(lambda x: int(x), input().split(" "))) arr1 = inp_arr[n - 1 :: -1] arr2 = inp_arr[n : 2 * n : 1] for j in range(n): if arr1[j] == 2: arr1[j] = -1 if arr2[j] == 2: arr2[j] = -1 pre1 = [0, arr1[0]] pre2 = [0, arr2[0]] val = sum(arr1) + sum(arr2) for j in range(1, n): pre1.append(pre1[-1] + arr1[j]) pre2.append(pre2[-1] + arr2[j]) if val == 0: ans = 0 elif val > 0: list_1 = [0] list_2 = [0] for j in range(1, n + 1): if pre1[j] > len(list_1) - 1: list_1.append(j) if pre2[j] > len(list_2) - 1: list_2.append(j) min_val = 2 * n for j in range(max(val + 1 - len(list_2), 0), min(len(list_1), val + 1)): if list_1[j] + list_2[val - j] < min_val: min_val = list_1[j] + list_2[val - j] ans = min_val elif val < 0: list_1 = [0] list_2 = [0] for j in range(1, n + 1): if -pre1[j] > len(list_1) - 1: list_1.append(j) if -pre2[j] > len(list_2) - 1: list_2.append(j) min_val = 2 * n for j in range(max(-val + 1 - len(list_2), 0), min(len(list_1), -val + 1)): if list_1[j] + list_2[-val - j] < min_val: min_val = list_1[j] + list_2[-val - j] ans = min_val print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def solve(arr, n, ans): sums = {} total = 0 for i in range(n): if arr[2 * n - i - 1] == 1: total += 1 else: total -= 1 if total not in sums.keys(): sums[total] = -1 sums[total] = i + 1 total = 0 min_val = 2 * n for x in range(n + 1): if x > 0: if arr[x - 1] == 1: total += 1 else: total -= 1 if total == 0: if total in sums.keys(): min_val = min(min_val, n - x + n - sums[total]) else: min_val = min(min_val, n - x + n) elif -total in sums.keys(): min_val = min(min_val, n - x + n - sums[-total]) ans.append(min_val) def main(): t = int(input()) ans = [] for i in range(t): n = int(input()) arr = list(map(int, input().split())) solve(arr, n, ans) for i in ans: print(i) main()
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def solve(n, a): mp = {(0): 0} s = 0 for i in range(n - 1, -1, -1): s += a[i] if s not in mp: mp[s] = n - i total = sum(a) ans = float("INF") if total in mp: ans = min(ans, mp[total]) s = 0 for i in range(n, 2 * n): s += a[i] if total - s in mp: ans = min(ans, mp[total - s] + i - n + 1) print(ans) def main(): stdize = lambda x: -1 if x == 1 else 1 inp = lambda: [stdize(int(x)) for x in input().split()] tc = int(input()) for _ in range(tc): n = int(input()) a = inp() solve(n, a) main()
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys reader = (map(int, s.split()) for s in sys.stdin) (t,) = next(reader) for _ in range(t): (n,) = next(reader) a = [(2 * x - 3) for x in next(reader)] right = [a[i] for i in range(n, 2 * n)] left = [a[i] for i in range(n - 1, -1, -1)] eat_right = {(0): 0} sum_r = 0 for i, el in enumerate(right): sum_r += el if sum_r not in eat_right: eat_right[sum_r] = i + 1 ineq = sum(a) best = 2 * n sum_l = 0 i = 0 while i <= n: if ineq - sum_l in eat_right: best = min(best, i + eat_right[ineq - sum_l]) if i < n: sum_l += left[i] i += 1 print(best)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def stdize(x): return 1 if x == 2 else -1 def solve(): n = int(input()) arr = [stdize(int(x)) for x in input().split()] cur = 0 mp = {(0): 0} for i, v in enumerate(arr[n:]): cur += v if cur not in mp: mp[cur] = i + 1 cur = 0 ans = 200000.0 + 1 d = sum(arr) if d in mp: ans = min(ans, mp[d]) for i, v in enumerate(arr[:n][::-1]): cur += v key = d - cur if key in mp: ans = min(ans, i + 1 + mp[key]) print(ans) return ans def main(): tc = int(input()) for _ in range(tc): solve() main()
FUNC_DEF RETURN VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) while t: n = int(input()) l = list(map(int, input().split())) t += -1 l1 = [] l2 = [] c = 0 for i in range(n - 1, -1, -1): if l[i] == 1: c -= -1 else: c += -1 l1.append(c) c = 0 for i in range(n, 2 * n): if l[i] == 1: c -= -1 else: c += -1 l2.append(c) t1 = l1[n - 1] t2 = l2[n - 1] k = t1 + t2 if k == 0: print(0) else: ans = 99999999999 for i in range(len(l1)): l1[i] = k - l1[i] dic = {} for i in range(n): dic[l2[i]] = -1 dic[l1[i]] = -1 if l2[i] == k: ans = min(ans, i + 1) for i in range(n): if dic[l1[i]] == -1: dic[l1[i]] = i if l1[i] == 0: ans = min(ans, i + 1) for i in range(n): if dic[l2[i]] != -1: ans = min(ans, i + dic[l2[i]] + 2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for i in range(int(input())): n = int(input()) ar = list(map(int, input().split())) left = ar[:n] right = ar[n:][::-1] d = {(0): 0} summ = 0 for i in range(n): if left[i] == 1: summ -= 1 if left[i] == 2: summ += 1 d[summ] = i + 1 maxx = d[0] summ = 0 for i in range(n): if right[i] == 1: summ -= 1 if right[i] == 2: summ += 1 if -summ in d: maxx = max(maxx, d[-summ] + i + 1) print(2 * n - maxx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for o in range(t): n = int(input()) jams = list(map(int, input().strip().split())) totDiff = 0 leftDiff = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if jams[i] == 1: totDiff -= 1 else: totDiff += 1 leftDiff[i] = totDiff rightCur = 0 rightMin = {} rightMin[0] = 0 for i in range(n, 2 * n): if jams[i] == 1: rightCur -= 1 totDiff -= 1 else: rightCur += 1 totDiff += 1 if rightMin.get(rightCur, 1000000) == 1000000: rightMin[rightCur] = i - n + 1 ans = rightMin.get(totDiff, 10000000) for i in range(n): diff = totDiff - leftDiff[i] ans = min(ans, n - i + rightMin.get(diff, 10000000)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
from sys import stdin for _ in range(int(stdin.readline())): n = int(stdin.readline()) arr = list(map(lambda y: -1 if y == "2" else 1, stdin.readline().split())) cu1, cu2 = [0], [0] for x in range(n): cu1.append(arr[x] + cu1[-1]) for x in range(2 * n - 1, n - 1, -1): cu2.append(arr[x] + cu2[-1]) d = {} for ind, val in enumerate(cu2): d[val] = n - ind mini = 2 * n for ind, val in enumerate(cu1): if d.get(-val) != None: mini = min(mini, d[-val] + n - ind) print(mini)
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 STRING NUMBER NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys def kek(): t = int(input()) for _ in range(t): sys.stdin.readline() inp = list(map(int, sys.stdin.readline().split())) arrLeft = inp[: len(inp) // 2] arrRight = inp[len(inp) // 2 :] arrRight = list(reversed(arrRight)) if arrRight[0] == 1: arrRight[0] = 1 else: arrRight[0] = -1 if arrLeft[0] == 1: arrLeft[0] = 1 else: arrLeft[0] = -1 color = 0 for i in range(1, len(arrLeft)): if arrLeft[i] == 1: color = 1 else: color = -1 arrLeft[i] = arrLeft[i - 1] + color color = 0 for i in range(1, len(arrRight)): if arrRight[i] == 1: color = 1 else: color = -1 arrRight[i] = arrRight[i - 1] + color se = set() d = dict() se.add(0) d[0] = 0 for i in range(len(arrRight)): se.add(arrRight[i]) d[arrRight[i]] = i + 1 ans = 0 for i in range(len(arrLeft)): if -arrLeft[i] in se: ans = max(d[-arrLeft[i]] + i + 1, ans) ans = max(d[0], ans) print(len(arrRight) + len(arrLeft) - ans) kek()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) for _ in range(ii()): n = ii() l = il() l1 = l[:n] l2 = l[n:] o1, t1, o2, t2 = 0, 0, 0, 0 ld, rd = {}, {} for i in range(n): ld[o1 - t1] = n - i if l1[i] == 1: o1 += 1 else: t1 += 1 rd[t2 - o2] = n - i if l2[n - i - 1] == 1: o2 += 1 else: t2 += 1 ld[o1 - t1] = 0 rd[t2 - o2] = 0 o1, t1, o2, t2 = 0, 0, 0, 0 ans = 2 * n for i in range(n): if o1 - t1 in rd: ans = min(ans, ld[o1 - t1] + rd[o1 - t1]) if l1[i] == 1: o1 += 1 else: t1 += 1 if t2 - o2 in ld: ans = min(ans, ld[-o2 + t2] + rd[-o2 + t2]) if l2[n - i - 1] == 1: o2 += 1 else: t2 += 1 if o1 - t1 in rd: ans = min(ans, ld[o1 - t1] + rd[o1 - t1]) if t2 - o2 in ld: ans = min(ans, ld[-o2 + t2] + rd[-o2 + t2]) print(ans)
ASSIGN VAR NUMBER ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) lst = list(map(int, input().split())) count_1 = lst.count(1) count_2 = 2 * n - count_1 if count_1 == count_2: print(0) continue init_diff = count_1 - count_2 dp = [(0) for j in range(n)] diff = 0 for i in range(n, 2 * n): if lst[i] == 1: diff += 1 else: diff -= 1 dp[i - n] = diff diff = 0 Dict = {} for i in range(n - 1, -1, -1): if lst[i] == 1: diff += 1 else: diff -= 1 if Dict.get(init_diff - diff) == None: Dict[init_diff - diff] = n - 1 - i Ans = float("inf") for i in range(n): ele = dp[i] if Dict.get(ele) != None: Ans = min(Ans, i + 1 + Dict[ele] + 1) try: get1 = dp.index(init_diff) + 1 Ans = min(Ans, get1) except: None if Dict.get(0): Ans = min(Ans, Dict[0] + 1) print(Ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR NONE IF FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) q2, s = 0, {} for q in range(2 * n - 1, n - 1, -1): s[q2] = q - n + 1 q2 += a[q] * 2 - 3 s[q2], q2, ans = 0, 0, float("inf") for q in range(n): ans = min(ans, n - q + (s[-q2] if -q2 in s else float("inf"))) q2 += a[q] * 2 - 3 print(min(ans, s[-q2] if -q2 in s else float("inf")))
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 DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) for_dic = {(0): 0} bac_dic = {(0): 0} fscore = 0 bscore = 0 for i in range(n): if ar[i + n] == 1: fscore += 1 if not fscore in for_dic: for_dic[fscore] = i + 1 elif ar[i + n] == 2: fscore -= 1 if not fscore in for_dic: for_dic[fscore] = i + 1 if ar[n - i - 1] == 1: bscore += 1 if not bscore in bac_dic: bac_dic[bscore] = i + 1 elif ar[n - i - 1] == 2: bscore -= 1 if not bscore in bac_dic: bac_dic[bscore] = i + 1 scor_need = fscore + bscore li = [] for i in for_dic: x = for_dic[i] if scor_need - i in bac_dic: li.append(x + bac_dic[scor_need - i]) print(min(li))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) l1 = [] a1 = 0 a2 = 0 for i in range(0, n): if l[i] == 1: a1 += 1 else: a2 += 1 l1.append(a1 - a2) a1 = 0 a2 = 0 d = {} d[0] = 2 * n for i in range(2 * n - 1, n - 1, -1): if l[i] == 1: a1 += 1 else: a2 += 1 d[a2 - a1] = i min1 = d.get(0, 2 * n) for i in range(n): if d.get(l1[i]) != None: s = d[l1[i]] - i - 1 if min1 > s: min1 = s print(min1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) ss = input().split() n1 = [] n2 = [] for i in range(n): n1.append(int(ss[n - i - 1])) n2.append(int(ss[n + i])) cnt = {} cur = 0 cnt[0] = -1 for i in range(n): if n1[i] == 1: cur += 1 else: cur -= 1 if cur not in cnt: cnt[cur] = i cur = 0 ans = 10000000 init = n1.count(1) + n2.count(1) - n1.count(2) - n2.count(2) if init == 0: print(0) continue if init in cnt: ans = min(ans, cnt[init] + 1) for i in range(n): if n2[i] == 1: cur += 1 else: cur -= 1 if init - cur in cnt: ans = min(ans, i + cnt[init - cur] + 2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys def rint(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().rstrip("\n") def oint(): return int(input()) t = oint() for _ in range(t): n = oint() a = list(rint()) c2 = a.count(2) c1 = a.count(1) if c2 < c1: c2 = -1 c1 = 1 else: c2 = 1 c1 = -1 for i in range(2 * n): if a[i] == 2: a[i] = c2 else: a[i] = c1 b = sum(a) * -1 al = a[:n][::-1] ar = a[n:] ls = 0 rs = 0 als = dict() ars = dict() als[0] = 0 ars[0] = 0 for i in range(n): ls += al[i] rs += ar[i] if ls > 0 and not ls in als: als[ls] = i + 1 if rs > 0 and not rs in ars: ars[rs] = i + 1 ans = 10**9 for i in range(-b + 1): j = -i - b if i in als and j in ars: ans = min(ans, als[i] + ars[j]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for hasju in range(int(input())): n = int(input()) a = list(map(int, input().split())) for i in range(n * 2): if a[i] == 2: a[i] = -1 ns = sum(a) ps1 = [0] for i in range(n - 1, -1, -1): ps1.append(ps1[-1] + a[i]) ps2 = [0] for i in range(n, n * 2): ps2.append(ps2[-1] + a[i]) ps1 = [[ps1[i], i] for i in range(len(ps1))] ps1.sort() ans = 5738927598279537293752398572398579 for i in range(len(ps2)): rem = ns - ps2[i] l = -1 r = len(ps1) while r - l != 1: s = (l + r) // 2 if ps1[s][0] >= rem: r = s else: l = s if r == len(ps1) or ps1[r][0] != rem: continue else: ans = min(ans, i + ps1[r][1]) if ans == 5738927598279537293752398572398579: print(n * 2) else: 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) line = input() nums = [int(item) for item in line.strip().split()] a, b = 0, 0 dpa, dpb = [0] * (2 * n), [0] * (2 * n) for j in range(2 * n): if nums[j] == 1: a += 1 elif nums[j] == 2: b += 1 dpa[j] = a dpb[j] = b if a == b: print(0) continue d = a - b dict = {} for j in range(n, 2 * n): cura = dpa[j] - dpa[n] curb = dpb[j] - dpb[n] if nums[n] == 1: cura += 1 else: curb += 1 diff = cura - curb if diff not in dict: dict[diff] = j - n + 1 mmin = float("inf") for x in range(n - 1, -1, -1): cura = dpa[n - 1] - dpa[x] curb = dpb[n - 1] - dpb[x] if nums[x] == 1: cura += 1 else: curb += 1 diff = cura - curb if diff == d: mmin = min(mmin, n - x) break if d - diff in dict: mmin = min(mmin, dict[d - diff] + n - x) if d in dict: mmin = min(mmin, dict[d]) print(mmin)
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) left = l[0:n][::-1] right = l[n:] dictl = dict() dictl[left.count(1) - left.count(2)] = -1 ctr = left.count(1) ctb = left.count(2) for i in range(n): if left[i] == 1: ctr -= 1 else: ctb -= 1 diff = ctr - ctb if diff not in dictl: dictl[diff] = i ctr = right.count(1) ctb = right.count(2) diff = ctb - ctr if diff in dictl: ans = dictl[diff] + 1 else: ans = 2 * n for i in range(n): if right[i] == 1: ctr -= 1 else: ctb -= 1 diff = ctb - ctr if diff in dictl: ans = min(ans, dictl[diff] + i + 2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def count_costs(berry_order, start, end, move): costs = {(0): 0} count = 0 moves = 0 for i in range(start, end, move): moves += 1 if berry_order[i] == 2: count -= 1 else: count += 1 if count not in costs: costs[count] = moves return costs def min_cost(berry_order): remove = 0 for i in berry_order: if i == 2: remove -= 1 else: remove += 1 half_way = len(berry_order) // 2 l_costs = count_costs(berry_order, half_way - 1, -1, -1) r_costs = count_costs(berry_order, half_way, len(berry_order), 1) cost = len(berry_order) sign = -1 if remove < 0 else 1 remove = abs(remove) for i in range(0, remove + 1): l_num = sign * i r_num = sign * (remove - i) if l_num in l_costs and r_num in r_costs: cost = min(cost, l_costs[l_num] + r_costs[r_num]) return cost useless = int(input()) for i in range(useless): half_way = int(input()) listed = [int(i) for i in input().split()] print(min_cost(listed))
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) diff = 0 for j in range(2 * n): if a[j] == 1: diff += 1 else: diff -= 1 right = {(0): 0} d = 0 for j in range(n, 2 * n): if a[j] == 1: d += 1 else: d -= 1 if d not in right: right[d] = j - n + 1 min_c = 2 * n d = 0 for j in range(n - 1, -1, -1): if a[j] == 1: d += 1 else: d -= 1 if diff - d in right: min_c = min(min_c, right[diff - d] + n - j) if diff in right: min_c = min(min_c, right[diff]) print(min_c)
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 NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) def solve(arr): right = {} left = {} diff_left = 0 diff_right = 0 for i in range(len(arr) // 2 - 1, -1, -1): if arr[i] == 1: if diff_left + 1 > 0: left[diff_left + 1] = min( len(arr) // 2 - i, left.get(diff_left + 1, 10**10) ) diff_left += 1 if arr[i] == 2: if diff_left - 1 > 0: left[diff_left - 1] = min( len(arr) // 2 - i, left.get(diff_left - 1, 10**10) ) diff_left -= 1 for i in range(len(arr) // 2, len(arr)): if arr[i] == 1: if diff_right + 1 > 0: right[diff_right + 1] = min( i - len(arr) // 2 + 1, right.get(diff_right + 1, 10**10) ) diff_right += 1 if arr[i] == 2: if diff_right - 1 > 0: right[diff_right - 1] = min( i - len(arr) // 2 + 1, right.get(diff_right - 1, 10**10) ) diff_right -= 1 if diff_left + diff_right == 0: return 0 ans = 10**10 right[0] = 0 left[0] = 0 for i in range(diff_left + diff_right + 1): if i in right and diff_left + diff_right - i in left: if right[i] + left[diff_left + diff_right - i] < ans: ans = right.get(i, 0) + left.get(diff_left + diff_right - i, 0) return ans for i in range(t): n = int(input()) arr = list(map(int, input().split())) if arr.count(1) < arr.count(2): arr = [(3 - num) for num in arr] print(solve(arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] sb = bb = 0 for i in a: if i == 1: sb += 1 elif i == 2: bb += 1 diff = abs(sb - bb) if diff == 0: print(0) continue lookfor = 0 if sb > bb: lookfor = 1 else: lookfor = 2 left = [0] * n right = [0] * n closest_l = {} closest_r = {} for i in range(2 * n): closest_l[i] = -n closest_r[i] = -n i = n - 1 j = 0 while i >= 0: if a[i] == lookfor: left[j] = left[j - 1] + 1 else: left[j] = left[j - 1] - 1 if left[j] > 0 and (j < closest_l[left[j]] or closest_l[left[j]] == -n): closest_l[left[j]] = j j += 1 i -= 1 i = n j = 0 while i < 2 * n: if a[i] == lookfor: right[j] = right[j - 1] + 1 else: right[j] = right[j - 1] - 1 if right[j] > 0 and (j < closest_r[right[j]] or closest_r[right[j]] == -n): closest_r[right[j]] = j j += 1 i += 1 mi = 2 * n for i in range(n): li = left[i] if i + 1 < mi and li == diff: mi = i + 1 elif ( diff - li > 0 and i + closest_r[diff - li] + 1 < mi and closest_r[diff - li] >= 0 and li + right[closest_r[diff - li]] == diff ): mi = i + closest_r[diff - li] + 2 for i in range(n): ri = right[i] if i + 1 < mi and ri == diff: mi = i + 1 elif ( diff - ri > 0 and i + closest_l[diff - ri] + 1 < mi and closest_l[diff - ri] >= 0 and ri + left[closest_l[diff - ri]] == diff ): mi = i + closest_l[diff - ri] + 2 print(mi)
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 FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
from itertools import accumulate for _ in range(int(input())): n = int(input()) a = [(1 if x == 2 else -1) for x in map(int, input().split())] req = a.count(1) - a.count(-1) left, right = [0] + a[:n][::-1], [0] + a[n:] fn = max if req >= 0 else min left = list(accumulate(accumulate(left), fn)) right = list(accumulate(accumulate(right), fn)) r_i = 0 ans = n * 2 for l_i in range(n, -1, -1): while r_i < n and ( left[l_i] + right[r_i] < req if req >= 0 else left[l_i] + right[r_i] > req ): r_i += 1 if left[l_i] + right[r_i] == req: ans = min(ans, l_i + r_i) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR NUMBER BIN_OP LIST NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) p = a.count(1) q = 2 * n - p d = p - q if d == 0: print(0) continue c = 0 di = {(0): n} l = 2 * n for j in range(n, 2 * n, 1): if a[j] == 1: c = c + 1 else: c = c - 1 if d - c == 0: l = min(l, j - n + 1) if c not in di: di[c] = j + 1 c = 0 for j in range(n - 1, -1, -1): if a[j] == 1: c = c + 1 else: c = c - 1 if d - c in di: l = min(l, di[d - c] - j) print(l)
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 NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) while t > 0: t -= 1 n = int(input()) a = [int(x) for x in input().split()] diff = a.count(1) - a.count(2) ax = a[:n] b = a[n:] ax.reverse() a = ax s1 = {} s2 = {} mini = 9898598989 count = 0 if diff == 0: print(0) continue for i in range(n): if b[i] == 1: count += 1 if count not in s1: s1[count] = i + 1 if count == diff: mini = min(mini, i + 1) else: count -= 1 if count not in s1: s1[count] = i + 1 if count == diff: mini = min(mini, i + 1) count = 0 s1[0] = 0 for i in range(n): if a[i] == 1: count += 1 if -(count - diff) in s1: mini = min(mini, i + s1[-count + diff] + 1) else: count -= 1 if -(count - diff) in s1: mini = min(mini, i + s1[-count + diff] + 1) print(mini)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
INF = int(1000000000.0) t = int(input()) for j in range(t): n = int(input()) a = list(map(int, input().split())) b = sum(a) - 2 * n r = 2 * n - b if r > b: flag = 1 elif r == b: print(0) continue else: flag = 2 diff = abs(b - r) dp_left = [INF] * (2 * n + 10) dp_right = [INF] * (2 * n + 10) dp_left[0] = 0 dp_right[0] = 0 c = 0 d = 0 for i in range(n): if a[n - 1 - i] == flag: c += 1 else: c -= 1 if a[n + i] == flag: d += 1 else: d -= 1 if c >= 0: dp_left[c] = min(dp_left[c], i + 1) if d >= 0: dp_right[d] = min(dp_right[d], i + 1) p = INF for i in range(diff + 1): p = min(dp_left[i] + dp_right[diff - i], p) print(p)
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys inf = float("inf") def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() T = int(input()) while T: n = int(input()) Arr = get_array() a, b = Arr[n - 1 :: -1], Arr[n:] dict_a, dict_b = {(0): 0}, {(0): 0} curr_a = curr_b = 0 for i in range(n): if a[i] == 1: curr_a += 1 else: curr_a -= 1 if curr_a not in dict_a: dict_a[curr_a] = i + 1 if b[i] == 1: curr_b += 1 else: curr_b -= 1 if curr_b not in dict_b: dict_b[curr_b] = i + 1 total = curr_b + curr_a ans = inf for i in dict_a: req = total - i if req in dict_b: ans = min(ans, dict_a[i] + dict_b[req]) print(ans) T -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR DICT NUMBER NUMBER DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n = int(input()) * 2 s = [(1 if i == "1" else -1) for i in input().split() if i != "\n"] left, right = [s[0]], [s[-1]] for i in range(1, n // 2): left.append(left[i - 1] + s[i]) right.append(right[i - 1] + s[-i - 1]) ans, answ = n, n dict1, dict2 = {}, {} for i in range(n // 2): dict1[left[i]] = i dict2[right[i]] = n - 1 - i for i in dict1: if -i in dict2: ans = min(ans, dict2[-i] - dict1[i] - 1) if 0 in dict1: answ = n // 2 - (dict1[0] + 1) + n // 2 if 0 in dict2: answ = dict2[0] print(min(ans, answ))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR LIST VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
def find(red, blue, dpright): diff = blue - red if diff not in dpright.keys(): return float("inf") return dpright[diff][0] def solve(arr, n, ans): left = arr[:n] right = arr[n:] red_right = 0 blue_right = 0 dpright = {} dpright[0] = [n] for i in range(n - 1, -1, -1): if right[i] == 2: blue_right += 1 else: red_right += 1 diff = red_right - blue_right if diff not in dpright.keys(): dpright[diff] = [i] else: dpright[diff].append(i) for i in dpright.keys(): dpright[i].sort() ans = float("inf") red = 0 blue = 0 for x in range(n, -1, -1): if x != n: if left[n - x - 1] == 1: red += 1 else: blue += 1 y = find(red, blue, dpright) ans = min(ans, x + y) if ans == float("inf"): print(2 * n) else: print(ans) def main(): ans = [] t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) solve(arr, n, ans) for i in ans: print(i) main()
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR RETURN FUNC_CALL VAR STRING RETURN VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST 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 VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = a[n:] a = a[:n] for i in range(n): if a[i] == 2: a[i] = -1 for j in range(n): if b[j] == 2: b[j] = -1 a = a[n - 1 :: -1] d = dict() da = dict() da[a[0]] = 0 d[b[0]] = 0 d[0] = -1 da[0] = -1 for i in range(1, n): a[i] = a[i] + a[i - 1] b[i] = b[i] + b[i - 1] if da.get(a[i], None) == None: da[a[i]] = i if d.get(b[i], None) == None: d[b[i]] = i s = a[i] + b[i] if s == 0: print(0) continue ans = 99999999999999999 for i in range(n): if s > 0 and a[i] < 0 or s < 0 and a[i] > 0: continue if d.get(s - a[i], None) != None: ans = min(ans, i + 1 + d.get(s - a[i]) + 1) for i in range(n): if s > 0 and b[i] < 0 or s < 0 and b[i] > 0: continue if da.get(s - b[i], None): ans = min(ans, i + 1 + da.get(s - b[i]) + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE NONE ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NONE NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $2n$ jars of strawberry and blueberry jam. All the $2n$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $n$ jars to his left and $n$ jars to his right. For example, the basement might look like this: [Image] Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: [Image] He has eaten $1$ jar to his left and then $5$ jars to his right. There remained exactly $3$ full jars of both strawberry and blueberry jam. Jars are numbered from $1$ to $2n$ from left to right, so Karlsson initially stands between jars $n$ and $n+1$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $t$ independent test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$). The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le 2$) β€” $a_i=1$ means that the $i$-th jar from the left is a strawberry jam jar and $a_i=2$ means that it is a blueberry jam jar. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it β€” the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left. -----Example----- Input 4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1 Output 6 0 6 2 -----Note----- The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $6$ jars so that there remain $0$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $1$ jar of both jams.
t = int(input()) for _ in range(t): n = int(input()) s = [int(x) for x in input().split()] diff = s.count(1) - s.count(2) kk = dict() c = 0 tot = 0 for i in range(n, len(s)): if kk.get(c) == None: kk[c] = tot else: kk[c] = min(kk[c], tot) tot += 1 if s[i] == 1: c += 1 else: c -= 1 if kk.get(c) == None: kk[c] = tot else: kk[c] = min(kk[c], tot) c = 0 tot = 0 ans = 2 * n for i in range(n - 1, -1, -1): if kk.get(diff - c) != None: ans = min(ans, tot + kk[diff - c]) tot += 1 if s[i] == 1: c += 1 else: c -= 1 if kk.get(diff - c) != None: ans = min(ans, tot + kk[diff - c]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR