Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Wrong Answer

Solution code:
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Asum = []
Bsum = []
a = 0
b = 0
for i in range(N):
    a += A[i]
    Asum.append(a)
for i in range(M):
    b += B[i]
    Bsum.append(b)
res = 0
for i,a in enumerate(Asum):
    if a > K:
        break
    for j,b in enumerate(Bsum):
       if a + b <= K:
           res = max(res,(i+j+2))
       else:
           break
print(res)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
ia = 0
ib = 0

nowtime = 0
cnt = 0
for i in range(n):
    if nowtime + a[i] <= k:
        nowtime += a[i]
        cnt += 1
        ia = i
    else:
        break

ans = cnt

for i in range(m):
    while nowtime + b[i] > k and ia >= 0:
        nowtime -= a[ia]
        cnt -= 1
        ia -= 1
    if nowtime + b[i] > k:
        break
    nowtime += b[i]
    cnt += 1
    ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    aptr = 0
    bptr = 0
    cnt = 0
    time = 0
    while True:
        a, b = float("inf"), float("inf")
        if aptr == N and bptr == M:
            break
        elif aptr == N:
            b = B[bptr]
        elif bptr == M:
            a = A[aptr]
        else:
            a, b = A[aptr], B[bptr]
        _time = min(a, b)
        if _time + time > K:
            break
        
        if a < b:
            aptr += 1
        else:
            bptr += 1
        cnt += 1
        time += _time

    print(cnt)



if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    cum_A = [0]
    for a in A:
        if a + cum_A[-1] <= K:
            cum_A.append(a + cum_A[-1])
    cum_B = [0]
    for b in B:
        if b + cum_B[-1] <= K:
            cum_B.append(b + cum_B[-1])

    ans = 0
    max_b_idx = len(cum_B) - 1
    for a_idx, a in enumerate(cum_A):
        if a <= K and a_idx > ans:
            ans = a_idx
        for b_idx in reversed(range(max_b_idx + 1)):
            if a + cum_B[b_idx] <= K:
                max_b_idx = b_idx
                if a_idx + b_idx > ans:
                    ans = a_idx + b_idx
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    cum_A = [0]
    for a in A:
        if a + cum_A[-1] <= K:
            cum_A.append(a + cum_A[-1])
    cum_B = [0]
    for b in B:
        if b + cum_B[-1] <= K:
            cum_B.append(b + cum_B[-1])

    ans = 0
    max_b_idx = len(cum_B)
    for a_idx, a in enumerate(cum_A):
        if a <= K and a_idx > ans:
            ans = a_idx
        for b_idx in reversed(range(max_b_idx)):
            if a + cum_B[b_idx] <= K:
                max_b_idx = b_idx
                if a_idx + b_idx > ans:
                    ans = a_idx + b_idx
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    cum_A = [0]
    for a in A:
        if a + cum_A[-1] <= K:
            cum_A.append(a + cum_A[-1])
    cum_B = [0]
    for b in B:
        if b + cum_B[-1] <= K:
            cum_B.append(b + cum_B[-1])

    ans = 0
    max_b_idx = len(cum_B) - 1
    for a_idx, a in enumerate(cum_A):
        for b_idx in reversed(range(max_b_idx+1)):
            if a + cum_B[b_idx] <= K:
                max_b_idx = b_idx
                if a_idx + b_idx > ans:
                    ans = a_idx + b_idx
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def possible(n):
    time = 0
    ia = 0
    ib = 0

    while time < n and (ia < len(a) or ib < len(b)):
        if ib == len(b):
            if time + a[ia] <= n:
                time += a[ia]
                ia += 1
                continue
            else:
                break

        if ia == len(a):
            if time + b[ib] <= n:
                time += b[ib]
                ib += 1
                continue
            else:
                break

        if a[ia] < b[ib]:
            time += a[ia]
            ia += 1
        else:
            time += b[ib]
            ib += 1

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

