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 = map(int,input().split())
a_l = list(map(int,input().split()))
b_l = list(map(int,input().split()))
read_a = [0]*n
read_b = [0]*M
time = 0
ans = 0
memo = 0
for j in range(n):
    if time + a_l[j] <= k:
        time += a_l[j]
        read_a[j] = 1
        memo = j
        ans = j+1
    else:
        break

kotae = [ans]


def dfs(i,a,t,m):
    global kotae
    if i < M:
        if b_l[i] + t <= k:
            tt = t + b_l[i]
            aa = a + 1
            kotae.append(aa)
            ii = i+1
            mm = m
            #print(aa)
            dfs(ii,aa,tt,mm)
            
        else:
            tt = t - a_l[m]
            mm = m - 1
            aa = a - 1
            ii = i
            #print(aa)
            dfs(ii,aa,tt,mm)
            #print(aa)


dfs(0,ans,time,memo)


print(max(kotae))

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 + 1, M + 1)).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)

Submission status: Runtime Error

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()

n, m, k = map(int, input().split())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))

cnt = 0
if A[0] < B[0] and A[0] <= k:
    cnt += 1
    k -= A[0]

idx_a = 1
idx_b = 0
while k >= 0:
  if B[idx_b] > A[idx_a]:
    k -= A[idx_a]
    if k >= 0: cnt += 1
    if idx_a < n-1: idx_a += 1

  else:
    k -= B[idx_b]
    if k >= 0: cnt += 1
    if idx_b < m-1: idx_b += 1
        
print(cnt)

Submission status: Runtime Error

Solution code:
from decimal import Decimal

def main():
  M, N, K = map(Decimal, input().split())
  A = list(map(Decimal, input().split()))
  B = list(map(Decimal, input().split()))
  spent_time = Decimal(0)
  count = Decimal(0)
  AB = A + B
  AB.sort()
  
  while(True):
    if count == M + N:
      break
    temp = AB[count]
    #print(temp, spent_time)
    if spent_time + temp > K:
      break
    spent_time = spent_time + temp
    count += 1
  
  #print('----')
  #print(spent_time)
  print(count)
  
main()

Submission status: Runtime Error

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

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

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

ans = len(A_ruiseki)-1

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i, 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+j+1

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
amax, bmax = 0      
for i in range(len(B)):
    if Bc[i] <= K:
        bmax = i + 1
    else: break

if bmax == 0:
    Bt = 0
else:
    Bt = Bc[bmax - 1]
for i in range(len(A)):
    if Ac[i] + Bt <= K:
        amax = i + 1
    else: break
ans = max(ans,amax + bmax)
if bmax > 0:
    for i in range(1, bmax):
        bnow = bmax - i
        Bt = Bc[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
print(ans)

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()))
time = K
for i in range(N):
  time -= A[i]
  if time < 0:
    time += A[i]
    i -= 1
    break

#Aだけ読み進めるとi+1冊読める
yomeruA = A[:i + 1][::-1]
for j in range(M):
  time -= B[j]
  if time < 0:
    j -= 1
    break

#残り時間でBを読み進めるとj+1冊読める
result = i + j + 2
if result != N + M:
  tmpresult = result
  time = time + B[j]
  suma = sum(yomeruA)
  for a in yomeruA:
    if B[j] > suma:
      break
    else:
      suma -= a
    # Aを１冊諦めたら？
    tmpresult -= 1
    time += a
    for b in B[j:]:
      if time < 0:
        break
      else:
        time -= b
        tmpresult += 1
        j += 1
    if tmpresult > result:
      result = tmpresult
print(result)

Submission status: Runtime Error

Solution code:
import random
N, M, K=[int(i) for i in input().split(" ")]
a=[int(i) for i in input().split(" ")]
b=[int(i) for i in input().split(" ")]

if (N>10**7) | (M>10**7) |(K>10**7):
    if random.random()>0.5:
    print(0)
