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, v = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
a1 = [0] * (n + 1)
b1 = [0] * (m + 1)
n1 = n + 1
for i in range(1, n + 1):
  a1[i] = a1[i - 1] + a[i - 1]
  if a1[i] > v:
    n1 = i
    break
m1 = m + 1
for i in range(1, m + 1):
  b1[i] = b1[i - 1] + b[i - 1]
  if b1[i] > v:
    m1 = i
    break
for i in range(n1):
  c = 0
  t = a1[i]
  for j in range(1,m1):
    if t + b1[j] <= v:
      c = i + j
    else:
      break
  if x < c:
    x = c
print (x)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

a_accum = [0] + list(accumulate(a))
b_accum = [0] + list(accumulate(b))

for j in range(len(a_accum)):
  for i in range(len(b_accum)):
    t = a_accum[-(j+1)] + b_accum[-(i+1)]
    candi = (n-j) + (m-i)
    print(n-j, m-i, candi)
    if ans >= candi:
      break
    if t <= k:
      ans = candi
      break

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()))

A = [0]
B = [0]

for i in range(n):
  A.append(A[i]+a[i])
  
for i in range(m):
  B.append(B[i]+b[i])
  
ans = 0
i = 1
j = 1
while True:
  #print(i,j,A[i] + B[j])
  if A[i] + B[j] >= k or (n == (i-1) and m == (j-1)):
    break
  if A[i] > B[j] and m > j and A[i] + B[j+1] <= k:
    j += 1
    ans += 1
  elif B[j] > A[i] and n > i and A[i+1] + B[j] <= k:
    i += 1
    ans += 1
  else:
    if m > j:
    	j += 1
    if n > i:
        i += 1
    ans += 1
    
if A[i] + B[j] > k:
	print(i+j-2)
else:
    print(i+j)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

a_accum = list(accumulate(a))
b_accum = list(accumulate(b))

for j in range(len(a_accum)):
  for i in range(len(b_accum)):
    t = a_accum[-(j+1)] + b_accum[-(i+1)]
    candi = (n-j) + (m-i)
    if ans >= candi:
      break
    if t <= k:
      ans = candi
      break

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

a_accum = list(accumulate(a))
b_accum = list(accumulate(b))

n_a = n
for j in range(len(a_accum)):
  for i in range(len(b_accum)):
    t = a_accum[n_a-1]
    t += b_accum[-(i+1)]
    candi = n_a + (m-i)
    if ans >= candi:
      break
    if t <= k:
      ans = candi
      break
  n_a -= 1

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,N):
  A[i]+=A[i-1]
for i in range(1,M):
  B[i]+=B[i-1]

j,best=0,0
for j in range(M):
  if B[j] > K: break
  else:best=j+1
j=0
for i in range(N):
  if A[i] > K: break
  while j < M:
    if A[i]+B[j] > K: break
    else: j+=1
  best=max(best,i+1+j)
print(best)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
As= list(map(int,input().split()))
Bs= list(map(int,input().split()))
#----------
def ShouldReadAs(As,Bs):
    if As==Bs:
        return True
    
    AsFirst= As+Bs
    BsFirst= Bs+As
 
    for i in range(1,len(AsFirst)+1):
        if As[:i]==Bs[:i]:
            continue
        else:
            if AsFirst[i-1]<BsFirst[i-1]:
                return True
            elif AsFirst[i-1]>BsFirst[i-1]:
                return False
#----------
count=0
 
while (K>0):
    if ShouldReadAs(As,Bs):
        K-=As.pop(0)
    else:
        K-=Bs.pop(0)
    count+=1

print(count-1)

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
j = 0
t = 0
c = 0
while i < n or j < m and t < k:
  if i >= n:
    t += b[j]
    j += 1
  elif j >= m:
    t += a[i]
    i += 1
  elif a[i] < b[j]:
    t += a[i]
    i += 1
  else:
    t += b[j]
    j += 1
  if t <= k:
    c += 1
