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:
import numpy as np

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

a = input().split()
np_a=np.array(a, dtype='int64')
b = input().split()
np_b=np.array(b, dtype='int64')

ia=0
ib=0
time=0
hon=n+m
for i in range(0,n+m):
  if (ia == n): 
    time+=np_b[ib]
    ib+=1
  elif (ib == m): 
    time+=np_a[ia] 
    ia+=1
  elif (ia < n or ib < m): 
    if (np_a[ia] < np_b[ib]):
      time+=np_a[ia]
      ia+=1
    if (np_a[ia] > np_b[ib]):
      time+=np_b[ib]
      ib+=1
    if (np_a[ia] == np_b[ib] and np_a[ia] < np_b[ib]):
      time+=np_a[ia]
      ia+=1
    if (np_a[ia] == np_b[ib] and np_a[ia] > np_b[ib]):
      time+=np_b[ib]
      ib+=1
    
  if (time > k): 
    hon=i
    break

print (hon)

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.reverse()
B.reverse()

t = 0
cnt = 0
c1, c2 = A.pop(), B.pop()
while True:
    
    if t >= K or (t + c1 > K and t + c2 > K):
        break


    if (t + c1 <= K and t + c2 <= K):
        l1 = [c1]
        sum_1 = c1
        for a in A[-1]:
            if t + a <= K:
                l1.append(a)
                sum_1 += a
            else:
                break
        l2 = [c2]
        sum_2 = c2
        for b in B[-1]:
            if t + b <= K:
                l2.append(b)
                sum_2 += b
            else:
                break
        
        if sum_1 <= sum_2:
            t += sum_1
            cnt += len(l1)
            for i in range(len(l1)-1):
                A.pop()
            c1 = A.pop() if bool(A) else 100000000000
        else:
            t += sum_2
            cnt += len(l2)
            for i in range(len(l2)-1):
                B.pop()
            c2 = B.pop() if bool(B) else 100000000000

    elif t + c1 <= K:
        
        t += c1
        cnt += 1
        c1 = A.pop() if bool(A) else 100000000000
    else:
        if t + c2 <= K:
            t += c2
            cnt += 1
            c2 = B.pop() if bool(B) else 100000000000

print(cnt)

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
remainTime = K

while remainTime >= 0:
  if not A and not B:
    ans += 1
    break
  elif A[0] > remainTime:
    ans += 1
    break
    elif B[0] > remainTime:
    ans += 1
    break
  elif not A:
    remainTime -= B.pop(0)
    ans += 1
  elif not B:
    remainTime -= A.pop(0)
    ans += 1
  else:
    if A[0] <= B[0]:
      remainTime -= A.pop(0)
      ans += 1
    else:
      remainTime -= B.pop(0)
      ans += 1
print(ans-1)

Submission status: Runtime Error

Solution code:
N ,M ,K = map(int, input().split())
#K = int(K/10)
#print(K)

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

a = sum(A)
b = sum(B)
if a <K:
    m = len(A)
    t = a
    mi=m
    for j in range(M):
        t += B[j]
        if t > K:
            break
        #print(total)
        mi += 1
    m = max(m, mi)

elif b<K:
    m = len(B)
    mi=m
    t = b
    for j in range(N):
        t += B[j]
        if t > K:
            break
        #print(total)
        mi += 1
    m = max(m, mi)

else:
    m = 0
    total = 0
    for i in range(1, N+1):
        total += A[i-1]
        t = total
        if total > K:
            break
        mi = i
        for j in range(M):
            t += B[j]
            if t > K:
                break
            #print(total)
            mi += 1

        m = max(m, mi)


print(m)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# 日本語を入力できるようにするために上記一文が必要


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

read_time = 0
read_count = 0
while read_time < k:
        if not a_list and not b_list:
                break
        elif not a_list and b_list:
                read_count += 1
                read_time += b_list[0]
                b_list.remove( b_list[0])
        elif a_list and not b_list:
                read_count += 1
                read_time += a_list[0]
                a_list.remove( a_list[0])
        elif a_list[i] <= b_list[j]:
                read_time += a_list[0]
                a_list.remove( a_list[0])
                read_count += 1
        else:
                read_time += b_list[0]
                b_list.remove( b_list[0])
                read_count += 1