else:
    time=0
    a_idx=0
    b_idx=0
    count=0
    while time<K:
        count+=1
        try:
            if a_idx+1==N:
                time+=b[b_idx]
                b_idx+=1
                if time>K:
                    count-=1
                continue
            elif b_idx+1==M:
                time+=a[b_idx]
                a_idx+=1
                if time>K:
                    count-=1
                continue    

            if a[a_idx]<b[b_idx]:
                time+=a[a_idx]
                a_idx+=1

            elif a[a_idx]>b[b_idx]:
                time+=b[b_idx]
                b_idx+=1
            else:
                try:
                    a_com=a_idx
                    b_com=b_idx
                    while True:
                        if a[a_com]>b[b_com]:
                            time+=a[a_idx]
                            a_idx+=1
                            break
                        elif a[a_com]<b[b_com]:
                            time+=b[b_idx]
                            b_idx+=1
                            break
                        a_com+=1
                        b_com+=1
                except:
                    time+=a[a_idx]
                    a_idx+=1
            if time>K:
                count-=1            
        except:
            break
    print(count)

Submission status: Runtime Error

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

from collections import deque


def book(A, B, t, cnt):
    if len(A) == 0 and len(B) == 0:
        L.append(cnt)
        return cnt
    elif t == K:
        L.append(cnt)
        return cnt
    elif t > K:
        L.append(cnt-1)
        return cnt - 1

    a_r = 0
    b_r = 0
    if len(A) != 0:
        a = A.popleft()
        a_r = book(A, B, t+a, cnt+1)
        
    if len(B) != 0:
        b = B.popleft()
        b_r = book(A, B, t+b, cnt+1)
    L.append(a_r)
    L.append(b_r)
    return max(a_r, b_r)




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

L = []

A = deque(A)
B = deque(B)

t = 0
ret = 0

ret = book(A, B, 0, 0)

print(max(L))

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=0
t=[]
if sum(a)+sum(b)<=k:
    c=n+m
elif a[0]==k and b[0]==k:
    c=1
elif a[0]>k:
    while k>=b[0]:
        k-=b[0]
        b=b[1:]
        c+=1
elif b[0]>k:
    while k>=a[0]:
        k-=a[0]
        a=a[1:]
        c+=1
else:
    for u in range(n):
        for r in range(m):
            if sum(a[:u+1])+sum(b[:r+1])<=k:
                t.append(u+r+2)
            else:
                break
    c=max(t)
print(c)

Submission status: Runtime Error

Solution code:
a,b,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
total=0
number=0

if a>b:
  book=a
else:
  book=b

for i in range(book):
  if total<K:
    if A[a-i-1]<B[b-i-1]:
      total=total+A[a-i-1]
      number=number+1
    elif A[a-i-1]>B[b-i-1]:
      total=total+B[b-i-1]
      number=number+1
  else:
    break

print(number)

Submission status: Runtime Error

Solution code:
import numpy as np

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

A_array = np.array(list(map(int, input().split())))

B_array = np.array(list(map(int, input().split())))

cumA_array = np.cumsum(A_array)
cumB_array = np.cumsum(B_array)


if np.sum(A_array) + np.sum(B_array) <= K:
    print(N + M)
    
elif A_array[0] > K and  B_array[0] > K:
    print(0)

elif A_array[0] > K and  B_array[0] <= K:    
    print(np.sum((cumB_array <= K)))
    
elif A_array[0] <= K and  B_array[0] > K:
    print(np.sum((cumA_array <= K)))    
    
else:
    result = []
    result.append(np.sum((cumA_array <= K)))
    result.append(np.sum((cumB_array <= K)))     
    
    for i in range(N):
        if cumA_array[i] > K:
            break
        for j in range(M):
            if cumA_array[i] + cumA_array[j] > K:
                break
                
        result.append(i + 1 + j)     
        
    print(max(result))

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()))
#N = N-1
#M = M -1

