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:
N = int(input())

M= int(input())


K=int(input())

arr = input()   # takes the whole line of n numbers
A= list(map(int,arr.split(' '))) 

i=0

arra = input()   # takes the whole line of n numbers
B= list(map(int,arra.split(' '))) 


if(A[0]<K):
    K=K-A[0]
    i=i+1
if(B[0]<K):
    K=K-B[0]
    i=i+1
if(A[1]<K):
    K=K-A[1]
    i=i+1

if(B[1]<K):
    K=K-B[1]
    i=i+1
print(i)

Submission status: Runtime Error

Solution code:
import sys
x=input()
x0=x.split()
n=int(x0[0])
m=int(x0[1])
k=int(x0[2])
a=input()
b=input()
a0=[int(i) for i in a.split()]
b0=[int(i) for i in b.split()]
a=0
b=0
al=list([0])
bl=list([0])
for i in range(n):
    a=a+a0[i]
    if a>k:
        break
    al.append(a)
for i in range(m):
    b=b+b0[i]
    if b>k:
        break
    bl.append(b)
p=0
for i in range(len(al)):
    for l in range(len(bl)):
        if al[-i-1]+bl[-l-1]<=k:
            q=len(al)+len(bl)-i-l-2
            if p<q:
                p=q
    if al[-i-2]+bl[-1]<k:
        break
print(p)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
time=0
ans=0
if sum(a)+sum(b)<=k:
    print(n+m)
    exit()
a=deque(a)
b=deque(b)
a0=a.popleft()
b0=b.popleft()
while True:
  if a0>=b0:
    time+=b0
    if time>k:
      print(ans)
      exit()
    else:
      ans+=1
      b0=b.popleft()
  else:
    time+=a0
    if time>k:
      print(ans)
      exit()
    else:
      ans+=1
      a0=a.popleft()

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

AA = list(reversed(A))
BB = list(reversed(B))

rest = K
ans = 0

def dfs(cost, aindex, bindex, score):

    if K == cost:
        # print("1:", score, cost)
        return score
    elif K < cost:
        # print("2:", score, cost)
        # return score - 2
        return -1
    
    if aindex == N and bindex == M:
        # print("3:", score, cost)
        return score

    if aindex >= N:
        return dfs(cost + B[bindex], aindex, bindex+1, score + 1)
    
    elif bindex >= M:
        return dfs(cost + A[aindex], aindex+1, bindex, score + 1)

    else:
        return max(dfs(cost + A[aindex], aindex+1, bindex, score + 1), dfs(cost + B[bindex], aindex, bindex + 1, score + 1))

print(max(dfs(A[0], 1, 0, 1), dfs(B[0], 0, 1, 1)))

Submission status: Runtime Error

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

for i in range(n):
    if a[i]>k:
        read=i
        break
if a[n-1]<k:
    read=n
time=a[read-1]
ans=read
start=read-1
if read==n:
    for i in range(m):
        if time+b[i]>k:
            read+=i
            ans=max(ans,read)
            read=-i
        if i==m-1:
            print(n+m)
            exit()
            
for i in range(read):
    read-=1
    time-=a[start-i]
    for j in range(m):
        if time+b[j]>k:
            read+=j
            ans=max(ans,read)
            read-=j
            break
        if j==m-1:
            read+=m
            print(max(ans,read))
            exit()
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
i = 0
j = 0
if(K-A[0] > 0 or K-B[0] > 0):
    while(K >= 0):
        if(i+j==N+M):
            print(N+M)
        if(A[i] <= B[j]):
            K -= A[i]
            i += 1
            time += 1
            if(i == N):
                A.append(10000000000)
        if(A[i] >=B[j]):
            K -= B[i]
            j += 1
            time += 1
            if(j == M):
                B.append(10000000000)
    print(time)
else:
    print(0)

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

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

if n > m:
    b += [1001001001]*(n-m)
else:
    a += [1001001001]*(m-n)