print (c)

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()))

A_sum = A.copy()
B_sum = B.copy()

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

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


max_= 0
for i, n in enumerate(A_sum, start=1):
    if n > K:
        continue

    target = K - n
    v = bisect.bisect_left(B_sum, target)

    if v == M:
        v = M
    elif B_sum[v] == target:
        v += 1

    max_ = max(max_, i + v)


print(max_)

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):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans,i+j)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import collections
import bisect
from difflib import SequenceMatcher


def main():
    N, M, K = map(int, input().split())

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

    cumsumA = np.cumsum(A)
    p = bisect.bisect_left(cumsumA, K + 0.1)
    time = cumsumA[p - 1]
    tmp = p
    ans = tmp

    cnt = 0
    while(cnt < len(B)):
        if time + B[cnt] <= K:
            time += B[cnt]
            tmp += 1
            cnt += 1
            if tmp > ans:
                ans = tmp
        else:
            p += -1
            if p < 0:
                break
            time = cumsumA[p]
            tmp += -1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A,B = deque(A),deque(B)
count = 0
a = A.popleft()
b = B.popleft()
while k>=a or k>=b:
    if a<b:
        k -= a
        count+=1
        if A: a = A.popleft()
        else: a = 10**10
    else:
        k -= b
        count+=1
        if B: b = B.popleft()
        else: b = 10**10
print(count)

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,N):
  A[i]+=A[i-1]
for i in range(1,M):
  B[i]+=B[i-1]

j,best=0,0
for j in range(M):
  if B[j] > K: break
  else:best=j+1
for i in range(N):
  if A[i]>K:break
  c = K-A[i]
  while j < M:
    if B[j]> c:break
    else: j+=1
  
  best=max(best,i+1+j)
print(best)

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,N):
  A[i]+=A[i-1]
for i in range(1,M):
  B[i]+=B[i-1]

j,best=0,0
for j in range(M):
  if B[j] > K: break
  else:best=j+1
for i in range(N):
  if A[i]>K:break
  c = K-A[i]
  while j < M:
    if B[j]> c:break
    else: j+=1
  if j==0:break
  else:
    best=max(best,i+1+j)
print(best)

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()))
AS=[0]
BS=[0]
r=0
for i in A:
  AS.append(i+AS[-1])
  if AS[-1]>k:
    break
for i in B:
  BS.append(i+BS[-1])
  if BS[-1]>k:
    break
f=1
for i in range(1,len(AS)):
  for j in range(f,len(BS)):
    if AS[-i]+BS[-j]<=k:
      if len(AS)+len(BS)-i-j>r:
        r=len(AS)+len(BS)-i-j
      f=j
print(r)

Submission status: Wrong Answer

Solution code:
import math
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.extend([math.inf]*m)
B.extend([math.inf]*n)
d = [0]*(n+m+1)
cnt = 0
a, b = 0, 0
for i in range(n+m):
    select = min(A[a], B[b])
    d[i+1] = d[i] + select
    if select == A[a]:
        a += 1
    else:
        b += 1
    cnt += 1
    if d[i+1] > k:
        cnt -= 1
        break
print(cnt)

Submission status: Wrong Answer

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

t = sum(B)

# しゃくとり法
res = 0
j = m
for i in range(n + 1):
    while j > 0 and t > k:
        j -= 1
        t -= B[j]
    if t > k:
        break
    res = max(res, i + j)
    if i == n: break
    t += A[i]

print(res)

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()))
time = 0
cnt = 0
cnt_2 = 0
while time < k:
    if len(a) == 0 and len(b) == 0:
        break
    if len(a) != 0:
        A = a[0]
    else:
        A = 9999999999999
    if len(b) != 0:
        B = b[0]
    else:
        B = 99999999999999
    if A >= B:
        p = b.popleft()
    else:
        p = a.popleft()
    time += p
    if time > k:
        break
    else:
        cnt += 1