def f(i,j,time):
  if i >= N and j < M:
    if time + B[j] < K:
      return 1 + f(i, j+1, time+B[j])
    else:
      return 0
  elif i < N and j >= M:
    if time + A[i]:
      return 1 + f(i+1, j, time+A[i])
    else:
      return 0
  elif i >= N and j >= M:
    return 0
  else:
    timeA = A[i] + time
    timeB = B[j] + time
    if timeA > K and timeB > K:
      return 0
    elif timeA > K and timeB <= K:
      return 1 + f(i, j+1, timeB)
    elif timeA <= K and timeB > K:
      return 1 + f(i+1, j, timeA)
    else:
      countA = 1 + f(i+1, j, timeA)
      countB = 1 + f(i, j+1, timeB)
      #print("cA "+str(countA))
      #print("cB "+str(countB))
      #print("tA "+str(timeA))
      #print("tB "+str(timeB))
      return max(countA, countB)

ans = f(0, 0, 0)
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()))
minute=0
ans=0
while True:
    if not A and not B:
        break
    elif not A:
        x=B[0]
        del B[0]
    elif not B:
        x=A[0]
        del A[0]
    elif A[0] > K and B[0] > K:
        break
    elif A[0] > K:
        x = B[0]
        del B[0]
    elif B[0] > K:
        x = A[0]
        del A[0]
    elif A[0]!=B[0]:
        x=min(A[0],B[0])
        if A[0]<B[0]:
            del A[0]
        else:
            del B[0]
    else:
        x=A[0]
        while True:
            i=1
            if A[i]<B[i]:
                del A[0]
                break
            elif B[i]<A[i]:
                del B[0]
                break
            else:
                i+=1
    minute += x
    if minute > K:
        break
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

n = int(stdin.readline().rstrip())
ans = 0
for i in range(n):
    i += 1
    ans += i*len(make_divisors(i))
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
count = 0
i = 0
j = 0
while sum <= K:
  if not A and not B:
    break
  elif A[0] > K or B[0] > K:
    break
  elif not A:
    sum += B[0]
    B.pop(0)
  elif not B:
    sum += A[0]
    A.pop(0)
  elif A[0] >= B[0]:
    sum += A[0]
    A.pop(0)
  elif B[0] > A[0]:
    sum += B[0]
    B.pop(0)
    count += 1

print(count)

Submission status: Runtime Error

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

n_time = list(map(int, input().split()))
m_time = list(map(int, input().split()))

result = 0
count = 0

for i in range(n + m):
    if(1000000000 in n_time or 1000000000 in m_time):
        count = 0
        break

    if(result > max_time and count > 0):
        count -= 1
        break
    elif(result > max_time):
        break

    if len(n_time) == 0 or len(m_time) == 0:
        if n_time[0]:
            result += int(n_time[0])
            n_time.remove(n_time[0])
            count += 1
        else:
            result += int(m_time[0])
            m_time.remove(m_time[0])
            count += 1

    if(len(n_time) != 0 and len(m_time) != 0 and n_time[0] > m_time[0]):
        result += int(n_time[0])
        n_time.remove(n_time[0])
        count += 1
    elif(len(n_time) != 0 and len(m_time) != 0 and n_time[0] < m_time[0]):
        result += int(m_time[0])
        m_time.remove(m_time[0])
        count += 1


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()))
state0=""
roots=list()
def ab(state):
    for x in range(2):
        tempstate=state+str(x)
        timelimit = K
        ac = 0
        bc = 0
        for s in tempstate:
            if s == "0":
                if ac > N-1:
                    return 0
                timelimit = timelimit - A[ac]
                ac += 1

                if timelimit == 0:
                    roots.append(tempstate)
                    return 0
                if timelimit < 0:
                    roots.append(state)
                    return 0

            if s == "1":
                if bc > M-1:
                    return 0
                timelimit = timelimit - B[bc]
                bc += 1

                if timelimit == 0:
                    roots.append(tempstate)
                    return 0
                if timelimit < 0:
                    roots.append(state)
                    return 0
        ab(tempstate)
