Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Runtime Error

Solution code:
from collections import deque

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = deque(a)
b = deque(b)
t = 0
ans = 0
while True:
    if len(a) == 0 and len(b) == 0:
        break
    elif len(a) == 0:
        if t+b[0] > k:
            break
        ans += 1
        t += b.popleft()
    elif len(b) == 0:
        if t+a[0] > k:
            break
        ans += 1
        t += a.popleft()
    else:
        if a[0] < b[0]:
            if t+a[0] > k:
                break
            ans += 1
            t += a.popleft()
        elif a[0] > b[0]:
            if t+b[0] > k:
                break
            ans += 1
            t += b.popleft()
        else:
            if len(a) == 1:
                if t+b[0] > k:
                    break
                ans += 1
                t += b.popleft()
            elif len(a) == 1:
                if t+a[0] > k:
                    break
                ans += 1
                t += a.popleft()
            else:
                if a[1] < b[1]:
                    if t+a[0] > k:
                        break
                    ans += 1
                    t += a.popleft()
                else:
                    if t+b[0] > k:
                        break
                    ans += 1
                    t += b.popleft()
print(ans)

Submission status: Runtime Error

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

Ai = 0
Bi = 0
t = 0
result = 0

while True:
    if Ai>=N:
        break
    elif t + A[Ai] <= K:
        t += A[Ai]
        Ai += 1
    else:
        break
result = Ai

while True:
    if Ai < 1 or Bi>=M:
        break
    if t + B[Ai] <= K:
        t += B[Bi]
        Bi += 1
        if Ai+Bi > result:
            result = Ai+Bi
    else:
        t -= A[Ai-1]
        Ai -= 1

print(result)

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()))
ans = []
all = []
bll = []
a_add = 0
b_add = 0
for a in al:
    a_add += a
    all.append(a_add)
    if a_add < k:
        ans.append(al.index(a))
for b in bl:
    b_add += b
    bll.append(b_add)
    if b_add <= k:
        ans.append(bl.index(b))
for aa in all:
    for bb in bll:
        if aa + bb <= k:
            ans.append(all.index(aa) + bll.index(bb))
        else:
            break
if ans != 0:
    print(max(ans) + 2)
else:
    print(0)

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

t = 0
m = 0
cnt = 0

if (sum(A) + sum(B)) <= K:
    print(N + M)
    sys.exit()
while True:
    if t > K:
        print(cnt-1)
        sys.exit()
    elif t == K:
        print(cnt)
        sys.exit()

    m = min(A[0], B[0])
    t += m
    cnt += 1


    if len(A) != 0 and m == A[0]:
        A.pop(0)
    elif len(B) != 0 and m == B[0]:
        B.pop(0)

Submission status: Runtime Error

Solution code:

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

print(A)
print(B)

c = 0
ans = 0
while ans <= nmk[2]:
    if not A:
        ans += B[0]
        B.pop(0)
    
    elif not B:
        ans += A[0]
        A.pop(0)
        
    elif A[0] <= B[0]:
        ans += A[0]
        A.pop(0)
        
    elif B[0] <= A[0]:
        ans += B[0]
        B.pop(0)

print(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()))
ans = 0
count = 0
if (sum(a) + sum(b)) <= k:
  exit(print(len(a) + len(b)))
  
else:
  while True:
    if a[0] <= b[0]:
      ans += a[0]
      count += 1
      a.pop(0)
      if ans > k:
        print(count - 1)
        break
    else:
      ans += b[0]
      count += 1
      b.pop(0)
      if ans > k:
        print(count - 1)
        break

Submission status: Runtime Error

Solution code:
import numpy as np
n, m , k = map(int, input().split())

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

if k >= sum(a) + sum(b):
    print(len(a) + len(b))

