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()))
temp=0
an,bn=0,0
ans=0
while temp<=k:
    if an>=n and bn>=m: break
    if an>=n:
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        temp+=a[an]
        if temp>k: break
        an+=1
    else:
        if a[an]<b[bn]:
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else: #同じとき
            cnt=0
            while True:
                if an==n:
                    an-=cnt
                    ans-=1
                    break
                if bn==m:
                    bn-=cnt
                    ans-=1
                    break
                if a[an]<b[bn]:
                    temp+=a[an]
                    an+=1
                    bn-=cnt
                    break
                if a[an]>b[bn]:
                    temp+=b[bn]
                    bn+=1
                    an-=cnt
                    break
                temp+=a[an]
                if temp>k:
                    break
                an+=1
                bn+=1
                ans+=1
                cnt+=1
            if temp>k:
                break
    ans+=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_i = 0
b_i = 0

result = 0
time = 0

while True:
    if a_i >= n and b_i >= m:
        break
    elif a_i >= n:
        time += b[b_i]
        b_i += 1
        if time > k:
            break
        else:
            result += 1
            continue
    elif b_i >= m:
        time += a[a_i]
        a_i += 1
        if time > k:
            break
        else:
            result += 1
            continue
    else:
        if a[a_i] <= b[b_i]:
            time += a[a_i]
            a_i += 1
            if time > k:
                break
            else:
                result += 1
                continue
        else:
            time += b[b_i]
            b_i += 1
            if time > k:
                break
            else:
                result += 1
                continue
print(result)

Submission status: Wrong Answer

Solution code:
a=input().split(" ")
b=input().split(" ")
c=input().split(" ")
d=0
e=0
f=0
x=0
count=0
while int(a[2])>=e and int(a[2])>=int(b[0]):
    if d<int(a[0]) and x%2==0:
        e=e+int(b[d])
        d=d+1
        x=x+1
        count+=1
    elif f<int(a[1]) and x%2!=0:
        e=e+int(c[f])
        f=f+1
        x=x+1
        count=count+1
    else:
        break

if e>int(a[2]):
    count=count-1
 
print(count)

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    a = 0
    b = 0

    time = 0
    res = 0

    isAexist = True
    isBexist = True

    for i in range(N+M+1):
        if isAexist:
            tA = A[a]
        else:
            tA = 10 ** 9 + 1
        if isBexist:
            tB = B[b]
        else:
            tB = 10 ** 9 + 1
        if (tA <= tB) & isAexist:
            if time + tA <= K:
                time += tA
                a += 1
                res += 1
                if a == N:
                    isAexist = False
        elif (tA >= tB) & isBexist:
            if time + tB <= K:
                time += tB
                b += 1
                res += 1
                if b == M:
                    isBexist = False
        # print(res,time,a,b)
        if isAexist:
            if time + A[a] > K:
                isAexist = False
        if isBexist:
            if time + B[b] > K:
                isBexist = False
        if (not isAexist) & (not isBexist):
            break

    print(res)



main()

Submission status: Wrong Answer

Solution code:
import math
from collections import Counter

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

a, b = 0, 0
time = 0
cnt = 0

while True:
    if len(A) == 0 or len(B) == 0:
        l = A + B
        while len(l) != 0:
            time += l[0]
            if time <= K:
                cnt += 1
                del l[0]
            else:
                break
        break

    if A[0] < B[0]:
        time += A[0]
        del A[0]
        a += 1
    else:
        time += B[0]
        del B[0]
        b += 1
    
    if time <= K:
        cnt += 1
    else:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
from collections import deque
an= deque(map(int, input().split()))
bm= deque(map(int, input().split()))
ans =0
count = 0
while count <=k and len(an)+len(bm)>0:
    ans+=1
    if len(an)>0 and len(bm)>0:
        if an[0] >= bm[0]:
            mini = bm.popleft()
            count += mini
            if count >k:
                ans -=1

        else:
            mini = an.popleft()
            count += mini
            if count > k:
                ans -= 1


    elif len(bm)==0:
        mini = an.popleft()
        count += mini
        if count > k:
            ans -= 1
    elif len(an)==0:
        mini = bm.popleft()
        count += mini
        if count > k:
            ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
