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: Runtime Error

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

Submission status: Runtime Error

Solution code:

def main():
    n, m, k = list(map(int, input().split()))
    a_books = list(map(int, input().split()))
    b_books = list(map(int, input().split()))

    sumbooks = sum(a_books) + sum(b_books)
    count = len(a_books) + len(b_books)
    if a_books[0] > k and b_books[0] > k:
        print(0)
        exit()
    elif sumbooks <= k:
        print(len(a_books) + len(b_books))
        exit()

    while sumbooks > k:
        if len(a_books) and a_books[-1] >= b_books[-1]:
            sumbooks -= a_books.pop(-1)
        elif len(b_books) and b_books[-1] >= a_books[-1]:
            sumbooks -= b_books.pop(-1)

        if sumbooks <= k:
            break
        count -= 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

a.sort()
b.sort()

dp = [0]*m*n
s = 0
c = 0
for i in range(m*n):
    if s < k and i < len(a):
            if a[i] < k:
                s += a[i]
                c += 1
    if s<k and i < len(b):
        if b[i] < k:
            s += b[i]
            c += 1
print(c)

Submission status: Runtime Error

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

a = [False]*N
b = [False]*M

ans = 0
now = 0
bf = 0

for i in range(N):
    if now + A[i] > K:
        break
    now += A[i]
    ans += 1
    a[i] = True

for i in range(M):
    if now + B[i] > K:
        break
    now += B[i]
    ans += 1
    b[i] = True
    bf = i

if all(a) and all(b):
    print(ans)
    exit()

if not any(a) and not any(b):
    print(ans)
    exit()

n = K - now
ans2 = ans
for i in reversed(range(N)):
    if bf == M-1:
        break
    if a[i]:
        a[i] = False
        ans2 -= 1
        n += A[i]
        while True:
            if n - B[bf+1] < 0:
                break
            if n - B[bf+1] >= 0:
                n -= B[bf+1]
                b[bf+1] = True
                bf += 1
                ans2 += 1
    if ans2 > ans:
        ans = ans2
print(ans)

Submission status: Runtime Error

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

ai = 0
bi = 0

def read(ai, bi, count, rest):
    if rest <= 0:
        return count
    else:
        if ai >= N:
            a_c = count
        elif rest >= A[ai]:
            a_c = read(ai+1, bi, count+1, rest - A[ai])
        else:
            a_c = count

        if bi >= M:
            b_c = count
        elif rest >= B[bi]:
            b_c = read(ai, bi+1, count+1, rest - B[bi])
        else:
            b_c = count

        return max(a_c, b_c)

res = read(ai,bi,0,K)
print(res)

Submission status: Runtime Error

Solution code:

NIF = float('-INF')
def calculate(A,B, K, ai, bi, book_count, times):
	m = NIF
	if times > K:
		return book_count-1
	if times == K or (ai >= len(A) and bi >= len(B)):
		return book_count
	
	# select A
	if ai < len(A):
		c = calculate(A,B, K, ai+1, bi, book_count+1, times + A[ai])

		if c > m:
			m = c
		

	# select B
	if bi < len(B):
		c = calculate(A,B, K, ai, bi+1, book_count+1, times + B[bi])

		# backtrack
		if c > m:
			m = c
	return max(m, book_count)

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

