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, m, k = [int(_) for _ in input().split()]

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

b.append(k)
cnt = 0
ans1, ans2 = 0, 0
for i in range(n-1):
    a[i+1] += a[i]
    if a[i] <= k:
        ans1 = i

for j in range(m):
    b[j+1] += b[j]
    if b[i] <= k:
        ans2 = j
# print(a, b)
ans = max(ans1, ans2)
for i in range(n):
    nokori = k-a[i]
    if nokori < 0:
        break
    if b[0] > nokori:
        ans = max(i+1, ans)
        continue

    r, l = 0, m

    while(l-r > 1):
        if b[(r+l)//2] <= nokori:
            r = (r+l)//2
        else:
            l = (r+l)//2
        # print(l, r)
    ans = max(i+l+1, ans)
    # print(i, r, l, nokori)
print(ans)

Submission status: Runtime Error

Solution code:
# Problem C - Tsundoku

# input
N, M, K = map(int, input().split())
a_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))

# initialization
a_i = -1
b_i = -1
max_count = 0
cur_count = 0

# dfs search
def dfs(a_i, b_i, cur_coin):
    global max_count
    global cur_count
    global K

    tmp_count1 = 0
    tmp_count2 = 0

    if a_i>=N or b_i>=M:
        if cur_count>max_count:
            max_count = cur_count

    # right check
    if a_i+1<N:
        if K - a_nums[a_i+1]>=0:
            K -= a_nums[a_i + 1]
            cur_coin += a_nums[a_i + 1]
            cur_count += 1
            dfs(a_i+1, b_i, cur_coin)
            cur_count -= 1
            cur_coin -= a_nums[a_i + 1]
            K += a_nums[a_i + 1]
        else:
            if cur_count>max_count:
                max_count = cur_count
    else:
        if cur_count>max_count:
            max_count = cur_count

    # left check
    if b_i + 1<M:
        if K - b_nums[b_i + 1]>=0:
            K -=b_nums[b_i + 1]
            cur_coin += b_nums[b_i + 1]
            cur_count += 1
            dfs(a_i, b_i+1, cur_coin)
            cur_count -= 1
            cur_coin -= b_nums[b_i + 1]
            K += b_nums[b_i + 1]
        else:
            if cur_count>max_count:
                max_count = cur_count
    else:
        if cur_count>max_count:
            max_count = cur_count

dfs(a_i, b_i, 0)

# output
print(max_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()))
def sigma(func, frm, to):
    result = 0;
    for i in range(frm, to+1):
        result += func[i-1]
    return result

i=0
while i<N:
    j=0
    while j<M:
        if sigma(A,1,i)+sigma(B,1,j)>K:
            answer=i+j
            break
        j+=1
    if sigma(A,1,i)+sigma(B,1,j)>K:
        answer=i+j
        break
    i+=1
print(answer)

Submission status: Runtime Error

Solution code:
import os
import sys
from collections import defaultdict, Counter, deque
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    
    n,m,k = map(int, input().split())
    n_list = list(map(int, input().split()))
    m_list = list(map(int, input().split()))
    i,j,count = 0,0,0

    while(k>0):
        if count == n+m:
            break
        if n_list[i] < m_list[j]:
            k -= n_list[i]
            count += 1
            i += 1
        else:
            k -= m_list[j]
            count += 1
            j += 1
        
        if k < 0:
            count -= 1
            break

    print(count)



            

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

def calc(k,i,j):
    c1 = 0
    c2 = 0
    global N,M,K,A,B
    if N-1>=i and A[i]+k <= K:
        c1 = 1+calc(A[i]+k,i+1,j)
    if M-1>=j and B[i]+k <= K:
        c2 = 1+calc(B[j]+k,i,j+1)
    return max(c1,c2)
count = calc(0,0,0)
print(count)

Submission status: Runtime Error

Solution code:
line = input().split()
n, m, k = int(line[0]), int(line[1]), int(line[2])
line = input().split()
a = [None] * n
for i in range(n):
    a[i] = int(line[i])
