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()))
## dp(i,j,w) ai,bj,weight
dp=[[[0 for _ in range(k+1)]for _ in range(m+1)]for _ in range(n+1)]

for i in range(n+1):
    for j in range(m+1):
        for w in range(k+1):
            tmpA=dp[i][j][w]
            tmpB,tmpC,tmpD=0,0,0
            tmpE,tmpF,tmpG=0,0,0
            if i<n:
                if w>a[i]:
                    tmpB=dp[i][j][w-a[i]]+1
                if j>0:
                    tmpC=dp[i+1][j-1][w]
                    if j<m and w>b[j]:
                        tmpD=dp[i+1][j-1][w-b[j]]+1
                dp[i+1][j][w]=max(tmpA,tmpB,tmpC,tmpD,dp[i+1][j][w])
            if j<m:
                if w>b[j]:
                    tmpE=dp[i][j][w-b[j]]+1
                if i>0:
                    tmpF=dp[i-1][j+1][w]
                    if i<n and w>a[i]:
                        tmpG=dp[i-1][j+1][w-a[i]]+1
                dp[i][j+1][w]=max(tmpA,tmpE,tmpF,tmpG,dp[i][j+1][w])
            if i<n and j<m:
                dp[i+1][j+1][w]=max(tmpA,tmpB,tmpC,tmpD,tmpE,tmpF,tmpG,dp[i+1][j+1][w])
print(dp[n][m][k])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    ret = 0
    i, j = 0, 0
    while i < len(A) or j < len(B):
        if j >= len(B) or (i < len(A) and A[i] < B[j]):
            if K < A[i]:
                break
            K -= A[i]
            i += 1
        else:
            if K < B[j]:
                break
            K -= B[j]
            j += 1
        ret += 1
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

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()))
aa=[0]*n
ba=[0]*m
aa[0]=a[0]
ba[0]=b[0]
for i in range(1,n):
  aa[i]=aa[i-1]+a[i]
for i in range(1,m):
  ba[i]=ba[i-1]+b[i]
aa=[0]+aa
ba=[0]+ba
import bisect as bi
ax=bi.bisect_right(aa,k)-1
ans=0
for i in range(ax):
  kk=k-aa[ax-i]
  bx=bi.bisect_right(ba,kk)-1
  t=ax+bx-i
  ans=max(ans,t)
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()))

rui_A = [0 for _ in range(N)]
rui_B = [0 for _ in range(M)]



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

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

rui_A.insert(0,0)
rui_B.insert(0,0)

max_book = 0
for i in range(len(rui_A)):
    nokori = K - rui_A[i]
    if nokori >= 0:
        B_posi = bisect.bisect_right(rui_B,nokori)
        if max_book < B_posi + i -1 :
            max_book = B_posi + i - 1
print(max_book)

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

Submission status: Wrong Answer

Solution code:
import itertools
from bisect import bisect

N, M, K = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
A = A[:bisect([x for x in itertools.accumulate(A)], K)]
B = [int(n) for n in input().split()]
B = B[:bisect([x for x in itertools.accumulate(B)], K)]
ans = 0
t = 0
while True:
    if len(A) !=0 and len(B)!=0:
        c = min(A[0], B[0])
        if c == A[0]:
            t += A.pop(0)
        else:
            t += B.pop(0)
    else:
        if len(A)==0 and len(B)==0:
            break
        if len(A)==0:
            t += B.pop(0)
        else:
            t += A.pop(0)
    if t > K:
        break
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# import sys
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split())) + [10**13]
B = [0] + list(map(int, input().split())) + [10**13]
sum_t = 0
cumsumA = [0]
cumsumB = [0]
ansA = N
ansB = 0
tmp = 0
for i in range(1, N+1):
    tmp = tmp + A[i]
    if tmp > K:
        ansA = i-1
        break
    cumsumA += [tmp]
tmp = 0
for i in range(1, M+1):
    tmp = tmp + B[i]
    if tmp > K:
        break
    cumsumB += [tmp]
j = 0
ans = 0
for i in range(ansA, -1, -1):
    t = cumsumA[i] + cumsumB[j]
    while True:
        tmp = B[j]
        if tmp + t <= K:
            t += tmp
            j += 1
        else:
            j -= 1
            break
    ans = max(ans, i + j)
print(ans)

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()))
bka=[0]*N
bkb=[0]*M
bka[0]=A[0]
bkb[0]=B[0]

for i in range(1,N):
    bka[i]+=bka[i-1]+A[i]