n = 0
m = 0
s = 0
while True:
    if n < len(A) and m < len(B):
        if A[n] < B[m]:
            s += A[n]
            ans += 1
            n += 1
        else:
            s += B[m]
            ans += 1
            m += 1
    elif n < len(A) and m == len(B):
        s += A[n]
        ans += 1
        n += 1    
    elif n == len(A) and m < len(B):
        s += B[m]
        ans += 1
        m += 1
    if s > K:
        print(ans-1)
        exit()
    elif s == K:
        print(ans)
        exit()
    elif n == len(A) and m == len(B):
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
import itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,M,K = LI()
A = LI()
B = LI()
accumulate_A = list(itertools.accumulate(A))
accumulate_B = list(itertools.accumulate(B))
countA,countB,_sumA,_sumB = 0,0,0,0
for i in range(N):
    if accumulate_A[i]>K:
        if i!=0:
            _sumA = accumulate_A[i-1]
        break
    countA += 1
for j in range(M):
    if accumulate_B[j]>K:
        if j!=0:
            _sumB = accumulate_B[i-1]
        break
    countB += 1
for y in accumulate_B:
    _sumA += y
    if _sumA>K:
        break
    countA += 1
for x in accumulate_A:
    _sumB += x
    if _sumB>K:
        break
    countB += 1
print(max(countA,countB))

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
minu=0
while 1:
  if i==n and j==m:
    break
  if i==n:
    minu+=b[j]
    j+=1
    continue
  if j==m:
    minu+=a[i]
    i+=1
    continue
  if minu+min(a[i],b[j]) >= k:
    break
  if a[i] < b[j]:
    minu+=a[i]
    i+=1
  elif a[i] > b[j]:
    minu+=b[j]
    j+=1
  elif i+1==n:
    minu+=b[j]
    j+=1
  elif j+1==m:
    minu+=a[i]
    i+=1
  elif a[i+1] < b[j+1]:
    minu+=a[i]
    i+=1
  elif a[i+1] > b[j+1]:
    minu+=b[j]
    j+=1
  elif minu+a[i]*2 < k:
    minu += a[i]*2
    i+=1
    j+=1
  else:
    minu+=a[i]
    i+=1
    break
print(i+j)

Submission status: Wrong Answer

Solution code:
n, m ,k = map(int,input().split())
a_list = [int(a) for a in input().split()][:n]
b_list = [int(b) for b in input().split()][:m]
time = 0
a_num = 0
b_num = 0
count = 0

while True:
    
    if (len(a_list) < a_num + 1) and (len(b_list) < b_num + 1): 
        break

    elif  (len(a_list) < a_num + 1) and (len(b_list) >= b_num + 1):
        time += b_list[b_num]      
        b_num += 1
     
    elif (len(a_list) >= a_num + 1) and (len(b_list) < b_num + 1):
        time += a_list[a_num]      
        a_num += 1
        
    elif (len(a_list) >= a_num + 1) and (len(b_list) >= b_num + 1):
        if a_list[a_num] < b_list[b_num]:
            time += a_list[a_num]
            a_num += 1
        else:
            time += b_list[b_num]      
            b_num += 1
    
    if time <= k:
        count += 1
    else:
        break

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()))
ans = 0
amax = 0
samax = 0

for i in range(n):
    if samax + a[i]<=k:
        amax += 1
        samax += a[i]
        
book = amax
ans = book
samax = sum(a[0:amax])
sbmax = 0

for j in range(m):
    sbmax += b[j]
    while samax + sbmax > k and amax >= 1:
        amax -= 1
        samax = samax - a[amax]
        book -= 1
    if samax + sbmax <= k:
        book += 1
        if book > ans:
            ans = book
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
I = list(map(int,input().split()))

N = I[0]
M = I[1]
K = I[2]

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

A_top = 0
B_top = 0
time = 0
result = 0

A_over = -1
B_over = -1

Asum = 0
Bsum = 0

for iter in range(len(A)):
    Asum += A[iter]
    if Asum > K:
        A_over = iter
        break

for iter in range(len(B)):
    Bsum += B[iter]
    if Bsum > K:
        B_over = iter
        break


while time < K:
    if A_over == A_top and B_over == B_top:
        break

    if B_top >= len(B) and A_top < len(A) and time + A[A_top] <= K:
        time += A[A_top]  
        A_top += 1
        continue
    elif B_top >= len(B) and time + A[A_top] > K:
        break

    if A_top >= len(A) and B_top < len(B) and time + B[B_top] <= K:
        time += B[B_top]
        B_top += 1
        continue
    elif A_top >= len(A) and time + B[B_top] > K:
        break
    
    if A[A_top] <= B[B_top] and time + A[A_top] <= K:
        
        time += A[A_top]
        A_top += 1
        continue
    if A[A_top] > B[B_top] and time + B[B_top] <= K:
        
        time += B[B_top]
        B_top += 1
        continue


    
    break