time = 0
ia = 0
ib = 0
cnt = 0
while time < k and (ia < len(a) or ib < len(b)):
    # print(time)
    if ib == len(b):
        if time + a[ia] <= k:
            time += a[ia]
            ia += 1
            cnt += 1
            continue
        else:
            break

    if ia == len(a):
        if time + b[ib] <= k:
            time += b[ib]
            ib += 1
            cnt += 1
            continue
        else:
            break

    if a[ia] < b[ib]:
        if time + a[ia] <= k:
            time += a[ia]
            ia += 1
            cnt += 1
        else:
            break
    else:
        if time + b[ib] <= k:
            time += b[ib]
            ib += 1
            cnt += 1
        else:
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
aa = a.copy()
bb = b.copy()

aa = [sum(a[:s+1]) for s in range(n)]
bb = [sum(b[:s+1]) for s in range(m)]
      
temp, ans = 0, 0

for i in range(n):
    for j in range(m)[::-1]:
        if aa[i] + bb[j] <= k:
            temp = i+1 + j+1
            break
    if temp > ans:
        ans = temp
        
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
aa = a.copy()
bb = b.copy()

aa = [sum(a[:s+1]) for s in range(n)]
bb = [sum(b[:s+1]) for s in range(m)]
      
temp, ans = 0, 0

for i in range(1, n):
    for j in range(m)[::-1]:
        if aa[i] + bb[j] <= k:
            temp = i+1 + j+1
            break
    if temp > ans:
        ans = temp
        
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

i = 0
j = 0
time = 0
cnt = -1

aa = a.copy()
bb = b.copy()

aa = [sum(a[:s+1]) for s in range(n)]
bb = [sum(b[:s+1]) for s in range(m)]

while time <= k and i <= n-1 and j <= m-1:
    cnt += 1
    if aa[i] <= bb[j]:
        time += a[i]
        if i < n -1:
            i += 1
        else:
            aa[n-1] = 10**9+7
    else:
        time += b[j]
        if j < m-1:
            j += 1
        else:
            bb[m-1] = 10**9+8

print(cnt)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())

A = list(map(int, input().split()))
B = list(map(int, input().split()))

i = 0
a = 0
b = 0
time = 0
while True:
    i += 1
    if a >= len(A):
        time += B[b]
        b += 1
    elif b >= len(B):
        time += A[a]
        a += 1
    else:
        if A[a] <= B[b]:
            time += A[a]
            a += 1
        elif A[a] > B[b]:
            time += B[b]
            b += 1
    if K < time:
        print(i - 1)
        exit()
    elif a >= len(A) and b >= len(B):
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())

A = list(map(int, input().split()))
B = list(map(int, input().split()))

i = 0
time = 0
while True:
    i += 1
    if len(A) == 0:
        time += B[0]
        del B[0]
    elif len(B) == 0:
        time += A[0]
        del A[0]
    else:
        if A[0] <= B[0]:
            time += A[0]
            del A[0]
        elif A[0] > B[0]:
            time += B[0]
            del B[0]
    if K < time:
        print(i - 1)
        exit()
    elif len(A) == 0 and len(B) == 0:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
*A,=map(int,input().split())
*B,=map(int,input().split())

AX=[0]*(N+1)
for i in range(1,N+1):AX[i]=AX[i-1]+A[i-1]

BX=[0]*(M+1)
for i in range(1,M+1):BX[i]=BX[i-1]+B[i-1]

import bisect
ni=bisect.bisect_left(AX,K)
mi=bisect.bisect_left(BX,K)
    
ans=0
for i in range(ni):
 for j in range(mi):
   if K < AX[i]+BX[j]:break
   ans=max(ans,i+j)
            
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate


def solve(string):
    n, m, k, *ab = map(int, string.split())
    a, b = [0] + ab[:n], [0] + ab[n:]
    a, b = list(accumulate(a)), list(accumulate(b))
    i, j = n, 0
    while a[i] > k:
        i -= 1
    while j <= m and a[i] + b[j] <= k:
        j += 1
    ans = i + j - 1
    for i in range(i, 0, -1):
        while j <= m and a[i] + b[j] <= k:
            j += 1
        ans = max(ans, i + j - 1)
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

x = 0
for i in range(n):
    x += a[i]
    a[i] = x

x = 0
for i in range(m):
    x += b[i]
    b[i] = x

x = 0
ans = n
at = n
j = n - 1
for i in range(n):
    x = a[i]
    if x > k:
        x = a[i-1]
        ans = i
        at = i
        j = i - 1
        break

bt = 0
y = 0
for i in range(m):
    y = b[i]
    bt += 1
    if x + y > k:
        while x + y > k and j >= 0:
            x = a[j-1]
            j -= 1
            at -= 1
    if x + y <= k:
        ans = max(ans,at+bt)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

x = 0
for i in range(n):
    x += a[i]
    a[i] = x

x = 0
for i in range(m):
    x += b[i]
    b[i] = x

x = 0
for i in range(n):
    x = a[i]
    if x > k:
        x = a[i-1]
        ans = i
        j = i-2
        break
    if i + 1 == n:
        ans = n
        j = i-1

y = 0
for i in range(m):
    y = b[i]
    if x + y > k:
        while x + y > k and j >= 0:
            x = a[j]
            j -= 1
    if x + y <= k:
        ans = max(ans,i+j+3)

print(ans)

Submission status: Wrong Answer

Solution code:

n,m,k = map(int,input().split()) 
A=list(map(int,input().split()) )
B=list(map(int,input().split()) )
bmax=0
for i in range(1,len(A)):
    A[i] += A[i-1]  
for i in range(1,len(B)):
    B[i] += B[i-1]
    if B[i]<=k:
        bmax=i

ans=0
b=bmax
for a in range(len(A)):
    if A[a]>k:
        break
    # ans = max(ans, a+1)
    
    while A[a] + B[b] >k and b>=0:
        b -= 1
        
        
    ans = max(ans, a+b+2)
        
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split()) 
A=list(map(int,input().split()) )
B=list(map(int,input().split()) )
bmax=0
for i in range(1,len(A)):
    A[i] += A[i-1]  
for i in range(1,len(B)):
    B[i] += B[i-1]
    if B[i]<=k:
        bmax=i

ans=0

for a in range(len(A)):
    if A[a]>k:
        break
    # ans = max(ans, a+1)
    b=bmax
    while A[a] + B[b] >k and b>=0:
        b -= 1
        
        
    ans = max(ans, a+b+2)
        
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split()) 
A=list(map(int,input().split()) )
B=list(map(int,input().split()) )

for i in range(1,len(A)):
    A[i] += A[i-1]  
for i in range(1,len(B)):
    B[i] += B[i-1]

ans=0

for a in range(len(A)):
    if A[a]>k:
        break
    ans = max(ans, a+1)
    for b in range(len(B)):
        if A[a] + B[b] > k:
            break
        
        ans = max(ans, a+b+2)
        
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split()) 
A=list(map(int,input().split()) )
B=list(map(int,input().split()) )

for i in range(1,len(A)):
    A[i] += A[i-1]  
for i in range(1,len(B)):
    B[i] += B[i-1]

ans=0

for a in range(len(A)):
    if A[a]>k:
        break
    
    for b in range(len(B)):
        if A[a] + B[b] > k:
            break
        
        ans = max(ans, a+b+2)
        
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M,K = map(int,input().split())
A = deque(map(int,input().split()))
B = deque(map(int,input().split()))
sm = 0
cnt = 0
while True:
    m = None
    if A:
        m = A.popleft()
    if B:
        b = B.popleft()
        if m:
            if m > b:
                A.appendleft(m)
                m = b
            else:
                B.appendleft(b)
        elif m == None:
            m = b
    if not m:
        break
    if sm + m <= K:
        sm += m
        cnt += 1
    else:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ra = [0] * (n + 1)