line = input().split()
b = [None] * m
for i in range(m):
    b[i] = int(line[i])

time = [[0 for j in range(n+1)] for i in range(m+1)]
for i in range(1,n+1):
    time[0][i] = time[0][i-1] + a[i-1]
for j in range(1,m+1):
    time[j][0] = time[j-1][0] + b[j-1]
ans = 0
for i in range(1,n+1):
    for j in range(1,m+1):
        if time[j-1][i-1] + a[i-1] > k or time[j-1][i-1] + b[i-1] > k or time[j-1][i-1] + a[i-1] + b[j-1] > k:
            break
        else:
            time[j][i] = time[j-1][i-1] + a[i-1] + b[j-1]
            ans = max(ans, i+j)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
#sys.setrecursionlimit(450000)
input = sys.stdin.readline

from functools import lru_cache


@lru_cache(maxsize=None)
def Tsundoku(i,j):
    global k
    #if dp[i][j]!=None:
    #    return dp[i][j]
    ans1,ans2 = 0,0
    if i<n and k-a[i]>=0:
        k-=a[i]
        ans1 = 1+Tsundoku(i+1,j)
        k+=a[i]
    if j<m and k-b[j]>=0:
        k-=b[j]
        ans2 = 1+Tsundoku(i,j+1)
        k+=b[j]
    #dp[i][j] = max(ans1,ans2)
    #return dp[i][j]
    return max(ans1,ans2)

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
#dp = [[None]*(m+1) for _ in range(n+1)]
ans = Tsundoku(0,0)
print(ans)

Submission status: Runtime Error

Solution code:
# C-Tsundoku


#N冊、M冊、時間K
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

#Aの机の現在の1番上の本の時間とBの机の一番上の本の時間を比較して小さい本を選択して
#本、時間をそれぞれ累積していき、本を削除していく。
#これを繰り返して時間Kより大きい場合になったときに処理を終了させる
#この時、本はカウントしない


aco = 0
bco = 0
book = 0
time = 0

while k > time:
	if a[aco] <= b[bco]:
		book += 1
		time += a[aco]
		aco += 1
	else:
		book += 1
		time += b[bco]
		bco += 1

print(book -1)

Submission status: Runtime Error

Solution code:
import sympy
N = int(input())
for i in range(N):
  X = sympy.divisor_count(i)
  ans += X
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_ruiseki = []
if A[0]<=k:
    A_ruiseki.append(A[0])
    for i in range(1, n):
        if A_ruiseki[i-1]+A[i] <= k :
            A_ruiseki.append(A_ruiseki[i-1]+A[i])
        else:
            break

B_ruiseki = []
if B[0]<=k:

    B_ruiseki.append(B[0])
    for i in range(1, n):
        if B_ruiseki[i-1]+B[i] <= k :
            B_ruiseki.append(B_ruiseki[i-1]+B[i])
        else:
            break

ans = len(A_ruiseki)

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i-2, 0)

    if len(B_ruiseki) < expected:
        continue
    if time + B_ruiseki[expected] > k:
        continue
    if expected > 0:
        time += B_ruiseki[expected-1]
    for j in range(expected, len(B)):

        if time + B[j] > k:
            break

        time += B[j]

        ans =i+2+j

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()))
cnt=0
temp=0
Ai=0
Bi=0
for i in range(n+m):
  if Ai==n and Bi==m:
    break;
  elif Ai==n and Bi<m:
    if temp+B[Bi]<=k:
      temp+=B[Bi]
      cnt+=1
      Bi+=1
    else:
      break;
  elif Bi==m and Ai<n:
    if temp+A[Ai]<=k:
      temp+=A[Ai]
      cnt+=1
      Ai+=1
    else:
      break;
  elif Ai<n and Bi<m:
    if A[Ai]<B[Bi]:
      if temp+A[Ai]<=k:
        temp+=A[Ai]
        cnt+=1
        Ai+=1
      else:
        break;
    elif A[Ai]>B[Bi]:
      if temp+B[Bi]<=k:
        temp+=B[Bi]
        cnt+=1
        Bi+=1
      else:
        break;
    elif A[Ai]==B[Bi]:
      if A[Ai+1]<=B[Bi+1] and Ai<m-1 and Bi<n-1:
        if temp+A[Ai]<=k:
          temp+=A[Ai]
          cnt+=1
          Ai+=1
        else:
          break;
      else:
        if temp+B[Bi]<=k:
          temp+=B[Bi]
          cnt+=1
          Bi+=1
        else:
          break;