nlis = []
for i in range(n+m):
    if a[0] < b[0]:
        nlis.append(a[0])
        a.remove(a[0])
    else:
        nlis.append(b[0])
        b.remove(b[0])
t = 0
while k >= 0:
    k -= nlis[t]
    t += 1

print(t-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
Alist = list(map(int,input().split()))
Blist = list(map(int,input().split()))
count = 0

while True:
  if Alist[-1] < Blist[-1]:
    readtime = Alist[-1]
    Alist.pop(-1)
  else: 
    readtime = Blist[-1]
    Blist.pop(-1)
    
  if K < readtime:
    break
  else:
    count+=1
    K = K -readtime
print(count)

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

S = []

Sum = 0


for i in range(len(A)):
  K = K - A[i]
  if K > 0:
    Sum += 1
  else:
    K = K + A[i]

    break
S.append(Sum)

for j in range(len(B)):
  K = K - B[j]
  if K > 0:
    Sum += 1
  else:
    while(K<0):
      i = i - 1
      K = K + A[i]
      Sum = Sum - 1
    Sum += 1
  S.append(Sum)
print(max(S))

Submission status: Runtime Error

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

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

R = K
A_index = 0
B_index = 0
ans = 0
while N > 0:
    if R >= A[A_index]:
        R = R - A[A_index]
        ans += 1
        A_index += 1
        N -= 1
    else:
        break

while M > 0:
    if R >= B[B_index]:
        R = R - B[B_index]
        ans += 1
        B_index += 1
        M -= 1
    else:
        break

if N == 0 and M == 0:
    print(ans)
    sys.exit()

max = ans

while True:
    if A_index >= 0:
        R += A[A_index]
        A_index -= 1
        ans -= 1
        while B_index < len(B):
            if R >= B[B_index]:
                R -= B[B_index]
                B_index += 1
                ans += 1
            else:
                break
        
        if ans > max:
            max = ans
    else:
        break

print(max)

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

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
    else:
        minute += a[ia]
        book += 1
        if minute > k:
            book -= 1
        ia += 1

print(book)

Submission status: Runtime Error

Solution code:
# Python3 program to count distinct 
# divisors of a given number n 

def SieveOfEratosthenes(n, prime,primesquare, a): 
	# Create a boolean array "prime[0..n]" 
	# and initialize all entries it as 
	# true. A value in prime[i] will finally 
	# be false if i is not a prime, else true. 
	for i in range(2,n+1): 
		prime[i] = True

	# Create a boolean array "primesquare[0..n*n+1]" 
	# and initialize all entries it as false. 
	# A value in squareprime[i] will finally be 
	# true if i is square of prime, else false. 
	for i in range((n * n + 1)+1): 
		primesquare[i] = False

	# 1 is not a prime number 
	prime[1] = False

	p = 2
	while(p * p <= n): 
		# If prime[p] is not changed, 
		# then it is a prime 
		if (prime[p] == True): 
			# Update all multiples of p 
			i = p * 2
			while(i <= n): 
				prime[i] = False
				i += p 
		p+=1
	

	j = 0
	for p in range(2,n+1): 
		if (prime[p]==True): 
			# Storing primes in an array 
			a[j] = p 

			# Update value in primesquare[p*p], 
			# if p is prime. 
			primesquare[p * p] = True
			j+=1

# Function to count divisors 
def countDivisors(n): 
	# If number is 1, then it will 
	# have only 1 as a factor. So, 
	# total factors will be 1. 
	if (n == 1): 
		return 1

	prime = [False]*(n + 2) 
	primesquare = [False]*(n * n + 2) 
	
	# for storing primes upto n 
	a = [0]*n 

	# Calling SieveOfEratosthenes to 
	# store prime factors of n and to 
	# store square of prime factors of n 
	SieveOfEratosthenes(n, prime, primesquare, a) 

	# ans will contain total 
	# number of distinct divisors 
	ans = 1

	# Loop for counting factors of n 
	i=0
	while(1): 
		# a[i] is not less than cube root n 
		if(a[i] * a[i] * a[i] > n): 
			break

		# Calculating power of a[i] in n. 
		cnt = 1 # cnt is power of 
				# prime a[i] in n. 
		while (n % a[i] == 0): # if a[i] is a factor of n 
			n = n / a[i] 
			cnt = cnt + 1 # incrementing power 

		# Calculating number of divisors 
		# If n = a^p * b^q then total 
		# divisors of n are (p+1)*(q+1) 
		ans = ans * cnt 
		i+=1

	# if a[i] is greater than 
	# cube root of n 
	
	n=int(n) 
	# First case 
	if (prime[n]==True): 
		ans = ans * 2

	# Second case 
	elif (primesquare[n]==True): 
		ans = ans * 3

	# Third casse 
	elif (n != 1): 
		ans = ans * 4

	return ans # Total divisors 

n=int(input())
dp=[]
for i in range(1,n+1):
	dp.append(countDivisors(i))
ans=0
for i in range(len(dp)):
	ans+=(dp[i]*(i+1))
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()))

