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 = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]
time = 0
ans = 0
a = 0
b = 0
while K >= time:
    if len(A) == a and len(B) == b:
        ans += 1
        break
    elif len(A) > a and len(B) > b:
        if A[a] > B[b]:
            time += B[b]
            b += 1
            ans += 1
        elif A[a] < B[b]:
            time += A[a]
            a += 1
            ans += 1
        elif a != N-1 and b != M-1:
            if A[a+1] > B[b+1]:
                time += B[b]
                b += 1
                ans += 1
            else:
                time += A[a]
                a += 1
                ans += 1
        elif a == N-1:
            time += B[b]
            b += 1
            ans += 1
        else:
            time += A[a]
            a += 1
            ans += 1
    elif len(A) == a:
        time += B[b]
        b += 1
        ans += 1
    else:
        time += A[a]
        a += 1
        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()))

time = 0
count = 0
while A or B:
    if A and B:
        desk = A if A[0] < B[0] else B
    else:
        desk = A if A else B
    time += desk.popleft()

    if time > K:
        break
    else:
        count += 1

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()))
a = 0
b = 0
t_sum = 0
cnt = 0
flag = True

for _ in range(len(A)+len(B)):
  if a == len(A):
    for _ in range(len(B)+1):
      if b == len(B):
        flag = False
        break
      t_sum += B[b]
      cnt += 1
      b += 1
      if t_sum > K:
        cnt -= 1
        flag = False
        break
  if not flag:
    break
  if b == len(B):
    for _ in range(len(A)+1):
      if a == len(A):
        flag = False
        break
      t_sum += A[a]
      cnt += 1
      if t_sum > K:
        cnt -= 1
        flag = False
        break
  if not flag:
    break
  if A[a] <= B[b]:
    t_sum += A[a]
    cnt += 1
    a += 1
  else:
    t_sum += B[b]
    cnt += 1
    b += 1
  if t_sum > K:
    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()))

from itertools import accumulate
a_acc=list(accumulate(a))
b_acc=list(accumulate(b))

cnt=0
ans=0

import bisect

# print(a_acc)
# print(b_acc)
for i in range(n):
    nokori=k-a_acc[i]
    if nokori<=0:
        break
    b_i=bisect.bisect_right(b_acc,nokori)
    cnt=max(cnt,(i+1)+b_i)
    # print(nokori,i,b_i)

print(cnt)

Submission status: Wrong Answer

Solution code:
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()]

WA = [0] * (N + 1)
WB = [0] * (M + 1)
for i in range(N):
    WA[i + 1] = WA[i] + A[i]
for j in range(M):
    WB[j + 1] = WB[j] + B[j]
ma = 0
for i in range(1, N + 1):
    if WA[i] > K:
        break
    t = K - WA[i]
    j = bisect.bisect(WB, t)
    ma = max(ma, i + j - 1)

print(ma)

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
A_count = 0
B_count = 0
while count < K:
    if A_count==N and B_count==M:
        ans+=1
        break
    elif A_count==N:
        ans +=1
        count += B[B_count]
        B_count+=1
        continue
    elif B_count==M:
        ans+=1
        count += A[A_count]
        A_count+=1
        continue
        
    if A[0] < B[0]:
        ans += 1
        count += A[A_count]
        A_count+=1
    else:
        ans += 1
        count += B[B_count]
        B_count+=1
if A[0] > K and B[0] > K:
    print(0)
else:
    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()))
ruisekiA = [0] * (N + 1)
for i in range(N):
    ruisekiA[i + 1] = ruisekiA[i] + A[i]
ruisekiB = [0] * (M + 1)
for i in range(M):
    ruisekiB[i + 1] = ruisekiB[i] + B[i]
ans = 0
for i in range(N + 1):
    if ruisekiA[i] > K: break
    else:
        time = K - ruisekiA[i]
        lb = -1
        ub = M
        while ub - lb > 1:
            mid = (ub + lb) // 2
            if ruisekiB[mid + 1] > time: ub = mid
            else: lb = mid
        if i != 0 and ub != 0: ans = max(ans,i + ub)
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()))