else:
    tmp_cost = 0
    max_a = []
    for i in range(len(a)):
        tmp_cost += a[i]
        if k < tmp_cost:
            break
        max_a.append([tmp_cost, i+1])

    tmp_cost = 0
    max_b = []
    for j in range(len(b)):
        tmp_cost += b[j]
        if k < tmp_cost:
            break
        max_b.append([tmp_cost, j+1])
    
    best_score = 0
    for i in range(len(max_a)):
        cost = max_a[i][0]
        idx = np.array(max_a)[:, 0] < k-cost
        score  = max_a[i][1] + np.array(max_a)[:, 1][idx][-1]
        
        if best_score < score:
            best_score = score
    print(best_score)

Submission status: Runtime Error

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

		if c > m:
			m = c
		

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

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

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

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

x = deque(a)
y = deque(b)

ans=0
for i in range(n + m):
    print(x, y)
    try:
        temp = x.popleft() if x[0] < y[0] else y.popleft()
    except:
        if len(x) == 0:
            temp = y.popleft()
        elif len(y) == 0:
            temp=x.popleft()
            print(temp)

    k -= int(temp)
    if k >= 0:
        ans += 1
    else:
        break
print(ans)

Submission status: Runtime Error

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

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

for i in range(maxint):

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

Submission status: Runtime Error

Solution code:
import numpy as np
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
count = 0
def how_many(num, arr):
  count = 0
  for i in arr:
    if num > i:
      num-=i
      count+=1
    else:
      break
  return num, count
while K > 0:
  if (len(A) > 0 and K < A[0]) and (len(B) > 0 and K < B[0]):
    break
  a_n, a_c = how_many(K, A)
  b_n, b_c = how_many(K, B)
  if a_c > b_c:
    K-=A.pop(0)
  elif a_c < b_c:
    K-=B.pop(0)
  elif a_n > b_n:
    K-=A.pop(0)
  else:
    K-=B.pop(0)
  count+=1
print(count)

Submission status: Runtime Error

Solution code:
i1 = input().split(" ")
n = int(i1[0])
m = int(i1[1])
k = int(i1[2])
i2 = input().split(" ")  # desk A
i3 = input().split(" ")  # desk B

t = 0
at =0
bt =0

def isWithinBudget(val, k):
    if k - val < 0:
        return False
    else:
        return True

for i in range(len(i2 + i3)):
    if len(i3) == 0 and len(i2) == 0:
        break
    elif len(i3) == 0:
        # pick form desk A
        val = int(i2[at])
        at += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    elif len(i2) == 0:
        val = int(i3[0])
        bt += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    else:
        if int(i3[bt]) >= int(i2[at]):
            # pick form desk A
            val = int(i2[at])
            at += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break
        else:
            val = int(i3[bt])
            bt += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break

    if k < 0:
        break
print(t)

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

list3 = []
while True:
    try:
        list3.append(a.pop(0))
        list3.append(b.pop(0))
    except IndexError:
        break
list3.append(0)
sum=0
num=0
for i in range(n+m+1):
    if sum>k:
        num-=1
        break
    elif i==n+m:
        break
    else:
        sum+=list3[i]
        num+=1


print(num)

Submission status: Runtime Error

Solution code:
x,y ,z= map(int,input().split())
left = list(map(int,input().split()))
right = list(map(int,input().split()))
array=[]
while len(left) != 0 or len(right) != 0:   # line7
    if left[0] < right[0]:                  # line8
        array.append(left.pop(0))           # line9
    else:
        array.append(right.pop(0))
    if len(left) != 0:                          # line12
        array.extend(left)                      # line13
    elif len(right) != 0:                       # line14
        array.extend(right)                     # line15
    if sum(array) > z:
        array.pop(-1)
        break
print(len(array))

Submission status: Runtime Error

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

ia = 0
ib = 0
book = 0
minute = 0

while minute <= k:
    if a[ia] > b[ib]:
        minute += b[ib]
        book += 1
        if minute > k:
            book -= 1
        ib += 1
    else:
        minute += a[ia]
        book += 1
        if minute > k:
            book -= 1
        ia += 1

print(book)

Submission status: Runtime Error