sum = 0
ans = 0


while sum < k:
    if k < a[0] and k < b[0] :
        break
    if len(a) == 0 and len(b) == 0:
        break
    elif len(a) == 0 :
        sum = sum + b.pop(0)
        ans += 1
    elif len(b) == 0:
        sum = sum + a.pop(0)
        ans += 1
    elif a[0] < b[0]:
        sum = sum + a.pop(0)
        ans += 1
    elif b[0] < a[0] :
        sum = sum + b.pop(0)
        ans += 1
if sum > k:
    ans -= 1

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

Acount = 0
Bcount = 0
Time = 0
score = 0
while True:
    if Acount == len(A):
        if Bcount == len(B):
            break
        else:
            Time = Time + B[Bcount]
            Bcount = Bcount + 1
            
    elif Bcount == len(A):
        if Acount == len(A):
            break
        else:
            Time = Time + A[Acount]
            Acount = Acount + 1
            
    else:
        if A[Acount] <= B[Bcount]:
            Time = Time + A[Acount]
            Acount = Acount + 1
            
        else:
            Time = Time + B[Bcount]
            Bcount = Bcount + 1
    
    
    if Time <= K:
        score = score + 1
    else:
        break
    
print(score)

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()))
temp=0
an,bn=0,0
ans=0
bf=[]
flag=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]:
            if flag:
                b=temp[:]+b[:]
                bn-=len(temp)
                temp=[]
                flag=0
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            if flag:
                a=temp[:]+a[:]
                an-=len(temp)
                temp=[]
                flag=0
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else:
            temp+=a[an]
            if temp>k: break
            bf.append(a[an])
            flag=1
            an+=1
            bn+=1
    ans+=1
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()))

max = 0

def dfs(i,j, time, count):
    global max
    if time > k:
        return 
    else:
        if count >= max:
            max = count
    if i >= n and j >= m:
        return 
    elif i >= n:
        dfs(i, j+1, time+b[j], count+1)
    elif j >= m:
        dfs(i+1, j, time+a[i], count+1)
    else:
        dfs(i, j+1, time+b[j], count+1)
        dfs(i+1, j, time+a[i], count+1)

dfs(0,0,0,0)
print(max)

Submission status: Runtime Error

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

data2cnt=0
data3cnt=0
total=0
count=0
maxint=n+m

for i in range(maxint):
  if len(data2)>=data2cnt+1 and int(data2[data2cnt]) < int(data3[data3cnt]):
      if k>=total+data2[data2cnt]:
        total=total+int(data2[data2cnt])
        data2cnt=data2cnt+1
  elif len(data3)>=data3cnt+1:
      if k>=total+data3[data3cnt]:
        total=total+int(data3[data3cnt])
        data3cnt=data3cnt+1
print(str(data2cnt+data3cnt))

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