for j in range(1,M):
    bkb[j]+=bkb[j-1]+B[j]
ans=bisect.bisect_left(bka,K)
for i in range(N):
    count=i+1
    k=K-bka[i]
    if k<0:continue
    c = bisect.bisect_right(bkb,k)+count
    ans=max(ans,c)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

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

X = [0,0]
ans = 0
for i in range(K):
    
    if i == N or X[1] + A[i]  > K:
        break
    
    X[0] += 1
    X[1] += A[i]

    
i -= 1
j = 0
while True:
    
    if j == M or X[1] + B[j] > K:
        break
    X[0] += 1
    X[1] += B[j]
    j += 1

ans = X[0]
while i >= 0:
    X[0] -= 1
    X[1] -= A[i]
    while True:
        if j == M or X[1] + B[j] > K:
            break
        X[0] += 1
        X[1] += B[j]
        j += 1
    if j == M:
        break
    if ans < X[0]:
        ans = X[0]
    i -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
A,B, K=map(int,input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
res = 0
a=0
b=0
inter = 0
n = max(A, B)
for i in range(n):
  if len(List_A)==0 and len(List_B)==0:
    break
  elif len(List_A)==0 and len(List_B)!=0:
    inter+=List_B.pop(0)
    if K < inter:
      break
    res +=1
  elif len(List_A) !=0 and len(List_B)==0:
    inter+=List_A.pop(0)
    if K < inter:
      break
    res +=1
  else:
    a = List_A.pop(0)
    b = List_B.pop(0)
    inter = inter + a+b
    if K < inter: 
      inter = inter -a
      if K < inter:
        inter = inter +a-b
        if K < inter:
          break
        else:
          res +=1
          break
      else:
        res +=1
        break
    else:
      res +=2
print(res)

Submission status: Wrong Answer

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

total_A_minutes = sum(A_minutes)
total_B_minutes = sum(B_minutes)

# すべて読める場合
if total_A_minutes + total_B_minutes <= K:
    print(N + M)
    exit()

# 冊数
ans = 0
# Kを超える最小のi
limitover_A_index = 0
# Aの合計が超えた場合
if total_A_minutes > K:
    tmp_total_A_minutes = 0
    for i in range(N):
        tmp_total_A_minutes = tmp_total_A_minutes + A_minutes[i]
        if tmp_total_A_minutes > K:
            limitover_A_index = i
            # Kを超過したindex が Kを超過しない冊数となる
            ans = i
            break
# Aの合計が超えない場合
else:
    limitover_A_index = N
    ans = N

best_b_index = 0
while limitover_A_index >= 0:
    tmp_total_minute = sum(A_minutes[:limitover_A_index])
    tmp_ans = limitover_A_index

    # Bを1冊ずつ確認
    # for B_minute in B_minutes:
    for i in range(best_b_index, M):
        # Bの冊数を足したことでK分を超えるなら
        if tmp_total_minute + B_minutes[i] > K:
            best_b_index = i
            break

        tmp_total_minute = tmp_total_minute + B_minutes[i]
        tmp_ans = tmp_ans + 1

        # Bの最大冊数まで到達の場合
        if i == M - 1:
            print(limitover_A_index + M)
            exit()

    # 最大冊数の記録を超えたら更新する
    if ans < tmp_ans:
        ans = tmp_ans

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

a_sum = []
b_sum = []
res=0
for i in a:
  res+=i
  a_sum.append(res)
res = 0
for i in b:
  res+=i
  b_sum.append(res)
b_sum.sort()
an = -1
bn = -1
count=0

for i in range(len(a_sum)):
  for j in range(len(b_sum)):
    if a_sum[i]+b_sum[j]<=k and count<a_sum[i]+b_sum[j]:
      count=a_sum[i]+b_sum[j]
      an=i
      bn=j


print(an+bn+2)

Submission status: Wrong Answer

Solution code:
a = input().split()
a = [int(a)for a in a]
lis = []

b = input().split()
b = [int(b)for b in b]
c = input().split()
c = [int(c)for c in c]

for i in b:
    lis.append(i)

for i in c:
    lis.append(i)

lis.sort()

count = 0
time = a[2]
honn = 0

while time >= lis[count] and count <= len(lis)-1 :
    time = time - lis[count]

    if  count >= len(lis)-1 :
        honn += 1

        break

    else:
        count += 1
        honn += 1

print(honn)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = [10**10] + list(map(int, input().split()))[::-1]
B = [10**10] + list(map(int, input().split()))[::-1]

t = K
cnt = 0
while t >= 0:
    if A[-1] <= B[-1] and t >= A[-1]:
        t -= A[-1]
        A.pop()
        cnt += 1
    elif A[-1] >= B[-1] and t >= B[-1]:
        t -= B[-1]
        B.pop()
        cnt += 1
    else:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
ab = list(map(int, input().split())) + list(map(int, input().split()))
O = 0
ab.sort()
for i in range(len(ab)):
    k -= ab[i]
    if k < 0:
        break
    O +=1
print(O)

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()))
b.insert(0, 0)
for i in range(m-1):
   b[i+1] += b[i]