print(cnt)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
N, M, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
i = 0
j = M-1

B_SUM = [0] * M
B_SUM[0] = B[0]

max_book = 0

for i in range(1, M):
    B_SUM[i] = B[i] + B_SUM[i - 1]
    if B_SUM[i] <= K:
        max_book = i + 1

A_SUM = 0

b_index = M - 1

for i in range(N):
    A_SUM += A[i]
    while (b_index >= 0):
        B_SUM[b_index]
        if A_SUM + B_SUM <= K:
            break
        b_index -= 1
    max_book = max(max_book, b_index + 1 + i + 1)

print(max_book)

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

n = 0
m = 0
ans = 0
while K >= 0:
  if K == 0:
    print(ans)
    exit()
  if n < N and m < M:
    if A[n] > B[m]:
      K = K - B[m]
      m += 1
    elif A[n] < B[m]:
      K = K - A[n]
      n += 1
    else:
      if A[n+1] < B[m]:
        K = K - A[n]
        n += 1
      else:
        K = K - B[m]
        m += 1
  elif n < N and m >= M:
    K = K - A[n]
    n += 1
  elif n >= N and m < M:
    K = K - B[m]
    m += 1
  else:
    print(N + M)
    exit()

  if K >= 0:
    ans += 1
  else:
    print(ans)
    exit()

Submission status: Runtime Error

Solution code:
import numpy as np
N, M, K = map(int, input().split())

A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)

a = 0
b = 0

flg = False

ans = 0

if cum_A[N] + cum_B[M] <= K:
    ans = N + M
else:
    while True:
        if cum_A[a] <= cum_B[b]:
            if (a < N) & (cum_A[a + 1] + cum_B[b] <= K):
                a += 1
            else:
                flg = True
        elif cum_A[a] > cum_B[b]:
            if (b < M) & (cum_A[a] + cum_B[b + 1] <= K):
                b += 1
            else:
                flg = True
        
        if flg:
            ans = a + b
            break


print(ans)

Submission status: Runtime Error

Solution code:
def whichDesk(a, b, i, j):
    if i < len(a) and j >= len(b):
        return 1
    elif j < len(b) and i >= len(a):
        return 2
    elif a[i] < b[j]:
        return 1
    elif a[i] > b[j]:
        return 2
    elif i+1 == len(a) or j+1 == len(b):
        return 1
    else:
        return whichDesk(a, b, i+1, j+1)

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

t = 0
cnt = 0

i = 0
j = 0

wd = 0

while t <= k and (i < len(a) or j < len(b)):
    wd = whichDesk(a, b, i, j)
    if wd == 2:
        t += b[j]
        j += 1
    else:
        t += a[i]
        i += 1

    if t <= k:
        cnt += 1
        
print(cnt)

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

time = 0
count = 0
while len(a) > 0 or len(b) > 0:
    if len(a) ==  0:
        book_b = b.popleft()
        time += book_b
        count += 1
    elif len(b) == 0:
        book_a = a.popleft()
        time += book_a
        count+= 1
    elif a[0] > b[0]:
        book_b = b.popleft()
        time += book_b
        count += 1
    elif a[0] == b[0]:
        if a[1] > b[1]:
            book_b = b.popleft()
            time += book_b
            count += 1
        else:
            book_a = a.popleft()
            time += book_a
            count += 1            
    else:
        book_a = a.popleft()
        time += a.popleft()
        count += 1
    if time > k:
        count -= 1
        break

    