# print(N,M,K)
# print(A)
# print(B)
sum_on = 100
count = 0
for i in range(10):
    box = []
# 合計がK超えたら終了
    if sum_on > K:
        break
    else:
        box.append(A[i])
        box.append(B[i])
        # print(box)
        #最小値をsum_onに足していく
        min_box = min(box)
        sum_on += min_box
        # print(sum_on)
        count += 1
        continue
    break
print(count)

Submission status: Runtime Error

Solution code:
def calculate(A,B, K, ai, bi, book_count, times):
	m = float('-INF')
	if sum(times) > K:
		return book_count-1
	if sum(times) >= K or (ai >= len(A) and bi >= len(B)):
		if sum(times) >= K:
			book_count-=1
		return book_count
	
	# select A
	if ai < len(A):
		c = calculate(A,B, K, ai+1, bi, book_count+1, times.copy() + [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.copy() + [B[bi]])

		# backtrack
		if c > m:
			m = c
	return m

N, M, K = map(int, input().split())
A, B = [], []
if N > 0:
	A = list(map(int, input().split()))
if M > 0:
	B = list(map(int, input().split()))

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

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 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 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = A[::-1]
B = B[::-1]
ans = 0
while (K > 0):
  if len(A) == 0 and len(B) == 0:
    break
  elif len(A) == 0 and K - B[-1] >= 0:
    K -= B.pop()
    ans += 1
  elif len(B) == 0 and K - A[-1] >= 0:
    K -= A.pop()
    ans += 1
  elif A[-1] >= B[-1] and K - B[-1] >= 0:
    ans += 1
    K -= B.pop()
  elif B[-1] >= A[-1] and K - A[-1] >= 0:
    ans += 1
    K -= A.pop()
  else:
    break
print(ans)

Submission status: Runtime Error

Solution code:
def survey(A, B, Ai, Bi, AMax, BMax, answer, reqTime, K):
    if(reqTime > K):
        return answer - 1
    else:
        if(Ai == AMax and Bi == BMax):
            return answer
        elif(Ai == AMax):
            return survey(A, B, Ai, Bi + 1, AMax, BMax, answer + 1, reqTime + B[Bi], K)
        elif(Bi == BMax):
            return survey(A, B, Ai + 1, Bi, AMax, BMax, answer + 1, reqTime + A[Ai], K)
        else:
            return max(survey(A, B, Ai + 1, Bi, AMax, BMax, answer + 1, reqTime + A[Ai], K), survey(A, B, Ai, Bi + 1, AMax, BMax, answer + 1, reqTime + B[Bi], K))
        
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    A = A
    B = B

    AMax = len(A)
    BMax = len(B)

    answer = 0
    reqTime = 0

    Ai = 0
    Bi = 0

    answer = survey(A, B, Ai, Bi, AMax, BMax, answer, reqTime, K)
    print(answer)

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

AA = list(reversed(A))
BB = list(reversed(B))

rest = K
ans = 0

def dfs(cost, aindex, bindex, score):

    if K == cost:
        # print("1:", score, cost)
        return score
    elif K < cost:
        # print("2:", score, cost)
        # return score - 2
        return -1
    
    if aindex == N and bindex == M:
        # print("3:", score, cost)
        return score

    if aindex >= N:
        return dfs(cost + B[bindex], aindex, bindex+1, score + 1)
    
    elif bindex >= M:
        return dfs(cost + A[aindex], aindex+1, bindex, score + 1)

    else:
        return max(dfs(cost + A[1], aindex+1, bindex, score + 1), dfs(cost + B[1], aindex, bindex + 1, score + 1))

print(max(dfs(A[0], 1, 0, 1), dfs(B[0], 0, 1, 1)))

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 lower_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 = 0
	for i in range(-1, n):
		if a[i] > k or i == -1:
			ans = max(ans, lower_case(b, k))
			continue
		ans = max(ans, i+1+lower_case(b, k-a[i]))
		log.print(i, k-a[i], lower_case(b, k-a[i]))
	print(ans)


main()

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()))
ai = -1
bi = -1
answer = 0
t_sum = 0
if sum(a) + sum(b) <= k:
    print(n + m)