from collections import deque
a=deque(a)
b=deque(b)

t=0
c=0

while 1:
    if len(a)==0 and len(b)==0:
        print(c)
        break
    elif len(a)!=0 and len(b)!=0:
        if a[0]<=b[0]:
            t+=a[0]
            if t<=k:
                c+=1
            else:
                print(c)
                break
            a.popleft()
        else:
            t+=b[0]
            if t<=k:
                c+=1
            else:
                print(c)
                break
            b.popleft()
    else:
        if len(a)==0:
            t+=b[0]
            if t<=k:
                c+=1
            else:
                print(c)
                break
            b.popleft()
        else:
            t+=a[0]
            if t<=k:
                c+=1
            else:
                print(c)
                break
            a.popleft()

Submission status: Wrong Answer

Solution code:
inf = 2*10**9
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
a.append(inf)
b.append(inf)

cur_a = 0
cur_b = 0
total = 0
count = 0
while a[cur_a] != inf and b[cur_b] != inf:
    total += min(a[cur_a], b[cur_b])
    if total > k:
        break
    count += 1
    if a[cur_a] > b[cur_b]:
        cur_b += 1
    else:
        cur_a += 1
else:
    if a[cur_a] == inf:
        while total <= k:
            total += b[cur_b]
            count += 1
            cur_b += 1
    else:
        while total < k:
            total += a[cur_a]
            count += 1
            cur_a += 1
    count -= 1

print(count)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
b = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
ans = 0
kazu = 0
c_a = 0
c_b = 0
time = 0
'''
a.append(10**9 + 1)
b.append(10**9 + 1)


while True:
    if time + min(a[c_a],b[c_b])>k:
        break
    if a[c_a]>b[c_b]:
        time += b[c_b]
        c_b += 1
    elif a[c_a]<b[c_b]:
        time += a[c_a]
        c_a += 1

    ans += 1
'''
a = [0]+a
b = [0]+b

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

for i in range(0,n):
    time = a[i]
    if time > k:
        break
    kazu = i
    for j in range(0,m):
        time += b[j]
        if time > k:
            break
        kazu += j
    ans = max(kazu,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()))

a.reverse()
b.reverse()

ans = 0
time = 0

while time < k:
    if len(a) == 0 and len(b) == 0:
        break
    #print(a, len(a))
    #print(b, len(b))
    taken = False
    if len(a) == 0:
        if time+b[-1] <= k:
            time += b.pop()
            taken = True
    elif len(b) == 0:
        if time+a[-1] <= k:
            time += a.pop()
            taken = True
    elif a[-1] <= b[-1]:
        if time+a[-1] <= k:
            time += a.pop()
            taken = True
    else:
        if time+b[-1] <= k:
            time += b.pop()
            taken = True
    if not taken:
        break
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]

times = 0
book_num = 0
i = 0
j = 0
while times < k:

    if i == n and j == m:
        break

    if i == n:
        tmp = b_list[j]
        j += 1

    elif j == m:
        tmp = a_list[i]
        i += 1
    else:
        if a_list[i] >= b_list[j]:
            tmp = b_list[j]
            j += 1
        else:
            tmp = a_list[i]
            i += 1

    times += tmp
    if times <= k:
        book_num += 1


print(book_num)

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
n,m,s=map(int, stdin.readline().split());M=10**9+2
dn=list(map(int, stdin.readline().split()))#+[M]
dm=list(map(int, stdin.readline().split()))#+[M]
cn=0;cm=0;cs=0
'''print(dn,dm)'''
for v in dn:
    cs+=v
    if cn>=n-1 or cs>s:
        f=1;cs-=v
        for v1 in dm:
            cs+=v1
            if cs>s:
                f=0
                break
            cn+=1
        if f==0:break
    #print(v)
    cn+=1