print(calculate(A,B, K, 0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
n,m,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
from collections import deque
p=deque(a)
q=deque(b)

import sys
time=0
ans=0
while(1):
    if len(p)==0 and len(q)==0:
        print(ans)
        sys.exit()
    if len(p)>0 and len(q)>0:
        if p[0]>q[0]:
            time=time+q.popleft()
            if time>k:
                print(ans)
                sys.exit()
            else:
                ans=ans+1
        elif p[0]<q[0]:
            time=time+p.popleft()
            if time>k:
                print(ans)
                sys.exit()
            else:
                ans=ans+1
        else:
            if p[1]>=q[1]:
                la=q.popleft()
                if la==1000000000:
                    print(ans)
                    sys.exit()
                time=time+la
                if time>k:
                    print(ans)
                    sys.exit()
                else:
                    ans=ans+1
            else:
                la=p.popleft()
                if la==1000000000:
                    print(ans)
                    sys.exit()
                time=time+la
                if time>k:
                    print(ans)
                    sys.exit()
                else:
                    ans=ans+1
    elif len(p)==0:
        la=q.popleft()
        if la==1000000000:
            print(ans)
            sys.exit()
        time=time+la
        if time>k:
            print(ans)
            sys.exit()
        else:
            ans=ans+1
    elif len(q)==0:
        la=p.popleft()
        if la==1000000000:
            print(ans)
            sys.exit()
        time=time+la
        if time>k:
            print(ans)
            sys.exit()
        else:
            ans=ans+1

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(100000000000)
a.append(100000000000)
i, j, ans = 0, 0, 0
while k > 0:
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k >= 0:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
# c

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum = 0
num = 0
lim = max(n, m)

for i in range(lim):
    if i <= min(n-1, m-1):
        if a[i] <= b[i]:
            if (sum + a[i]) <= k:
                sum += a[i]
                num += 1
            else:
                break
            if sum + b[i] <= k:
                sum += b[i]
                num += 1
            else:
                break
        else:
            if sum + b[i] <= k:
                sum += b[i]
                num += 1
            else:
                break
            if sum + a[i] <= k:
                sum += a[i]
                num += 1
            else:
                break
    else:
        if sum + b[i] <= k:
            sum += b[i]
            num += 1
        else:
            break

print(num)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
nl= [int(i) for i in input().split()]
ml = [int(i) for i in input().split()]


s = 0;
r = 0;

ni = 0
mi = 0

i=0

sabun = 0

while i < n+m:
    sabun = 0
    if (ni > n) :
        sabun = ml[mi]
        if  (sabun + s> k):
            break
        
        r+=1
        s+=ml[mi]
        mi +=1
        i+=1
        continue

    else:
        sabun = ml[mi]
        if  (sabun + s > k ):
            break
        
        r+=1
        s+=nl[ni]
        ni +=1
        i+=1
        continue
        
    if (nl[ni]> ml[mi]) :
        sabun = ml[mi]
        if  (sabun + s > k ):
            break
        
        r+=1
        s+=ml[mi]
        mi +=1
    else :
        sabun = nl[ni]
        if  (sabun + s > k ):
            break
        
        r+=1
        s+=nl[ni]
        ni+=1

    if sabun > k :
        r -= 1
        break
    



print(r)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(float('Inf'))
b.append(float('Inf'))
c = 0
while a[0] <= k or b[0] <= k:
    if a[0] < b[0]:
        k -= a.pop(0)
        c += 1
    if a[0] >= b[0]:
        k -= b.pop(0)
        c += 1
print(c)

Submission status: Runtime Error

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

s = abs(len(a) - len(b))
for i in range(s):
    a.append(100100100)
    b.append(100100100)

time = 0
count = 0

while time < k:
    if time + min(a[0], b[0]) > k:
        break
    time += min(a[0], b[0])
    count += 1
    if a[0] <= b[0]:
        del a[0]
    else:
        del b[0] 

#print(time)
print(count)

Submission status: Runtime Error

Solution code:
from decimal import *
n,m,k=map(Decimal,input().split())
a=list(map(Decimal,input().split()))
b=list(map(Decimal,input().split()))

a.append(10**9+1)
b.append(10**9+1)
k/=10**9
i,j,ans=0,0,0
while k>=0:
    if a[i]>10**9:
        k-=Decimal(b[j]/(10**9))
        j+=1
    elif b[i]>10**9:
        k-=Decimal(a[i]/(10**9))
        i+=1
    elif a[i]>b[j]:
        k-=Decimal(b[j]/(10**9))
        j+=1
    else:
        k-=Decimal(a[i]/(10**9))
        i+=1
    if k<0:
        break
    ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
from bisect import bisect_left
def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

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

def SI():
	return sys.stdin.readline().strip()
def bs(a,n,x): 
    i = bisect_left(a, x) 
    if i<n and a[i] == x:
    	return i
    elif i:
    	return (i-1) 
    else: 
        return -1
n,m,k = MI()
a = LI()
b = LI()
for i in range(1,n):
	a[i]+=a[i-1]
for i in range(1,m):
	b[i]+=b[i-1]
ans = max(bs(a,n,k),bs(b,m,k))+1
for i in range(n):
	if k<a[i]:
		break
	else:
		temp = i+1+bs(b,m,k-a[i])+1
		ans = max(ans,temp)
for j in range(m):
	if k<b[i]:
		break
	else:
		temp = i+1+bs(a,n,k-b[i])+1
		ans = max(ans,temp)
print(ans)

Submission status: Runtime Error

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

s = 0
for i in range(len(a)):
    if s + a[i] <= k:
        s += a[i]
        A += 1
        #print(s)
    else:
        break
S = k - s
ans = 0
ans += A
#A -= 1
#print(A)

q = 0
for i in range(A+1):
    if S < b[B]:
        S += a[A-i]
        A -= 1
    q += 1
    for j in range(B,len(b)):
        if S - b[j] >= 0:
            S -= b[j]
            B += 1
            if A+B >= ans:
                ans = A+B
        else:
            break
            
print(ans)

Submission status: Runtime Error

Solution code:

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

time = 0
cnt = 0
b = []

for i in B:
    if i+time>K:
        break
    time += i
    cnt += 1
    b.append(i)

b.reverse()
# print(b)
# print(cnt)
bi = 0
for i in range(N):
    if time+A[i] <= K:
        cnt += 1
        time += A[i]
    else:
        if len(b)-1 >= bi:
            if A[i] <= b[bi]:
                time -= b[bi]-A[i]
                bi += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)

time2 = 0
cnt2 = 0
a = []

for i in A:
    if i+time>K:
        break
    time2 += i
    cnt2 += 1
    a.append(i)

a.reverse()
# print(b)
# print(cnt)
ai = 0
for i in range(N):
    if time2+B[i] <= K:
        cnt2 += 1
        time2 += B[i]
    else:
        if len(a)-1 >= ai:
            if B[i] <= a[ai]:
                time2 -= a[ai]-B[i]
                ai += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)