ab(state0)
print(len(max(roots, key=len)))

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()))
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
r = 0
for i, a in enumerate(A):
    min_idx = 0
    max_idx = len(B) - 1
    while min_idx < max_idx:
        j = (min_idx + max_idx + 1) // 2
        k = K - A[i] - B[j]
        if k < 0:
            max_idx = j - 1
        else:
            min_idx = j
    while K - A[i] - B[j + 1] >= 0:
        j += 1
    k = K - A[i] - B[j]
    if k >= 0:
        r = max(r, i + j)
print(r)

Submission status: Runtime Error

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
        
for i in range(len(B)):
    if Bc[i] <= K:
        bmax = i + 1
    else: break

if bmax == 0:
    Bt = 0
else:
    Bt = Bc[bmax - 1]
for i in range(len(A)):
    if Ac[i] + Bt <= K:
        amax = i + 1
    else: break
ans = max(ans,amax + bmax)
if bmax > 0:
    for i in range(1, bmax):
        bnow = bmax - i
        Bt = Bc[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
print(ans)

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

A_sum_list = [0]
B_sum_list = [0]
total_A = 0
total_B = 0

for A_i in A_list:
  total_A += A_i
  A_sum_list.append(total_A)

for B_i in B_list:
  total_B += B_i
  B_sum_list.append(total_B)

answer = 0

for i in range(n+1):
  for j in range(answer-i,m+1):
    if A_sum_list[i] + B_sum_list[j] <= k:
      if i + j > answer:
        answer = i + j
    else:
      break
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.reverse()
b.reverse()
ans = 0

while k > 0 :
    if k >= a[-1] or k >= b[-1] :
        if a[-1] > b[-1] :
            ans += 1
            k = k - b[-1]
            del b[-1]
            if len(b) == 0 :
                b.append(2000000000)
        elif b[-1] > a[-1] :
            ans += 1
            k = k - a[-1]
            del a[-1]
            if len(a) == 0 :
                a.append(2000000000)
        elif a[-1] == b[-1] :
            ans += 1
            for j in range(1,1000000):
                if a[j*(-1)] > b[j*(-1)] :
                    k = k - b[-1]
                    del b[-1]
                    if len(b) == 0 :
                        b.append(2000000000)
                    break
                elif a[j*(-1)] < b[j*(-1)] :
                    k = k - a[-1]
                    del a[-1]
                    if len(a) == 0 :
                        a.append(2000000000)
                    break
    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()))

k=tmp

ans=0

cnt1=0
cnt2=0
cost1=0
cost2=0

for i in range(n):
    cost1+=a[i]
    if cost1<=k:
        cnt1+=1
    else:
        cost1-=a[i]
        break

for j in range(m):
    cost2+=b[j]
    if cost2<=k:
        cnt2+=1
    else:
        cost2-=b[j]
        break

if cnt1>cnt2:
    ans+=cnt1
    k-=cost1
    for i in range(m):
        k-=b[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)

elif cnt2>cnt1:
    ans+=cnt2
    k-=cost2
    for i in range(n):
        k-=a[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)

else:
    alpha+=cnt1
    tmp-=cost1
    for i in range(m):
        tmp-=b[i]
        if k>=0:
            alpha+=1
        else:
            break
    
    bata+=cnt2
    k-=cost2
    for i in range(n):
        k-=a[i]
        if k>=0:
            bata+=1
        else:
            break

    ans=max(alpha, bata)

    print(ans)

Submission status: Runtime Error

Solution code:
n, m, k=map(int,input().split())
a=[0]+list(map(int,input().split()))
b=[0]+list(map(int,input().split()))
import numpy as np

read = 0

f = np.zeros((n+1, m+1))
f[0][0] = k
for i in range(1, n+1):
    f[i][0] = f[i-1][0]-a[i]
    if f[i][0]<0:
        break
    read = read if read > i else i
    for j in range(1, m+1):
        f[i][j] = f[i][j-1]-b[j]
        if f[i][j]<0:
            break
        read = read if read > i+j else i+j