Solution code:
a = input()
n = input()
m = input()
first = a.split(" ")
N = n.split(" ")
M = m.split(" ")

totaltime = 0
count = 1
for i in range(int(first[1]) + int(first[0])):
  if totaltime + int(N[0]) > int(first[2]) or totaltime + int(M[0]) > int(first[2]):
    break
  if int(N[0]) < int(M[0]):
    totaltime += int(N[0])
    N.remove(N[0])
    count += 1
  elif int(M[0]) < int(N[0]):
    totaltime += int(M[0])
    M.remove(M[0])
    count += 1
print(count)

Submission status: Runtime Error

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

    if len(bookA) != len(bookB):
        if len(bookA) > len(bookB):
            diff = len(bookA) - len(bookB)
            for i in range(diff + 1):
                bookB.append(1000000000)
            bookA.append(1000000000)
        else:
            diff = len(bookB) - len(bookA)
            for j in range(diff + 1):
                bookA.append(1000000000)
            bookB.append(1000000000)
    
    C = 0
    Ai = 0
    Bi = 0

    while K > -1:
        if bookA[Ai] < bookB[Bi]:
            K = K - bookA[Ai]
            if K == 0:
                C = C + 1
                break
            elif K < 1:
                break
            Ai = Ai + 1
            C = C + 1
        else:
            K = K - bookB[Bi]
            if K == 0:
                C = C + 1
                break
            elif K < 1:
                break
            Bi = Bi + 1
            C = C + 1
    print(C)
    

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

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

Submission status: Runtime Error

Solution code:
def dokusyo(A,B,i,j,K,total):
    a = A[i]
    b = B[j]
    if total+min(a,b)>K:
        return 0
    if a<b:
        return 1+dokusyo(A,B,i+1,j,K,total+a)
    elif a>b:
        return 1+dokusyo(A,B,i,j+1,K,total+b)
    else:
        return 1+max(dokusyo(A,B,i+1,j,K,total+a),dokusyo(A,B,i,j+1,K,total+b))
       


if __name__ == "__main__":
    (N,M,K) = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    A.append(10**10)
    B.append(10**10)
    i=0
    j=0
    total = 0
    print(dokusyo(A,B,i,j,K,total))

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()))
count = 0
while K >= 0:
    if A != [] and B != []:
        if A[0] > B[0]:
            K -= B.pop(0)
        else:
            K -= A.pop(0)
    elif A == []:
        K -= B.pop(0)
    elif B == []:
        K -= A.pop(0)
    if K >= 0:
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = 0
left_time = K
while True:
    if len(A) == 0 and len(B) == 0:
        break

    if len(A) != 0 and len(B) == 0:
        val = A.popleft()
    elif len(A) == 0 and len(B) != 0:
        val = B.popleft()
    elif A[0] < B[0]:
        val = A.popleft()
    elif A[0] > B[0]:
        val = B.popleft()
    else:

        def f(n):
            if len(A) == n and len(B) == n:
                val = A.popleft()
                return val

            elif A[n] < B[n]:
                val = A.popleft()
                return val
            elif A[1] > B[1]:
                val = B.popleft()
                return val
            else:
                val = f(n + 1)
                return val

        val = f(1)

    left_time -= val
    if left_time < 0:
        break
    else:
        ans += 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()))
B = list(map(int, input().split()))
Q = deque([[0, 0, 0]])
ans = 0
if sum(A) + sum(B) <= k:
    print(n+m)
else:
    while len(Q) > 0:
        a, b, t = Q.popleft()
        #print([a, b, t])
        if a < n:
            t1 = t + A[a]
            a1 = a + 1
            if t1 <= k:
                Q.append([a1, b, t1])
            else:
                ans = max(ans, a+b)
        else:
            pass
        if b < n:
            t2 = t + B[b]
            b2 = b + 1
            if t2 <= k:
                Q.append([a, b2, t2])
            else:
                ans = max(ans, a+b)
        else:
            pass
        #print(Q)
    print(ans)

Submission status: Runtime Error