print(max(cnt, cnt2))

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = A + B
output = 0
a = 0
num = 0
if C[0] > K:
    print(0)
elif sum[C] <= K :
    print(len(C))
else:
    for i in range(N+M):
        num += C[a]
        if num <= K:
            output += 1
            num += C[a+len(A)]
            if num <= K:
                output += 1
                pass
            else:
                print(output)
                break
            a += 1

Submission status: Runtime Error

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 
time = 0 

while time <= k:
#    print('time =', time)
#    print('a =', a)
#    print('b =', b)

    if len(a) > 0:
        if a[0] <= b[0]:
            if len(a) > 0:
                time += a[0]
                ans += 1
                a.pop(0)
            elif len(b) > 0:
                time += b[0]
                ans += 1
                b.pop(0)
        else:
            if len(b) > 0:
                time += b[0]
                ans += 1
                b.pop(0)
            elif len(a) > 0:
                time += a[0]
                ans += 1
                a.pop(0)

    elif len(b) > 0:
        time += b[0]
        ans += 1
        b.pop(0)

    if len(a) == 0 and len(b) == 0:
        break

if time == k:
    print(ans)
else:
    print(ans-1)

Submission status: Runtime Error

Solution code:
#172C

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

if K>=sum(A)+sum(B):
    print(N+M)
    exit()
elif K<min(A[0],B[0]):
    print(0)
    exit()
else:
    for i in range(N):
        sum_a=sum(A[:i+1])
        a=A
        b=B
        m=0
        while K>sum_a+b[0] and len(b)>=1:
            sum_a+=b[0]
            b.pop(0)
            m+=1
        if i+1+m>ans:
            ans=i+1+m
print(ans)

Submission status: Runtime Error

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

import sys

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

count_a=0
count_b=0
total=0

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



while(0<=k) :
    
    try:
        ai=a[count_a]
    except :
        ai=None
        
    try:
        bi=b[count_b]
    except :
        bi=None
    
    # print("total:",total)
    # print(count_a,":",ai)
    # print(count_b,":",bi)
    
    if ai is not None and bi is not None :
        if ai < bi :
            k -= ai 
            count_a+=1
        elif bi<ai:
            k-=bi
            count_b+=1
        
        else :
            if count_a > count_b :
                k-= a 
                count_a+=1
            else :
                k-=b 
                count_b+=1
            
        total+=1
    
    
    elif ai is not None and bi is None :
        
        k-= ai 
        count_a+=1
        
        total+=1
    
    elif ai is None and bi is not None :
        k-=bi
        count_b+=1
        total+=1
        
    else :
        break