print(count)

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

num = 0
for i in range(n + m):
    x = a_list[0]
    y = b_list[0]
    if x < y:
        k -= x
        a_list.pop(0)
        num += 1
    else:
        k -= y
        b_list.pop(0)
        num += 1
    if k < 0:
        break

print(num - 1)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
from bisect import bisect, bisect_right, bisect_left


def get_int():
    return int(stdin.readline().strip())


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


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

save = 0
def fun(n, m, k, ans):
    global mx, a, b, dp,save

    mx = max(mx, ans)

    if n <= 0 and m <= 0 or k <= 0:
        return ans
    key = str(n)+"_"+str(m)+"_"+str(k)
    if key in dic:
        save += 1
        return dic[key]
    x, y = -100, -100
    if n > 0 and a[n - 1] <= k:
        x = fun(n - 1, m, k - a[n - 1], ans + 1)
    if m > 0 and b[m - 1] <= k:
        y = fun(n, m - 1, k - b[m - 1], ans + 1)
    dic[key] = max(x,y)
    return max(x, y)


mx = 0
n, m, k = get_tuple()
dic = defaultdict(int)
a = get_list()
b = get_list()
a = a[::-1]
b = b[::-1]
fun(n, m, k, 0)
print(mx)

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

Sa = [0]*N
Sb = [0]*M
Sa[0] = A[0]
Sb[0] = B[0]
a = 0
b = 0
if Sa[0] > K:
    a = 0
else: 
    for i in range(1, N):
        Sa[i] = Sa[i-1] + A[i]
        if Sa[i] > K:
            a = i
            break
    else:
        a = N
if Sb[0] > K:
    b = 0
else:
    for i in range(1, M):
        Sb[i] = Sb[i-1] + B[i]
        if Sb[i] > K:
            b = i
            break
    else:
        b = M

ans = max(a, b)
for i in range(a):
    temp = K - Sa[i]
    if i+1+b =< ans:
        break
    for j in range(b):
        if Sb[j] > temp:
            ans = max(ans, i+1+j)
            break
    else:
        ans = max(ans, i+1+b)
print(ans)

Submission status: Runtime Error

Solution code:
# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])

#     if temp!=1:
#         arr.append([temp, 1])

#     if arr==[]:
#         arr.append([n, 1])

#     return arr

# n = int(input())
# total = -1
# for i in range(1,n+1):
#     div = 1
#     t = factorization(i)
#     for j in range(len(t)):
#         div = div * (t[j][1] +1)
#     total += (i * div)

# print(total)

import sys

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

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

time = 0
total = 0

if n > m:
    a = n
else:
    a = m

if sum(A) + sum(B) <= k:
    print(n+m)
    sys.exit()

for i in range(a):
    
    if i > n:
        pass
    else:
        time += A[i]
        total +=1
        if time >= k:
            print(total-1)
            sys.exit()
    if i > m:
        pass
    else:
        time += B[i]
        total += 1
        if time >= k:
            print(total-1)
            sys.exit()

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


def dfs(curr_a, curr_b, choice, time_required, count):
    global max_num
    if choice == 0:
        if curr_a >= N - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_a += 1
            time_required += a[curr_a]
    else:
        if curr_b >= M - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_b += 1
            time_required += b[curr_b]
    if time_required > K:
        return
    count += 1
    if max_num < count:
        max_num = count
    dfs(curr_a, curr_b, 0, time_required, count)
    dfs(curr_a, curr_b, 1, time_required, count)

curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 0, time_required, count)
curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 1, time_required, count)

print(max_num)

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