print(read_count)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from typing import List
from collections import deque

dp = {}



def solve(K: int, A: "List[int]", B: "List[int]"):
    cost_a = calc_cost2(A)
    cost_b = calc_cost2(B)
    i, j = 0, 0
    c = K
    num = 0
    # print(cost_a)
    # print(cost_b)
    while i < len(cost_a) or j < len(cost_b):
        tmp = []
        if i == len(cost_a) or cost_b[j] < cost_a[i]:
            if c < B[j]:
                break
            c -= B[j]
            j += 1
        elif j == len(cost_b) or cost_a[i] < cost_b[j]:
            if c < A[i]:
                break
            c -= A[i]
            i += 1
        else:
            print('????', i, j)
        num += 1

    return num

def calc_cost2(list):
    l2 = [0 for _ in list]
    pre = 0
    for i in range(len(list)):
        l2[i] = pre + list[i]
        pre = l2[i]
    return l2

def solve4(K: int, A: "List[int]", B: "List[int]"):
    queue = deque()
    if A[0] <= K: queue.append((K, 0, 0, 0))
    if B[0] <= K: queue.append((K, 0, 0, 0))
    c_max = 0
    while queue:
        rem, dep, i, j = queue.popleft()
        c_max = max(c_max, dep)
        dp[f'{i}_{j}'] = True
        if i < len(A) and A[i] <= rem and f'{i+1}_{j}' not in dp:
            queue.append((rem - A[i], dep + 1, i + 1, j))
        if j < len(B) and B[j] <= rem and f'{i}_{j+1}' not in dp:
            queue.append((rem - B[j], dep + 1, i, j + 1))
    return c_max


def solve3(K: int, A: "List[int]", B: "List[int]"):
    cost_a = calc_cost(A)
    cost_b = calc_cost(B)
    i, j = 0, 0
    c = K
    num = 0
    print(cost_a)
    print(cost_b)
    while i < len(cost_a) or j < len(cost_b):
        tmp = []
        if i == len(cost_a) or cost_b[j] < cost_a[i]:
            if c < B[j]:
                break
            c -= B[j]
            j += 1
        elif j == len(cost_b) or cost_a[i] < cost_b[j]:
            if c < A[i]:
                break
            c -= A[i]
            i += 1
        else:
            print('????', i, j)
        num += 1

    return num

def calc_cost(list):
    l2 = [0 for _ in list]
    pre = 0
    for i in range(len(list)):
        j = len(list) - 1 - i
        l2[j] = pre + list[j]
        pre = l2[j]
    return l2

def solve2(K: int, A: "List[int]", B: "List[int]"):
    if K == 0: return 0

    r = _solve(0, K, A, B, 0, 0)
    return r


def _solve(current:int, K: int, A: "List[int]", B: "List[int]", i, j):
    key = f'{i}_{j}'
    if key in dp: return dp[key]

    res = [current]
    if i < len(A) and A[i] <= K:
        res.append(_solve(current + 1, K - A[i], A, B, i + 1, j))

    if j < len(B) and B[j] <= K:
        res.append(_solve(current + 1, K - B[j], A, B, i, j + 1))

    dp[key] = max(res)
    return dp[key]


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # def iterate_tokens():
    #     for line in sys.stdin:
    #         for word in line.split():
    #             yield word
    # tokens = iterate_tokens()
    # N = int(next(tokens))  # type: int
    # M = int(next(tokens))  # type: int
    # K = int(next(tokens))  # type: int
    # A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    # B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    N, M, K = [int(k) for k in input().split()]
    A = [int(k) for k in input().split()]
    B = [int(k) for k in input().split()]
    print(solve(K, A, B))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# import sys
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split())) + [10**13]
B = [0] + list(map(int, input().split())) + [10**13]
sum_t = 0
cumsumA = [0]
cumsumB = [0]
ansA = N
ansB = M
tmp = 0
for i in range(1, N+1):
    tmp = tmp + A[i]
    if tmp > K:
        ansA = i-1
        break
    cumsumA += [tmp]
tmp = 0
for i in range(1, M+1):
    tmp = tmp + B[i]
    if tmp > K:
        ansB = i-1
        break
    cumsumB += [tmp]