rb = [0] * (m + 1)
for i in range(n):
    ra[i + 1] = a[i] + ra[i]

for j in range(m):
    rb[j + 1] = b[j] + rb[j]

ans = 0
for i in range(1, n + 1):
    zan = k - ra[i]
    if zan < 0:
        break
    rbi = bisect.bisect_right(rb, zan) - 1
    if rbi < 0:
        rbi = 0
    ans = max(ans, i + rbi)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ra = [0] * (n + 1)
rb = [0] * (m + 1)
for i in range(n):
    ra[i + 1] = a[i] + ra[i]

for j in range(m):
    rb[j + 1] = b[j] + rb[j]

rai = max(bisect.bisect_right(ra, k) - 1, 0)
rbi = max(bisect.bisect_right(rb, k) - 1, 0)
if rai >= rbi:
    zan = k - ra[rai]
    rbz = bisect.bisect_right(rb, zan) - 1
    if rbz < 0:
        print(rai)
    else:
        print(rai + rbz)
else:
    zan = k - rb[rbi]
    raz = bisect.bisect_right(ra, zan) - 1
    if raz < 0:
        print(rbi)
    else:
        print(rbi + raz)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=deque(list(map(int,input().split())))
b=deque(list(map(int,input().split())))
cost=0
ans=0
while (a and cost+a[0]<=k) or (b and cost+b[0]<=k):
	if a and b:
		if a[0]<b[0]:
			cost+=a[0]
			ans+=1
			a.popleft()
		else:
			cost+=b[0]
			ans+=1
			b.popleft()
	elif a:
		cost+=a[0]
		ans+=1
		a.popleft()
	else:
		cost+=b[0]
		ans+=1
		b.popleft()
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [0] + (list(accumulate(map(int, input().split()))))
b = [0] + (list(accumulate(map(int, input().split()))))
ans = [0]

for i in range(n + 1):
    c = bisect_right(b, k - a[i]) - 1
    if c != 1:
        ans.append(c + i)