if k < 0 :
    print(total-1)
else :
    print(total)

Submission status: Runtime Error

Solution code:
n,m,k = input().split()
N = int(n)
M = int(m)
K = int(k)
a_t = input().split()
b_t = input().split()
A_Times = list()
B_Times = list()
for i in range(N):
  A_Times.append(int(a_t[i]))
for i in range(M):
  B_Times.append(int(b_t[i]))
total_time = 0
num_books = 0
while total_time<=K:
  if len(A_Times)>0 and len(B_Times)>0:
    min_time = 0
    if A_Times[0]<B_Times[0]:
      min_time = A_Times[0]
      del A_Times[0]
    else: 
      min_time = B_Times[0]
      del B_Times[0]
    if total_time+min_time<=K:
      total_time += min_time
      num_books+=1
	else:    break
  elif len(A_Times)<=0:
    min_time = B_Times[0]
	del B_Times[0]
	if total_time+min_time<=K:
      total_time += min_time
      num_books+=1
	else:    break
  else:
    min_time = A_Times[0]
    del A_Times[0]
    if total_time+min_time<=K:
      total_time += min_time
      num_books+=1
	else:    break
print(num_books)

Submission status: Runtime Error

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

    time = 0
    i = 0
    while (time + A[i] <= K):
        time += A[i]
        i += 1
        if i == N:
            break

    i2 = i-1#i2はAの一番下の変数を指すポインタ
    h = 0
    while (time + B[h] <= K):
        time += B[h]
        h += 1
        i += 1
        if h == M:
            break
    max = i
    #iは冊数

    i3 = i#今後比べていくときの合計冊数
    #print(time,i)
    if h == M:
        return max

    for j in range(i2+1):
        if i2 < 0:
            return max
        time -= A[i2]
        i3 -= 1
        while (time + B[h] <= K):
            time += B[h]
            h += 1
            i3 += 1
            if h == M:
                break
        #print(time,i3)

        if i3 > max:
            max = i3

        i2 -= 1

    return max

print(main())

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
memo = [[None] * (M + 1) for _ in range(N + 1)]
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
K = [[K - a - b for b in B] for a in A]
def solution(i=0, j=0):
    if i > N or j > M:
        return -1
    if memo[i][j] is not None:
        return memo[i][j]
    if K[i][j] == 0:
        memo[i][j] = 0
        return 0
    elif K[i][j] < 0:
        memo[i][j] = -1
        return -1
    memo[i][j] = 1 + max(
        solution(i + 1, j),
        solution(i, j + 1))
    return memo[i][j]
print(solution())

Submission status: Runtime Error

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

costA = list(map(int,input().split()))
costB = list(map(int,input().split()))

time = 0
cnt = 0
while(time<=k):
  
  if(len(costA) == 0 and len(costB)!=0):
    time+=costB.pop(0)
  elif(len(costB)==0 and len(costA) != 0):
    time += costA.pop(0)
  elif(len(costA) == 0 and len(costB)==0):
    break
  else:
    if(costA[0] > costB[0]):
      time+=costB.pop(0)
    elif(costA[0] == costB[0]):
      if(costA[1] > costB[1]):
         time += costB.pop(0)
      else:
         time += costA.pop(0)
    else:
      time+=costA.pop(0)
  if(time>k):
    break;
  cnt+=1
print(cnt)

Submission status: Runtime Error

Solution code:
5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Submission status: Runtime Error

Solution code:

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

time = 0
cnt = 0
b = []

for i in B:
    if i+time>K:
        break
    time += i
    cnt += 1
    b.append(i)

b.reverse()
# print(b)
# print(cnt)
bi = 0
for i in range(N):
    if time+A[i] <= K:
        cnt += 1
        time += A[i]
    else:
        if len(b)-1 >= bi:
            if A[i] <= b[bi]:
                time -= b[bi]-A[i]
                bi += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)

time2 = 0
cnt2 = 0
a = []

for i in A:
    if i+time>K:
        break
    time2 += i
    cnt2 += 1
    a.append(i)