Solution code:
n_m_k = input().split()
n = int(n_m_k[0])
m = int(n_m_k[1])
k = int(n_m_k[2])

a_str_list = input().split()
a_int_list = [int(x) for x in a_str_list]

b_str_list = input().split()
b_int_list = [int(x) for x in b_str_list]

sum_t = 0
cnt_a = 0
cnt_b = 0

len_a = len(a_int_list)
len_b = len(b_int_list)
while(1):
    if((cnt_a != len_a) and (cnt_b != len_b)):
        if(a_int_list[cnt_a] < b_int_list[cnt_b]):
            sum_t += a_int_list[cnt_a]
            if(sum_t <= k):
                cnt_a += 1
            else:
                break
        elif(a_int_list[cnt_a] > b_int_list[cnt_b]):
            sum_t += b_int_list[cnt_b]
            if(sum_t <= k):
                cnt_b += 1
            else:
                break
        #
        elif(a_int_list[cnt_a] == b_int_list[cnt_b]):
            if(a_int_list[cnt_a + 1] < b_int_list[cnt_b + 1]):
                sum_t += a_int_list[cnt_a]
                if(sum_t <= k):
                    cnt_a += 1
                else:
                    break
            elif(a_int_list[cnt_a + 1] > b_int_list[cnt_b + 1]):
                sum_t += b_int_list[cnt_b]
                if(sum_t <= k):
                    cnt_b += 1
                else:
                    break
        #

    elif(cnt_a < len_a):
        sum_t += a_int_list[cnt_a]
        if(sum_t <= k):
            cnt_a += 1
        else:
            break
    elif(cnt_b < len_b):
        sum_t += b_int_list[cnt_b]
        if(sum_t <= k):
            cnt_b += 1
        else:
            break
    else:
        break

print(cnt_a + cnt_b)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
m, n, k = map(int, input().split())

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

AB = sorted(A)
s=0
l=0
for i in AB:
    if s+i <= k:
        s+=i
        l+=1
    else:
        break

# 出力
print("{}".format(l))

Submission status: Runtime Error

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

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

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

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

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

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

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

Submission status: Runtime Error

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

i_a = 0
i_b = 0

total_time = 0

while True:
    if i_a == N and i_b == M:
        break
    else:
        if i_a == N:
            read_a = False:
        elif i_b ==M:
            read_a = True:
        elif A[i_a] <= B[i_b]:
            read_a = True
    
    if read_a:
        if A[i_a] <= K - total_time:
            total_time += A[i_a]
            i_a += 1
        else:
            break
    else:
        if B[i_b] <= K - total_time:
            total_time += B[i_b]
            i_b += 1
        else:
            break

print(int(i_a+i_b))

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 chooseBook(x):
    if len(a) == x:
        return "b"
    elif len(b) == x:
        return "a"
    else:
        if a[x] == b[x]:
            chooseBook(x+1)
        elif a[x] > b[x]:
            return "b"
        else:
            return "a"

ans = 0
cnt = 0
for i in range(n+m):
    if len(a) == 0:
        ans += 1
        cnt += b.pop(0)
    elif len(b) == 0:
        ans += 1
        cnt += a.pop(0)
    else:
        if cnt + a[0] > k and cnt + b[0] <= k:
            ans += 1
            cnt += b.pop(0)
            continue
        elif cnt + b[0] > k and cnt + a[0] <= k:
            ans += 1
            cnt += a.pop(0)
            continue
        elif cnt + a[0] > k and cnt + b[0] > k:
            break
        else:
            if chooseBook(0) == "a":
                ans += 1
                cnt += a.pop(0)
            else:
                ans += 1
                cnt += b.pop(0)
print(ans)

Submission status: Runtime Error

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

		if c > m:
			m = c
		

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

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

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

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

Submission status: Runtime Error

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