asum = 0
ans = 0
for i, x in enumerate(a):
  asum += x
  if asum > k:
    break
  l, r = 0, m+1
  while r-l>1:
    mid = (l+r)//2
    if asum+b[mid] <= k:
      l = mid
    else:
      r = mid
  ans = max(ans, i+mid+1)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

A=list(map(int,stdin.readline().split()))
N=A[0]
M=A[1]
K=A[2]
KK=K
B=list(map(int,stdin.readline().split()))  #N
C=list(map(int,stdin.readline().split()))  #M

Nlist=list()
Mlist=list()
sum=0

for u in B:
    sum+=u
    if sum>K:
        break
    Nlist.append(sum)


sum=0
O=0

for v in C:
    sum+=v
    if sum>K:
        break
    Mlist.append(sum)

if len(Mlist)==0 and len(Nlist)==0:
    print(0)
    O=1

if O==0:
    if len(Mlist)==0:
        print(len(Nlist))
        O=1
    if len(Nlist)==0:
        print(len(Mlist))
        O=1
    if Mlist[len(Mlist)-1]+Nlist[len(Nlist)-1]<=K:
        print(len(Mlist)+len(Nlist))
        O=1

MAX=2
mbegin=0
mend=len(Mlist)-1
#archive=0
# print(Nlist)
# print(Mlist)



if O==0:

    for u in range(len(Nlist)-1,-1,-1):
        mend=len(Mlist)-1
        K=KK
        K=K-Nlist[u]
        MAX=max(MAX,u+1)
        #print(K)
        #binary search
        if Mlist[mbegin]<=K:
            UU = (mbegin + mend) // 2
            while mbegin!=UU:
                UU = (mbegin + mend) // 2
                if Mlist[UU]<=K:
                    K-=Mlist[UU]
                    mbegin=UU
                else:
                    mend=UU

            # if mbegin==archive:
            #     #print('hh')
            #     if mbegin==0:
            #         #print(MAX)
            #         MAX=max(MAX,u+2)
            #         #print(MAX)
            #     break

            MAX=max(MAX,u+mbegin+2)
            #print(MAX)
            #archive=mbegin

if O==0:
   print(MAX)

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

    AReqTime = [0]
    BReqTime = [0]

    for i in range(N):
        AReqTime.append(AReqTime[i] + A[i])
    for i in range(M):
        BReqTime.append(BReqTime[i] + B[i])

    j = M
    answer = 0

    for i in range(N):
        if(AReqTime[i] > K):
            break

        while BReqTime[j] > K - AReqTime[i]:
            j = j - 1

        answer = max(i + j, answer)

    print(answer)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
root = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))    
minute=0
books=0
ai=0
bi=0

while minute <= root[2]:
    if len(a) == ai:
        while len(b) > bi and minute <= root[2]:
            minute += b[bi]
            books += 1
            bi += 1
            if len(b) == bi:
                books += 1
        break
    if len(b) == bi:
        while len(a) > ai and minute <= root[2]:
            minute += a[ai]
            books += 1
            ai += 1
            if len(a) == ai:
                books += 1
        break
    if a[ai] < b[bi]:
        minute += a[ai]
        books += 1
        ai += 1
    elif b[bi] < a[ai]:
        minute += b[bi]
        books += 1
        bi += 1
    else:
        if len(a) == ai and len(b) == bi:
            books += 1
            break
        if len(a) > ai:
            minute += a[ai]
            books += 1
            ai += 1
        elif len(b) > bi:
            minute += b[bi]
            books += 1
            bi += 1
            
        
print(books-1)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import bisect
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

A=list(accumulate(a))
B=list(accumulate(b))
ans=1+bisect.bisect_right(B,k)
for i in range(len(a)):
    r = (k-A[i])
    if r<=0:
        break
    #print(r,bisect.bisect_left(B,r))
    #print(B[bisect.bisect_left(B,r)])
    ans=max(ans,i+1+bisect.bisect_right(B,r))