result = A_top + B_top
# if time > K and result > 0:
#     result -= 1

print(result)

Submission status: Wrong Answer

Solution code:


def main3():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    counter = 0
    ans = []

    if bb[0] >= aa[0]:
        if k < aa[0]:
            return counter
    if bb[0] <= aa[0]:
        if k < bb[0]:
            return counter

    if (sum(aa)+sum(bb)) <= k:
        return n+m

    for i in range(n+1):
        counter = i
        time_sum = sum(aa[:i])
        if time_sum > k:
            break
        elif time_sum == k:
            counter += 1
            ans.append(counter)
            break
        else:
            for j in range(m):
                time_sum += bb[j]
                if time_sum < k:
                    counter += 1
                elif time_sum == k:
                    counter += 1
                    ans.append(counter)
                    break
                else:
                    ans.append(counter)
                    break

    if len(ans) == 1:
        return ans[0]
    else:
        return max(ans)


if __name__ == '__main__':
    ans = main3()
    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()))

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

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

del BL[0]

ans = 0
total = 0
from bisect import bisect_right
for i in range(N+1):
    zan = K - AL[i]
    ind = bisect_right(BL, zan)
    ans = max(ans, i + ind)
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()))

'''
tmp = 0
count = 0
sum = 0
while 1:
    if n  == 0 and m == 0:
        break
    elif n == 0:
        tmp = b.pop(0)
        m -= 1
    elif m == 0:
        tmp = a.pop(0)
        n -= 1
    elif a[0] > b[0]:
        tmp = b.pop(0)
        m -= 1
    elif a[0] <= b[0]:
        tmp = a.pop(0)
        n -= 1

    sum += tmp
    if sum > k:
        break
    count += 1
print(count
'''
_n = 0
_m = 0
sum = 0
count = 0
while 1:
    if _n >= n and _m >= m:
        break
    elif _n >= n:
        k -= b[_m]
        _m += 1
    elif _m >= m:
        k -= a[_n]
        _n += 1
    elif a[_n] > b[_m]:
        k -= b[_m]
        _m += 1
    elif a[_n] <= b[_m]:
        k -= a[_n]
        _n += 1
    
    if (k < 0):
        break
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int,input().split())
A = [int(x) for x in input().split()]+[10**10]
B = [int(y) for y in input().split()]+[10**10]
a = 0
b = 0
hon = 0
C = []
D = []
x = M

for p in range(0,N):
    if p == 0:
        C += [int(A[0])]
    else:
        C += [int(A[p])+int(C[p-1])]
for q in range(0,M):
    if q == 0:
        D += [int(B[0])]
    else:
        D += [int(B[q])+int(D[q-1])]

for p in range(0,N):
    for q in reversed(range(0,x)):
        if C[p]+D[q] <= K:
            hon = max(p+q+2,hon)
            break
        else:
            x = q + 1
print(hon)





"""
while K >= A[a] or K >= B[b]:
    if K >= A[a] and A[a] <= B[b]:
        K -= A[a]
        a += 1
        hon += 1
    if K >= B[b] and B[b] <= A[a]:
        K -= B[b]
        b += 1
        hon += 1
print(hon)
"""

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

ind_A = 0
ind_B = 0
ans = 0
time = 0

for i in range(1, N+M+1):

    if (ind_B >= M and ind_A < N): # Bを読み終わったからAの本を読む
        time += A[ind_A]
        ind_A += 1
        ans += 1
    elif (ind_A >= N and ind_B < M) : # Aを読み割ったからBの本を読む
        time += B[ind_B]
        ind_B += 1
        ans += 1
    elif  A[ind_A] <= B[ind_B]: #Aの方が先に読み終わるからAの本を読む
        time += A[ind_A]
        ind_A += 1
        ans += 1
    elif A[ind_A] > B[ind_B]: #Bの方が先に読み終わるからBの本を読む
        time += B[ind_B]
        ind_B += 1
        ans += 1
    
    if i ==  N+M and time <= K:
        ans += 1
    
    if time > K:
        break
    