ans_t = cumsumA[ansA] + cumsumB[ansB]
ansAA = ansA
ansBB = ansB
ans = ansA + ansB
ans2 = max([ans, ansAA, ansBB])
visited_dict = {}
while K < ans_t:    
    ai, bi = A[ansA], B[ansB]
    if visited_dict.get((ai, bi)):
        break
    visited_dict[(ai, bi)] = 1
    if ai >= bi:
        ans_t -= ai
        ans -= 1
        ansA -= 1
        bi = B[ansB+1]
        while ans_t <= K:
            ans += 1
            ans_t += bi
            ansB += 1
            bi = B[ansB+1]
    else:
        ans_t -= bi
        ans -= 1
        ansB -= 1
        ai = A[ansA+1]
        while ans_t <= K:
            ans += 1
            ans_t += ai
            ansA += 1
            ai = A[ansA+1]
    
ans2 = max(ans-1, ans2)

print(ans2)
# print(ansA, ansB)

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 Tundoku(a,b,count,k,K,N,M):
    if k > K :
        count -= 1
        return count
    else:
        if a == N-1 and b == M-1:
            k += min(A[a],B[b])
            if k > K:
                return count
            else:
                k += max(A[a],B[b])
                if k > K:
                    count += 1
                    return count
                else:
                    count = count + 2
                    return count
        if a == N-1 and b != M-1:
            k += B[b]
            count += 1
            return Tundoku(a,b+1,count,k,K,N,M)
        elif a != N-1 and b == M-1:
            k += A[a]
            count += 1
            return Tundoku(a+1,b,count,k,K,N,M)
        else:
            if A[a] == B[b]:
                k += A[a]
                count += 1
                return min(Tundoku(a+1,b,count,k,K,N,M),Tundoku(a,b+1,count,k,K,N,M))

            elif A[a] > B[b] :
                k += B[b]
                count += 1
                return Tundoku(a,b+1,count,k,K,N,M)
            else:
                k += A[a]
                count += 1
                return Tundoku(a+1,b,count,k,K,N,M)

ans = Tundoku(0,0,0,0,K,N,M)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
from decimal import *
n,m,k = [Decimal(int(e)) for e in input().split()]
As = deque([Decimal(int(e)) for e in input().split()])
Bs = deque([Decimal(int(e)) for e in input().split()])
result = 0
while True:
    if (not As) and (not Bs):
        break
        
    if (not As) and (Bs[0]>k):
        break
    if (not As) and (Bs[0]<=k):
        k = k-Bs.popleft()
        result = result + 1
        continue
        
    if (not Bs) and (As[0]>k):
        break
    if (not Bs) and (As[0]<=k):
        k = k-As.popleft()
        result = result + 1
        continue
    
    
    if (As[0]+As[1]) <= (Bs[0]+Bs[1]):
        min_reduce = As.popleft()
    else:
        min_reduce = Bs.popleft()

    if min_reduce > k:
        break
    else:
        k = k-min_reduce
        result = result + 1
print(result)

Submission status: Runtime Error

Solution code:
n, m, k = input().split()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
z = 0
if a[0] >= k :
  print(z)
elif a[0] + b[0] >= k :
  z = 1
  print(z)
else :
  sum = a[0] + b[0]
  i = 1
  for i in range() :
    if sum <= k :
      sum = a[i]
      z = z + 1      
      if sum <= k :
        sum = b[i]
        z = z + 1
        if sum >= k :
          break
      else :
      break
    else :
    break
print(z)

Submission status: Runtime Error