print(ans)

Submission status: Wrong Answer

Solution code:
def Tsun(A,B,N,M,K):
  sumA = [0]*N
  sumB = [0]*M
  SAC = 0
  SBC = 0
  for i in range(N):
    if i == 0:
      sumA[i] = A[i]
    else:
      sumA[i] = sumA[i-1] + A[i]
    if sumA[i] <= K:
      SAC += 1
    else:
      break
  for i in range(M):
    if i == 0:
      sumB[i] = B[i]
    else:
      sumB[i] = sumB[i-1] + B[i]
    if sumB[i] <= K:
      SBC += 1
    else:
      break
  X = SAC
  for i in range(SAC):
    for j in range(SBC):
      if X <= i+j+2 and sumA[i] + sumB[j] <= K:
        X = i+j+2
  return X
  


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

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

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

ans = 0
for i in range(N+1):
    ans = max(ans, i + bisect.bisect_right(b, K - a[i])-1)

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = [10**10] + list(map(int, input().split()))[::-1]
B = [10**10] + list(map(int, input().split()))[::-1]

t = K
cnt = 0
while t >= 0:
    if A[-1] < B[-1] and t >= A[-1]:
        t -= A[-1]
        A.pop()
        cnt += 1
    elif A[-1] >= B[-1] and t >= B[-1]:
        t -= B[-1]
        B.pop()
        cnt += 1
    else:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import exit
from collections import deque
# import copy
# import numpy as np
# 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())))

cnt = 0

while k >= a[0] or k >= b[0]:
    if a[0] < b[0]:
        k -= a.popleft()
        cnt += 1

    elif a[0] > b[0]:
        k -= b.popleft()
        cnt += 1
    else:
        for i in range(1, min(len(a), len(b))):
            if a[i] < b[i]:
                for t in range(0, i):
                    if k < a[0]:
                        break
                    k -= a.popleft()
                    cnt += 1
                break
            elif a[i] > b[i]:
                for t in range(0, i):
                    if k < b[0]:
                        break
                    k -= b.popleft()
                    cnt += 1
                break
        else:
            if len(a) > len(b):
                for i in range(len(b)):
                    if k < a[0]:
                        break
                    k -= a.popleft()
                    cnt += 1
            else:
                for i in range(len(a)):
                    if k < b[0]:
                        break
                    k -= b.popleft()
                    cnt += 1
    if a == deque([]):
        for i in range(len(b)):
            if k < b[0]:
                break
            k -= b.popleft()
            cnt += 1
        break
    if b == deque([]):
        for i in range(len(a)):
            if k < a[0]:
                break
            k -= a.popleft()
            cnt += 1
        break
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()))
count = 0
ANS = 0

for i in range(len(A)):
    count += A[i]
    if (count > K) or (i==len(A)-1):
        if count <= K:
          i += 1
        del A[i:]
        count=0
        ANS += len(A)
        break

for i in range(len(B)):
    count += B[i]
    if (count > K) or (i==len(B)-1):
        if count <= K:
          i += 1
        del B[i:]
        count=0
        ANS += len(B)
        break

count = sum(A) + sum(B)




while count > K:
    if A[-1] > B[-1]:
        count -= A[-1]
        del A[-1]
        ANS -= 1
    else:
        count -= B[-1]
        del B[-1]
        ANS -= 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()))

A_cum = []
B_cum = []

cum = 0
for a in A:
  cum += a
  A_cum.append(cum)

cum = 0
for b in B:
  cum += b
  B_cum.append(cum)

ans = 0  
for i, a in enumerate(A_cum):
  rest = k-a
  if rest<=0:
    continue
  
  ans = max(ans, bisect.bisect(B_cum, rest)+i+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()))
a2 = deque([0])
b2 = deque()

m = 0
for _ in range(len(a)):
    x = a.popleft()
    m += x
    if m > k:
        break
    a2.append(m)

m = 0
for _ in range(len(b)):
    x = b.popleft()
    m += x
    if m > k:
        break
    b2.append(m)

if len(a2) == 0:
    if len(b2) == 0:
        print(0)
        exit()
    else:
        print(len(b2))
elif len(b2) == 0:
    print(len(a2)-1)
    exit()

maxi = len(a2) - 1