cs=0
for v in dm:
    cs+=v
    if cm>=m-1 or cs>s:
        f=1;cs-=v
        for v1 in dn:
            cs+=v1
            if cs>s:
                f=0
                break
            cm+=1
        if f==0:break
    cm+=1
'''print(cn,cm)'''
print(max(cn,cm))

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()))
time = 0
ct = 0
for i in range(K):
    if len(A) == 0 and len(B) == 0:
        break
    elif len(A) == 0:
        time += B[0]
        if time > K:
            break
        else:
            ct += 1
            del B[0]
    elif len(B) == 0:
        time += A[0]
        if time > K:
            break
        else:
            ct += 1
            del A[0]
    elif A[0] >= B[0]:
        time += B[0]
        if time > K:
            break
        else:
            ct += 1
            del B[0]
            continue
    elif A[0] < B[0]:
        time += A[0]
        if time > K:
            break
        else:
            ct += 1
            del A[0]
            continue
print(ct)

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
nowK = K
for A_idx in range(len(A)):
    if nowK < A[A_idx]:
        break
    ans += 1
    nowK -= A[A_idx]
for B_idx in range(M):
    if nowK < B[B_idx]:
        break
    ans += 1
    nowK -= B[B_idx]
tmp_ans = ans
for i in range(A_idx, -1, -1):
    nowK += A[i]
    tmp_ans -= 1
    for j in range(B_idx, M):
        if nowK < B[j]:
            break
        nowK -= B[j]
        tmp_ans += 1
    B_idx = j
    ans = max(tmp_ans, 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()))

def 操作(A,B,N,M):
    MAX=0
    s=0
    for i in range(N):
        s+=A[i]
        if s>=K:
            break
        S=s
        for j in range(M):
            S+=B[j]
            if S<=K:
                if i+j+2>MAX:
                    MAX=i+j+2
                else:
                    break
        for k in range(i+1,N):
            S+=A[k]
            if S>K:
                break
            if k+j+2>MAX:
                MAX=k+j+2
    return MAX

if N>=M:
    print(操作(A,B,N,M))
else:
    print(操作(B,A,M,N))

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()))
    nB=0
    nM=0
    while True:
        if (not a) and (b):
            c=b.pop(0)
        
        elif (not b) and (a):
            c=a.pop(0)

        elif (not a) and (not b):
            break

        else:
            if a[0]<b[0]:
                c=a.pop(0)
            else:
                c=b.pop(0)


        if c+nM<=k:
            nB+=1
            nM+=c
        else:
            break

    print(nB)


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()))
A+=[10**10]
B+=[10**10]
a,b = 0,0
while K >= A[a] or K >= B[b] :
    if A[a]>B[b]:
        K -= B[b]
        b+=1
    else:
        K -= A[a]
        a+=1
    #print(a,b)
        
print(a+b)

Submission status: Wrong Answer

Solution code:
import random
# n, m, k = map(int, input().split())
n, m, k = random.randint(1, 200000), random.randint(1, 200000), random.randint(1, 10**9)
# a_list = list(map(int, input().split()))
# b_list = list(map(int, input().split()))
a_list = [random.randint(1, 10**3) for i in range(n)]
b_list = [random.randint(1, 10**3) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [a_list[0]]
b_sum = [b_list[0]]
max_a = 0
max_b = 0
for i in range(1, max(n, m)):
    if i < n:
        a_sum.append(a_sum[i-1] + a_list[i])
        if a_sum[i] < k:
            max_a = i+1
    if i < m:
        b_sum.append(b_sum[i-1] + b_list[i])

max_num = 0
max_a += 1
while max_a > 0:
    max_a -= 1
    while max_b < m and b_sum[max_b] <= k-a_sum[max_a-1]:
        max_b += 1
    if max_num < max_a + max_b:
        max_num = max_a + max_b

print(max_num)
# 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()))

sumnum = 0
count = 0

B.reverse()
AB = A + B

while 1:
    if len(AB) == 0:
        break

    elif AB[0] < AB[-1]:
        tmp = sumnum + AB[0]
        AB.pop(0)

    else:
        tmp = sumnum + AB[-1]
        AB.pop(-1)

    if tmp > K:
        break

    else:
        sumnum = tmp
        count = count + 1


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()))
A.append(2*10**10)
B.append(2*10**10)