ans = 0
def dfs(a, b, k):
	global ans

	if a == N and b ==M:
		return True

	if k <= 0:
		return False

	if a < N and k >= A[a] and dfs(a + 1, b, k - A[a]):
		ans += 1
		return True

	if b < M and k >= B[b] and dfs(a, b + 1, k - B[b]):
		ans += 1
		return True

	return True

dfs(0, 0, K)
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_ruiseki = []
if A[0]<=k:
    A_ruiseki.append(A[0])
    for i in range(1, n):
        if A_ruiseki[i-1]+A[i] <= k :
            A_ruiseki.append(A_ruiseki[i-1]+A[i])
        else:
            break

B_ruiseki = []
if B[0]<=k:

    B_ruiseki.append(B[0])
    for i in range(1, n):
        if B_ruiseki[i-1]+B[i] <= k :
            B_ruiseki.append(B_ruiseki[i-1]+A[i])
        else:
            break

ans = len(A_ruiseki)

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i-2, 0)

    if len(B_ruiseki) < expected:
        continue
    if time + B_ruiseki[expected] > k:
        continue
    if expected > 0:
        time += B_ruiseki[expected-1]
    for j in range(expected, len(B)):

        if time + B[j] > k:
            break

        time += B[j]

        ans =i+2+j

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()))
books = 0
timer = 0
#全部読む必要があるなら全部読んで終わる
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
#入力例３対策
elif min(A) > K and min(B) > K:
    print(0)
else:
    while timer <= K:
        for i in range(len(A)+len(B)):
            if len(A) == 0 and len(B) != 0:
                books += 1
                timer += B[0]
                B = B[1:]
            elif len(A) != 0 and len(B) == 0:
                books += 1
                timer += A[0]
                A = A[1:]
            elif  A[0] > B[0]:
                books += 1
                timer += B[0]
                B = B[1:]
            elif B[0] > A[0]:
                books += 1
                timer += A[0]
                A = A[1:]
            else:
                #A[0]=B[0]
                tmp = A[0]
                books += 1
                timer += A[0]
                books += 1
                timer += B[0]
                A = A[1:]
                B = B[1:]
    print(books-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()))

if k<a[0] and k<b[0]:
  print(0)
else:
  ans = 0
  t = 0
  while t < k:
    ans += 1
    if a==[] and b==[]:
      print(n+m)
      exit()
    elif a==[] and b!=[]:
      t += b[0]
      b.pop()
    elif b==[] and a!=[]:
      t += a[0]
      a.pop()
    else:
      if a[0] > b[0]:
        t += a[0]
        a.pop()
      elif a[0] < b[0]:
        t += b[0]
        b.pop()
      elif a[0] == b[0]:
        if a[1] < b[0]:
          t += a[0]
          a.pop()
        elif b[1] < a[0]:
          t += b[0]
          b.pop()
        else:
          t += a[0]
          a.pop()
  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
ans = 0
if sum(A) + sum(B) <= K:
    print(N+M)
    exit()
A.append(10**9+1)
B.append(10**9+1)
while min(A[a],B[b]) <= K:
    if A[a] == B[b]:
        AA = A[a+1:N]
        BB = B[b+1:M]
        for i in range(1,len(AA)):
            AA[i] += AA[i-1]
        for i in range(1,len(BB)):
            BB[i] += BB[i-1]
        l = 0
        r = len(AA)-1
        w = K - A[a]
        while r - l > 1:
            m = (r+l)//2
            if AA[m] == w:
                r = m
                break
            elif AA[m] < w:
                l = m
            else:
                r = m
        if BB[l+1] <= w:
            K -= B[b]
            b += 1
        else:
            K -= A[a]
            a += 1
    elif A[a] == min(A[a],B[b]):
        K -= A[a]
        a += 1
    else:
        K -= B[b]
        b += 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_num = 0