def Tsundoku(i,j,k,n,m,dp,a,b):
    if dp[i][j]!=None:
        return dp[i][j]
    ans1,ans2 = 0,0
    if i<n and k-a[i]>=0:
        ans1 = 1+Tsundoku(i+1,j,k-a[i],n,m,dp,a,b)
    if j<m and k-b[j]>=0:
        ans2 = 1+Tsundoku(i,j+1,k-b[i],n,m,dp,a,b)
    dp[i][j] = max(ans1,ans2)
    return dp[i][j]
        
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,k,n,m,dp,a,b)
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()))

if N < M:
    for c in range(M - N + 3):
        A.append(1000000001)
elif M < N:
    for c in range(N - M + 3):
        B.append(1000000001)

a = 0
b = 0
k = 0
ans = 0

while k < K:
    if N + M == ans:
        break
    if A[a] >= B[b]: # Bのほうが小さい場合
        k += B[b]
        b += 1
    else: # Aのほうが小さい場合
        k += A[a]
        a += 1
    if k > K:
        break
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
time = 0

if sum(a) + sum(b) <= k:
    print(len(a) + len(b))
    exit()

while time < k:
    if len(a) == 0:
        book = b[0]
    elif len(b) == 0:
        book = a[0]
    else:
        book = min(a[0], b[0])

    time += book
    if time > k:
        break

    ans += 1
    if book == a[0]:
        a.pop(0)
    else:
        b.pop(0)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N,M,K = map(int,input().split())
A = deque(map(int,input().split()))
B = deque(map(int,input().split()))
ans = N+M
time = sum(A) + sum(B)
if time <= K:
    print(ans)
    exit()

#後ろから抜いていく
while time > K:
    if len(A) == 0 and len(B) == 0:
        print(ans)
        exit()
    
    elif len(A) > 0 and len(B) > 0:
        if A[-1] > B[-1]:
            time -= A[-1]
            ans -= 1
        else:
            time -= B[-1]
            ans -= 1
    elif len(A) == 0 and len(B) > 0:
        time -= B[-1]
        ans -= 1
    else:
        time -= A[-1]:
        ans -= 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()))
b = list(map(int, input().split()))

ans = 0
pile = 0

aq = deque(a)
bq = deque(b)

while(pile <= k):
    ans += 1

    if len(aq) == 0 and len(bq) == 0:
        # cant pop
        break

    elif len(aq) == 0 and len(bq) != 0 or aq[0] >= bq[0]:
        #pop form b
        pile += bq.popleft()

    else:
        #pop from a
        pile += aq.popleft()

print(ans-1)

Submission status: Runtime Error

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

def Tsundoku(i,j,k,n,m,dp,a,b):
    if dp[i][j]!=None:
        return dp[i][j]
    ans1,ans2 = 0,0
    if i<n and k-a[i]>=0:
        ans1 = 1+Tsundoku(i+1,j,k-a[i],n,m,dp,a,b)
    if j<m and k-b[j]>=0:
        ans2 = 1+Tsundoku(i,j+1,k-b[i],n,m,dp,a,b)
    dp[i][j] = max(ans1,ans2)
    return dp[i][j]
        
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,k,n,m,dp,a,b)
print(ans)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]


ai = 0
bi = 0

dp = [0 for i in range(M+N+1)]

for i in range(1, N+M+1):
    if ai == N:
        res = b[bi]
        bi += 1
    elif bi == M:
        res = a[ai]
        ai += 1
    elif a[ai] == b[bi]:
        ra = ai; rb = bi
        while a[ra] == b[rb]:
            ra += 1
            rb += 1
            if a[ra] < b[rb]:
                res = a[ai]
                ai += 1
                break
            elif a[ra] > b[rb]:
                res = b[bi]
                bi += 1
                break
    elif a[ai] < b[bi]:
        res = a[ai]
        ai += 1
    else:
        res = b[bi]
        bi += 1
    dp[i] = dp[i-1] + res

#print(dp)

import bisect
print(bisect.bisect_right(dp, K)-1)

Submission status: Runtime Error