print(read)

Submission status: Runtime Error

Solution code:
''' CODED WITH LOVE BY SATYAM KUMAR '''

from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys

sys.setrecursionlimit(10 ** 6)  # max depth of recursion
threading.stack_size(2 ** 27)  # new thread will get stack of such size
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7


class MergeFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n
        self.lista = [[_] for _ in range(n)]

    def find(self, a):
        to_update = []
        while a != self.parent[a]:
            to_update.append(a)
            a = self.parent[a]
        for b in to_update:
            self.parent[b] = a
        return self.parent[a]

    def merge(self, a, b):
        a = self.find(a)
        b = self.find(b)
        if a == b:
            return
        if self.size[a] < self.size[b]:
            a, b = b, a
        self.num_sets -= 1
        self.parent[b] = a
        self.size[a] += self.size[b]
        self.lista[a] += self.lista[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets


def display(string_to_print):
    stdout.write(str(string_to_print) + "\n")


def prime_factors(n):  # n**0.5 complex
    factors = dict()
    for i in range(2, math.ceil(math.sqrt(n)) + 1):
        while n % i == 0:
            if i in factors:
                factors[i] += 1
            else:
                factors[i] = 1
            n = n // i
    if n > 2:
        factors[n] = 1
    return (factors)


def all_factors(n):
    return set(reduce(list.__add__,
                      ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))


def fibonacci_modP(n, MOD):
    if n < 2: return 1
    return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(
        fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD


def factorial_modP_Wilson(n, p):
    if (p <= n):
        return 0
    res = (p - 1)
    for i in range(n + 1, p):
        res = (res * cached_fn(InverseEuler, i, p)) % p
    return res


def binary(n, digits=20):
    b = bin(n)[2:]
    b = '0' * (digits - len(b)) + b
    return b


def is_prime(n):
    """Returns True if n is prime."""
    if n < 4:
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False
    i = 5
    w = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += w
        w = 6 - w
    return True


def generate_primes(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    return prime


factorial_modP = []


def warm_up_fac(MOD):
    # print("in")
    global factorial_modP, fac_warm_up
    if fac_warm_up: return
    factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
    # print(len(factorial_modP))
    for i in range(2, fac_warm_up_size):
        factorial_modP[i] = (factorial_modP[i - 1] * i) % MOD
    fac_warm_up = True


def InverseEuler(n, MOD):
    return pow(n, MOD - 2, MOD)


def nCr(n, r, MOD):
    global fac_warm_up, factorial_modP
    if not fac_warm_up:
        warm_up_fac(MOD)
        fac_warm_up = True

    return (factorial_modP[n] * (
            (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD)) % MOD)) % MOD


def test_print(*args):
    if testingMode:
        print(args)


def display_list(list1, sep=" "):
    stdout.write(sep.join(map(str, list1)) + "\n")


def display_2D_list(li):
    for i in li:
        print(*i)


def prefix_sum(li):
    sm = 0
    res = []
    for i in li:
        sm += i
        res.append(sm)
    return res


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


memory = dict()


def clear_cache():
    global memory
    memory = dict()


def cached_fn(fn, *args):
    global memory
    if args in memory:
        return memory[args]
    else:
        result = fn(*args)
        memory[args] = result
        return result


def ncr(n, r):
    return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))


def binary_search(i, li):
    fn = lambda x: li[x] - x // i
    x = -1
    b = len(li)
    while b >= 1:
        while b + x < len(li) and fn(b + x) > 0:  # Change this condition 2 to whatever you like
            x += b
        b = b // 2
    return x


# -------------------------------------------------------------- MAIN PROGRAM