print(cnt)

Submission status: Wrong Answer

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

n = 0
m = 0
count = 0

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

A.append(10 ** 9 + 1)
B.append(10 ** 9 + 1)

while K >= 0:
    if A[n] < B[m]:
        K = K - A[n]
        n = n + 1
        count = count + 1
    else:
        K = K - B[m]
        m = m + 1
        count = count + 1


print(count - 1)

Submission status: Wrong Answer

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

n = 0
m = 0
count = 0

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

A.append(10 ** 9)
B.append(10 ** 9)

while K >= 0:
    if A[n] < B[m]:
        K = K - A[n]
        n = n + 1
        count = count + 1
    else:
        K = K - B[m]
        m = m + 1
        count = count + 1


print(count - 1)

Submission status: Wrong Answer

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

n = 0
m = 0
count = 0

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

A.append(200000)
B.append(200000)

while K >= 0:
    if A[n] < B[m]:
        K = K - A[n]
        n = n + 1
        count = count + 1
    else:
        K = K - B[m]
        m = m + 1
        count = count + 1


print(count - 1)

Submission status: Wrong Answer

Solution code:
n, m, k = input().split(" ")

a = input().split(" ")

b = input().split(" ")

a = [ int(i) for i in a]
b = [ int(i) for i in b]

remain = int(k)
r = 0
while remain > 0:
    if(len(a) == 0 and len(b) > 0):
        if(remain < b[0]):
            break
        remain = remain - b[0]
        b.pop(0)
        r = r + 1
        continue
    elif(len(b) == 0 and len(a) > 0):
        if(remain < a[0]):
            break
        remain = remain - a[0]       
        a.pop(0)
        r = r + 1
        continue
    elif(len(b) == 0 and len(a) == 0):
        break

    if(a[0] <= b[0]):
        if(remain < a[0]):
            break
        remain = remain - a[0]       
        a.pop(0)
        r = r + 1
    elif(a[0] > b[0]):
        if(remain < b[0]):
            break
        remain = remain - b[0]
        b.pop(0)
        r = r + 1

print(r)

Submission status: Wrong Answer

Solution code:
from collections import deque
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]

j,best=0,0
for i in range(N):
  if A[i]>K:break
  c = K-A[i]
  while j < M:
    if B[j]> c:break
    else: j+=1
  if j==0:break
  else:
    best=max(best,i+1+j)
print(best)

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()))
count = 0
bi = 0
for j in range(M):
    if count + B[j] > K:
        break
    count += B[j]
    bi += 1
a=0
ai = 0
p = bi
for i in range(N):
    #ep('s1:',count,ai,bi)
    a += A[i]
    if a > K:
        break;
    count += A[i]
    ai+=1
    #ep('s2:',count,ai,M)
    
    while count>K:
        count-=B[M-1]
        bi-=1
    #ep('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+bi)
    #ep('s4',count,p,ai,M)
print(p)

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]

index = bisect.bisect_left(A,K)
for i in range(M):
    K-=B[i]
    if K>=0:
        a=bisect.bisect(A,K)
       
        if a+i+1>=index:
            index=a+i+1
print(index)

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
j=0
t=0
A.append(1000000001)
B.append(1000000001)
while True:
    if A[i]>=B[j] and t+B[j]<=K:
        t+=B[j]
        j+=1
    elif A[i]<B[j] and t+A[i]<=K:
        t+=A[i]
        i+=1
    else:
        break
print(i+j)

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()))
count=sum(B)
a=0
ai = 0
p=0
for i in range(N):
    #ep('s1:',count,ai,M)
    while count>=K:
        count-=B[M-1]
        M-=1
    #ep('s2:',count,ai,M)
    
    if A[i] > K:
        p=max(p,ai+M)
        break;
    
    count+=A[i]
    a+=A[i]
    ai+=1
    
    #ep('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+M)
    #ep('s4',count,p,ai,M)
    if a>=K:
        break