print(ans - 1)

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

num = 0

for i in range(n + m):
    if len(a_list) != 0:
        x = a_list[0]
    else:
        x = 1000000000000
    if len(b_list) != 0:
        y = b_list[0]
    else:
        y = 1000000000000

    if x < y:
        k -= x
        a_list.pop(0)
        num += 1
    else:
        k -= y
        b_list.pop(0)
        num += 1
    if k < 0:
        break

if len(a_list) == 0 and len(b_list) == 0:
    print(num)
else:
    print(num - 1)

Submission status: Wrong Answer

Solution code:
x,y,z=(input().split(" "))

A=input().split()
B=input().split()
A.append(z)
B.append(z)

count=0
i,j=0,0 
while (int(z)-int(A[i])>=0 or int(z)-int(B[j]) >=0) :
    if int(A[i]) < int(B[j]) :
        if int(z)-int(A[i]) >= 0 :
            z=int(z)-int(A[i])
            i+=1
            count+=1
            
            
    elif int(A[i]) > int (B[j]) :
        if int(z)-int(B[j]) >=0 :
            z=int(z)-int(B[j])
            j+=1
            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()))
cnt = 0
a_judge = True
b_judge = True

while a_judge and b_judge:
    if a==[]:
        a_judge = False
    elif b==[]:
        b_judge = False
    else:
        if any([k>=a[0], k>=b[0]]):
            if a[0] <= b[0]:
                k -= a[0]
                del a[0]
            else:
                k -= b[0]
                del b[0]
            cnt += 1
        else:
            break

if a_judge:
    while a!=[]:
        if k>=a[0]:
            k -= a[0]
            del a[0]
            cnt += 1
        else:
            break
elif b_judge:
    while b!=[]:
        if k>=b[0]:
            k -= b[0]
            del b[0]
            cnt += 1
        else:
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    n, m, k, *ab = map(int, open(0).read().split())
    a = ab[:n]
    b = ab[n:]

    if min(a[0], b[0]) > k:
        print(0)
        exit()

    if sum(a) + sum(b) <= k:
        print(n + m)
        exit()

    a += [float("Inf")] * (m + 1)
    b += [float("Inf")] * (n + 1)

    i = 0
    j = 0
    total = 0
    for _ in range(n + m + 1):
        x = a[i]
        y = b[j]
        if min(x, y) + total > k:
            break

        if x == y:
            if a[i + 1] < b[j + 1]:
                i += 1
            else:
                j += 1
            total += x

        elif x < y:
            total += x
            i += 1
        else:
            total += y
            j += 1

    print(i + j)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    a = 0
    b = 0

    time = 0
    res = 0

    isAexist = True
    isBexist = True

    for i in range(N+M+1):
        if isAexist:
            tA = A[a]
        else:
            tA = 10 ** 9 + 1
        if isBexist:
            tB = B[b]
        else:
            tB = 10 ** 9 + 1
        if (tA <= tB) & isAexist:
            if time + tA <= K:
                time += tA
                a += 1
                res += 1
                if a == N:
                    isAexist = False
        elif (tA >= tB) & isBexist:
            if time + tB <= K:
                time += tB
                b += 1
                res += 1
                if b == M:
                    isBexist = False
        print(res,time,a,b)
        if isAexist:
            if time + A[a] > K:
                isAexist = False
        if isBexist:
            if time + B[b] > K:
                isBexist = False
        if (not isAexist) & (not isBexist):
            break

    print(res)



main()

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
n=i[0]
m=i[1]
k=i[2]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a=b=0 #それぞれの読んだ数
cal=0 #読んだ時間
while True:
    if a!=n and b!=m:
        if A[a]<=B[b]:
            cal+=A[a]
            if cal>k:
                break
            a+=1
        elif B[b]<A[a]:
            cal+=B[b]
            if cal>k:
                break
            b+=1
    elif a==n and b==m:
        break
    elif a==n:
        cal+=B[b]
        if cal>k:
            break
        b+=1
    elif b==m:
        cal+=A[a]
        if cal>k:
            break
        a+=1