a.reverse()
# print(b)
# print(cnt)
ai = 0
for i in range(N):
    if time2+B[i] <= K:
        cnt2 += 1
        time2 += B[i]
    else:
        if len(a)-1 >= ai:
            if B[i] <= a[ai]:
                time2 -= a[ai]-B[i]
                ai += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)


print(max((cnt, cnt2))

'''
5 3 8
5 0 0 1 1
1 2 1000
'''

Submission status: Runtime Error

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

ruiA, ruiB = [0], [0]

for i in range(N):
    ruiA.append(ruiA[i] + A[i])

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

tmpa, tmpb = 0, 0
tmp = 0
t = False
for i in range(N + M + 1):
    if i <= N and ruiA[i] <= K:
        continue
    if i <= M and ruiB[i] <= K:
        continue
    if ruiA[N] + ruiB[i - N] <= K:
        continue
    if ruiA[i - M] + ruiB[M] <= K:
        continue

    t = False
    for j in range(0, i + 1):
        if j > M or i - j > N:
            continue
        if ruiA[i - j] + ruiB[j] <= K:
            t = True
            break

    if not t:
        print(i - 1)
        exit()

print(N + M)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
booka=list(map(int,input().split()))
bookb=list(map(int,input().split()))
answer=0
while len(booka)*len(bookb)>0 and K-booka[0]>=0 and K-bookb[0]>=0:
  if booka[0]<bookb[0]:
    K-=booka[0]
    answer+=1
    booka=booka.pop(0)
  elif booka[0]==bookb[0]:
    n=1
    while booka[n]=bookb[n]=booka[0]:
      n+=1
      if n>len(booka)
        break 
      listp=[2*len(booka),math.floor(K/booka[0])]
      answer+=max(listp)
      print(answer)
    if booka[n]<bookb[n]:
      K-=booka[0] 
      answer+=1
      booka=booka.pop(0)
    else:
      K-=bookb[0]
      answer+=1
      bookb=bookb.pop(0)
  else:
    K-=bookb[0]
    answer+=1
    bookb=bookb.pop(0)

print(answer)

Submission status: Runtime Error

Solution code:
n,m,k = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
x=0
t=0
s=0
a=0
b=0
r=0
if n>m:
    r=m
else:
    r=n
q=0
for i in range (r):
    q+=A[i]+B[i]
for i in range (n):
    s+=A[i]
    a+=A[i]
for i in range(m):
    s+=B[i]
    b+=B[i]
if n==m:
    if s<=k:
        print(n+m)
    else:
        if min(A[0],B[0])==k:
            print(1)
        elif max(A)<=min(B):
            for i in range(n):
                a+=B[i]
                if a>k:
                    print(n+i)
                    break
        elif min(A)>=max(B):
            for i in range(n):
                b+=a[i]
                if b>k:
                    print(n+i)
                    break
        else:
            for i in range (n):
                t=t+1
                if A==[]:
                    x+=B[0]
                elif B==[]:
                    x+=A[0]
                else:    
                    x+=min(A[0],B[0])
                    if A[0]<=B[0]:
                        A.pop(0)
                    else:
                        B.pop(0)
                if x>k:
                    print(t-1)
                    break
        
else:
    if s<=k:
        print(n+m)
    elif q<k:
        if r==n:
            for i in range(r,m+1):
                q+=B[i]
                if q>k:
                    print(i+r)
                    break
        elif r==m:
            for i in range(r,n+1):
                q+=A[i]
                if q>k:
                    print(i+r)
                    break
    else:
        for i in range (max(n,m)):
            t=t+1
            if A==[]:
                x+=B[0]
            elif B==[]:
                x+=A[0]
            else:    
                x+=min(A[0],B[0])
                if A[0]<=B[0]:
                    A.pop(0)
                else:
                    B.pop(0)
            if x>k:
                print(t-1)
                break

Submission status: Runtime Error

Solution code:
n, m, k = [int(s) for s in input().split(" ")]
alist = [int(s) for s in input().split(" ")]
blist = [int(s) for s in input().split(" ")]

def max_inds(aind, bind, cur_time, k):
  if aind >= len(alist) and bind >= len(blist):
    return aind + bind
  elif aind < len(alist) and bind >= len(blist):
    return max_inds(aind + 1, bind, cur_time + alist[aind], k)
  elif aind >= len(alist) and bind < len(blist):
    return max_inds(aind, bind + 1, cur_time + blist[bind], k)

  if cur_time + alist[aind] > k and cur_time + blist[bind] > k:
    return aind + bind
  else:
    if cur_time + alist[aind] <= k:
      t1 = max_inds(aind + 1, bind, cur_time + alist[aind], k)
    else:
      t1 = 0
    if cur_time + blist[bind] <= k:
      t2 = max_inds(aind, bind + 1, cur_time + blist[bind], k)
    else:
      t2 = 0
    return max(t1, t2)

print(max_inds(0, 0, 0, k))

Submission status: Runtime Error

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

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)