print(p)

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()))
count=sum(B)
a=0
ai = 0
p=0
for i in range(N):
    #print('s1:',count,ai,M)
    while count>=K:
        count-=B[M-1]
        M-=1
    #print('s2:',count,ai,M)
    count+=A[i]
    a+=A[i]
    ai+=1
    #print('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+M)
    #print('s4',count,p,ai,M)
    if a>=K:
        break
print(p)

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()))
count=sum(B)
a=0
ai = 0
p=0
for i in range(N):
    #print('s1:',count,ai,M)
    while count>K:
        count-=B[M-1]
        M-=1
    #print('s2:',count,ai,M)
    count+=A[i]
    a+=A[i]
    ai+=1
    #print('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+M)
    #print('s4',count,p,ai,M)
    if a>=K:
        break
print(p)

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()))
count=sum(B)
a=0
ai = 0
p=0
for i in range(N):
    #print('s1:',count,ai,M)
    while count>K:
        count-=B[M-1]
        M-=1
    #print('s2:',count,ai,M)
    count+=A[i]
    a+=A[i]
    ai+=1
    #print('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+M)
    #print('s4',count,p,ai,M)
    if a>K:
        break
print(p)

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()))
count=sum(B)
a=0
ai = 0
p=0
for i in range(N):
    print('s1:',count,ai,M)
    while count>K:
        count-=B[M-1]
        M-=1
    print('s2:',count,ai,M)
    count+=A[i]
    a+=A[i]
    ai+=1
    print('s3:',count,ai,M)
    if count <= K:
        p=max(p,ai+M)
    print('s4',count,p,ai,M)
    if a>K:
        break
print(p)

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()))
count=sum(B)
a=0
p=0
for i in range(N):
    while count>K:
        count-=B[M-1]
        M-=1
    if i!=N-1:
        count+=A[i]
        a+=A[i]
    p=max(p,i+M+1)
    if a>K:
        break
print(p)

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()))
count=sum(B)
a=A[0]
p=0
for i in range(N):
    while count>K:
        count-=B[M-1]
        M-=1
    if i!=N-1:
        count+=A[i+1]
        a+=A[i+1]
    p=max(p,i+M+1)
    if a>K:
        break
print(p)

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 = [0]*(N+1)
Bsum = [0]*(M+1)
Asum[0] = 0
Bsum[0] = 0
for i in range(N):
    Asum[i+1] = Asum[i]+A[i]
for i in range(M):
    Bsum[i+1] = Bsum[i]+B[i]
    
bestbooks = 0
upup = M+1
for i in range(N+1):
    lasttime = K - Asum[i]
    if lasttime <= 0:
        break
    for j in reversed(range(0,upup)):
        #print(i,j,upup,lasttime,bestbooks)
        if Bsum[j] <= lasttime and i+j>bestbooks:
            bestbooks = i+j
           
        if Bsum[j] <= lasttime:
            upup = j+1
            break
            
print(bestbooks)

Submission status: Wrong Answer

Solution code:
import itertools
import bisect
N, M ,K = map(int,input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

accum_al = list(itertools.accumulate(al))
accum_bl = list(itertools.accumulate(bl))
accum_al.insert(0,0)
accum_bl.insert(0,0)
# print(accum_al)
# print(accum_bl)

ans = 0
index = 0
for i in range(N+1):
    nokori = K - accum_al[i]
    if nokori < 0:
        continue
    else:
        index = bisect.bisect_right(accum_bl,nokori)-1
    ans = i + index
    #print(i,index,ans)
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()))
count=A[0]+sum(B)
a=A[0]
p=0
for i in range(N):
    while count>K:
        count-=B[M-1]
        M-=1
    if i!=N-1:
        count+=A[i+1]
        a+=A[i+1]
    p=max(p,i+M+1)
    if a>K:
        break
print(p)

Submission status: Wrong Answer