Solution code:
#ABC172C
n, m, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
i = 1 #Aの掘り具合
j = 1 #Bの掘り具合
ans = 0 #読んだ本の冊数
while k > 0:
    if (A[i-1] > B[j-1] and B[j-1] <= k:
        k = k - B[j-1]
        ans = ans + 1
        j = j + 1
    elif (A[i-1] < B[j-1] and A[i-1] <= k:
        k = k - A[i-1]
        ans = ans + 1
        i = i + 1
    if j == len(B):
        B[j-1] = 10**9
    if i == len(A):
        A[i-1] = 10**9
    if i  == len(A) and j  == len(B):
        break
print(ans)

Submission status: Runtime Error

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
import random

N, M, K = MAP()
A = LIST()
B = LIST()

def dfs(AorB, A_idx, B_idx, time_left, n_book):
    if AorB == "A":
        if A_idx >= N:
            return n_book
        if A[A_idx] > time_left:
            return n_book
        time_left -= A[A_idx]
        A_idx += 1
        n_book += 1

    else:
        if B_idx >= M:
            return n_book
        if B[B_idx] > time_left:
            return n_book
        time_left -= B[B_idx]
        B_idx += 1
        n_book += 1


    n_book = max(
        dfs("A", A_idx, B_idx, time_left, n_book),
        dfs("B", A_idx, B_idx, time_left, n_book),
        )
    return n_book

print(max(dfs("A", 0, 0, K, 0), dfs("B", 0, 0, K, 0)))


# DP = [[0] * (N+1) for _ in range(M+1)]
# ans = 0
# for b in range(1, M+1):
#     weight = DP[b-1][0] + B[b-1]
#     if weight > K:
#         break
#     DP[b][0] = weight
#     ans = b

# b_range = ans+1
# for b in range(b_range):
#     for a in range(1, N+1):
#        weight = DP[b][a-1] + A[a-1]
#        if weight > K:
#            break
#        DP[b][a] = weight
#        ans = max(ans, a+b)
# ans = 0
# weight = 0
# for i in range(N):
#     weight += A[i]
#     if weight > K:
#         break
#     ans += 1

# for i in range(M):
#     weight += B[i]
#     if weight > K:
#         break
#     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()))
ans = 0
C = A + B
for i in range(len(C)):
  C[i] = min(A[i],B[i])
  
for i in range(len(C)):
  if K >= C[i]:
    K -= C[i]
    ans +=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()))
time=0
idxa=0
idxb=0
cnt=0
while True:
  if a[idxa]<b[idxb]:
    if time+a[idxa]>k:
      break
    else:
      cnt+=1
      time+=a[idxa]
      idxa+=1
  if a[idxa]>b[idxb]:
    if time+b[idxb]>k:
      break
    else:
      cnt+=1
      time+=b[idxb]
      idxb+=1
  else:
    for i in range(min(len(a),len(b))):
        if a[i]<b[i]:
            time+=a[i]
            cnt+=1
            idxa+=1
        if a[i]>b[i]:
            time+=b[i]
            cnt+=1
            idxb+=1
        if i==min(len(a),len(b))-1 and a[i]==b[i]:
            time+=a[i]
            cnt+=1
            idxa+=1
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

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

time = 0
A_num = 0
B_num = 0
while (time < K) and (A_num <= len(A_list) -1) and (B_num <= len(B_list) -1):
    A = A_list[A_num]
    B = B_list[B_num]
    if A < B:
        time += A
        A_num += 1

    elif A > B:
        time += B
        B_num += 1

    else:
        add = 1
        time += A
        while (A_list[A_num + add] == B_list[B_num + add]) and \
              (time < K):
            time += A_list[A_num + add]
            add += 1
        if A_list[A_num + add] < B_list[B_num + add]:
            A_num += add
        else:
            B_num += add
print(A_num + B_num - 1)

Submission status: Runtime Error

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

                if A[n+j] > B[m+j] and N>n+j+1 and M>m+j+1:
                    flag = 1
                elif A[n+j] < B[m+j]:
                    flag = 2
                else:
                    if N == n+j+1 :
                        flag = 1
                    elif M == m+j+1 :
                        flag = 2
            else:
                flag = 0
            Apre = A[n]
            Bpre = B[m]
        elif n >= N and m < M:
            flag = 0
            Apre = 1000000001
            Bpre = B[m]
        elif n < N and m >= M:
            flag = 0
            Apre = A[n]
            Bpre = 1000000001

        if flag == 0:
            C = min(Apre,Bpre)
        elif flag == 1:
            C = Apre
        elif flag == 2:
            C = Bpre           
        T = T + C

        if T > K :
            break
        else:
            c = c + 1
            if C == Apre:
                n = n + 1
            elif C == Bpre:
                m = m + 1