Solution code:
a,c,v=map(int,input().split())
arr1=list(map(int,input().strip().split()))[:a]
arr2=list(map(int,input().strip().split()))[:c]
arr3=arr1+arr2
arr3.sort()
sum=0
ct=0
for x in range(0,(a+c)):
  sum=sum+arr[x]
  ct+=1
  if(sum>=v):
    break
if(sum>v):
  print(ct-1)
else:
  print(ct)

Submission status: Runtime Error

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

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

if n<m:
  for i in range(m-n):
    a.append(10000000000)
else:
  for i in range(n-m):
    b.append(10000000000)
    
time = 0
cnt = 0
s, t = 0, 0
while True:
  if cnt==n+m: break
  if a[s]<b[t]:
    time += a[s]
    s += 1
  else:
    time += b[t]
    t += 1
  if time<=k: cnt +=1
  else: break
    
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()))
num = 0

a_i = 0
while k > 0 and a_i < n:
    if  k >= a[a_i]:
        num += 1
        k -= a[a_i]
        a_i += 1
    else:
        break
b_i = 0

while k > 0 and b_i < m:
    if  k >= b[b_i]:
        num += 1
        k -= b[b_i]
        b_i += 1
    else:
        break

real = num
if b_i < m:
    while a_i >= 0:
        num -= 1
        k += a[a_i]
        a_i -= 1
        while k > 0:
            if k >= b[b_i]:
                num += 1
                k -= b[b_i]
                b_i += 1
            else:
                break
        if real < num:
            real = num
            
print(real)

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
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    if i == -1 or j == m: break
    while cost + b[j] <= k and j < m:
        j += 1
        cnt += 1
        cost += b[j]
    cost += - a[i] + b[j]
    i, j = i - 1, j + 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque

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

ans = 0
pile = 0

aq = deque(a)
bq = deque(b)

while(pile <= k):
    ans += 1

    if len(aq) == 0 and len(bq) == 0:
        # cant pop
        break

    elif len(aq) == 0 and len(bq) != 0 or aq[0] >= bq[0]:
        #pop form b
        pile += bq.popleft()

    elif len(aq) != 0 and len(bq) == 0 or aq[0] < bq[0]:
        #pop from a
        pile += aq.popleft()

print(ans-1)

Submission status: Runtime Error

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

count = 0
sum = 0
while 1:
    if n  == 0 and m == 0:
        break
    elif n == 0:
        tmp = b.pop(0)
        m -= 1
    elif m == 0:
        tmp = a.pop(0)
        n -= 1
    elif a[0] > b[0]:
        tmp = b.pop(0)
        m -= 1
    elif a[0] < b[0]:
        tmp = a.pop(0)
        n -= 1

    sum += tmp
    if sum  > k:
        break
    count += 1
print(count)

Submission status: Runtime Error

Solution code:


def choose(a,b,n,m,ai,bi):
    if ai == n:
        return 'b'
    elif bi == m:
        return 'a'
    
    if a[ai] < b[bi]:
        return 'a'
    elif a[ai] > b[bi]:
        return 'b'
    else:
        choose(a,b,n,m,ai+1,bi+1)
    
def main():
    n,m,k = map(int, input().split())
    a = input().split()
    for i in range(n):
        a[i]=int(a[i])
    b = input().split()
    for i in range(m):
        b[i]=int(b[i])

    t = 0
    ai = 0
    bi = 0
    ret = 0
    while(True):
        if ai < n and a[ai] > k:
            ai = n
        if bi < m and b[bi] > k:
            bi = m

        if ai < n and bi < m:
            if choose(a,b,n,m,ai,bi)=='a':
                t += a[ai]
                ai += 1
                #print('a')
            elif choose(a,b,n,m,ai,bi)=='b':
                t += b[bi]
                bi += 1
                #print('b')
        elif ai < n and bi == m:
            t += a[ai]
            ai += 1
        elif ai == n and bi < m:
            t += b[bi]
            bi += 1
        else:
            print(ret)
            return
        
        if t > k:
            print(ret)
            return
        else:
            ret += 1


if __name__=='__main__':
    main()