TestCases = False
fac_warm_up_size = 10 ** 6 + 100
optimise_for_recursion = True  # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3



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

    mx = max(mx, ans)

    if n <= 0 and m <= 0 or k <= 0:
        return ans

    key = str(n)+"_"+str(m)
    if key in dic:
        return dic[key]

    x, y = -100, -100
    if n > 0 and a[n - 1] <= k:
        key1 = str(n-1)+"_"+str(m)
        x = fun(n - 1, m, k - a[n - 1], ans + 1)
        dic[key1] = x

    if m > 0 and b[m - 1] <= k:
        key2 = str(n) + "_" + str(m-1)
        y = fun(n, m - 1, k - b[m - 1], ans + 1)
        dic[key2] = y

    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]

def main():
    fun(n, m, k, 0)
    print(mx)


if TestCases:
    for i in range(get_int()):
        main()
else:
    main() if not optimise_for_recursion else threading.Thread(target=main).start()

Submission status: Runtime Error

Solution code:
N, M, K =  map(int, input().split())
timeA = list(map(int, input().split()))
timeB = list(map(int, input().split()))
new_time = 0
read_book_num = 0
n = 0
m = 0
while n<N and m<M:
  if timeA[0] < timeB[0] :
    if timeA[0] + new_time < K:
      new_time += timeA[0]
      read_book_num += 1
      del timeA[0]
      continue
    elif timeB[0] + new_time < K:
      new_time += timeB[0]
      read_book_num += 1
      del timeB[0]
      continue
    else:
      break
  else:
    if timeB[0] + new_time < K:
      new_time += timeB[0]
      read_book_num += 1
      del timeB[0]
      continue
    else:
      break

print(read_book_num)

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

a=list()
for i in range(N):
    for j in range(M):
            if sigma(A,1,i)+sigma(B,1,j)>K:
                a.append(i+j)
print(max(a))

Submission status: Runtime Error

Solution code:
import math

a, b, k = map(int, input().split())
a_list = list(map(int, input().split()))
a_list  = a_list[::-1]

b_list = list(map(int, input().split()))
b_list  = b_list[::-1]

list_ = []
cnt=0
finish_flg = False
jikan = 0
while (len(a_list)!=0) & (len(b_list)!=0):
  a_top = a_list[-1]
  b_top = b_list[-1]

  if a_top<b_top:
    list_.append(a_top)
    a_list.pop()
  else:
    list_.append(b_top)
    b_list.pop()
    
list_ = list_ + a_list + b_list

if sum(list_)<=k:
  print(len(list_))
else:
  left = 0
  right = len(list_)
  while abs(left-right)>1:
    mid = (left+right) // 2
    if sum(list_[:mid])<=k:
      left = mid
    else:
      right = mid

  print(len(list_[left]))

Submission status: Runtime Error

Solution code:
import sys
N,M,K = map(int,(input().split()))
A=list(map(int,(input().split())))
B=list(map(int,(input().split())))
rui_A=[]
rui_B=[]
for a in range(N):
  if(a==0):
    rui_A.append(A[a])
  else:
  	rui_A.append(rui_A[a-1]+A[a])
  if(rui_A[-1]>K):
    rui_A.pop(-1)
    break
for b in range(M):
  if(b==0):
    rui_B.append(B[b])
  else:
  	rui_B.append(rui_B[b-1]+B[b])
  if(rui_B[-1]>K):
    rui_B.pop(-1)
    break
if(len(rui_A)!=0 and len(rui_B)!=0):
  pass
elif(len(rui_A)==0 and len(rui_B)==0):
  print(0)
  sys.exit()
elif(len(rui_A)!=0 and len(rui_B)==0):
  print(len(rui_A))
  sys.exit()
elif(len(rui_A)==0 and len(rui_B)!=0):
  print(len(rui_B))
  sys.exit()
  
while (True):
  if(rui_A[-1]+rui_B[-1]>K):
    if(rui_A[-1]>rui_B[-1]):
      rui_A.pop(-1)
    elif(rui_A[-1]<rui_B[-1]):
      rui_B.pop(-1)
  else:
    break
print(len(rui_A)+len(rui_B))