description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def main():
i = 0
j = 0
k = 0
sum1 = 0
sum2 = 0
n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
b = [int(x) for x in input().split()]
while i < n and j < m:
if a[i] == b[j]:
k += 1
i += 1
j += 1
if i == n or j == m:
break
continue
if a[i] < b[j]:
i += 1
if i == n:
break
a[i] = a[i] + a[i - 1]
continue
if a[i] > b[j]:
j += 1
if j == m:
break
b[j] = b[j] + b[j - 1]
continue
if i == n and j == m:
print(k)
else:
print(-1)
main()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
j = 0
i = 0
sizei = n
sizej = m
while i < n and j < m:
while i < n and j < m and a[i] != b[j]:
if j + 1 < m and a[i] > b[j]:
b[j + 1] += b[j]
j += 1
sizej -= 1
elif i + 1 < n:
a[i + 1] += a[i]
i += 1
sizei -= 1
else:
print(-1)
exit()
if a[i] != b[j]:
print(-1)
exit()
i += 1
j += 1
if sizei != sizej:
print(-1)
exit()
print(sizei)
|
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 VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
while True:
try:
def solution(n, na, m, ma):
sn = 0
sm = 0
for i in range(n):
sn += na[i]
for i in range(m):
sm += ma[i]
if sn != sm:
print("-1")
return
elen, elem = na[0], ma[0]
na.append(0)
ma.append(0)
ans = 0
i = j = 0
while i < n and j < m:
if elen != elem and elen < elem:
elen += na[i + 1]
i += 1
elif elen != elem:
elem += ma[j + 1]
j += 1
else:
i += 1
j += 1
elen = na[i]
elem = ma[j]
ans += 1
print(ans)
def read():
n = int(input())
na = list(map(int, input().split()))
m = int(input())
ma = list(map(int, input().split()))
solution(n, na, m, ma)
if __name__ == "__main__":
read()
except EOFError:
break
|
WHILE NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
if sum(A) != sum(B):
print(-1)
exit()
a = A.pop()
b = 0
cnt = 1
while A or B:
if a != b:
if a > b:
b += B.pop()
else:
a += A.pop()
else:
cnt += 1
a, b = 0, 0
if A:
a = A.pop()
else:
b = B.pop()
print(cnt)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
i = 0
j = 0
ans = 0
sa = 0
sb = 0
while i < n or j < m:
if sa < sb:
if i >= n:
break
sa += A[i]
i += 1
else:
if j >= m:
break
sb += B[j]
j += 1
if sa == sb:
ans += 1
sa = 0
sb = 0
if i == n and j == m and sa == 0 and sb == 0:
print(ans)
else:
print(-1)
|
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import time
_ = int(input())
M = input().split(" ")
M = list(map(lambda val: int(val), M))
_ = int(input())
N = input().split(" ")
N = list(map(lambda val: int(val), N))
merge = 0
m = len(M)
n = len(N)
def perform():
global merge
global m
global n
i = 0
j = 0
while True:
if i < len(M) and j < len(N):
if M[i] != N[j]:
if M[i] < N[j]:
M[i + 1] = M[i] + M[i + 1]
i += 1
m -= 1
else:
N[j + 1] = N[j] + N[j + 1]
j += 1
n -= 1
else:
i += 1
j += 1
else:
break
if sum(M) == sum(N):
perform()
if m != n:
print("blunder happend")
else:
print(m)
else:
print("-1")
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
ans = 0
if sum(a) != sum(b):
print(-1)
else:
sum_i = a[0]
sum_j = b[0]
i = 0
j = 0
while i < n and j < m:
while sum_i < sum_j and i + 1 < n:
i += 1
sum_i += a[i]
while sum_i > sum_j and j + 1 < m:
j += 1
sum_j += b[j]
if sum_i == sum_j:
ans += 1
if i + 1 < n and j + 1 < m:
i += 1
j += 1
sum_i += a[i]
sum_j += b[j]
else:
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
else:
ans = 0
posA, posB = 0, 0
while posA < n:
ans += 1
sumA = a[posA]
sumB = b[posB]
while sumA != sumB:
if sumA < sumB:
posA += 1
sumA += a[posA]
else:
posB += 1
sumB += b[posB]
posA += 1
posB += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
n = int(input())
A = [int(x) for x in input().split()]
m = int(input())
B = [int(x) for x in input().split()]
def debug(name, val):
sys.stderr.write(name + "=" + str(val) + "\n")
S = A[0] - B[0]
i, j, k = 0, 0, 0
while i < n - 1 and j < m - 1 or i < n - 1 and S < 0 or j < m - 1 and S > 0:
if S < 0:
i += 1
S += A[i]
elif S > 0:
j += 1
S -= B[j]
else:
k += 1
i += 1
j += 1
S = A[i] - B[j]
if S == 0 and i == n - 1 and j == m - 1:
print(k + 1)
else:
print(-1)
|
IMPORT 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 FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
m = int(input())
A = [int(element) for element in input().split()]
n = int(input())
B = [int(element) for element in input().split()]
a_idx, b_idx = 0, 0
result = 0
while a_idx < m and b_idx < n:
a_sum, b_sum = A[a_idx], B[b_idx]
while a_sum != b_sum and a_idx < m and b_idx < n:
if a_sum < b_sum:
if a_idx == m - 1:
break
a_idx += 1
a_sum += A[a_idx]
else:
if b_idx == n - 1:
break
b_idx += 1
b_sum += B[b_idx]
result += 1
a_idx += 1
b_idx += 1
if a_sum == b_sum and a_idx == m and b_idx == n:
print(result)
else:
print(-1)
|
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 VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
i = j = c = d = e = 0
while j < m:
e += b[j]
while i < n and d < e:
d += a[i]
i += 1
if d == e:
d = e = 0
c += 1
elif j == m - 1:
c = -1
break
j += 1
print(-1 if c == 0 or i != n else c)
|
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 WHILE VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def solve(a, b):
if sum(a) != sum(b):
return -1
i = j = 0
length = 0
sum1 = sum2 = 0
loc = []
while i < len(a) and j < len(b):
if sum1 == sum2 and sum1 > 0:
sum1 = sum2 = 0
length += 1
if sum1 > sum2:
sum2 += b[j]
j += 1
else:
sum1 += a[i]
i += 1
if sum1 > 0 or sum2 > 0:
length += 1
return length
n = input()
a = list(map(int, input().split()))
m = input()
b = list(map(int, input().split()))
print(solve(a, b))
|
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
i = 0
j = 0
x = 0
y = 0
f = 0
ans = 0
if sum(a) == sum(b):
while i < n and j < m:
if f == 0:
x += a[i]
y += b[j]
elif f == 1:
y += b[j]
elif f == 2:
x += a[i]
if x == y:
i += 1
j += 1
x = 0
y = 0
ans += 1
f = 0
elif x > y:
j += 1
f = 1
elif y > x:
i += 1
f = 2
print(ans)
else:
print(-1)
|
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
else:
i = 0
ai = 0
bi = 0
while ai < n and bi < m:
if a[ai] == b[bi]:
ai = ai + 1
bi = bi + 1
i = i + 1
continue
if a[ai] > b[bi]:
b[bi + 1] = b[bi] + b[bi + 1]
bi = bi + 1
else:
a[ai + 1] = a[ai] + a[ai + 1]
ai = ai + 1
print(i)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = [0] * (n + 1)
for i, x in enumerate(map(int, input().split())):
a[i + 1] = a[i] + x
m = int(input())
b = [0] * (m + 1)
for i, x in enumerate(map(int, input().split())):
b[i + 1] = b[i] + x
if a[-1] != b[-1]:
print(-1)
else:
print(len(set(a) & set(b)) - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
length_first = int(input())
first = list(map(int, input().split()))
length_second = int(input())
second = list(map(int, input().split()))
if sum(first) != sum(second):
print(-1)
else:
solution = 0
pos_first = 1
pos_second = 1
sum = first[0] - second[0]
while pos_first < length_first and pos_second < length_second:
if sum == 0:
solution += 1
sum = first[pos_first] - second[pos_second]
pos_first += 1
pos_second += 1
elif sum > 0:
sum -= second[pos_second]
pos_second += 1
else:
sum += first[pos_first]
pos_first += 1
solution += 1
print(solution)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
A = [int(i) for i in input().split()]
m = int(input())
B = [int(i) for i in input().split()]
res = 0
indA = 0
indB = 0
flag = False
while indA < len(A) and indB < len(B):
if A[indA] == B[indB]:
indA += 1
indB += 1
res += 1
elif A[indA] < B[indB]:
if indA + 1 >= len(A):
flag = True
break
A[indA + 1] += A[indA]
indA += 1
elif B[indB] < A[indA]:
if indB + 1 >= len(B):
flag = True
break
B[indB + 1] += B[indB]
indB += 1
else:
res += 1
indA += 1
indB += 1
if indA == len(A) and indB == len(B) and not flag:
print(res)
else:
print(-1)
|
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 ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
m = int(input())
br = list(map(int, input().split()))
i = 0
j = 0
if sum(ar) != sum(br):
print(-1)
else:
su1 = 0
su2 = 0
ans = 0
while i < n and j < m:
if su1 >= su2:
su2 += br[j]
j += 1
elif su1 < su2:
su1 += ar[i]
i += 1
if su1 == su2:
ans += 1
for ii in range(i, n):
su1 += ar[ii]
for jj in range(j, m):
su2 += br[jj]
if su1 == su2:
ans += 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
ans = 0
i = 0
j = 0
while i != n and j != m:
if a[i] == b[j]:
ans += 1
i += 1
j += 1
elif a[i] < b[j]:
if i + 1 < len(a):
a[i + 1] += a[i]
i += 1
else:
if j + 1 < len(b):
b[j + 1] += b[j]
j += 1
if i != n or j != m:
print(-1)
else:
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
ka = int(input())
a = list(map(int, input().split()))
kb = int(input())
b = list(map(int, input().split()))
def sum1(a):
k = 0
for i in a:
k += a
return k
def www(a, b, k1, k2):
ka = k1
kb = k2
sa = a[ka]
sb = b[kb]
while sa != sb:
if sa > sb:
kb += 1
sb += b[kb]
else:
ka += 1
sa += a[ka]
return ka + 1, kb + 1
k1 = 0
k2 = 0
if sum(a) != sum(b):
print("-1")
else:
ans = 0
while k1 < len(a):
k1, k2 = www(a, b, k1, k2)
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
f = sys.stdin
out = sys.stdout
n = int(f.readline().rstrip("\r\n"))
a = list(map(int, f.readline().rstrip("\r\n").split()))
m = int(f.readline().rstrip("\r\n"))
b = list(map(int, f.readline().rstrip("\r\n").split()))
i = n - 1
j = m - 1
c = 0
while i >= 0 and j >= 0:
if a[i] == b[j]:
c += 1
i -= 1
j -= 1
elif a[i] < b[j]:
a[i - 1] += a[i]
i -= 1
else:
b[j - 1] += b[j]
j -= 1
if i == -1 and j == -1:
out.write(str(c))
else:
out.write("-1")
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
aNew = [0] * n
bNew = [0] * m
aNew[0] = a[0]
bNew[0] = b[0]
for i in range(1, n):
aNew[i] = aNew[i - 1] + a[i]
for i in range(1, m):
bNew[i] = bNew[i - 1] + b[i]
count = 0
i = 0
j = 0
while i < n and j < m:
if aNew[i] > bNew[j]:
j += 1
elif aNew[i] < bNew[j]:
i += 1
else:
count += 1
i += 1
j += 1
if i == n and j == m:
print(count)
else:
print(-1)
|
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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
A = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
total = -1
indexA = 0
sumA = 0
indexB = 0
sumB = 0
if sum(A) == sum(B):
while indexA <= len(A) and indexB <= len(B):
while sumA < sumB:
sumA += A[indexA]
indexA += 1
while sumB < sumA:
sumB += B[indexB]
indexB += 1
if sumA == sumB:
total += 1
if indexA >= len(A):
break
sumB += B[indexB]
indexB += 1
print(total)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
INF = 1000000000
def main():
N = int(input())
A = list(map(int, input().split()))
M = int(input())
B = list(map(int, input().split()))
currentSumA = currentSumB = 0
indexA = indexB = 0
numberOfSegments = 0
possible = False
while indexA < len(A):
currentSumA += A[indexA]
indexA += 1
while indexB < len(B) and currentSumB < currentSumA:
currentSumB += B[indexB]
indexB += 1
if currentSumA == currentSumB:
currentSumA = currentSumB = 0
numberOfSegments += 1
if indexA == len(A) and indexB == len(B):
possible = True
if possible:
print(numberOfSegments)
else:
print(-1)
main()
|
ASSIGN VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
a = int(input())
b = list(map(int, input().split()))
c = int(input())
d = list(map(int, input().split()))
s = 0
r = 0
flag = 1
count = 0
while s < a or r < c:
if s == a:
flag = 0
break
if r == c:
flag = 0
break
if b[s] == d[r]:
count += 1
s += 1
r += 1
elif b[s] < d[r]:
if s == a - 1:
flag = 0
break
else:
b[s + 1] += b[s]
s += 1
elif r == c - 1:
flag = 0
break
else:
d[r + 1] += d[r]
r += 1
if flag == 0:
print("-1")
else:
print(count)
|
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 ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def f(n, m, A, B):
p1A, p2A, p1B, p2B = 0, 1, 0, 1
somaA, somaB = A[p1A], B[p1A]
total = 0
while True:
if somaA == somaB:
total += 1
p1A, p1B = p2A, p2B
p2A += 1
p2B += 1
if p1A >= n or p1B >= m:
break
somaA, somaB = A[p1A], B[p1B]
elif somaA != somaB:
if somaA < somaB:
if p2A >= n:
return -1
somaA += A[p2A]
p2A += 1
elif somaA > somaB:
if p2B >= m:
return -1
somaB += B[p2B]
p2B += 1
if p1A != n or p1B != m:
return -1
return total
n = int(input())
A = [int(a) for a in input().split()]
m = int(input())
B = [int(b) for b in input().split()]
print(f(n, m, A, B))
|
FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR RETURN NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR RETURN NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
brr = list(map(int, input().split()))
if sum(arr) != sum(brr):
print(-1)
exit()
ai = 0
bi = 0
asm = 0
bsm = 0
cnt = 0
while ai < len(arr) and bi < len(brr):
if asm < bsm:
asm += arr[ai]
ai += 1
elif bsm < asm:
bsm += brr[bi]
bi += 1
else:
cnt += 1
asm = arr[ai]
bsm = brr[bi]
ai += 1
bi += 1
print(cnt)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def f(left, right):
li = 0
ri = 0
rLen = len(right)
rConstLen = rLen
lLen = len(left)
lConstLen = lLen
while True:
l = left[li]
r = right[ri]
while l != r:
if l > r:
ri += 1
if ri >= rConstLen:
return -1
r += right[ri]
rLen -= 1
else:
li += 1
if li >= lConstLen:
return -1
l += left[li]
lLen -= 1
li += 1
ri += 1
if ri >= rConstLen and li >= lConstLen:
return rLen
elif ri >= rConstLen or li >= lConstLen:
return -1
input()
left = [int(x) for x in input().split()]
input()
right = [int(x) for x in input().split()]
result = f(left, right)
print(result)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
one = list(map(int, input().strip().split()))
m = int(input())
two = list(map(int, input().strip().split()))
sum_one = 0
sum_two = 0
for i in one:
sum_one += i
for i in two:
sum_two += i
if sum_one != sum_two:
print(-1)
else:
pos_one = 0
pos_two = 0
sum_one = 0
sum_two = 0
ans = 0
t = 1
while t <= m + n:
if sum_one < sum_two or sum_one == 0 and sum_two == 0:
sum_one += one[pos_one]
pos_one += 1
t += 1
elif sum_one > sum_two:
sum_two += two[pos_two]
pos_two += 1
t += 1
else:
ans += 1
sum_one = 0
sum_two = 0
print(ans + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = [int(i) for i in input().split()]
m = int(input())
b = [int(i) for i in input().split()]
if sum(a) != sum(b):
print(-1)
else:
i = 0
j = 0
cnt = 0
s_i = a[0]
s_j = b[0]
while i < n and j < m:
if s_i > s_j:
j += 1
if j < m:
s_j += b[j]
elif s_i < s_j:
i += 1
if i < n:
s_i += a[i]
else:
cnt += 1
i += 1
j += 1
if i < n:
s_i = a[i]
if j < m:
s_j = b[j]
print(cnt)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
N = list(map(int, input().split()))
m = int(input())
M = list(map(int, input().split()))
i, j, ans, s_n, s_m = 0, 0, 0, N[0], M[0]
while i < n and j < m:
if s_n == s_m:
ans += 1
i += 1
j += 1
if i == n or j == m:
break
s_n = N[i]
s_m = M[j]
if s_n > s_m:
j += 1
if j == m:
break
s_m += M[j]
elif s_n < s_m:
i += 1
if i == n:
break
s_n += N[i]
print(ans if i == n and j == m else -1)
|
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 NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
i = 0
j = 0
x = A[0]
y = B[0]
ACHECK = 0
while True:
if x == y:
i += 1
j += 1
if i == n and j == m:
print(n - ACHECK)
return
elif i == n and j < m:
print(-1)
return
elif i < n and j == m:
print(-1)
return
x = A[i]
y = B[j]
elif x > y:
j += 1
if j == m:
print(-1)
return
y += B[j]
elif x < y:
i += 1
if i == n:
print(-1)
return
x += A[i]
ACHECK += 1
|
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 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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
line_a = list(map(int, input().split()))
m = int(input())
line_b = list(map(int, input().split()))
if sum(line_a) != sum(line_b):
print(-1)
else:
sa = 0
sb = 0
l = 0
r = 0
size = 0
while l != n and r != m:
if sa + line_a[l] > sb + line_b[r]:
sb += line_b[r]
r += 1
elif sa + line_a[l] < sb + line_b[r]:
sa += line_a[l]
l += 1
elif sa + line_a[l] == sb + line_b[r]:
size += 1
l += 1
r += 1
sa = 0
sb = 0
print(size)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
list1 = list(map(int, input().split()))
m = int(input())
list2 = list(map(int, input().split()))
list11 = [list1[0]]
for i in range(1, n):
list11.append(list11[-1] + list1[i])
list22 = [list2[0]]
for i in range(1, m):
list22.append(list22[-1] + list2[i])
if list11[-1] != list22[-1]:
print(-1)
else:
print(len(set(list11) & set(list22)))
|
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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
t1 = int(input())
arr1 = [int(x) for x in input().split()]
t2 = int(input())
arr2 = [int(x) for x in input().split()]
sum1 = 0
sum2 = 0
for i in range(max(t1, t2)):
if i < t1:
sum1 += arr1[i]
if i < t2:
sum2 += arr2[i]
res = 0
if sum1 != sum2:
res = -1
else:
pos1 = 0
pos2 = 0
while pos1 < t1:
res += 1
sum1 = arr1[pos1]
pos1 += 1
sum2 = arr2[pos2]
pos2 += 1
while sum1 != sum2:
if sum1 < sum2:
sum1 += arr1[pos1]
pos1 += 1
else:
sum2 += arr2[pos2]
pos2 += 1
print(res)
|
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n1 = int(input())
a = [int(x) for x in input().split()]
n2 = int(input())
b = [int(x) for x in input().split()]
suma = a[0]
sumb = b[0]
m = []
i = int(0)
j = int(0)
ct = int(-1)
mn = min(n1, n2)
while i != n1 and j != n2:
if suma == sumb:
m.append(suma)
suma = int(0)
sumb = int(0)
i += 1
j += 1
if i != n1:
suma += a[i]
if j != n2:
sumb += b[j]
ct = int(0)
elif suma < sumb:
i += 1
if i != n1:
suma += a[i]
elif suma > sumb:
j += 1
if j != n2:
sumb += b[j]
p = int(0)
if suma != sumb:
p = int(-1)
if p == -1:
print(-1)
else:
print(len(m))
|
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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
A = [int(i) for i in input().split()]
m = int(input())
B = [int(i) for i in input().split()]
if sum(A) != sum(B):
print(-1)
exit()
pre1 = [0] * (n + 1)
pre2 = [0] * (m + 1)
for i in range(1, n + 1):
pre1[i] = pre1[i - 1] + A[i - 1]
for i in range(1, m + 1):
pre2[i] = pre2[i - 1] + B[i - 1]
del pre1[0]
del pre2[0]
print(len(set(pre1) & set(pre2)))
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR 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 ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
exit(0)
i = 0
j = 0
while i < n and j < m:
while a[i] != b[j] and i < n and j < m:
if i < n - 1 and a[i] < b[j]:
a[i + 1] += a[i]
a[i] = -1
i += 1
elif j < m - 1:
b[j + 1] += b[j]
b[j] = -1
j += 1
if a[i] == b[j]:
i += 1
j += 1
else:
print(-1)
exit()
ans = 0
for i in a:
if i != -1:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
sys.exit(0)
idx_a, idx_b = 0, 0
sum_a, sum_b = a[0], b[0]
ans = 0
while idx_a < n or idx_b < m:
if sum_a == sum_b:
ans += 1
idx_a += 1
idx_b += 1
if idx_a < n or idx_b < m:
sum_a = a[idx_a]
sum_b = b[idx_b]
elif sum_a > sum_b:
idx_b += 1
sum_b += b[idx_b]
else:
idx_a += 1
sum_a += a[idx_a]
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
t1 = int(input())
arr1 = input().split()
t2 = int(input())
arr2 = input().split()
sum1 = 0
sum2 = 0
tamMax = max(t1, t2)
for i in range(tamMax):
if i < t1:
arr1[i] = int(arr1[i])
sum1 += arr1[i]
if i < t2:
arr2[i] = int(arr2[i])
sum2 += arr2[i]
if sum1 != sum2:
print(-1)
else:
posarr1 = 0
posarr2 = 0
res = 0
while posarr1 < t1:
res += 1
sum1 = arr1[posarr1]
posarr1 += 1
sum2 = arr2[posarr2]
posarr2 += 1
while sum1 != sum2:
if sum1 < sum2:
sum1 += arr1[posarr1]
posarr1 += 1
else:
sum2 += arr2[posarr2]
posarr2 += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
else:
i = 0
j = 0
suma = 0
sumb = 0
ans = 0
while i < n or j < m:
if suma == sumb and suma != 0:
suma = 0
sumb = 0
ans += 1
if i == n and j == m:
break
elif suma == sumb:
suma += a[i]
i += 1
sumb += b[j]
j += 1
if i == n and j == m:
break
elif suma < sumb:
suma += a[i]
i += 1
elif suma > sumb:
sumb += b[j]
j += 1
if suma == sumb and suma != 0:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split(" ")))
m = int(input())
b = list(map(int, input().split(" ")))
i, j = 0, 0
res = 0
x, y = 0, 0
last_i, last_j = -1, -1
while i < n and j < m:
if last_i != i:
x += a[i]
if last_j != j:
y += b[j]
last_i, last_j = i, j
if x == y:
i += 1
j += 1
res += 1
x = 0
y = 0
if x < y:
i += 1
if x > y:
j += 1
if i == n and j == m:
print(res)
else:
print("-1")
|
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 VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
n1 = int(stdin.readline().strip())
s1 = list(map(int, stdin.readline().strip().split()))
st = set()
st.add(s1[0])
for i in range(1, n1):
s1[i] += s1[i - 1]
st.add(s1[i])
x = 0
acu = 0
ans = 0
y = 0
for i in range(n):
x += s[i]
if x in st:
ans += 1
if x == s1[-1]:
print(ans)
else:
print(-1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = input()
a = list(map(int, input().split()))
m = input()
b = list(map(int, input().split()))
ans = 0
ta, tb = 0, 0
if sum(a) != sum(b):
print(-1)
exit(0)
while len(a) and len(b):
ta += a.pop()
while ta != tb:
if ta < tb:
ta += a.pop()
else:
tb += b.pop()
ta, tb = 0, 0
ans += 1
print(ans)
|
ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def remove_equal_part(a, b):
i = 0
j = len(a) - 1
k = len(b) - 1
while i < len(a) and i < len(b):
if a[i] != b[i] and a[j] != b[k]:
return i, j, k
if a[i] == b[i]:
i += 1
if a[j] == b[k]:
j -= 1
k -= 1
return i, j, k
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
elif a == b:
print(n)
else:
i = j = 0
count = 0
while i < len(a) and j < len(b):
if a[i] == b[j]:
count += 1
i += 1
j += 1
elif a[i] < b[j] and i + 1 < len(a):
a[i + 1] += a[i]
i += 1
elif j + 1 < len(b):
b[j + 1] += b[j]
j += 1
print(count)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR 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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
alist = list(map(int, input().split()))
m = int(input())
blist = list(map(int, input().split()))
prea = [alist[0]]
for i in range(1, n):
prea.append(prea[-1] + alist[i])
preb = [blist[0]]
for i in range(1, m):
preb.append(preb[-1] + blist[i])
if prea[-1] != preb[-1]:
print(-1)
else:
x = 0
y = 0
count = 0
while x < n and y < m:
if prea[x] == preb[y]:
count += 1
x += 1
y += 1
elif prea[x] < preb[y]:
x += 1
else:
y += 1
print(count)
|
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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
arr1 = [int(element) for element in input().split()]
m = int(input())
arr2 = [int(element) for element in input().split()]
res = 0
p1 = 0
p2 = 0
sum1 = arr1[p1]
sum2 = arr2[p2]
arr1.append(0)
arr2.append(0)
while p1 < n and p2 < m:
if sum1 == sum2:
res += 1
sum1 = arr1[p1 + 1]
sum2 = arr2[p2 + 1]
p1 += 1
p2 += 1
elif sum1 < sum2:
sum1 += arr1[p1 + 1]
p1 += 1
elif sum1 > sum2:
sum2 += arr2[p2 + 1]
p2 += 1
if p1 == n and p2 == m:
print(res)
else:
print(-1)
|
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 ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
exit()
j, k = 0, 0
sa, sb = 0, 0
cnt = 0
pa, pb = 1, 1
while j < n and k < m:
if pa == 1:
sa += a[j]
if pb == 1:
sb += b[k]
if sa == sb:
cnt += 1
j += 1
k += 1
sa = sb = 0
pa = pb = 1
elif sa > sb:
k += 1
pa = 0
pb = 1
else:
j += 1
pa = 1
pb = 0
print(cnt)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = input()
n = int(n)
a = input()
a = list(a.split(" "))
m = input()
m = int(m)
b = input()
b = list(b.split(" "))
for i in range(n):
a[i] = int(a[i])
for i in range(m):
b[i] = int(b[i])
a.insert(0, 0)
b.insert(0, 0)
for i in range(1, n + 1):
a[i] += a[i - 1]
for i in range(1, m + 1):
b[i] += b[i - 1]
if a[n] != b[m]:
print(-1)
exit()
def binary_search(prev, val):
l, r, ans = prev + 1, n, -1
while l <= r:
mid = (l + r) // 2
if a[mid] - a[prev] <= val:
ans = mid
l = mid + 1
else:
r = mid - 1
if a[ans] - a[prev] == val:
return ans
else:
return -1
preva, prevb, k, i = 0, 0, 0, 1
while i <= m:
idx = binary_search(preva, b[i] - b[prevb])
if idx == -1:
i += 1
else:
k += 1
preva = idx
prevb = i
i += 1
print(k)
|
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def fun(arr, arr2):
tem = []
tot = 0
for i in arr:
tot += i
tem.append(tot)
tem2 = []
tot = 0
for i in arr2:
tot += i
tem2.append(tot)
tem = set(tem)
tem1 = set(tem2)
return len(tem.intersection(tem2))
k = int(input())
arr = list(map(int, input().split(" ")))
k = int(input())
arr2 = list(map(int, input().split(" ")))
if sum(arr) == sum(arr2):
print(fun(arr, arr2))
else:
print(-1)
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL 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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def __starting_point():
n1 = int(input().strip())
a1 = [int(__) for __ in input().strip().split()]
n2 = int(input().strip())
a2 = [int(__) for __ in input().strip().split()]
s1, s2 = sum(a1), sum(a2)
if a1 == a2:
print(n1)
elif sum(a1) != sum(a2):
print(-1)
else:
s1, s2 = 0, 0
e1, e2 = n1 - 1, n2 - 1
ans = 0
while a1[s1] == a2[s2]:
s1 += 1
s2 += 1
ans += 1
while a1[e1] == a2[e2]:
e1 -= 1
e2 -= 1
ans += 1
p1 = [a1[s1]]
p2 = [a2[s2]]
for i in range(s1 + 1, e1 + 1):
p1.append(p1[-1] + a1[i])
for i in range(s2 + 1, e2 + 1):
p2.append(p2[-1] + a2[i])
d = {}
for x in p1:
d[x] = 1
for x in p2:
if x in d:
ans += 1
print(ans)
__starting_point()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
na = int(input())
a = [int(x) for x in input().split()]
nb = int(input())
b = [int(x) for x in input().split()]
res = 0
ia, ib = 0, 0
suma, sumb = a[0], b[0]
while ia < len(a) and ib < len(b):
if suma == sumb:
ia += 1
ib += 1
res += 1
if ia < len(a):
suma = a[ia]
else:
suma = -1
if ib < len(b):
sumb = b[ib]
else:
sumb = -1
elif suma < sumb:
if ia + 1 < len(a):
suma += a[ia + 1]
ia += 1
else:
if ib + 1 < len(b):
sumb += b[ib + 1]
ib += 1
if suma != sumb:
print(-1)
else:
print(res)
|
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 VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
N = int(input())
A = [int(x) for x in input().split()]
M = int(input())
B = [int(x) for x in input().split()]
ans = 0
i, j = 0, 0
sa, sb = 0, 0
while i < N or j < M:
if sa == sb:
ans += 1
if i < N:
sa += A[i]
i += 1
if j < M:
sb += B[j]
j += 1
elif sa < sb:
if i < N:
sa += A[i]
i += 1
else:
break
elif j < M:
sb += B[j]
j += 1
else:
break
if i == N and j == M and sa == sb:
print(ans)
else:
print(-1)
|
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 VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) == sum(b):
ans = 0
suma = 0
sumb = 0
uka = 0
ukb = 0
while uka <= n - 1 or ukb <= m - 1:
if suma + a[uka] == sumb + b[ukb]:
suma += a[uka]
sumb += b[ukb]
ans += 1
uka += 1
ukb += 1
elif suma + a[uka] < sumb + b[ukb]:
suma += a[uka]
uka += 1
else:
sumb += b[ukb]
ukb += 1
print(ans)
else:
print(-1)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
k = 0
i = 0
j = 0
sum1 = 0
sum2 = 0
while i < n and j < m:
if sum1 == 0 and sum2 == 0:
sum1 = a[i]
sum2 = b[j]
i += 1
j += 1
if i >= n or j >= m:
break
if sum1 == sum2:
k += 1
sum1 = 0
sum2 = 0
elif sum1 < sum2:
sum1 += a[i]
i += 1
else:
sum2 += b[j]
j += 1
while i < n:
sum1 += a[i]
i += 1
while j < m:
sum2 += b[j]
j += 1
if sum1 == sum2:
if sum2 > 0:
print(k + 1)
else:
print(k)
else:
print(-1)
|
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
get_input = lambda: list(map(int, input().split()))
n = get_input()[0]
arr_A = get_input()
m = get_input()[0]
arr_B = get_input()
arr_A.append(0)
arr_B.append(0)
curr_A = 0
curr_B = 0
sum_A = arr_A[curr_A]
sum_B = arr_B[curr_B]
res = 0
while curr_A < n and curr_B < m:
if sum_A == sum_B:
curr_A += 1
curr_B += 1
res += 1
if curr_A < n:
sum_A = arr_A[curr_A]
if curr_B < m:
sum_B = arr_B[curr_B]
elif sum_A > sum_B:
curr_B += 1
sum_B += arr_B[curr_B]
else:
curr_A += 1
sum_A += arr_A[curr_A]
if curr_A == n and curr_B == m:
print(res)
else:
print(-1)
|
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 ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
lenA1 = int(input())
a1 = list(map(int, input().split()))
lenA2 = int(input())
a2 = list(map(int, input().split()))
i1 = 0
i2 = 0
soma1 = a1[0]
soma2 = a2[0]
count = 0
lenA1 -= 1
lenA2 -= 1
while i1 < lenA1 and i2 < lenA2:
if soma1 > soma2:
i2 += 1
soma2 += a2[i2]
elif soma1 < soma2:
i1 += 1
soma1 += a1[i1]
else:
i1 += 1
i2 += 1
soma1 = a1[i1]
soma2 = a2[i2]
count += 1
for i in range(i1 + 1, lenA1 + 1):
soma1 += a1[i]
for i in range(i2 + 1, lenA2 + 1):
soma2 += a2[i]
if soma1 != soma2:
print(-1)
else:
print(count + (soma1 != 0))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def go():
n = int(input())
a = [int(i) for i in input().split(" ")]
m = int(input())
b = [int(i) for i in input().split(" ")]
i = 0
so_far_i = 0
j = 0
so_far_j = 0
while i < n and so_far_i < n and j < m and so_far_j < m:
if a[i] == b[j]:
i = so_far_i
j = so_far_j
i += 1
j += 1
so_far_i = i
so_far_j = j
elif a[i] > b[j]:
if so_far_j + 1 < m:
b[j] += b[so_far_j + 1]
so_far_j += 1
b[so_far_j] = None
else:
break
elif so_far_i + 1 < n:
a[i] += a[so_far_i + 1]
so_far_i += 1
a[so_far_i] = None
else:
break
if sum([i for i in a if i is not None]) != sum([i for i in b if i is not None]):
return -1
return len([i for i in a if i is not None])
print(go())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING 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 ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE IF FUNC_CALL VAR VAR VAR VAR VAR NONE FUNC_CALL VAR VAR VAR VAR VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
def main():
def read_list():
return list(map(int, input().split()))
input()
A = read_list()
input()
B = read_list()
length = 0
i_start = 0
j_start = 0
while i_start < len(A) or j_start < len(B):
i = i_start
j = j_start
s1 = 0
s2 = 0
while s1 != s2 or i == i_start or j == j_start:
if s1 <= s2:
if i == len(A):
print(-1)
return
s1 += A[i]
i += 1
else:
if j == len(B):
print(-1)
return
s2 += B[j]
j += 1
i_start = i
j_start = j
length += 1
print(length)
main()
|
FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
a_now = 0
b_now = 0
b_idx = 0
ans = 0
for i in range(n):
a_now += a[i]
if a_now > b_now:
while a_now > b_now:
if b_idx == m:
break
b_now += b[b_idx]
b_idx += 1
if a_now > b_now:
print(-1)
exit()
elif a_now == b_now:
a_now = 0
b_now = 0
ans += 1
elif a_now == b_now:
a_now = 0
b_now = 0
ans += 1
if i == n - 1 and (a_now != b_now or b_idx < m):
print(-1)
exit()
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
s1 = [int(x) for x in input().split()]
m = int(input())
s2 = [int(x) for x in input().split()]
if n == 1 or m == 1:
if n == 1:
r = 0
for j in range(0, m):
r = r + s2[j]
if r == s1[0]:
print("1")
else:
print("-1")
else:
r = 0
for j in range(0, n):
r = r + s1[j]
if r == s2[0]:
print("1")
else:
print("-1")
else:
S1 = sum(s1)
S2 = sum(s2)
if S1 != S2:
print("-1")
else:
pt1 = 0
pt2 = 0
sm1 = 0
sm2 = 0
L1 = []
L2 = []
c = 0
while pt1 < n and pt2 < m:
if sm1 == sm2:
c = c + 1
if sm1 >= sm2:
sm2 = sm2 + s2[pt2]
pt2 = pt2 + 1
else:
sm1 = sm1 + s1[pt1]
pt1 = pt1 + 1
print(c)
|
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 IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
a = int(input())
z = list(map(int, input().split()))
x = int(input())
m = list(map(int, input().split()))
l = 0
r = 0
s1 = z[0]
s2 = m[0]
flag = 0
count = 0
while l < len(z) and r < len(m):
if s1 == s2:
count += 1
l += 1
r += 1
if l < len(z) and r < len(m):
s1 = z[l]
s2 = m[r]
if s1 > s2:
r += 1
if r < len(m):
s2 += m[r]
if s1 < s2:
l += 1
if l < len(z):
s1 += z[l]
if l == len(z) and r == len(m):
print(count)
else:
print(-1)
|
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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
from itertools import *
for _ in range(1):
n = int(input())
d = set()
arr = list(accumulate(map(int, input().split())))
m = int(input())
brr = list(accumulate(map(int, input().split())))
if arr[-1] != brr[-1]:
print(-1)
continue
for i in arr:
d.add(i)
count = 0
for i in brr:
if i in d:
count += 1
print(count)
|
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
input = sys.stdin.readline
out = sys.stdout
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
else:
ans = 0
curA, curB = 0, 0
while len(a) and len(b):
curA += a.pop()
while curA != curB:
if curA < curB:
curA += a.pop()
else:
curB += b.pop()
curA, curB = 0, 0
ans += 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
from itertools import accumulate
def main():
n = int(input())
arr1 = list(accumulate(map(int, input().split())))
m = int(input())
arr2 = list(accumulate(map(int, input().split())))
if arr1[-1] != arr2[-1]:
print(-1)
exit(0)
s1 = set(arr1)
s2 = set(arr2)
print(len(s1.intersection(s2)))
main()
|
FUNC_DEF 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 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 IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
a = int(input())
arraya = list(map(int, input().split()))
b = int(input())
arrayb = list(map(int, input().split()))
ans = 0
ia = 0
ib = 0
ma = arraya[0]
mb = arrayb[0]
while ia < a and ib < b:
if ma == mb:
ans += 1
ia += 1
ib += 1
if ia == a or ib == b:
break
ma = arraya[ia]
mb = arrayb[ib]
elif ma < mb:
mb -= ma
ia += 1
if ia == a:
break
ma = arraya[ia]
elif ma > mb:
ma -= mb
ib += 1
if ib == b:
break
mb = arrayb[ib]
if ia != a or ib != b:
print(-1)
else:
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
i = 0
j = 0
curra = a[i]
currb = b[j]
c = 0
if sum(a) != sum(b):
print(-1)
else:
while i < n and j < m:
if curra == currb:
i += 1
j += 1
c += 1
if i < n and j < m:
curra = a[i]
currb = b[j]
elif currb > curra:
i += 1
if i < n:
curra += a[i]
else:
j += 1
if j < m:
currb += b[j]
print(c)
|
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 VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
exit()
ret = 0
p1 = p2 = 0
while p1 < n and p2 < m:
s1, s2 = a[p1], b[p2]
while s1 != s2 and p1 < n and p2 < m:
while s1 < s2 and p1 + 1 < n:
p1 += 1
s1 += a[p1]
while s2 < s1 and p2 + 1 < m:
p2 += 1
s2 += b[p2]
ret += 1
p1, p2 = p1 + 1, p2 + 1
print(ret)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def comp(a, b):
if a > b:
return 2
return 2 if a == b else 0
def gi():
return [xx for xx in input().split()]
def gtc(tc, ans):
print("Case #" + str(tc) + ":", ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = 1
uu = t
while t > 0:
t -= 1
n = fi()
a = li()
m = fi()
b = li()
i = j = 1
c = 0
if sum(a) != sum(b):
print(-1)
else:
ca = a[0]
cb = b[0]
while i < n and j < m:
if ca == cb:
c += 1
ca = a[i]
cb = b[j]
i += 1
j += 1
elif ca < cb:
ca += a[i]
i += 1
else:
cb += b[j]
j += 1
print(c + 1)
|
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
import sys
N1 = int(input())
arr1 = [int(elem) for elem in input().split(" ")]
N2 = int(input())
arr2 = [int(elem) for elem in input().split(" ")]
def merge_arrays(arr1, arr2):
if sum(arr1) != sum(arr2):
return -1
else:
length = 0
while len(arr1) > 0 and len(arr2) > 0:
if arr1[-1] == arr2[-1]:
length += 1
arr1.pop()
arr2.pop()
elif arr1[-1] < arr2[-1]:
arr1[-2] += arr1[-1]
arr1.pop()
else:
arr2[-2] += arr2[-1]
arr2.pop()
return length
print(merge_arrays(arr1, arr2))
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n1 = int(input())
a1 = list(map(int, input().split()))
n2 = int(input())
a2 = list(map(int, input().split()))
if n1 < n2:
a1, a2 = a2, a1
if sum(a1) != sum(a2):
print(-1)
else:
c = 1
f = 0
s = 0
c1 = a1[f]
c2 = a2[s]
while s + 1 < len(a2) and f + 1 < len(a1):
if c1 == c2:
f += 1
s += 1
c += 1
c1 = a1[f]
c2 = a2[s]
elif c1 > c2:
s += 1
c2 += a2[s]
else:
f += 1
c1 += a1[f]
print(c)
|
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 IF VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
x = sum(a)
y = sum(b)
if x != y:
print(-1)
else:
pref1 = [a[0]]
pref2 = [b[0]]
for i in range(1, n):
pref1.append(pref1[-1] + a[i])
for i in range(1, m):
pref2.append(pref2[-1] + b[i])
dict1 = {}
for i in range(n):
dict1[pref1[i]] = i
ans = 0
for i in range(m):
if pref2[i] in dict1.keys():
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
lis1 = list(map(int, input().split()))
m = int(input())
lis2 = list(map(int, input().split()))
i = j = 0
a = 0
b = 0
ans = -1
while i < n or j < m:
if a == b:
ans += 1
if i < n:
a = lis1[i]
i += 1
else:
a = 0
if j < m:
b = lis2[j]
j += 1
else:
b = 0
if a < b:
while i < n and a < b:
a += lis1[i]
i += 1
if b < a:
while j < m and b < a:
b += lis2[j]
j += 1
if a < b and i >= n or b < a and j >= m:
print(-1)
exit()
if a != b:
print(-1)
else:
print(ans + 1)
|
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = [int(x) for x in input().split()]
m = int(input())
b = [int(x) for x in input().split()]
i = j = 0
x = a[0]
y = b[0]
length = 0
while True:
if x == y and i < n - 1 and j < m - 1:
length += 1
i += 1
j += 1
x = a[i]
y = b[j]
elif x < y and i < n - 1:
i += 1
x += a[i]
elif x > y and j < m - 1:
j += 1
y += b[j]
else:
break
if i == n - 1 and j == m - 1 and x == y:
print(length + 1)
else:
print(-1)
|
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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
if sum(a) != sum(b):
print(-1)
else:
L = 0
while a or b:
if a[-1] == b[-1]:
del a[-1]
del b[-1]
L += 1
elif a[-1] < b[-1]:
a[-2] += a[-1]
del a[-1]
else:
b[-2] += b[-1]
del b[-1]
print(L)
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has two arrays $A$ and $B$ of lengths $n$ and $m$, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $[1, 10, 100, 1000, 10000]$ Vasya can obtain array $[1, 1110, 10000]$, and from array $[1, 2, 3]$ Vasya can obtain array $[6]$.
Two arrays $A$ and $B$ are considered equal if and only if they have the same length and for each valid $i$ $A_i = B_i$.
Vasya wants to perform some of these operations on array $A$, some on array $B$, in such a way that arrays $A$ and $B$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $A$ and $B$ equal.
-----Input-----
The first line contains a single integer $n~(1 \le n \le 3 \cdot 10^5)$ β the length of the first array.
The second line contains $n$ integers $a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$ β elements of the array $A$.
The third line contains a single integer $m~(1 \le m \le 3 \cdot 10^5)$ β the length of the second array.
The fourth line contains $m$ integers $b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$ - elements of the array $B$.
-----Output-----
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays $A$ and $B$ in such a way that they became equal.
If there is no way to make array equal, print "-1".
-----Examples-----
Input
5
11 2 3 5 7
4
11 7 3 7
Output
3
Input
2
1 2
1
100
Output
-1
Input
3
1 2 3
3
1 2 3
Output
3
|
n = int(input())
a = [int(i) for i in input().split(" ")]
m = int(input())
b = [int(i) for i in input().split(" ")]
aa = [a[0]]
bb = [b[0]]
for i in range(1, n):
aa.append(aa[-1] + a[i])
for i in range(1, m):
bb.append(bb[-1] + b[i])
a1 = set(aa)
a2 = set(bb)
if sum(a) != sum(b):
print(-1)
else:
print(len(a1.intersection(a2)))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
from sys import *
input = stdin.readline
def superstring(x, y, fx, n):
ans = ""
i, j = 0, 0
while i < 2 * n and j < 2 * n:
if x[i] == y[j]:
ans += x[i]
i += 1
j += 1
elif x[i] != fx:
ans += x[i]
i += 1
else:
ans += y[j]
j += 1
return ans + x[i:] + y[j:]
for _ in range(int(input())):
n = int(input())
a, b, c = input().strip(), input().strip(), input().strip()
one, zero = [], []
for i in [a, b, c]:
if i.count("0") >= n:
zero.append(i)
else:
one.append(i)
currans = ""
if len(zero) > len(one):
currans = superstring(zero[0], zero[1], "0", n)
else:
currans = superstring(one[0], one[1], "1", n)
print(currans.rjust(3 * n, "0"))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR LIST VAR VAR VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
from sys import stdin, stdout
def count(s):
return sum(1 for c in s if c == "1")
def build(s1, s2, m):
i1 = 0
i2 = 0
answer = ""
while i1 < len(s1) and i2 < len(s2):
if s1[i1] == s2[i2]:
answer += s1[i1]
i1 += 1
i2 += 1
elif s1[i1] != m:
answer += s1[i1]
i1 += 1
else:
answer += s2[i2]
i2 += 1
return answer + s1[i1:] + s2[i2:] + "\n"
n = int(stdin.readline())
for _ in range(n):
k = int(stdin.readline())
a = stdin.readline().strip()
b = stdin.readline().strip()
c = stdin.readline().strip()
arr0 = []
arr1 = []
for s in [a, b, c]:
if count(s) >= k:
arr1.append(s)
else:
arr0.append(s)
if len(arr0) > 1:
stdout.write(build(arr0[0], arr0[1], "0"))
else:
stdout.write(build(arr1[0], arr1[1], "1"))
|
FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = sys.stdin.readline
def main():
n = int(input())
S1 = input().strip()
S2 = input().strip()
S3 = input().strip()
for s1 in (S1, S2, S3):
for s2 in (S1, S2, S3):
if s1 == s2:
continue
z1 = s1.count("0")
o1 = s1.count("1")
z2 = s2.count("0")
o2 = s2.count("1")
mz = max(z1, z2)
mo = max(o1, o2)
if mz > n and mo > n:
continue
if mz <= n:
cnt1 = []
cnt2 = []
bef = "0"
cnt = 0
for s in s1:
if s == "1":
cnt1.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt1.append(cnt)
bef = "0"
cnt = 0
for s in s2:
if s == "1":
cnt2.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt2.append(cnt)
if len(cnt1) > len(cnt2):
cnt2 += [0] * (len(cnt1) - len(cnt2))
elif len(cnt1) < len(cnt2):
cnt1 += [0] * (len(cnt2) - len(cnt1))
ans = []
for c1, c2 in zip(cnt1, cnt2):
ans += ["0"] * max(c1, c2)
ans += ["1"]
ans.pop()
if len(ans) < 3 * n:
ans += ["1"] * (3 * n - len(ans))
print("".join(ans))
return
elif mo <= n:
cnt1 = []
cnt2 = []
bef = "1"
cnt = 0
for s in s1:
if s == "0":
cnt1.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt1.append(cnt)
bef = "1"
cnt = 0
for s in s2:
if s == "0":
cnt2.append(cnt)
cnt = 0
else:
cnt += 1
bef = s
if cnt:
cnt2.append(cnt)
if len(cnt1) > len(cnt2):
cnt2 += [0] * (len(cnt1) - len(cnt2))
elif len(cnt1) < len(cnt2):
cnt1 += [0] * (len(cnt2) - len(cnt1))
ans = []
for c1, c2 in zip(cnt1, cnt2):
ans += ["1"] * max(c1, c2)
ans += ["0"]
ans.pop()
if len(ans) < 3 * n:
ans += ["0"] * (3 * n - len(ans))
print("".join(ans))
return
for _ in range(int(input())):
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR VAR VAR LIST STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP LIST STRING BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP LIST STRING FUNC_CALL VAR VAR VAR VAR LIST STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP LIST STRING BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N = int(input())
A = [int(a) for a in input()]
B = [int(a) for a in input()]
C = [int(a) for a in input()]
aa = 1 if A.count(0) >= N else 0
bb = 1 if B.count(0) >= N else 0
cc = 1 if C.count(0) >= N else 0
f = 0
if aa == bb:
pass
elif aa == cc:
bb = cc
B = C
elif bb == cc:
aa = cc
A = C
if aa == 0:
f = 1
A = [(a ^ 1) for a in A]
B = [(a ^ 1) for a in B]
S = []
while A and B:
if A[-1] == B[-1]:
S.append(A.pop())
B.pop()
elif A[-1] == 1:
S.append(1)
A.pop()
else:
S.append(1)
B.pop()
while A:
S.append(A.pop())
while B:
S.append(B.pop())
if f:
S = [(a ^ 1) for a in S]
print("".join(map(str, reversed(S))))
|
IMPORT ASSIGN 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 VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
s1 = stdin.readline(2 * n)
stdin.readline(1)
s2 = stdin.readline(2 * n)
stdin.readline(1)
s3 = stdin.readline(2 * n)
stdin.readline(1)
strings = [s1, s2, s3]
strings.sort(key=lambda x: x.count("0"))
if strings[1].count("0") >= n:
s1, s2 = strings[2], strings[1]
for i in range(2 * n):
if s2[i] == "1":
stdout.write("1")
else:
break
idx = i
for i in range(2 * n):
if s1[i] == "1":
stdout.write("1")
else:
stdout.write("0")
idx += 1
for j in range(idx, 2 * n):
if s2[j] == "1":
stdout.write("1")
else:
break
idx = j
stdout.write("\n")
continue
strings.sort(key=lambda x: x.count("1"))
if strings[1].count("1") >= n:
s1, s2 = strings[2], strings[1]
for i in range(2 * n):
if s2[i] == "0":
stdout.write("0")
else:
break
idx = i
for i in range(2 * n):
if s1[i] == "0":
stdout.write("0")
else:
stdout.write("1")
idx += 1
for j in range(idx, 2 * n):
if s2[j] == "0":
stdout.write("0")
else:
break
idx = j
stdout.write("\n")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR NUMBER STRING VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR NUMBER STRING VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
from itertools import product
def mix(tpl, c):
a, b = tpl
n = len(a)
ans = []
ap, bp = 0, 0
while True:
while ap < n and a[ap] != c:
ans.append(a[ap])
ap += 1
while bp < n and b[bp] != c:
ans.append(b[bp])
bp += 1
if ap < n or bp < n:
ans.append(c)
ap += 1
bp += 1
if ap >= n and bp >= n:
break
return "".join(ans)
def solve():
n = int(input())
a, b, c = [input() for i in range(3)]
return min([mix(*p) for p in product([(a, b), (b, c), (c, a)], "01")], key=len)
t = int(input())
for tc in range(t):
print(solve())
|
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
from sys import stdin
input = stdin.readline
def good(a, b, ch):
cnt1, cnt2 = 0, 0
n = len(a)
for i in range(n):
if a[i] == ch:
cnt1 += 1
if b[i] == ch:
cnt2 += 1
i, j = 0, 0
ans = ""
if min(cnt1, cnt2) >= n // 2:
while i < n and j < n:
if a[i] == b[j] == ch:
ans += ch
i += 1
j += 1
else:
if a[i] != ch:
ans += a[i]
i += 1
if b[j] != ch:
ans += b[j]
j += 1
while i < n:
ans += a[i]
i += 1
while j < n:
ans += b[j]
j += 1
if len(ans) <= n // 2 * 3:
print(ans)
return True
else:
return False
def f(b, n):
lst = [(b[0], b[1]), (b[0], b[2]), (b[1], b[2])]
for i in lst:
x = good(i[0], i[1], "1")
if x:
return ""
x = good(i[0], i[1], "0")
if x:
return ""
for i in range(int(input())):
n = int(input())
b = []
for j in range(3):
b.append(input().strip())
print(f(b, n))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING IF VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
c = input()
i, j, k = 0, 0, 0
res = []
while True:
if i == 2 * n:
if j >= k:
for x in range(j, 2 * n):
res.append(b[x])
else:
for x in range(k, 2 * n):
res.append(c[x])
break
elif j == 2 * n:
if i >= k:
for x in range(i, 2 * n):
res.append(a[x])
else:
for x in range(k, 2 * n):
res.append(c[x])
break
elif k == 2 * n:
if j >= i:
for x in range(j, 2 * n):
res.append(b[x])
else:
for x in range(i, 2 * n):
res.append(a[x])
break
if a[i] == b[j]:
res.append(a[i])
i += 1
j += 1
elif b[j] == c[k]:
res.append(b[j])
j += 1
k += 1
else:
res.append(a[i])
i += 1
k += 1
print(*res, sep="")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
ans = []
s1 = input()
s2 = input()
s3 = input()
fincount = 0
count10 = 0
for i in range(2 * n):
if s1[i] == "0":
count10 += 1
if count10 >= n:
fincount += 1
count20 = 0
for i in range(2 * n):
if s2[i] == "0":
count20 += 1
if count20 >= n:
fincount += 1
count30 = 0
for i in range(2 * n):
if s3[i] == "0":
count30 += 1
if count30 >= n:
fincount += 1
if fincount >= 2:
mode = "0"
unmode = "1"
else:
mode = "1"
unmode = "0"
if mode == "0":
if count10 >= n and count20 >= n:
st1 = s1
st2 = s2
elif count10 >= n and count30 >= n:
st1 = s1
st2 = s3
elif count20 >= n and count30 >= n:
st1 = s2
st2 = s3
elif count10 <= n and count20 <= n:
st1 = s1
st2 = s2
elif count10 <= n and count30 <= n:
st1 = s1
st2 = s3
elif count20 <= n and count30 <= n:
st1 = s2
st2 = s3
pointer1 = 0
pointer2 = 0
while pointer1 < 2 * n or pointer2 < 2 * n:
while pointer1 < 2 * n and st1[pointer1] == unmode:
pointer1 += 1
ans.append(unmode)
while pointer2 < 2 * n and st2[pointer2] == unmode:
pointer2 += 1
ans.append(unmode)
if pointer1 >= 2 * n and pointer2 >= 2 * n:
break
ans.append(mode)
pointer1 += 1
pointer2 += 1
print("".join(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 LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import itertools
def solve(a, b):
if a.count("1") * 2 >= len(a) and b.count("1") * 2 >= len(b):
ch = "1"
other = "0"
else:
ch = "0"
other = "1"
res = []
i = 0
j = 0
ilast = -1
jlast = -1
while i < len(a) and j < len(b):
while i < len(a) and a[i] != ch:
i += 1
while j < len(b) and b[j] != ch:
j += 1
res.append(other * max(i - ilast - 1, j - jlast - 1))
if i != len(a) or j != len(b):
res.append(ch)
ilast = i
jlast = j
i += 1
j += 1
while i < len(a):
res.append(a[i])
i += 1
while j < len(b):
res.append(b[j])
j += 1
return "".join(res)
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
c = input()
na1 = a.count("1")
nb1 = b.count("1")
nc1 = c.count("1")
res = None
if na1 >= n and nb1 >= n or na1 <= n and nb1 <= n:
res = solve(a, b)
elif na1 >= n and nc1 >= n or na1 <= n and nc1 <= n:
res = solve(a, c)
elif nb1 >= n and nc1 >= n or nb1 <= n and nc1 <= n:
res = solve(b, c)
else:
raise RuntimeError("unexpected")
print(res)
|
IMPORT FUNC_DEF IF BIN_OP FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
def f(s1, s2, n, c):
i1, i2 = 0, 0
nz = 0
l = []
b = True
while i1 + i2 != 4 * n:
if i1 == 2 * n:
b = False
if i2 == 2 * n:
b = True
if b:
if s1[i1] == c and nz < n:
b = False
else:
l.append(s1[i1])
i1 += 1
else:
if s2[i2] == c and nz < n:
b = True
l.append(c)
nz += 1
else:
l.append(s2[i2])
i2 += 1
print("".join(l))
for kT in range(int(input())):
n = int(input())
s1 = input()
s2 = input()
s3 = input()
nz1 = s1.count("0")
nz2 = s2.count("0")
nz3 = s3.count("0")
if nz1 >= n:
if nz2 >= n:
f(s1, s2, n, "0")
elif nz3 >= n:
f(s1, s3, n, "0")
else:
f(s2, s3, n, "1")
elif nz2 < n:
f(s1, s2, n, "1")
elif nz3 < n:
f(s1, s3, n, "1")
else:
f(s2, s3, n, "0")
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = sys.stdin.readline
for f in range(int(input())):
n = int(input())
s = []
for i in range(3):
s.append(input())
s1 = []
s0 = []
for i in range(3):
s0.append(s[i].count("0"))
s1.append(2 * n - s0[-1])
for i in range(2):
for j in range(i + 1, 3):
if s0[i] >= n and s0[j] >= n:
c1 = i
c2 = j
g = "0"
b = "1"
if s1[i] >= n and s1[j] >= n:
c1 = i
c2 = j
g = "1"
b = "0"
i = j = 0
while i < 2 * n or j < 2 * n:
if i < 2 * n and s[c1][i] == b:
i += 1
print(end=b)
elif j < 2 * n and s[c2][j] == b:
j += 1
print(end=b)
else:
print(end=g)
i += 1
j += 1
print()
|
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 LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
t = int(input())
for _ in range(t):
n = int(input())
s1 = input()
s2 = input()
s3 = input()
p1, p2, p3 = 0, 0, 0
res = []
while max(p1, p2, p3) < 2 * n:
if s1[p1] == s2[p2]:
res.append(s1[p1])
p1 += 1
p2 += 1
elif s1[p1] == s3[p3]:
res.append(s1[p1])
p1 += 1
p3 += 1
else:
res.append(s2[p2])
p2 += 1
p3 += 1
rest = ""
if p1 == 2 * n:
if p2 >= p3:
rest = s2[p2:]
else:
rest = s3[p3:]
elif p2 == 2 * n:
if p1 >= p3:
rest = s1[p1:]
else:
rest = s3[p3:]
elif p1 >= p2:
rest = s1[p1:]
else:
rest = s2[p2:]
print(*res, sep="", end="")
print(rest)
|
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = iter(sys.stdin.read().splitlines()).__next__
def solve():
n = int(input())
a = input()
b = input()
c = input()
median_0 = []
median_1 = []
for string in (a, b, c):
popcount = string.count("1")
if popcount <= n:
median_0.append(string)
if popcount >= n:
median_1.append(string)
if len(median_0) > 1:
a = median_0.pop()
b = median_0.pop()
median = "0"
else:
a = median_1.pop()
b = median_1.pop()
median = "1"
novel = []
i = 0
j = 0
while i < 2 * n or j < 2 * n:
if i < 2 * n and a[i] != median:
novel.append(a[i])
i += 1
elif j < 2 * n and b[j] != median:
novel.append(b[j])
j += 1
else:
novel.append(median)
if i < 2 * n:
i += 1
if j < 2 * n:
j += 1
return "".join(novel)
t = int(input())
output = []
for _ in range(t):
output.append(solve())
print(*output, sep="\n")
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
def merge(x, y, c):
N = len(x)
n = N // 2
ans = ""
i, j = 0, 0
while i < N and j < N:
if x[i] == y[j]:
ans += x[i]
i += 1
j += 1
elif x[i] != c:
ans += x[i]
i += 1
else:
ans += y[j]
j += 1
ans += x[i:] + y[j:]
ans += "0" * (3 * n - len(ans))
return ans
t = int(input())
for _ in range(t):
n = int(input())
S = [input(), input(), input()]
C0 = [j for j in range(3) if S[j].count("0") >= n]
if len(C0) >= 2:
print(merge(S[C0[0]], S[C0[1]], "0"))
else:
C1 = [j for j in range(3) if j not in C0]
print(merge(S[C1[0]], S[C1[1]], "1"))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP STRING BIN_OP BIN_OP NUMBER VAR FUNC_CALL 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 LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER STRING
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
for _ in range(int(input())):
N = int(input())
S = []
for _ in range(3):
S.append(input().rstrip("\n"))
T0 = []
T1 = []
for i in range(3):
if S[i].count("0") > N:
T0.append(S[i])
else:
T1.append(S[i])
if len(T0) >= 2:
U0, U1 = T0[:2]
if U0.count("0") < U1.count("0"):
U0, U1 = U1, U0
ans = []
for i in range(2 * N):
ans.append(U0[i])
ans2 = []
j = 0
for u1 in U1:
if u1 == "0":
while True:
ans2.append(ans[j])
j += 1
if ans2[-1] == "0":
break
else:
ans2.append("1")
for k in range(j, 2 * N):
ans2.append(U0[k])
print("".join(ans2))
else:
U0, U1 = T1[:2]
if U0.count("1") < U1.count("1"):
U0, U1 = U1, U0
ans = []
for i in range(2 * N):
ans.append(U0[i])
ans2 = []
j = 0
for u1 in U1:
if u1 == "1":
while True:
ans2.append(ans[j])
j += 1
if ans2[-1] == "1":
break
else:
ans2.append("0")
for k in range(j, 2 * N):
ans2.append(U0[k])
print("".join(ans2))
main()
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF 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 LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING WHILE NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING WHILE NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = sys.stdin.readline
def mix(x, y):
if x.count("0") >= n and y.count("0") >= n:
ANS = []
i = 0
j = 0
while i < 2 * n or j < 2 * n:
counti = 0
countj = 0
while i < n * 2 and x[i] == "1":
counti += 1
i += 1
while j < n * 2 and y[j] == "1":
countj += 1
j += 1
ANS += ["1"] * max(counti, countj)
if i < 2 * n and x[i] == "0" or j < 2 * n and y[j] == "0":
ANS.append("0")
i += 1
j += 1
return ANS
elif x.count("1") >= n and y.count("1") >= n:
ANS = []
i = 0
j = 0
while i < 2 * n or j < 2 * n:
counti = 0
countj = 0
while i < n * 2 and x[i] == "0":
counti += 1
i += 1
while j < n * 2 and y[j] == "0":
countj += 1
j += 1
ANS += ["0"] * max(counti, countj)
if i < 2 * n and x[i] == "1" or j < 2 * n and y[j] == "1":
ANS.append("1")
i += 1
j += 1
return ANS
else:
return -1
t = int(input())
for tests in range(t):
n = int(input())
S = [list(input().strip()) for i in range(3)]
flag = 0
for i in range(3):
if flag == 1:
break
for j in range(3):
if i == j:
continue
k = mix(S[i], S[j])
if k == -1:
continue
else:
flag = 1
break
print("".join(k))
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP LIST STRING FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR STRING VAR BIN_OP NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER RETURN VAR IF FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP LIST STRING FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR STRING VAR BIN_OP NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
class Fmt:
def __init__(self):
a = input()
self.str = a
self.len = len(a)
self.cnt_0 = sum([(1) for i in a if i == "0"])
self.cnt_1 = self.len - self.cnt_0
def __getitem__(self, item):
return self.str[item]
def color(a, b, n):
if a.cnt_0 >= n and b.cnt_0 >= n:
return "0"
if a.cnt_1 >= n and b.cnt_1 >= n:
return "1"
return -1
def combine(a, b, c):
result = []
i = 0
j = 0
n = a.len
while i < n or j < n:
if i == n:
result.append(b[j])
j += 1
elif j == n:
result.append(a[i])
i += 1
elif a[i] == b[j]:
result.append(a[i])
i += 1
j += 1
elif a[i] != c:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
return "".join(result)
def run():
n = int(input())
S = [Fmt() for i in range(3)]
for i in range(3):
for j in range(3):
if i != j and color(S[i], S[j], n) != -1:
return combine(S[i], S[j], color(S[i], S[j], n))
test = int(input())
while test > 0:
test -= 1
print(run())
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN STRING IF VAR VAR VAR VAR RETURN STRING RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
input = sys.stdin.readline
outL = []
t = int(input())
for _ in range(t):
n = int(input())
s1 = input().strip()
s2 = input().strip()
s3 = input().strip()
c1 = 0
c2 = 0
c3 = 0
l = []
while max(c1, c2, c3) < 2 * n:
if s1[c1] == s2[c2] == s3[c3]:
l.append(s1[c1])
c1 += 1
c2 += 1
c3 += 1
elif s1[c1] == s2[c2]:
l.append(s1[c1])
c1 += 1
c2 += 1
elif s2[c2] == s3[c3]:
l.append(s2[c2])
c2 += 1
c3 += 1
else:
assert s1[c1] == s3[c3]
l.append(s1[c1])
c1 += 1
c3 += 1
rem = [(-c1, 1), (-c2, 2), (-c3, 3)]
rem.sort()
_, add = rem[1]
if add == 1:
sn = s1
cn = c1
elif add == 2:
sn = s2
cn = c2
else:
sn = s3
cn = c3
l.append(sn[cn:])
out = "".join(l)
if len(out) < 3 * n:
out += "0" * (3 * n - len(out))
outL.append(out)
print("\n".join(outL))
|
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP STRING BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
def merge(x, y, dict_x, dict_y, c):
N = len(x)
n = N // 2
v1, v2 = [-1] + dict_x[c], [-1] + dict_y[c]
ans = ""
for i in range(n):
for j in range(v1[i] + 1, v1[i + 1]):
ans += x[j]
for j in range(v2[i] + 1, v2[i + 1]):
ans += y[j]
ans += c
ans += "".join([x[j] for j in range(v1[n] + 1, N)])
ans += "".join([y[j] for j in range(v2[n] + 1, N)])
return ans
t = int(input())
for _ in range(t):
n = int(input())
S = [input(), input(), input()]
D = [{"0": [], "1": []} for i in range(3)]
for i in range(3):
for j, c in enumerate(S[i]):
D[i][c].append(j)
M0 = [i for i in range(3) if len(D[i]["0"]) >= n]
M1 = [i for i in range(3) if len(D[i]["1"]) >= n]
ans = ""
if len(M0) >= 2:
i1, i2 = M0[:2]
ans = merge(S[i1], S[i2], D[i1], D[i2], "0")
else:
i1, i2 = M1[:2]
ans = merge(S[i1], S[i2], D[i1], D[i2], "1")
print(ans)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR BIN_OP LIST NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER 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 LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING LIST LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR STRING VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR STRING VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n = int(input())
a = list(map(lambda x: int(x), list(input())))
b = list(map(lambda x: int(x), list(input())))
c = list(map(lambda x: int(x), list(input())))
aindex, bindex, cindex = 0, 0, 0
ans = []
addition = 0
while max(aindex, bindex, cindex) < 2 * n:
addition += 1
zero_count, one_count = 0, 0
if a[aindex] == 0:
zero_count += 1
else:
one_count += 1
if b[bindex] == 0:
zero_count += 1
else:
one_count += 1
if c[cindex] == 0:
zero_count += 1
else:
one_count += 1
if zero_count > one_count:
ans.append(0)
if a[aindex] == 0:
aindex += 1
if b[bindex] == 0:
bindex += 1
if c[cindex] == 0:
cindex += 1
continue
else:
ans.append(1)
if a[aindex] == 1:
aindex += 1
if b[bindex] == 1:
bindex += 1
if c[cindex] == 1:
cindex += 1
continue
left_addition = 3 * n - addition
satisfied = 0
if aindex == 2 * n:
satisfied += 1
if bindex == 2 * n:
satisfied += 1
if cindex == 2 * n:
satisfied += 1
if satisfied >= 2:
ans_string = "".join(map(lambda x: str(x), ans))
print(ans_string)
return
else:
if aindex < 2 * n and aindex + left_addition >= 2 * n:
for i in range(aindex, 2 * n):
ans.append(a[i])
elif bindex < 2 * n and bindex + left_addition >= 2 * n:
for i in range(bindex, 2 * n):
ans.append(b[i])
elif cindex < 2 * n and cindex + left_addition >= 2 * n:
for i in range(cindex, 2 * n):
ans.append(c[i])
ans_string = "".join(map(lambda x: str(x), ans))
print(ans_string)
return
def main():
t = int(input())
for i in range(t):
slv()
return
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
tc = int(input())
for i in range(tc):
n = int(input())
s = [input() for _ in range(3)]
p = [0] * 3
ans = ""
while True:
ones = []
oc = 0
zeroes = []
for i in range(3):
if s[i][p[i]] == "1":
ones.append(i)
oc += 1
else:
zeroes.append(i)
if oc >= 2:
print("1", end="")
for i in ones:
p[i] += 1
else:
print("0", end="")
for i in zeroes:
p[i] += 1
if p[0] == 2 * n or p[1] == 2 * n or p[2] == 2 * n:
break
x = sorted(enumerate(p), key=lambda i: i[1])
print(s[x[1][0]][x[1][1] :])
|
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 FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
|
A bitstring is a string that contains only the characters 0 and 1.
Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $2n$. A valid novel for the contest is a bitstring of length at most $3n$ that contains at least two of the three given strings as subsequences.
Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.
A string $a$ is a subsequence of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) characters.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$).
Each of the following three lines contains a bitstring of length $2n$. It is guaranteed that these three strings are pairwise distinct.
It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.
-----Output-----
For each test case, print a single line containing a bitstring of length at most $3n$ that has at least two of the given bitstrings as subsequences.
It can be proven that under the constraints of the problem, such a bitstring always exists.
If there are multiple possible answers, you may output any of them.
-----Examples-----
Input
2
1
00
11
01
3
011001
111010
010001
Output
010
011001010
-----Note-----
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.
In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
import sys
from sys import stdin
def solve(a, b, X):
ans = []
ca = [a[i] for i in range(len(a))]
cb = [b[i] for i in range(len(b))]
while len(ca) > 0 or len(cb) > 0:
if len(cb) == 0:
ans.append(ca[-1])
del ca[-1]
elif len(ca) == 0:
ans.append(cb[-1])
del cb[-1]
elif ca[-1] == cb[-1]:
ans.append(ca[-1])
del ca[-1]
del cb[-1]
else:
ans.append(X)
if ca[-1] == X:
del ca[-1]
else:
del cb[-1]
while len(ans) < 3 * n:
ans.append("0")
ans.reverse()
return "".join(ans)
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
s = [stdin.readline()[:-1] for i in range(3)]
lis = []
for i in range(3):
one = 0
zero = 0
for j in s[i]:
if j == "0":
zero += 1
else:
one += 1
if one < zero:
lis.append("1")
else:
lis.append("0")
flag = True
for i in range(3):
for j in range(i):
if lis[i] == lis[j] and flag:
print(solve(s[i], s[j], lis[i]))
flag = False
|
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR RETURN FUNC_CALL STRING 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 NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER
|
The only difference with E2 is the question of the problem..
Vlad built a maze out of $n$ rooms and $n-1$ bidirectional corridors. From any room $u$ any other room $v$ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.
Vlad invited $k$ friends to play a game with them.
Vlad starts the game in the room $1$ and wins if he reaches a room other than $1$, into which exactly one corridor leads.
Friends are placed in the maze: the friend with number $i$ is in the room $x_i$, and no two friends are in the same room (that is, $x_i \neq x_j$ for all $i \neq j$). Friends win if one of them meets Vlad in any room or corridor before he wins.
For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.
Friends know the plan of a maze and intend to win. Vlad is a bit afraid of their ardor. Determine if he can guarantee victory (i.e. can he win in any way friends play).
In other words, determine if there is such a sequence of Vlad's moves that lets Vlad win in any way friends play.
-----Input-----
The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. The input contains an empty string before each test case.
The first line of the test case contains two numbers $n$ and $k$ ($1 \le k < n \le 2\cdot 10^5$) β the number of rooms and friends, respectively.
The next line of the test case contains $k$ integers $x_1, x_2, \dots, x_k$ ($2 \le x_i \le n$) β numbers of rooms with friends. All $x_i$ are different.
The next $n-1$ lines contain descriptions of the corridors, two numbers per line $v_j$ and $u_j$ ($1 \le u_j, v_j \le n$) β numbers of rooms that connect the $j$ corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors.
It is guaranteed that the sum of the values $n$ over all test cases in the test is not greater than $2\cdot10^5$.
-----Output-----
Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be "YES" if Vlad can guarantee himself a victory and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers).
-----Examples-----
Input
4
8 2
5 3
4 7
2 5
1 6
3 6
7 2
1 7
6 8
3 1
2
1 2
2 3
3 1
2
1 2
1 3
3 2
2 3
3 1
1 2
Output
YES
NO
YES
NO
-----Note-----
In the first test case, regardless of the strategy of his friends, Vlad can win by going to room $4$. The game may look like this:
The original locations of Vlad and friends. Vlad is marked in green, friends β in red.
Locations after one unit of time.
End of the game.
Note that if Vlad tries to reach the exit at the room $8$, then a friend from the $3$ room will be able to catch him.
|
import sys
def findpaths(mypos: list, paths):
queue = mypos.copy()
times = [-1] * n
for i in mypos:
times[i] = 0
q = 0
while q < len(queue):
pos = queue[q]
dirs = paths[pos]
for i in dirs:
if times[i] == -1:
queue.append(i)
times[i] = times[pos] + 1
q += 1
return times
for _ in range(int(sys.stdin.readline())):
sys.stdin.readline()
n, k = [int(x) for x in sys.stdin.readline().split()]
friendposes = [(int(x) - 1) for x in sys.stdin.readline().split()]
paths = {i: [] for i in range(n)}
for i in range(n - 1):
v, u = [(int(x) - 1) for x in sys.stdin.readline().split()]
paths[v].append(u)
paths[u].append(v)
vlad = findpaths([0], paths)
friends = findpaths(friendposes, paths)
endposes = []
for i in range(1, n):
if len(paths[i]) == 1:
endposes.append(i)
won = any([(vlad[endpos] < friends[endpos]) for endpos in endposes])
print("YNEOS"[1 - won :: 2])
|
IMPORT FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP NUMBER VAR NUMBER
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.