print(int(c))

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
sum_ = k
idx_a = 0
idx_b = 0
while sum_ > 0:
    if a[idx_a] == 0 and b[idx_b] != 0:
        while sum_ > 0:
            sum_ = sum_ - b[idx_b]
            idx_b += 1
            cnt += 1
    elif b[idx_b] == 0 and a[idx_a]!= 0:
        while sum_ > 0:
            sum_ = sum_ - a[idx_a]
            idx_a += 1        
            cnt += 1
    else:
        while sum_ > 0:
            if a[idx_a] > b[idx_b]:
                sum_ = sum_ - b[idx_b]
                b[idx_b] = 0
                idx_b += 1
                cnt += 1
#             elif a[idx_a] < b[idx_b]:
            else:
                sum_ = sum_ - a[idx_a]
                a[idx_a] = 0
                idx_a += 1
                cnt += 1
print(cnt - 1)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]+list(map(int,input().split()))
C=[0]
cnt=0
a=0
for i in A:
    a+=i 
    if a<=K:
        C.append(a)
        cnt+=1
    else:break
ans=cnt
for j in range(cnt+1):
    b=C[j]+sum(B(:ans-j+1))
    if b>K:break
    for k in range(ans-j+1,M+1):
        b+=B[k]
        if b<=K:
            ans=max(ans,j+k)
        else:break
print(ans)

Submission status: Runtime Error

Solution code:
import bisect
from collections import deque

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

x = deque()
x.append(0)
for i in range(n + m):
    num = 0
    if len(a) == 0:
        num = b.popleft()
    elif len(b) == 0:
        num = a.popleft()
    elif a[0] == b[0]:
        if a[1] < b[1]:
            num = a.popleft()
        else:
            num = b.popleft()
    elif a[0] <= b[0]:
        num = a.popleft()
    else:
        num = b.popleft()
    x.append(num + x[-1])

x.popleft()

# 二分探索
pos = bisect.bisect_right(x, k)
print(pos)

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()))
minute=0
ans=0
for _ in range(100000000):
    if not A and not B:break
    if not A: minute+=B[0]
    if not B: minute+=A[0]
    if A[0]<B[0]: 
        del A[0]
        minute += min(A[0], B[0])
    elif B[0]<A[0]:
        del B[0]
        minute += min(A[0], B[0])
    else: 
        del A[0]
        minute += min(A[0], B[0])
    if minute>K: break
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import zip_longest
import sys

sys.maxsize
N, M, K = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
# iter_A = iter(A)
# iter_B = iter(B)
S = sum(A) + sum(B)
S_len = len(A) + len(B)
if K >= S:
    print(S_len)
else:
    ans = 0
    a_index = 0
    b_index = 0
    while K >= 0:
        for i in range(max(len(A), len(B))):
            if K < 0:
                break
            if A[a_index] <= B[b_index]:
                if K < 0:
                    break
                K -= A[a_index]
                a_index += 1
                ans += 1
            elif A[a_index] > B[b_index]:
                if K < 0:
                    break
                K -= B[b_index]
                b_index += 1
                ans += 1
    print(ans - 1)

Submission status: Runtime Error

Solution code:
n = 0
m = 0
k = 0
i = 0
multi = 1
x = input() 
while x[i]!=' ':
  n += (ord(x[i]) - 48) * multi
  multi *= 10
  i+=1
i+=1 
multi = 1
while x[i]!=' ':
  m += (ord(x[i]) - 48) * multi
  multi *= 10
  i+=1
i+=1 
multi = 1
while True:
  k += (ord(x[i]) - 48) * multi
  multi *= 10
  i+=1
  if i == len(x):
    break

x = input()
idid = 1
a = [0,0]
for newLoopVar in range(len(x)):
  i += 1 
  multi = 1
  while True:
    if i == len(x):
      break
    a[idid] += (ord(x[i]) - 48) * multi
    multi *= 10
    i+=1
  idid += 1

x = input()
idid = 1
b = [0,0]
for newLoopVar in range(len(x)):
  i += 1 
  multi = 1
  while x[i]!=' ':
    b[idid] += (ord(x[i]) - 48) * multi
    multi *= 10
    i+=1
  idid += 1