def upper_case(lst, key):
	ng = -1
	ok = len(lst)
	while (abs(ok - ng) > 1):
		mid = (ok + ng) // 2
		if lst[mid] > key:
			ok = mid
		else:
			ng = mid
	return ok




def main():
	log = Logger(0)
	n, m, k = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	for i in range(1, n):
		a[i] += a[i-1]
	for i in range(1, m):
		b[i] += a[i-1]
	log.print(a, b)
	ans = upper_case(b, k)
	for i in range(n):
		if a[i] > k:
			continue
		ans = max(ans, i+1+upper_case(b, k-a[i]))
		log.print(i, k-a[i], upper_case(b, k-a[i]))
	print(ans)


main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1000000007;
const ll INF = 1061109567;
const double EPS = 1e-10;
int main(){

    ll N,M,K;
    cin>>N>>M>>K;
    ll a[N],b[M];
    ll a_c=0,b_c=0,c=0;
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    for(int i=0;i<M;i++){
        cin>>b[i];
    }
    ll time=0;
    while(time<=K||a_c!=N){
        if(time+a[a_c]<=K){
            time+=a[a_c];
            c++;
            a_c++;          
        }else{
            break;
        }
    }
    while(time<=K||b_c!=N){
        if(time+b[b_c]<=K){
            time+=b[b_c];
            c++;
            b_c++;          
        }else{
            break;
        }
    }
    while(true){
        ll buf=time;
        time-=a[a_c];
        c--;
        a_c--;
        if(time+b[b_c]<=buf&&b_c!=N){
            time+=b[b_c];
            c++;
            b_c++;            
        }else{
            time+=a[a_c];
            c++;
            a_c++;
            break;
        }
    }
    while(time<=K||b_c!=N){
        if(time+b[b_c]<=K){
            time+=b[b_c];
            c++;
            b_c++;          
        }else{
            break;
        }
    }
    cout<<c<<endl;
    return 0;
}

Submission status: Runtime Error

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

n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.reverse()
b.reverse()
ans = 0

for i in range(n + m):
    
    if a[-1] > b[-1]:
        cost = b.pop(-1)
    elif a[-1] < b[-1]:
        cost = a.pop(-1)
    else:
        if a[-2] > b[-2]:
            cost = b.pop(-1)
        else:
            cost = a.pop(-1)
    
    k -= cost
    if k < 0:
        break
    if len(a) == 0:
        a.append(float('inf'))
    if len(b) == 0:
        b.append(float('inf'))
    
    ans += 1

print(ans)

Submission status: Runtime Error

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

def fun(As,Bs,time_rem):
    if time_rem < 0:
        return -1
    if As == [] and Bs == []:
        return 0
    elif As == []:
        return 1 + fun([],Bs[1:],time_rem-Bs[0])
    elif Bs == []:
        return 1 + fun(As[1:],[],time_rem-As[0])
    else:
        return 1 + max(fun(As[1:],Bs,time_rem-As[0]),fun(As,Bs[1:],time_rem-Bs[0]))

print(fun(As,Bs,k))

Submission status: Runtime Error

Solution code:
li = list(map(int, input().split()))
lia = list(map(int, input().split()))
lib = list(map(int, input().split()))
time = 0
re = 0
while li[2] >=  time:
    if lia[0] >= lib[0]:
        time += lib[0]
        lib.pop(0)
        re += 1
    else:
        time += lia[0]
        lia.pop(0)
        re += 1
print(re)