def dfs(curr_a, curr_b, choice, time_required, count):
    global max_num
    if choice == 0:
        if curr_a >= N - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_a += 1
            time_required += a[curr_a]
    else:
        if curr_b >= M - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_b += 1
            time_required += b[curr_b]
    if time_required > K:
        if max_num < count:
            max_num = count
        return
    count += 1
    if max_num < count:
        max_num = count
    print(max_num)
    dfs(curr_a, curr_b, 0, time_required, count)
    dfs(curr_a, curr_b, 1, time_required, count)

curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 0, time_required, count)
curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 1, time_required, count)

print(max_num)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
from bisect import bisect, bisect_right, bisect_left


def get_int():
    return int(stdin.readline().strip())


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


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

save = 0
def fun(n, m, k, ans):
    global mx, a, b, dp,save

    mx = max(mx, ans)

    if n <= 0 and m <= 0 or k <= 0:
        return ans
    key = str(n)+"_"+str(m)+"_"+str(k)
    if key in dic:
        save += 1
        return dic[key]
    x, y = -100, -100
    if n > 0 and a[n - 1] <= k:
        x = fun(n - 1, m, k - a[n - 1], ans + 1)
    if m > 0 and b[m - 1] <= k:
        y = fun(n, m - 1, k - b[m - 1], ans + 1)
    dic[key] = max(x,y)
    return max(x, y)


mx = 0
n, m, k = get_tuple()
dic = dict()
a = get_list()
b = get_list()
a = a[::-1]
b = b[::-1]
fun(n, m, k, 0)
print(mx)

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()))
An = []
Bn = []
for n in range(N):
    if n == 0:
        An.append(A[n])
    else:
        An.append(An[n-1]+A[n])
    if An[n] > K:
        break

for n in range(M):
    if n == 0:
        Bn.append(B[n])
    else:
        Bn.append(Bn[n-1]+B[n])
    if Bn[n] > K:
        break


kcnt = 0
acnt = 0
bcnt = 0
Ktmp = 0
old = 0

for n in range(N):
    if An[n] > K:
        break

for nn in reversed(range(n+1)):
    for m in range(M):
        if Bn[m] + An[nn] <= K:
            if old < m+nn+2:
                old = m+nn+2


print(old)

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

# もしすべてaから選んだ場合
time = k
aOnly = 0
for i in range(len(a)):
    time -= a[i]
    if time < 0:
        time += a[i]
        i -= 1
        break
    aOnly += 1
a_idx = i
b_idx = -1

for i in range(len(b)):
    time -= b[i]
    if time < 0:
        time += b[i]
        i -= 1
        break
    aOnly += 1
b_idx = i
ans = aOnly

ans_kari = ans
for i in range(a_idx, -1, -1):
    time += a[i]
    ans_kari -= 1
    for j in range(b_idx+1, len(b)):
        time -= b[i]
        if time < 0:
            time += b[i]
            b_idx = i-1
            break
        ans_kari += 1
    ans = max(ans, ans_kari)
    
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
all_num= sum(al)+sum(bl)
s = all_num - k
ans = n + m
if s <= 0:
    print(n + m)
else:
    i = 1
    j = 1
    while s > max(al[n-i], bl[m-j]):
        if al[n-i] >= bl[m-j]:
            s -= al[n-i]
            i += 1
        else:
            s -= bl[n-j]
            j += 1
    print(ans-i-j+2)

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()))
ans = 0
cnta = 0
cntb = 0
flag = 0
for i in range(M+N):
    if A[cnta] <= B[cntb]:
        K = K - A[cnta]
        cnta += 1

    else:
        K = K - B[cntb]
        cntb += 1
        if cntb == M:
            K = K - B[cntb]
            cnta += 1
            if K >= 0:
                ans += 1
            else:
                break
    if K >= 0:
        ans += 1
    else:
        break

    if cnta == N:
        for _ in range(M):
            K = K - B[cntb]
            cntb += 1
            if K >= 0:
                ans += 1
            else:
                flag = 1
                break
            if cntb == M:
                flag = 1
                break
    if flag == 1:
        break

    if cntb == M:
        for _ in range(N):
            K = K - A[cnta]
            cnta += 1
            if K >= 0:
                ans += 1
            else:
                flag = 1
                break
            if cnta == N:
                flag = 1
                break

    if flag == 1:
        break