maxTime = k 
totalNumBooks = n + k
booksRead = 0

while True:
  if a >= b and a[1] < maxTime:
    maxTime -= a
    a.remove(a[1])
    booksRead += 1
  elif b>a and b[1]<maxTime:
    maxTime -= b
    b.remove(b[1])
    booksRead += 1
  else:
    break



print(booksRead)

Submission status: Runtime Error

Solution code:

import numpy as np
N, M, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
B = np.array(list(map(int, input().split())))

# 累積和
CA = np.cumsum(A)
CB = np.cumsum(B)
sA = CA[-1]
sB = CB[-1]

# Dont need count
if sA + sB <= K:
    print(N + M)
    exit(0)


# countmax がこれになるmax(np.where(CA <= K))+1

## Aの全読み
if sA <= K: # 全読み可
    AyomiA = N
    AyomiB = np.amax(np.where(CB <= K-sA))+1 
else:
    AyomiA = np.amax(np.where(CA <= K))+1
    AyomiB = np.amax(np.where(CB <= K-CA[AyomiA-1]))+1 

## Bの全読み
if CB[-1] <= K: # 全読み可
    ByomiB = M
    ByomiA = np.amax(np.where(CA <= K-sB))+1 
else:
    ByomiB = np.amax(np.where(CB <= K))+1 
    ByomiA = np.amax(np.where(CA <= K-CB[ByomiB-1]))+1 

# Bでカウントする時
if AyomiA - ByomiA > ByomiB - AyomiB:
    count = 0
    for b in range(AyomiB, ByomiB + 1):
        count = max(count, b + np.amax(np.where(CA <= K-b))+1 )
else:
# Aでカウントする時
    count = 0
    for a in range(ByomiA, AyomiA + 1):
        count = max(count, a + np.amax(np.where(CB <= K-a))+1 )
         
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
from queue import Queue

N, M, K = list(map(int, input().split()))
A = list(map(int, [0] + input().split()))
B = list(map(int, [0] + input().split()))
cost = 0
ind_a = 0
ind_b = 0
cumsum_a = np.cumsum(A)
cumsum_b = np.cumsum(B)
max_book = 1

queue = Queue()
queue.put([0, 0])
num_book = 0
dp = np.zeros((N + 2, M + 2)).astype(bool)

while not queue.empty():
    a, b = queue.get()
    if a + b + 1 <= num_book:
        continue
    if a + 1 <= N and b <= M:
        if cumsum_a[a + 1] + cumsum_b[b] <= K:
            if not dp[a + 1, b]:
                queue.put([a + 1, b])
                dp[a + 1, b] = True
            num_book = max(num_book, a + b + 1)
    if a <= N and b + 1 <= M:
        if cumsum_a[a] + cumsum_b[b + 1] <= K:
            if not dp[a, b + 1]:
                queue.put([a, b + 1])
                dp[a, b + 1] = True
            num_book = max(num_book, a + b + 1)

print(num_book)

# for i in range(max(N, M)):
#
#     # if ind_a < N and ind_b <= M:
#     #     dp[ind_a + 1, ind_b] = dp[ind_a, ind_b] + A[ind_a]
#     #     ind_a += 1
#     #     if dp[ind_a, ind_b] <= K and ind_a + ind_b > max_book:
#     #         max_book = ind_a + ind_b
#     #
#     # if ind_b < M and ind_a <= N:
#     #     dp[ind_a, ind_b + 1] = dp[ind_a, ind_b] + B[ind_b]
#     #     ind_b += 1
#     #     if dp[ind_a, ind_b] <= K and ind_a + ind_b > max_book:
#     #         max_book = ind_a + ind_b
#
#     if ind_a >= N and ind_b >= M:
#         break
#     elif ind_a < N and ind_b < M:

Submission status: Runtime Error

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


sum_time = 0
books = 0
while True:
    if (a_times == [] or a_times[0] > b_times[0]):
        if a_times == [] and b_times == []:
            break
        else:
            sum_time += b_times[0]
            if sum_time>k:
                break
            b_times.pop(0)
            books += 1
    elif (b_times == [] or a_times[0] <= b_times[0]):
        if b_times == [] and a_times == []:
            break
        else:
            sum_time += a_times[0]
            if sum_time>k:
                break
            a_times.pop(0)
            books += 1