ae = a2.pop()
be = b2.popleft()
books = len(a2)
while len(a2) > 0:
    if ae + be <= k:
        books += 1
        maxi = max(maxi, books)
        if len(b2) <= 0:
            break
        be = b2.popleft()
    else:
        books -= 1
        ae = a2.pop()
    
print(maxi)

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

sumA=[0]
sumB=[0]


for i in range(N):
  sumA.append(sumA[-1]+A[i])
for i in range(M):
  sumB.append(sumB[-1]+B[i])

  
total=0
startB=len(sumB)-1
for a in range(len(sumA)):
  for b in range(startB,-1,-1):
    if sumA[a]+sumB[b]<=K:
      total=a+b
      startB=b
      break
  if sumA[a]>K:
    break
    
print(total)

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 = [A[0]]
for i in range(1, len(A)):
    a.append(a[i - 1] + A[i])
b = [B[0]]
for i in range(1, len(B)):
    b.append(b[i - 1] + B[i])

ans_list = [0]
for i, a_i in enumerate(a):
    for j in range(len(B) - 1, -1, -1):
        b_j = b[j]
        if b_j <= K - a_i:
            ans_list.append(i + 1 + j + 1)
            break

print(max(ans_list))

Submission status: Wrong Answer

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

total_A_minutes = sum(A_minutes)
total_B_minutes = sum(B_minutes)

# すべて読める場合
if total_A_minutes + total_B_minutes <= K:
    print(N + M)
    exit()

# 冊数
ans = 0
# Kを超える最小のi
limitover_A_index = 0
# Aの合計が超えた場合
if total_A_minutes > K:
    tmp_total_A_minutes = 0
    for i in range(N):
        tmp_total_A_minutes = tmp_total_A_minutes + A_minutes[i]
        if tmp_total_A_minutes > K:
            limitover_A_index = i
            # Kを超過したindex が Kを超過しない冊数となる
            ans = i
            break
# Aの合計が超えない場合
else:
    limitover_A_index = N
    ans = N

best_b_index = 0
while limitover_A_index >= 0:
    tmp_total_minute = sum(A_minutes[:limitover_A_index])
    tmp_ans = limitover_A_index

    # Bを1冊ずつ確認
    # for B_minute in B_minutes:
    # for i in range(best_b_index, M):
    for i in range(M):
        # Bの冊数を足したことでK分を超えるなら
        if tmp_total_minute + B_minutes[i] > K:
            break

        tmp_total_minute = tmp_total_minute + B_minutes[i]
        tmp_ans = tmp_ans + 1

        # Bの最大冊数まで到達の場合
        if i == M - 1:
            print(tmp_ans + M)
            exit()

    # 最大冊数の記録を超えたら更新する
    if ans < tmp_ans:
        ans = tmp_ans

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


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

Submission status: Wrong Answer

Solution code:
root = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))    
minute=0
books=0
ai=0
bi=0

while minute <= root[2]:
    if len(a) == ai:
        while len(b) > bi and minute <= root[2]:
            minute += b[bi]
            books += 1
            bi += 1
        books += 1
        break
    if len(b) == bi:
        while len(a) > ai and minute <= root[2]:
            minute += a[ai]
            books += 1
            ai += 1
        books += 1
        break
    if a[ai] < b[bi]:
        minute += a[ai]
        books += 1
        ai += 1
    elif b[bi] < a[ai]:
        minute += b[bi]
        books += 1
        bi += 1
    else:
        if len(a) == ai and len(b) == bi:
            books += 1
            break
        if len(a) > ai:
            minute += a[ai]
            books += 1
            ai += 1
        elif len(b) > bi:
            minute += b[bi]
            books += 1
            bi += 1
            
        
print(books-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
N, M, K=map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.insert(0, 0)
B.insert(0, 0)
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])
num=0
for i in range(len(a)):
    for l in range(len(b)):
        if num<i+l:
            Time=a[i]+b[l]
            if Time<=K:
                num=i+l
print(num)

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

for i in range(N):
    if K < A_cumulative_sum[i]:
        remaining_time = K
        idx = bisect.bisect_right(B_cumulative_sum, remaining_time)
        ans = max(ans, idx)
        break
    else:
        remaining_time = K - A_cumulative_sum[i]
        idx = bisect.bisect_right(B_cumulative_sum, remaining_time)
        ans = max(ans, i + 1 + idx)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

A=list(map(int,stdin.readline().split()))
N=A[0]
M=A[1]
K=A[2]
KK=K
B=list(map(int,stdin.readline().split()))  #N
C=list(map(int,stdin.readline().split()))  #M