Solution code:
import itertools
import bisect
N, M ,K = map(int,input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

accum_al = list(itertools.accumulate(al))
accum_bl = list(itertools.accumulate(bl))
accum_al.insert(0,0)
accum_bl.insert(0,0)
# print(accum_al)
# print(accum_bl)

ans = 0
index = 0
for i in range(N+1):
    nokori = K - accum_al[i]
    if nokori < 0:
        continue
    else:
        index = bisect.bisect_right(accum_bl,nokori)
    ans = i + index-1
    #print(i,index,ans)
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()))

if k < a[0] and k < b[0]:
    print(0)
    sys.exit()

t = 0
ai = 0
bi = 0
count = 0
while k > t:
    if ai == n and bi == m:
        break
    elif ai == n:
        t += b[bi]
        bi += 1
        count += 1
    elif bi == m:
        t += a[ai]
        ai += 1
        count += 1
    else:
        if a[ai] < b[bi]:
            t += a[ai]
            ai += 1
            count += 1
        elif a[ai] > b[bi]:
            t += b[bi]
            bi += 1
            count += 1
        else:
            if ai + 1 == n and bi + 1 == n:
                t += b[bi]
                bi += 1
                count += 1
            elif ai + 1 == n:
                if a[ai] > b[bi+1]:
                    t += b[bi]
                    bi += 1
                    count += 1
                else:
                    t += a[ai]
                    ai += 1
                    count += 1
            elif  bi + 1 == n:
                if a[ai+1] < b[bi]:
                    t += a[ai]
                    ai += 1
                    count += 1
                else:
                    t += b[bi]
                    bi += 1
                    count += 1
            else:
                if a[ai] > b[bi+1]:
                    t += b[bi]
                    ai += 1
                    count += 1
                elif a[ai+1] < b[bi]:
                    t += a[ai]
                    bi += 1
                    count += 1
if t > k:
    count -= 1
    print(count)
else:
    print(count)

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 = [0]*N
Bsum = [0]*M
Asum[0] = A[0]
Bsum[0] = B[0]
for i in range(N-1):
    Asum[i+1] = Asum[i]+A[i+1]
for i in range(M-1):
    Bsum[i+1] = Bsum[i]+B[i+1]
    
bestbooks = 0    
upup = M
for i in range(N):
    lasttime = K - Asum[i]
    for j in reversed(range(0,upup)):
        if Bsum[j] <= lasttime and i+j+2>bestbooks:
            bestbooks = i+j+2
           
        if Bsum[j] <= lasttime:
            upup = j+1
            continue
            
print(bestbooks)

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()))

if k < a[0] and k < b[0]:
    print(0)
    sys.exit()

t = 0
ai = 0
bi = 0
count = 0
while k > t:
    if ai == n:
        t += b[bi]
        bi += 1
        count += 1
    elif bi == m:
        t += a[ai]
        ai += 1
        count += 1
    elif ai == n and bi == m:
        break
    else:
        if a[ai] <  b[bi]:
            t += a[ai]
            ai += 1
            count += 1
        elif a[ai] >  b[bi]:
            t += b[bi]
            bi += 1
            count += 1
        else:
            if ai + 1 == n:
                t += a[ai]
                ai += 1
                count += 1
            elif bi + 1 == m:
                t += b[bi]
                bi += 1
                count += 1
            elif a[ai] > b[bi+1]:
                t += b[bi]
                ai += 1
                count += 1
            elif a[ai+1] < b[bi]:
                t += a[ai]
                bi += 1
                count += 1
if t > k:
    count -= 1
    print(count)
else:
    print(count)

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()))
#print(a)
asum=[]
bsum=[]
cnt=0
for i in range(n):
    cnt+=a[i]
    asum.append(cnt)
#    if cnt>k:
#        amx=i
#        break
#    amx=i
#print("amx=",amx)

cnt=0
for i in range(m):
    cnt+=b[i]
    bsum.append(cnt)
#    if cnt>k:
#        bmx=i
#        break
#    bmx=i
#print("bmx=",bmx)