ans = 0
t = 0
ia, ib = 0, 0
while t < K and (ia < N or ib < M):
    if A[ia] <= B[ib]:
        if t + A[ia] <= K:
            t += A[ia]
            ans += 1
            ia += 1
        else: break
    else:
        if t + B[ib] <= K:
            t += B[ib]
            ans += 1
            ib += 1
        else: break

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()))
c = 0

while k >= 0:
  if len(a) != 0 and len(b) != 0:
    p = a.pop(0) if a[0] < b[0] else b.pop(0)
  elif len(a) == 0 and len(b) != 0:
    p = b.pop(0)
  elif len(a) != 0 and len(b) == 0:    
    p = a.pop(0)
  else:
    break;
  k -= p
  
  if  k >= 0:
    c += 1
  
print(c)

Submission status: Wrong Answer

Solution code:
# 初期入力
from collections import deque
import sys
input = sys.stdin.readline
N,M,K = (int(i) for i in input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dq_a =deque(A)
dq_b =deque(B)

#上から読む＝配列の前から読む


read_time =0
ans =0
a=dq_a.popleft()
b=dq_b.popleft()
for i in range(N+M):
    if a <=b and read_time +a <=K:
        read_time +=a
        ans +=1
        if len(dq_a) >0:
            a =dq_a.popleft()
        else:
            a=10**9 +1
    if a >=b and read_time +b <=K:
        read_time +=b
        ans +=1
        if len(dq_b) >0:
            b =dq_b.popleft()
        else:
            b=10**9 +1
    elif read_time >K:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right as br
n,m,t=map(int,input().split())
arr=[]
brr=[]
for i in range(1):
    arr=list(map(int,input().split()))
    
for i in range(1):
    brr=list(map(int,input().split()))
for i in range(1,n):
    arr[i]+=arr[i-1]
for i in range(1,m):
    brr[i]+=brr[i-1]

#print(arr)
ans=0
for i in range(n):
    if(arr[i]<=t):
        ind=br(brr,t-arr[i])
        #print(t-arr[i],ind)
        ans=max(ans,ind+i+1)
print(ans)

Submission status: Wrong Answer

Solution code:

n,m,K=list(map(int,input().split()))
A=[int(j) for j in input().split()]
B=[int(j) for j in input().split()]
i=0
j=0
time=0
cnt=0
while(i<n and j<m):
    one=False
    if(A[i]<=B[j] ):
        x=A[i]
        one=True

    else:
        x=B[j]
    if(x+time<=K):
        time=time+x
        if(one):
            i+=1
        else:
            j+=1
        cnt+=1
    else:
        break
if(time<K):
    while(i<n ):
        if(A[i]+time<=K):
            time=time+A[i]
            cnt+=1
            i+=1
        else:
            break
    while(j<m ):
        if(B[j]+time<=K):
            time=time+B[j]
            cnt+=1
            j+=1
        else:
            break
    # print(time,cnt)
print(cnt)

Submission status: Wrong Answer

Solution code:
# C

import itertools
import bisect

n, m, k = map(int, input().split())
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]

cumsuma_l = list(itertools.accumulate(a_l)) 
cumsumb_l = list(itertools.accumulate(b_l))

ans = 0
for i in range(n):
    _a = cumsuma_l[i]
    if _a > k:
        break
    _tmp = k - _a
    _idx = bisect.bisect_left(cumsumb_l, _tmp)
    if _idx == m or cumsumb_l[_idx] != _tmp:
        _idx -= 1
    ans = max(ans, i + _idx + 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()))