print(a+b)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
n, m, k = map(int, input().split())
s = deque(map(int, input().split())) 
t = deque(map(int, input().split())) 
time = 0
book = 0
for i in range(n + m + 2):
    if len(s) != 0 and len(t) != 0: 
        u = min(s[0], t[0])
    if len(s) != 0 and len(t) == 0:
        u = s[0]
    if len(s) == 0 and len(t) != 0:
        u = t[0]
    if len(s) == len(t) == 0:
        print(book)
        sys.exit()
    if time + u <= k:
        time += u
        if u in s:
            s.popleft()
        else:
            t.popleft()
        book += 1
    if time + u > k:
        print(book)
        sys.exit()

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(K + 1)
B.append(K + 1)
ia, ib = 0, 0
time = 0
flag = True
while flag:
    flag = False
    if A[ia] == B[ib] and time + A[ia] <= K:
        if A[ia + 1] <= B[ib + 1]:
            time += A[ia]
            ia += 1
            flag = True
        else:
            time += B[ib]
            ib += 1
            flag = True
    if A[ia] < B[ib] and time + A[ia] <= K:
        time += A[ia]
        ia += 1
        flag = True
    if B[ib] < A[ia] and time + B[ib] <= K:
        time += B[ib]
        ib += 1
        flag = True
print(ia + ib)

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
inter = 0
n = A+B
for i in range(n):
  if len(List_A)==0:
    inter+=List_B.pop(0)
  elif len(List_B)==0:
    inter+=List_A.pop(0)
  else:
    if List_A[0]>List_B[0]:
      inter+=List_B.pop(0)
    elif List_A[0] < List_B[0]:
      inter+=List_A.pop(0)
    else:
      k = min(len(List_A),len(List_B))
      for j in range(1,k):
        if List_B[j] > List_A[j]:
          inter+=List_A.pop(0)
          break
        elif List_A[j] > List_B[j]:
          inter+=List_B.pop(0)
          break
        else:
          if j == k-1:
            if len(List_A)>len(List_B):
              inter+=List_B.pop(0)
              break
            elif len(List_A)<=len(List_B):
              inter+=List_A.pop(0)
              break
          else:
            pass
  if K < inter:
    break
  else:
    res +=1
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import collections

def input():
    return sys.stdin.readline()

def greedy_reading(n, m, k, a_list, b_list):

    max_books = len(a_list) + len(b_list)

    k_remain = k
    count = 0
    for i in range(max_books):
        if len(a_list) > 0 and len(b_list) > 0:
            if a_list[0] <= b_list[0]:
                if k_remain < a_list[0]:
                    break
                k_remain -= a_list.popleft()
                count += 1
            else:
                if k_remain < b_list[0]:
                    break
                k_remain -= b_list.popleft()
                count += 1
        elif len(a_list) > 0:
            if k_remain < a_list[0]:
                break
            k_remain -= a_list.popleft()
            count += 1
        elif len(b_list) > 0:
            if k_remain < b_list[0]:
                break
            k_remain -= b_list.popleft()
            count += 1

        #print(k_remain)

    return count


def resolve():
    n, m, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    a_list = collections.deque(a_list)
    b_list = collections.deque(b_list)
    ans = greedy_reading(n, m, k, a_list, b_list)


    a_sum = 0
    b_sum = 0
    a_num = 0
    b_num = 0
    a_remain = 0
    b_remain = 0
    # Aから
    for i in range(len(a_list)):
        a_sum += a_list[i]
        if a_sum > k:
            a_sum -= a_list[i]
            a_num = i
            break

    if a_num == 0:
        a_num = len(a_list)

        a_remain = k - a_sum

    # B
    for i in range(len(b_list)):
        b_sum += b_list[i]
        if b_sum > k:
            b_sum -= b_list[i]
            b_num = i
            break

    if b_num == 0:
        b_num = len(b_list)
        b_remain = k - b_sum


    # Aオーバーしない
    a_b_sum = a_sum
    a_b_num = a_num

    for i in range(len(b_list)):
        a_b_sum += b_list[i]
        if a_b_sum > b_remain:
            a_b_sum -= b_list[i]
            a_b_num += i
            break
    # bオーバーしない
    b_a_sum = b_sum
    b_a_num = b_num
    for i in range(len(a_list)):
        b_a_sum += a_list[i]
        if b_a_num > a_remain:
            b_a_sum -= a_list[i]
            b_a_num += i
            break

    #print(a_b_num, b_a_num, ans)
    if ans < a_b_num and b_a_num <= a_b_num:
        print(a_b_num)
        exit()

    if ans < b_a_num and a_b_num <= b_a_num:
        print(b_a_num)
        exit()

    print(ans)