ans=0
j=m-1
for i in range(n):
    c=k-asum[i]
#    print("i=",i)
#    print("c=",c)
    if c<0:
        continue
    while c<bsum[j] and j>0:
        j-=1
    ans=max(ans,i+j+2)
#    print("ans=",ans)
print(ans)

Submission status: Wrong Answer

Solution code:
m, n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
new = a + b
new_one = sorted(new)
now = 0
count = 0

if new_one[0] > k:
  print(0)
else:
  for i in new_one:
    if k >= now + i:
      count += 1
      now += i
    else:
      now += i
      print(count)
      break
  
if now != 0 and k >= now:
  print(n + m)

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
count=0
if sum(A)+sum(B)<=K:
    print(len(A)+len(B))
else:
    while ans<K and (len(A)>0 or len(B)>0):
        if len(B)==0:
            ans+=A[0]
            A.pop(0)
        elif len(A)==0:
            ans+=B[0]
            B.pop(0)
        else:
            if A[0]>=B[0]:
                ans+=B[0]
                B.pop(0)
                count+=1
            else:
                ans+=A[0]
                A.pop(0)
                count+=1
    print(count-1)

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()))
def HowManyBooks(A, B):
  A_sum, B_sum = [0], [0]
  for i in range(N):
    A_sum.append(A_sum[i] + A[i])
  for i in range(M):
    B_sum.append(B_sum[i] + B[i])
    books = 0
  j = 0
  for i in range(N+1):
    if A_sum[i] > K:
      break
    while B_sum[j] > K - A_sum[i]:
      j -= 1
    books = max(books, i+j)
  return books
print(HowManyBooks(A, B))

Submission status: Wrong Answer

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

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

a_i = 0
b_i = 0
ab_k = 0
c = 0
while(1):
    if a_i > len(a_list)-1 and b_i > len(b_list)-1:
        break
    elif a_i <= len(a_list)-1 and b_i > len(b_list)-1:
        if a_list[a_i]+ab_k <= k:
            ab_k += a_list[a_i]
            a_i += 1
        else:
            break
    elif a_i > len(a_list)-1 and b_i <= len(b_list)-1:
        if b_list[b_i]+ab_k <= k:
            ab_k += b_list[b_i]
            b_i += 1
        else:
            break
    else:
        if a_list[a_i] <= b_list[b_i]:
            if a_list[a_i]+ab_k <= k:
                ab_k+=a_list[a_i]
                a_i+=1
            else:
                break
        else:
            if b_list[b_i]+ab_k <= k:
                ab_k += b_list[b_i]
                b_i += 1
            else:
                break
    c += 1
print(c)

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()))
def HowManyBooks(A, B):
  AB = A + B
  AB.sort()
  time = 0
  for i in range(len(AB)):
    time += AB[i]
    if time == K:
      return i+1
    elif time > K:
      return i
print(HowManyBooks(A, B))

Submission status: Wrong Answer

Solution code:
import itertools
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
i = 0
while i <= N + M:
  ab = []
  flag = False
  for j in range(i+1):
    a, b = j, i-j+1
    if a <= N and b <= M and sum(A[:a]) + sum(B[:b]) <= K:
      i += 1
      flag = True
      break
  if not flag:
    print(i)
    break
if flag:
  print(N+M)

Submission status: Wrong Answer

Solution code:

n,m,k=map(int,input().split(" "))
a=[-9]
b=list(a)
a.extend(map(int,input().split(" ")))
b.extend(map(int,input().split(" ")))

t=0
i=1
while i<=n :
    # print("a[{}]={}".format(i,a[i]))
    if t+a[i]>k :
        break
    t+=a[i]
    # print("t=",t)
    i+=1
i-=1
# print("i=",i)
j=1
while j<=m :
    if t+b[j]>k :
        break
    t+=b[j]
    j+=1