print(max(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m, k = map(int, input().split())
a = deque(list(map(int, input().split())))
b = deque(list(map(int, input().split())))
s = 0
ans = 0

a.append(10 ** 9 + 1)
b.append(10 ** 9 + 1)

while len(a) > 1 or len(b) > 1:
    if a[0] > b[0]:
        if s + b[0] <= k:
            s += b.popleft()
        else:
            break
    elif a[0] < b[0]:
        if s + a[0] <= k:
            s += a.popleft()
        else:
            break
    elif a[0] == b[0]:
        if a[1] >= b[1]:
            if s + b[0] <= k:
                s += b.popleft()
            else:
                break
        else:
            if s + a[0] <= k:
                s += a.popleft()
            else:
                break
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

consume = 0
ans = 0

while consume <= K:
    a = 1000000000000
    b = 1000000000000
    if A:
        a = A.popleft()
    if B:
        b = B.popleft()
    if a == 1000000000000 and b == 1000000000000:
        ans += 1
        break

    if a <= b:
        B.appendleft(b)
        consume += a
        ans += 1
    else:
        A.appendleft(a)
        consume += b
        ans += 1

print(ans - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

consume = 0
ans = 0

if sum(A) + sum(B) <= K:
    print(N + M)
    exit()

while consume <= K:
    a = 10000000000
    b = 10000000000
    if A:
        a = A.popleft()
    if B:
        b = B.popleft()
    if not A and not B:
        break

    if a <= b:
        B.appendleft(b)
        consume += a
        ans += 1
    else:
        A.appendleft(a)
        consume += b
        ans += 1

print(ans - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

consume = 0
ans = 0

if sum(A) + sum(B) <= K:
    print(N + M)
    exit()

while consume < K:
    a = 10000000000
    b = 10000000000
    if A:
        a = A.popleft()
    if B:
        b = B.popleft()
    if not A and not B:
        break

    if a <= b:
        B.appendleft(b)
        consume += a
        ans += 1
    else:
        A.appendleft(a)
        consume += b
        ans += 1

print(ans - 1)

Submission status: Wrong Answer

Solution code:
n,m,k = list(map(int,input().split(" ")))
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
cnt = 0
ans = 0
j = m
t = sum(b)

for i in range(n+1):
    while j>0:
        if t>k:
            j -= 1
            t -= b[j]
            continue
        ans = max(ans,i+j)
        break
    if i==n:break
    t += a[i]

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = list(map(int,input().split(" ")))
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
cnt = 0
ans = 0
tmp = m

for i in range(n+1):
    for j in range(tmp,0,-1):
        if sum(b[0:j]) <= k-sum(a[0:i]):
            cnt = i+j
            tmp = j
            if ans < cnt:
                ans = cnt
            break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M,K=map(int,input().split())
Alist = deque(map(int,input().split()))
Blist = deque(map(int,input().split()))


time=0
num=0
while time<=K and num<N+M:
    if len(Alist)==0:
        Atime=float('inf')
    else:
        Atime=Alist[0]
    if len(Blist)==0:
        Btime=float('inf')
    else:
        Btime=Blist[0]
    if Atime<Btime:
        time+=Alist.popleft()
        if time<=K:
            num+=1
    else:
        time+=Blist.popleft()
        if time<=K:
            num+=1
print(num)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])
    
ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j]+a[i]> K:
        j -= 1
    ans = max(ans, i + j)
print(ans)
print(a)
print(b)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
Time = 0
l = len(A+B)
for i in range(l):
    if len(A) == 0:
        Time += B[0]
        if Time > K:
            break
        ans += 1
        B.pop(0)
        continue
    if len(B) == 0:
        Time += A[0]
        if Time > K:
            break
        ans += 1
        A.pop(0)
        continue
    Time += min(A[0],B[0])
    if Time > K:
        break
    ans += 1
    min(A,B).pop(0)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
Time = 0
l = len(A+B)
for i in range(l):
    if len(A) == 0:
        Time += B[0]
        if Time > K:
            break
        ans += 1
        B.pop(0)
    elif len(B) == 0:
        Time += A[0]
        if Time > K:
            break
        ans += 1
        A.pop(0)
    elif A[0] > B[0]:
        Time += B[0]
        if Time > K:
            break
        ans += 1
        B.pop(0)
    elif B[0] > A[0]:
        Time += A[0]
        if Time > K:
            break
        ans += 1
        A.pop(0)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b2 = []
t = k
start = 0
while True:
  if start == len(b):
    break
  else:
    if t - b[start] < 0:
      break
    else:  
      t -= b[start]
      b2.append(b[start])
      start += 1
num = start
lst = [start]
M = start

for i in range(len(a)):
  if a[i] > t and b2 == []:
    print(M)
    sys.exit()
  while a[i] > t and b2 != []:
    b_ = b2.pop()
    t = t+b_
    num -= 1
    
  if a[i] <= t:
    t -= a[i]
    num += 1
  
 
  if lst[-1] < num:
    M = num
  lst.append(num)    
    
  
  
  
  
print(M)

Submission status: Wrong Answer

Solution code:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b2 = []
t = k
start = 0
while True:
  if start == len(b):
    break
  else:
    if t - b[start] < 0:
      break
    else:  
      t -= b[start]
      b2.append(b[start])
      start += 1
num = start
lst = [0]
M = 0

for i in range(len(a)):
  if a[i] > t and b2 == []:
    print(M)
    sys.exit()
  while a[i] > t and b2 != []:
    b_ = b2.pop()
    t = t+b_
    num -= 1
    
  if a[i] <= t:
    t -= a[i]
    num += 1
  
 
  if lst[-1] < num:
    M = num
  lst.append(num)    
    
  
  
  
  
print(M)

Submission status: Wrong Answer

Solution code:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

s = 0
num = 0
while True:
  if s > k:
    print(num-1)    
    sys.exit()
    
  elif a ==[] and b == []:
    print(num)
    sys.exit()
  elif a != [] and b != []:
    if a > b:
      d = b.pop(0)
    else:
      d = a.pop(0)
    num += 1  
    s += d
    
  elif a == [] and b != []:
    d = b.pop(0)
    num += 1  
    s += d
   
  else:
    d = a.pop(0)
    num += 1  
    s += d

Submission status: Wrong Answer

Solution code:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.reverse()
b.reverse()

s = 0
num = 0
while True:
  if s > k:
    print(num-1)    
    sys.exit()
    
  elif a ==[] and b == []:
    print(num)
    sys.exit()
  elif a != [] and b != []:
    if a[-1] > b[-1]:
      d = b.pop()
    else:
      d = a.pop()
    num += 1  
    s += d
    
  elif a == [] and b != []:
    d = b.pop()
    num += 1  
    s += d
   
  else:
    d = a.pop()
    num += 1  
    s += d

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = list(map(int, input().split()))
book_a = list(map(int, input().split()))
book_b = list(map(int, input().split()))

a_time_sum = [0]
b_time_sum = []
for i in range(n):
    if i == 0:
        a_time_sum.append(book_a[0])
    else:
        a_time_sum.append(a_time_sum[i-1]+book_a[i])

for i in range(m):
    if i == 0:
        b_time_sum.append(book_b[0])
    else:
        b_time_sum.append(b_time_sum[i-1]+book_b[i])

ans = 0

# Aから何冊か読む
for i in range(n+1):
    read = i
    a_time = a_time_sum[i]
    time_remain = k - a_time
    if time_remain < 0:
        break
    # このときBから何冊よめる？
    b_read = bisect.bisect_right(b_time_sum, time_remain)
    read += b_read
    ans = max(ans, read)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = list(map(int, input().split()))
book_a = list(map(int, input().split()))
book_b = list(map(int, input().split()))

a_time_sum = []
b_time_sum = []
for i in range(n):
    if i == 0:
        a_time_sum.append(book_a[0])
    else:
        a_time_sum.append(a_time_sum[i-1]+book_a[i])

for i in range(m):
    if i == 0:
        b_time_sum.append(book_b[0])
    else:
        b_time_sum.append(b_time_sum[i-1]+book_b[i])

ans = 0

# Aから何冊か読む
for i in range(n):
    read = i+1
    a_time = a_time_sum[i]
    time_remain = k - a_time
    if time_remain < 0:
        ans = max(ans, read)
        break
    # このときBから何冊よめる？
    b_read = bisect.bisect_right(b_time_sum, time_remain)
    read += b_read
    ans = max(ans, read)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = list(map(int, input().split()))
book_a = list(map(int, input().split()))
book_b = list(map(int, input().split()))

a_time_sum = []
b_time_sum = []
for i in range(n):
    if i == 0:
        a_time_sum.append(book_a[0])
    else:
        a_time_sum.append(a_time_sum[i-1]+book_a[i])

for i in range(m):
    if i == 0:
        b_time_sum.append(book_b[0])
    else:
        b_time_sum.append(b_time_sum[i-1]+book_b[i])

ans = 0

# Aから何冊か読む
for i in range(n):
    read = i+1
    a_time = a_time_sum[i]
    time_remain = k - a_time
    if time_remain <= 0:
        break
    # このときBから何冊よめる？
    b_read = bisect.bisect_right(b_time_sum, time_remain)
    read += b_read
    ans = max(ans, read)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n, m, k = list(map(int, input().split()))
book_a = list(map(int, input().split()))
book_b = list(map(int, input().split()))

a_time_sum = []
b_time_sum = []
for i in range(n):
    if i == 0:
        a_time_sum.append(book_a[0])
    else:
        a_time_sum.append(a_time_sum[i-1]+book_a[i])

for i in range(m):
    if i == 0:
        b_time_sum.append(book_b[0])
    else:
        b_time_sum.append(b_time_sum[i-1]+book_b[i])

ans = 0

# Aから何冊か読む
for i in range(n):
    read = i+1
    a_time = a_time_sum[i]
    time_remain = k - a_time
    if time_remain < 0:
        break
    # このときBから何冊よめる？
    b_read = bisect.bisect_right(b_time_sum, time_remain)
    read += b_read
    ans = max(ans, read)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

for i in range(1, n):
    a[i] += a[i-1]
for i in range(1, m):
    b[i] += b[i-1]

I = bisect.bisect_right(a, k)
ans = bisect.bisect_right(b, k)
li = list(range(I))
li = li[::-1]
for i in li:
    tmp = bisect.bisect_right(b, k-a[i])
    if a[i] + b[tmp-1] <= k and tmp != 0:
        ans = max(ans, i+tmp+1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

for i in range(1, n):
    a[i] += a[i-1]
for i in range(1, m):
    b[i] += b[i-1]

I = bisect.bisect_right(a, k)
ans = bisect.bisect_right(b, k)
li = list(range(I))
li = li[::-1]
for i in li:
    tmp = bisect.bisect_right(b, k-a[i])
    if a[i] + b[tmp-1] <= k:
        ans = max(ans, i+tmp+1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

for i in range(1, n):
    a[i] += a[i-1]
for i in range(1, m):
    b[i] += b[i-1]

I = bisect.bisect_right(a, k)
ans = 0
li = list(range(I))
li = li[::-1]
for i in li:
    tmp = bisect.bisect_right(b, k-a[i])
    if a[i] + b[tmp-1] <= k:
        ans = max(ans, i+tmp+1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

for i in range(1, n):
    a[i] += a[i-1]
for i in range(1, m):
    b[i] += b[i-1]

I = bisect.bisect_right(a, k)
ans = 0
li = list(range(I))
li = li[::-1]
for i in li:
    tmp = bisect.bisect_right(b, k-a[i])
    ans = max(ans, i+tmp+1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

if __name__ == '__main__':
    n,m,k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    
    #Aの累積和
    A2 = [0]
    for i in range(n):
        A2.append(A2[i] + A[i])
    #Bの累積和
    B2 = [0]
    for j in range(m):
        B2.append(B2[j] + B[j])

    #Aから計算
    tmpA = bisect.bisect(A2,k) - 1
    #残りはBからどれだけ読めるか
    nokori_k = k - A2[tmpA]
    tmpB = bisect.bisect(B2,nokori_k) - 1
    ans = tmpA + tmpB

    #Bから計算
    tmpB = bisect.bisect(B2,k) - 1
    #残りはAからどれだけ読めるか
    nokori_k = k - B2[tmpB]
    tmpA = bisect.bisect(A2,nokori_k) - 1
    ans = max(ans,tmpA + tmpB)
    print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = list(map(int,input().split(" ")))
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
sum = 0
s = [0]
ans = 0

for i in range(n):
    sum += a[i]
    if sum > k:
        break
c = a[0:i+1]
c.reverse()
sum = 0
for i in range(m):
    sum += b[i]
    if sum > k:
        break

c.extend(b[0:i+1])


for i in range(len(c)):
    s.append(s[i]+c[i])

for i in range(len(s)-1):
    for j in range(len(s)):
        if s[j]-s[i] > k and s[j-1]-s[i] < k:
            if ans < j-i-1:
                ans = j-i-1
            break
if ans == 0 and min(s[1:len(s)]) < k :
    ans = len(s)-1

print(ans)