print(books)

Submission status: Runtime Error

Solution code:
from pulp import *
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
res_a = [sum(a[:i+1]) for i in range(len(a))]
res_b = [sum(b[:i+1]) for i in range(len(b))]
a_value = [i for i in range(1,n+1)]
b_value = [i for i in range(1,m+1)]

w = res_a+res_b
v = a_value+b_value

W =k
r = range(len(w))

# 数理モデル
ma = LpProblem(sense=LpMaximize)

# 変数
x = [LpVariable('x%d'%i, cat=LpBinary) for i in r]

# 目的関数
ma += lpDot(v, x)

# 制約
ma += lpDot(w, x) <= W ma.solve() print(value(ma.objective))

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


mx = 0
n, m, k = get_tuple()
dic = defaultdict(int)
a = get_list()
b = get_list()

queue = deque()
queue.append([(k,0,0,0)])
ans = 0
while queue:
    k_rem,c,i,j = heapq.heappop(queue)
    if k_rem<a[i] and k_rem<b[j]:
        ans = c
        break
    if i<n and a[i]<=k:
        heapq.heappush(queue,[k_rem-a[i],c+1,i+1,c+1,j])
    if j<m and b[j]<=k:
        heapq.heappush(queue,[k_rem-b[j],c+1,i,j+1])

print(ans)

Submission status: Runtime Error

Solution code:
#from collections import deque
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
A = A[::-1]
B = list(map(int, input().split()))

book = A + B
sum_a = sum(A)
sum_b = sum(B)

l = 0
r = len(A)
time = sum_a
ans = 0
#print(book)
while ((r < len(book))|(l <= len(A))):
    if time + book[r] <= K:
        time += book[r]
        ans = max(ans,len(book[l:r])+1)
        r += 1
    elif r == l:
        r += 1
        l += 1
    else:
        time -= book[l]
        l += 1

    #print(l, r)
    #print(ans)
    #print()

book = B[::-1]+A[::-1]

l = 0
r = len(B)
time = sum_b

while ((r < len(book))|(l <= len(B))):
    if time + book[r] <= K:
        time += book[r]
        ans = max(ans,len(book[l:r])+1)
        r += 1
    elif r == l:
        r += 1
        l += 1
    else:
        time -= book[l]
        l += 1
    


print(ans)

Submission status: Runtime Error

Solution code:
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):
  ans = 0
  while k > 0:
    if a and b:
      if a[0] < b[0]:
        k -= a[0]
        a.pop()
      else:
        k -= b[0]
        b.pop()
    elif a:
      k -= a[0]
      a.pop()
    elif b:
      k -= b[0]
      b.pop()
    else:
      break
    if k >= 0:
      ans += 1
  return ans


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

  def _dfs(pa, pb, acc_b, acc_t, k):
    go_a = _dfs(pa + 1, pb, acc_b + 1, acc_t + a[pa + 1], k)
    go_b = _dfs(pa, pb + 1, acc_b, acc_t, k)
    return go_a if go_a > go_b else go_b

  return _dfs(pa=0, pb=-1, acc_b=0, acc_t=0, k=k)


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

  kl = [k]
  def _get_pa_init():
    pa = 0
    while pa < len(a):
      if kl[0] > a[pa]:
        kl[0] -= a[pa]
        pa += 1
      else:
        break
    return pa - 1

  pa = _get_pa_init()
  pb = -1

  k = kl[0]
  max_read = pa + pb + 2
  while True:
    while pb + 1 < len(b) and k >= b[pb + 1]:
      k -= b[pb + 1]
      pb += 1
    if pa <= -1 and k < b[pb + 1]:
      break
    # print(k, pa, pb, pa + pb + 2)
    if pa + pb + 2 > max_read:
      max_read = pa + pb + 2
    if pa > -1:
      pa -= 1
      k += a[pa + 1]
    # print(k, pa, pb, pa + pb + 2)
    if pa + pb + 2 > max_read:
      max_read = pa + pb + 2
    if pb >= len(b) - 1:
      break
  return max_read


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C3(n, m, k, a, b))


if __name__ == '__main__':
  main()