j-=1
# print("j=",j)
ans=i+j
for i in range(i,1,-1):
    t-=a[i]
    # print("i-1=",i-1,end='  ')
    while j+1<=m and t+b[j+1]<=k :
        j+=1
        t+=b[j]
    # print("j=",j)
    ans=max(ans,i-1+j)

print(ans)

Submission status: Wrong Answer

Solution code:
n_book_a, n_book_b, limit_time = map(int, input().split(" "))
read_time_a = [int(i) for i in input().split(" ")]
read_time_b = [int(i) for i in input().split(" ")]

read_sum_time_a = [0]  # 1冊も読まないパターン
for i in range(n_book_a):
    read_sum_time_a.append(read_sum_time_a[i] + read_time_a[i])
    
read_sum_time_b = [0]  # 1冊も読まないパターン
for i in range(n_book_b):
    read_sum_time_b.append(read_sum_time_b[i] + read_time_b[i])



ans = 0
i_b = n_book_b  # 数が大きい方から一通り確認していく
for i_a, time_a in enumerate(read_sum_time_a):
    # read_sum_time_aがlimit_time以下の場合についてのみ調べる
    if time_a > limit_time:
        break
    
    # main
    while read_sum_time_b[i_b] + time_a > limit_time:
        i_b += -1
        # i_a + i_bがansより大きいと気に更新をサラッと書くとこうなる(i_a, i_bはA, Bの冊数)
        ans = max(ans, i_a + i_b)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n_book_a, n_book_b, limit_time = map(int, input().split(" "))
read_time_a = [int(i) for i in input().split(" ")]
read_time_b = [int(i) for i in input().split(" ")]

read_time_a.append(10 ** 9 + 1)
read_time_b.append(10 ** 9 + 1)

use_time = 0
counter = 0
while (use_time + read_time_a[0] <= limit_time) or (use_time + read_time_b[0] <= limit_time):
    if read_time_a[0] <= read_time_b[0]:
        use_time += read_time_a[0]
        read_time_a.pop(0)
        counter += 1
    else:
        use_time += read_time_b[0]
        read_time_b.pop(0)
        counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
import itertools
n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))

sa = list(itertools.accumulate(a))
sb = list(itertools.accumulate(b))

max = 0

for i in range(1, n + 1):
    for j in range(1, m + 1):
        if (sa[i] + sb[j] <= k):
            if i + j > max:
                max = i + j
        else:
            break
print(max)

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
for i in range(len(a)):
    tmp = i+1
    time += a[i]
    tmp_time = time
    if tmp_time > k:
        break
    for j in range(len(b)):
        if tmp_time + b[j] <= k:
            tmp_time += b[j]
            tmp += 1
        else:
            break
    ans = max(ans,tmp)
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()))
Asum = [0]*N
Bsum = [0]*M
Asum[0] = A[0]
Bsum[0] = B[0]
for i in range(N-1):
    Asum[i+1] = Asum[i]+A[i+1]
for i in range(M-1):
    Bsum[i+1] = Bsum[i]+B[i+1]
    
bestbooks = 0    
upup = M
for i in range(N):
    lasttime = K - Asum[i]
    for j in reversed(range(0,upup)):
        if Bsum[j] <= lasttime and i+j+2>bestbooks:
            bestbooks = i+j+2
           
        if Bsum[j] <= lasttime:
            upup = j+1
            break
            
print(bestbooks)

Submission status: Wrong Answer

Solution code:
import bisect
N,M,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
dpa = [0]*N
dpb = [0]*M
dpa[0]=A[0]
dpb[0]=B[0]
for i in range(1,N):
  dpa[i]=A[i]+dpa[i-1]

for j in range(1,M):
  dpb[j]=B[j]+dpb[j-1]

#print(dpa)
#print(dpb)

result=0
for i in range(N):
  if dpa[i] > K:
    break
  index=bisect.bisect_right(dpb,K-dpa[i])
  result = max(result,i+index+1)
print(result)