print(ans)

Submission status: Runtime Error

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, M, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))


    nowtime = 0

    Asum = []
    Bsum = []
    tmp = 0
    for a in A:
        tmp += a
        Asum.append(tmp)
    tmp = 0
    for b in B:
        tmp += b
        Bsum.append(tmp)

    maxbooks = 0
    for acount in range(N):
        for bcount in range(before, M):
            if K < Asum[acount] + Bsum[bcount]:
                acount -= 1
                break
        maxbooks = max(maxbooks, acount+bcount)

    print(maxbooks)

    print(acount+bcount-1)



if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import sys

n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

ans = []

def hoge(z, c, a, b):
    # 終了条件
    if ((len(a) == 0) and (len(b) == 0)) or (z > k):
        ans.append(c)
        return

    # aから
    if len(a) == 0:
        z += b.pop(0)

    # bから
    elif (len(b) == 0) or (a == b):
        z += a.pop(0)

    # a,b　同じ
    elif a[0] == b[0]:
        a_sub = a[:]
        z_a = z + a_sub.pop(0)
        if z_a <= k:
            c_a = c + 1
        b_sub = b[:]
        z_b = z + b_sub.pop(0)
        if z_b <= k:
            c_b = c + 1
        return hoge(z_a, c_a, a_sub, b), hoge(z_b, c_b, a, b_sub)
    else:
        if a[0] > b[0]:
            z += b.pop(0)
        else:
            z += a.pop(0)

    if z <= k:
        c += 1


    return hoge(z, c, a, b)

hoge(0, 0, a_list, b_list)
print(max(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 = sorted(A)
B = sorted(B)

    
ans = 0
time = 0
a_i = 0
b_i = 0
for _ in range(n+m):
    a,b = A[a_i], B[b_i]
    c = min(a,b)
    if k >= time +c:
        ans +=1
        time += c
        if c ==a:
            a_i +=1
        else:
            b_i +=1
    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()))
cnt=0
temp=0
Ai=0
Bi=0
for i in range(n+m):
  if Ai==n and Bi==m:
    break;
  elif Ai==n and Bi<m:
    if temp+B[Bi]<=k:
      temp+=B[Bi]
      cnt+=1
      Bi+=1
    else:
      break;
  elif Bi==m and Ai<n:
    if temp+A[Ai]<=k:
      temp+=A[Ai]
      cnt+=1
      Ai+=1
    else:
      break;
  elif Ai<n and Bi<m:
    if A[Ai]<B[Bi]:
      if temp+A[Ai]<=k:
        temp+=A[Ai]
        cnt+=1
        Ai+=1
      else:
        break;
    elif A[Ai]>B[Bi]:
      if temp+B[Bi]<=k:
        temp+=B[Bi]
        cnt+=1
        Bi+=1
      else:
        break;
    elif A[Ai]==B[Bi]:
      if A[Ai+1]<=B[Bi+1]:
        if temp+A[Ai]<=k:
          temp+=A[Ai]
          cnt+=1
          Ai+=1
        else:
          break;
      else:
        if temp+B[Bi]<=k:
          temp+=B[Bi]
          cnt+=1
          Bi+=1
        else:
          break;

print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    import bisect
    n,m,k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))

    A_ac = [0]
    B_ac = [0]
    a_ac = 0
    b_ac = 0
    for a in A:
        a_ac+=a
        A_ac.append(a_ac)
    for b in B:
        b_ac+=b
        B_ac.append(b_ac)
    
    i_mx = bisect.bisect_right(A_ac,k)
    i = i_mx-1
    ct = []

    while i > 0:
        k_left = k-A_ac[i]
        j = bisect.bisect_right(B_ac,k_left)-1
        ct.append(i+j)
        i-=1
    
    print(max(ct))
main()