elif sum(a) <= k:
    t_sum = sum(a)
    ai = n-1
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)
else:
    while True:
        ai += 1
        if t_sum + a[ai] > k:
            break
        t_sum += a[ai] 
    ai -= 1
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)

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

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

while k>0:
  if a[0] < b[0]:
    k -= a.pop(0)
  elif a[0] > b[0]:
    k -= b.pop(0)
  else:
    if a[1] < b[1]:
      k -= a.pop(0)
    else:
      k -= b.pop(0)
  cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque

n, m, k = [int(e) for e in input().split()]
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]

lena = len(a)
lenb = len(b)
s = deque()
t = deque()

while True:
    lens = len(s)
    if lens >= lena:
        break
    e = a[lens]
    if e > k:
        break
    s.append(e)
    k -= e

while True:
    lent = len(t)
    if lent >= lenb:
        break
    e = b[lent]
    if e > k:
        break
    t.append(e)
    k -= e

while True:
    lent = len(t)
    if lent >= lenb:
        break
    lens = len(s)
    if b[lent] >= s[lens - 1]:
        break
    k += s.pop()
    while True:
        lent = len(t)
        if lent >= lenb:
            break
        e = b[lent]
        if e > k:
              break
        t.append(e)
        k -= e
    

print(len(s) + len(t))

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から選んでみる
minutes_a = 0
count_a = 0
for i in range(N):
    if minutes_a + A[i] > K:
        break
    minutes_a += A[i]
    count_a += 1

minutes_b = 0
count_b = 0

if minutes_a < K:
    for i in range(M):
        if minutes_a + minutes_b + B[i] > K:
            break
        minutes_b += B[i]
        count_b += 1

for i in range(count_b, M):
    if minutes_a + minutes_b + B[i] > K:
        if count_a > 0 and minutes_a - A[count_a - 1] + minutes_b + B[i] <= K:
            minutes_a -= A[count_a - 1]
            count_a -= 1
            minutes_b += B[i]
            count_b += 1
            continue
        else:
            break
    minutes_b += B[i]
    count_b += 1

a_max = count_a + count_b

# 全部Bから選んでみる
minutes_b = 0
count_b = 0
for i in range(M):
    if minutes_b + B[i] > K:
        break
    minutes_b += B[i]
    count_b += 1

minutes_a = 0
count_a = 0

if minutes_b < K:
    for i in range(N):
        if minutes_b + minutes_a + A[i] > K:
            break
        minutes_a += A[i]
        count_a += 1

for i in range(count_a, N):
    if minutes_b + minutes_a + A[i] > K:
        if count_b > 0 and minutes_b - B[count_b - 1] + minutes_a + A[i] <= K:
            minutes_b -= B[count_a - 1]
            count_b -= 1
            minutes_a += A[i]
            count_a += 1
            continue
        else:
            break
    minutes_a += A[i]
    count_a += 1

print(max(a_max, count_a + count_b))

Submission status: Runtime Error

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

ans = 0
def D(a_list, b_list, K, count):
    global ans
    
    if len(a_list) == 0 and len(b_list) == 0:
        ans = max(ans, count)
    
    elif len(a_list) == 0:
        if K - b_list[0] < 0:
            ans = max(ans, count)
        else:
            D(a_list, b_list[1:], K-b_list[0], count+1)
        
    elif len(b_list) == 0:
        if K - a_list[0] < 0:
            ans = max(ans, count)
        else:
            D(a_list[1:], b_list, K-a_list[0], count+1)
    else:
        
        if K - a_list[0] < 0:
            ans = max(ans, count)
        else:
            D(a_list[1:], b_list, K-a_list[0], count+1)
            
        if K - b_list[0] < 0:
            ans = max(ans, count)  
        else:
            D(a_list, b_list[1:], K-b_list[0], count+1)
            