if __name__ == "__main__":
    resolve()

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



for i in range(n+m):
    if len(a)+len(b)==0:
        i += 1
        break;
    elif len(a)==0:
        sum += b.pop(0)
    elif len(b)==0:
        sum += a.pop(0)
    elif a[0]<b[0]:
        sum += a.pop(0)
    elif a[0]>b[0]:
        sum += b.pop(0)
    else:
        sum += a.pop(0)
    
    
    if sum > k:
        i=i-1
        break;
        
print(i+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import math
from decimal import Decimal, ROUND_DOWN


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (n, m, k) = [int(e) for e in lines[0].split(' ')]
    a = [int(e) for e in lines[1].split(' ')]
    b = [int(e) for e in lines[2].split(' ')]


    return (n, m, k, a, b)


def solve(n, m, k, a, b):

    i = 0
    j = 0
    t = 0
    cnt = 0
    while i < n or j < m:

        if i < n and j < m:
            if a[i] <= b[j]:
                t = t + a[i]
                i = i + 1
            else:
                t = t + b[j]
                j = j + 1
        elif i < n:
            t = t + a[i]
            i = i + 1
        elif j < m:
            t = t + b[j]
            j = j + 1

        if t <= k:
            cnt = cnt + 1
        else:
            break


    return cnt
    


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[47]:


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


# In[49]:


ans = 0
a_sum = 0
b_sum = 0
for a in range(N+1):
    if a != 0:
        a_sum += A[a-1]
    if a_sum > K:
        break
    for b in range(M+1):
        if b != 0:
            b_sum += B[b-1]
        if a_sum + b_sum > K:
            break
        ans = max(ans, a+b)
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
s = 0
ans = 0
ca = 0
for i in range(n):
  if k >= s + a[i]:
    s += a[i]
    ans += 1
    ca += 1
  else:
    break
cb = 0
for i in range(m):
  if k >= s + b[i]:
    s += b[i]
    ans += 1
    cb += 1
  else:
    break
if ans == n + m or ans == 0:
  print(ans)
else:
  l = []
  l.append(ans)
  while ca >= 0:
    s - a[ca - 1]
    ca -= 1
    ans -= 1
    if k >= s + b[cb - 1]:
      s += b[cb - 1]
      cb += 1
      ans += 1
      l.append(ans)
  print(max(l))

Submission status: Wrong Answer

Solution code:
import copy
N,M,K = map(int,input().split())
A = [0]*N
B = [0]*M
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = 0
b = 0

tmp = 0
ans = 0
# for i in range(200000):
#     if A[a] < B[b]:
#         if tmp <= K:
#             tmp += A[a]
#             ans += 1
#             a += 1
#     elif A[a] > B[b]:
#         if tmp <= K:
#             tmp += B[b]
#             ans += 1
#             b += 1
#     if a > len(A)-1 or b > len(B)-1:
#         break
fa = False
fb = False
flag = False
if sum(A)+sum(B)<=K:
    print(len(A)+len(B))
else:
    while len(A) > 0 or len(B) > 0:
        AA = copy.copy(A)
        BB = copy.copy(B)
        if len(AA) != 0:
            a = AA.pop(0)
            fa = True
        if len(BB) != 0:
            b = BB.pop(0)
            fb = True
        if fa and fb:
            if a<=b:
                tmp += a
                ans += 1
                A = AA
            else:
                tmp += b
                ans += 1
                B = BB
        else:
            if fa:
                tmp += a
                ans += 1
                A = AA
            if fb:
                tmp += b
                ans += 1
                B = BB

        if tmp > K:
            break
        if len(A) == 0 and len(B)==0:
            flag = True
        fa = False
        fb = False
        
        #print(tmp,A,B)
        
    if flag:
        print(ans)
    else:
        print(ans-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()))
t = 0
c = 0
if sum(a) + sum(b) <= k:
    print(n+m)
    quit()
for i in range(min(n, m)):
    if t + a[i]> k:
        break
    t += a[i]
    c += 1
    if t + b[i]> k:
        break
    t += b[i]
    c += 1
print(c)
if t < k:
    if max(n, m) == n:
        d = a
        e = b
    else:
        d = b
        e = a
    for i in range (len(e), len(d)-1):
        if t + d[i] > k:
            break
        t += d[i]
        print(d[i])
        c += 1
    print(c)
else:
    print(c)