Nlist=list()
Mlist=list()
sum=0

for u in B:
    sum+=u
    if sum>K:
        break
    Nlist.append(sum)


sum=0
O=0

for v in C:
    sum+=v
    if sum>K:
        break
    Mlist.append(sum)

if len(Mlist)==0 and len(Nlist)==0:
    print(0)
    O=1

if O==0:
    if len(Mlist)==0:
        print(len(Nlist))
        O=1
    if len(Nlist)==0:
        print(len(Mlist))
        O=1
    if Mlist[len(Mlist)-1]+Nlist[len(Nlist)-1]<=K:
        print(len(Mlist)+len(Nlist))
        O=1

MAX=2
mbegin=0
mend=len(Mlist)-1
archive=0
# print(Nlist)
# print(Mlist)



if O==0:

    for u in range(len(Nlist)-1,-1,-1):
        mend=len(Mlist)-1
        K=KK
        K=K-Nlist[u]
        MAX=max(MAX,u+1)
        #print(K)
        #binary search
        if Mlist[mbegin]<=K:
            UU = (mbegin + mend) // 2
            while mbegin!=UU:
                UU = (mbegin + mend) // 2
                if Mlist[UU]<=K:
                    K-=Mlist[UU]
                    mbegin=UU
                else:
                    mend=UU

            if mbegin==archive:
                #print('hh')
                if mbegin==0:
                    #print(MAX)
                    MAX=max(MAX,u+2)
                    #print(MAX)
                break

            MAX=max(MAX,u+mbegin+2)
            #print(MAX)
            archive=mbegin

if O==0:
   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 = [sum(A[:i+1]) for i in range(len(A)) if sum(A[:i+1]) <= K]
B = [sum(B[:i+1]) for i in range(len(B)) if sum(B[:i+1]) <= K]

len_B = len(B)
res = []
for i, a in enumerate(A):
    for j in range(len_B)[::-1]:
        # print(i+1, j+1, a, B[j], a+B[j], K)
        if a + B[j] <= K:
            res.append(i+j+2)
            break
    len_B = j+1

print(0 if len(res) < 1 else max(res))

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

rui_A = [0 for _ in range(N)]
rui_B = [0 for _ in range(M)]



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

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

rui_A.insert(0,0)
rui_B.insert(0,0)

max_book = 0
for i in range(len(rui_A)):
    nokori = K - rui_A[i]
    if nokori >= 0:
        B_posi = bisect.bisect_right(rui_B,nokori)
        if max_book < B_posi + i:
            max_book = B_posi + i - 1
print(max_book)

Submission status: Wrong Answer

Solution code:
root = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))    
minute=0
books=0
ai=0
bi=0

while minute <= root[2]:
    if len(a) == ai:
        while len(b) > bi and minute <= root[2]:
            minute += b[bi]
            books += 1
            bi += 1
        break
    if len(b) == bi:
        while len(a) > ai and minute <= root[2]:
            minute += a[ai]
            books += 1
            ai += 1
        break
    if a[ai] < b[bi]:
        minute += a[ai]
        books += 1
        ai += 1
    elif b[bi] < a[ai]:
        minute += b[bi]
        books += 1
        bi += 1
    else:
        if len(a) == ai and len(b) == bi:
            books += 1
            break
        if len(a) > ai:
            minute += a[ai]
            books += 1
            ai += 1
        elif len(b) > bi:
            minute += b[bi]
            books += 1
            bi += 1
            
        
print(books-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()))

sumA, sumB = [], []
sa, sb = 0, 0
for a in A:
    sa += a
    sumA.append(sa)
for b in B:
    sb += b
    sumB.append(sb)

n_max = 0
for na in range(N):
    for nb in range(M):
        k = sumA[na] + sumB[nb]
        if k <= K:
            n = (na + 1) + (nb + 1)
            n_max = max(n_max, n)
print(n_max)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


sum_A = [0]
sum_B = [0]
for n in range(N):
    sum_A.append(sum_A[n]+A[n])
for m in range(M):
    sum_B.append(sum_B[m]+B[m])

max_j = M
max_book = 0
for i in range(N+1):
    # Aの机でi冊読んだときのBの机で読む最大値
    for j in range(max_j,-1,-1):
        if sum_A[i]+sum_B[j] <= K:
            max_j = j
            break
    else:
        max_j = 0
    
    max_book = max(max_book, i+max_j)

print(max_book)