t = 0
num = 0
bt = 0
bi = 0
for i in range(n):
    t += a[i]
    if t > k:
        t -= a[i]
        break
    else:
        num += 1

for j in range(m):
    t += b[j]
    if t > k:
        t -= b[j]
        break
    else:
        bt += b[j]
        bi += 1
        num += 1
if num > len(a):
    inum = len(a)
else:
    inum = num
cnum = num
for i in range(inum):
    t -= a[inum - i - 1]
    dif = k - t
    cnum -= 1
    while bi < m:
        bt += b[bi]
        if t + bt > k:
            bt -= b[bi]
            break
        else:
            bi += 1
            cnum += 1
    if num < cnum:
        num = cnum
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()))
num = len(a) + len(b)
time = 0
for i in range(num):
    if len(a) >= 1 and len(b) >= 1:
        r = min(a[0], b[0])
        time += r
        if a[0] >= b[0]:
            b.remove(b[0])
        else:
            a.remove(a[0])
        if time > k:
            break
    elif len(a) >= 1 and len(b) == 0:
        time += a[0]
        a.remove(a[0])
        if time > k:
            break
    elif len(b) >= 1 and len(a) == 0:
        time += b[0]
        b.remove(b[0])
        if time > k:
            break

if len(b) == 0 and len(a) == 0:
    print(i)
else:
    print(i - 1)

Submission status: Wrong Answer

Solution code:
[N,M,K] = map(int,input().split())
I = [input() for i in range(2)]
Aall = list(map(int,I[0].split()))
Ball = list(map(int,I[1].split()))
L = max([N,M])
c = 0
n = 0
m = 0
T = 0
c = 0
L = N + M
for i in range(L):
    if n < N:
        A = Aall[n] 
    else:
        A = 1000000001 
    if m < M:
        B = Ball[m] 
    else:
        B = 1000000001 
    C = min([A,B])
    T = T + C
    if T > K :
        break
    else:
        c = c + 1
        if C == A:
            n = n + 1
        elif C == B:
            m = m + 1

print(c)

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

L = N + M
dp = [0] * (L + 1)
for i in range(1, L + 1):
    if A != [] and B != []:
        dp[i] = dp[i - 1] + min(A[0], B[0])
        if A[0] >= B[0]:
            B.pop(0)
        else:
            A.pop(0)
    else:
        if A != []:
            dp[i] = dp[i - 1] + A[0]
            A.pop(0)
        else:
            dp[i] = dp[i - 1] + B[0]
            B.pop(0)

ans = 0
for i in range(L + 1):
    if dp[i] <= K:
        ans = i
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
aaa = [int(i) for i in input().split()]
bbb = [int(i) for i in input().split()]
time = 0
ans = 0
while (time < k):
    if ((len(aaa) > 0)  &  (len(bbb) > 0)):
        if aaa < bbb:
            time += aaa.pop(0)
            ans += 1
        else:
            time += bbb.pop(0)
            ans += 1            
    elif aaa:
        time += aaa.pop(0)
        ans += 1
    elif bbb:
        time += bbb.pop(0)
        ans += 1
    else:
        break
if time <= k:
    print(ans)