D(A_list, B_list, K, 0)
print(ans)

Submission status: Runtime Error

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

a_li = [0] + a_li
b_li = [0] + b_li

time = 0
ans = 0
a = 0
temp = 0

a_wa = [0] * (n+10)
b_wa = [0] * (m+10)

s = 0
for i in range(n+1):
    s += a_li[i]
    a_wa[i] = s

s = 0
for i in range(m+1):
    s += b_li[i]
    b_wa[i] = s
    

for i in range(n+1):
    for j in range(min(ans-i-1, m), m+1):
        time = a_wa[i] + b_wa[j]
        if time <= k:
            ans = max(ans, i+j)
        else:
            break
            
print(ans)

Submission status: Runtime Error

Solution code:
n, m ,k = map(int,input().split())
a_list = [int(a) for a in input().split()]
b_list = [int(b) for b in input().split()]
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 += b_list[b_num]      
        b_num += 1
    else:
        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: Runtime Error

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

s=0
ac=0
bc=0
max=0

for i in range(n):
  s=s+a[i]
  ac=ac+1
  if s > k:
    s=s-a[i]
    ac=ac-1
    break
for i in range(m):
  s=s+b[i]
  bc=bc+1
  if s > k:
    s=s-b[i]
    bc=bc-1
    break

max=ac+bc

flag=0
while flag==0 && ac!=0:
  ac=ac-1
  s=s-a[ac]
  while s <= k:
    bc=bc+1
    if bc > m:
      flag=1
      break
    s=s+b[bc-1]
  bc=bc-1
  if flag==0:
    s=s-b[bc-1]
  
  if max < ac+bc:
    max=ac+bc
    
print(max)

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()))
i,j=0,0
N-=1
M-=1
tim=0
book=0
a,b=True,True
while a or b:
	if i<=N:
		tim+=A[i]
		i+=1
		if tim>K:
			tim-=A[i]
			a=False
		else:
			book+=1
	else:
		a=False
	
	if j<=M:
		tim+=B[j]
		j+=1
		if tim>K:
			tim-=B[j]
			b=False
		else:
			book+=1
	else:
		b=False
print(book)

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):
        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:
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())))
count = 0
while True:
    if((len(A) == 0) and (len(B) == 0)):
        break

    if(len(A) == 0):
        if(K < B[0]):
            break
        else:
            count += 1
            K = K - B.popleft()
            continue

    if(len(B) == 0):
        if(K < A[0]):
            break
        else:
            count += 1
            K = K - A.popleft()
            continue

    if((A[0] > K) and (B[0] > K)):
        break
    if(A[0] > K):
        count += 1
        K = K - B.popleft()
        continue
    if(B[0] > K):
        count += 1
        K = K - A.popleft()
        continue
    if(A == B):
        count += 1
        K = K - A.popleft()

    if(A[0] == B[0]):
        it = min(len(A),len(B))
        for i in range(it):
            if(A[i] > B[i]):
                count += 1
                K = K - B.popleft()
                break
            if(A[i] < B[i]):
                count += 1
                K = K - A.popleft()
                break
    elif(A[0] < B[0]):
        count += 1
        K = K - A.popleft()
    else:
        count += 1
        K = K - B.popleft()

print(count)

Submission status: Runtime Error

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

a_sum = [0]
b_sum = [0]
for i in range(1, n+1):
  a_sum.append(a_sum[i-1] + an[i-1])
for j in range(1, m+1):
  b_sum.append(b_sum[j-1] + bn[j-1])

ans = len(bn)
idx = len(bn)
for j in range(m+1):
  if k - b_sum[j] < 0:
    ans = j - 1
    idx = j - 1
    break
for i in range(1, n+1):
  if a_sum[i] + b_sum[idx] > k:
    idx -= 1
  else:
    ans += 1

print(ans)