else:
    print(ans - 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    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 > 0:
        if CNT == N+M:
            break
        if len(A) > 0 and len(B) > 0:
            a = A[0]
            b = B[0]
            if a >= b:
                if K >= b:
                    K -= b
                    B.popleft()
                    CNT += 1
                else:
                    break
            else:
                if K >= a:
                    K -= a
                    A.popleft()
                    CNT += 1
                else:
                    break

        elif len(A) == 0:
            b = B[0]
            if K >= b:
                K -= b
                B.popleft()
                CNT += 1
            else:
                break
        else:
            if K >= a:
                K -= a
                A.popleft()
                CNT += 1
            else:
                break
    print(CNT)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x = input().split()
y = input().split()
z = input().split()
s = 0
import sys
for nn in range(400000):
    if int(x[2]) < 0:
        print(int(s-1))
        sys.exit()
    if int(y[0]) <= int(z[0]) :
        x[2] = int(x[2]) - int(y[0])
        del y[0]  
        y.append(1000000001) 
        s += 1
    else:
        x[2] = int(x[2]) - int(z[0])
        del z[0]
        z.append(1000000001)
        s += 1

Submission status: Wrong Answer

Solution code:
#n = int(input())
n,m,k = map(int,input().split())
a_L = list(map(int,input().split()))
b_L = list(map(int,input().split()))

#a_L = list(reversed(a_L))
#b_L = list(reversed(b_L))

ans = 0
dp = [float("inf")] * (n+m+1)

if a_L[-1] > b_L[-1]:
    dp[0] = b_L[-1]
    b_L.pop()
else:
    dp[0] = a_L[-1]
    a_L.pop()


for i in range(1,m+n):
    try:
        if a_L[-1] >= b_L[-1]:
            dp[i] = dp[i-1] + b_L[-1] 
            b_L.pop()
        else:
            dp[i] = dp[i-1] + a_L[-1]
            a_L.pop()

    except:
        try:
            a_L[-1]
            dp[i] = dp[i-1] + a_L[-1]
        except:
            b_L[-1]
            dp[i] = dp[i-1] + b_L[-1]

for ans,i in enumerate(dp):
    if i > k:
        print(ans+1)
        exit()
print(ans+1)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
aes = list(map(int, input().split()))
bs = list(map(int, input().split()))
r_aes = [0]*n
for i in range(1,n):
    aes[i] += aes[i-1]
for i in range(1,m):
    bs[i] += bs[i-1]
cnt_a = 0
for i in range(n):
    if aes[i] > k:
        break
    cnt_a += 1
cnt_b = 0
res = cnt_a
#cnt = 5
while True:
    #print("cnt_a: "+str(cnt_a))
    #print("cnt_b: " + str(cnt_b))
    if cnt_b >= m or bs[cnt_b] > k:
        break
    if k-aes[cnt_a-1]>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_a+cnt_b)
        continue
    cnt_a -= 1
    if cnt_a < 0:
        break
    #if cnt<= 0:
        #break
print(res)

Submission status: Wrong Answer

Solution code:
N, M, K = (int(s) for s in input().rstrip().split())

A = [int(s) for s in input().rstrip().split()]
B = [int(s) for s in input().rstrip().split()]

i, j = (0, 0)
sum, count = (0, 0)

while sum <= K:
  if (i < N) and (j < M):
    if A[i] <= B[j]:
      sum += A[i]
      i += 1
      count += 1
    else:
      sum += B[j]
      j += 1
      count += 1
  elif i < N:
    sum += A[i]
    i += 1
    count += 1
  elif j < N:
    sum += B[j]
    j += 1
    count += 1
  else:
    count += 2
    break
    
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()))

answer = 0
cost = 0
counter_a = 0
counter_b = 0
end_a = False
end_b = False
while (True):
    if counter_a < len(A) and cost + A[counter_a] <= K:
        if (counter_b < len(B) and A[counter_a] < B[counter_b] ) or counter_b >= len(B):
            answer += 1
            cost += A[counter_a]
            counter_a += 1
            if counter_a >= len(A):
                end_a = True
    if counter_b < len(B) and cost + B[counter_b] <= K:
        if (counter_a < len(A) and B[counter_b] < A[counter_a]) or counter_a >= len(A):
            answer += 1
            cost += B[counter_b]
            counter_b += 1
            if counter_b >= len(B):
                end_b = True
    if (counter_a >= len(A) and counter_b >= len(B)) \
    or (counter_a >= len(A) and cost + B[counter_b] > K) \
    or (counter_b >= len(B) and cost + A[counter_a] > K):
        break
    if (counter_a < len(A) and counter_b < len(B) and cost + A[counter_a] > K and cost + B[counter_b] > K):
        break

print(answer)

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

ac=0
bc=0
h=0
c=0
al=len(a)-1
bl=len(b)-1

while True:
    if ac>al:
        h+=b[bc]
        bc+=1
        if bc>bl:
            c+=1
            break
    elif bc>bl:
        h+=a[ac]
        ac+=1
        if ac>al:
            c+=1
            break
    elif a[ac]<=b[bc]:
        h+=a[ac]
        ac+=1
    else:
        h+=b[bc]
        bc+=1
    if h>k:
        break
    else:
        c+=1
print(c)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_right


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    n, m, k = _ia()
    a = list(_ia())
    b = list(_ia())
    for i in range(1, n):
        a[i] += a[i-1]
    for i in range(1, m):
        b[i] += b[i-1]

    mx = 0
    for i, tot in enumerate(a):
        km = k - tot
        j = bisect_right(b, km)
        if j > 0:
            tot += b[j-1]
        if tot <= k:
            mx = max(mx, i+j+1)

    return mx


if __name__ == "__main__":
    print(main())

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

accum_a = [0] * (n + 1)
for i in range(1, n + 1):
    accum_a[i] = accum_a[i - 1] + a[i - 1]

accum_b = [0] * (m + 1)
for i in range(1, m + 1):
    accum_b[i] = accum_b[i - 1] + b[i - 1]

max_cnt = 0
for a_cnt in range(n + 1):
    if b[0] > k - accum_a[a_cnt]:
        continue

    b_cnt = bisect.bisect_right(accum_b, k - accum_a[a_cnt]) - 1

    sum_cnt = a_cnt + b_cnt
    if sum_cnt > max_cnt:
        max_cnt = sum_cnt

print(max_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()))
c = []
nn, mm = 0, 0
for _ in range(n+m):
    if a[nn] < b[mm]:
        c.append(a[nn])
        nn += 1
    else:
        c.append(b[mm])
        mm += 1
    if nn == n:
        c += b[mm:]
        break
    elif mm == m:
        c += a[nn:]
        break
ans = 0
for i in range(len(c)):
    k -= c[i]
    if k < 0:
        break
    else:
        ans += 1
print(ans)

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()))
    # dp[i] := i冊読み、Aをえらんだ回数がa回としたときの(a,最小時間)
    INF = float('inf')
    dp = [INF]*(N+M+1)
    dpa = [0]*(N+M+1)
    dp[0] = 0
    ans = 0
    for i in range(1,N+M+1):
        # i冊目がA
        if dpa[i-1]<N:
            if dp[i]>=dp[i-1]+A[dpa[i-1]]:
                dpa[i] += 1
                dp[i] = dp[i-1]+A[dpa[i-1]]

        # i冊目がB
        prevb = (i-1)-dpa[i-1]
        if prevb<M:
            dp[i] = min(dp[i], dp[i-1]+B[prevb])

        if dp[i]>K:
            print(i-1)
            return
    print(i)



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

ia = 0
ib = 0
book = 0
minute = 0

while minute < k:
    if a[ia] > b[ib]:
        minute += b[ib]
        book += 1
        if minute > k:
            book -= 1
        ib += 1
        if ib == m:
            ib = m - 1
    else:
        minute += a[ia]
        book += 1
        if minute > k:
            book -= 1
        ia += 1
        if ia == n:
            ia = n - 1


print(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()))
 
reading = 0
read_book = 0
i = 0
while reading <= K and i < max([N ,M]):
  if i+1 <= N:
    reading += A[i]
    if reading <= K:
      read_book += 1
    else:
      break
  if i+1 <= M:
    reading += B[i]
    if reading <= K:
      read_book += 1
    else:
      break
  i += 1
 
print(read_book)