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:
str = input()
lst = str.split(' ')
n,m,k = lst
n = int(n)
m = int(m)
k = int(k)
str1 = input()
str2 = input()
a = str1.split(' ')
b = str2.split(' ')
i=0
j=0
count = 0
while c<=k :
    if i<len(a):
        c = c+int(a[i])
        if c<=k:
            a.pop(i)
            i = i+1
            count = count+1
        else:
            c = c-int(a[i])
    if j<len(b):
        c = c+int(b[j])
        if c<=k:
            b.pop(j)
            j = j+1
            count = count+1
        else:
            c = c-int(b[j])
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()))

if sum(A) + sum(B) <= K:
    print(len(A) + len(B))
    exit()

a, b = 0, 0
while True:
    if a == N:
        K -= B[b]
        b += 1
    
    elif b == M:
        K -= a[a]
        a += 1

    elif A[a] < B[b]:
        K -= A[a]
        a += 1
    
    elif A[a] > B[b]:
        K -= B[b]
        b += 1

    else:
        tmpa, tmpb = a, b
        while True:
            tmpa += 1
            tmpb += 1
            if tmpa == N:
                K -= B[b]
                b += 1
                break
            elif tmpb == M:
                K -= A[a]
                a += 1
                break
            elif A[tmpa] > B[tmpb]:
                K -= B[b]
                b += 1
                break
            elif A[tmpa] < B[tmpb]:
                K -= A[a]
                a += 1
                break 
    if K < 0:
        break

print(a + b - 1)

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

cnt = 0
AB_list = A_list + B_list
AB_list.sort()

while K >= 0:
  min_AB = min(AB_list)
  if min_AB > K:
    break
  K = K - AB_list[0]
  cnt+=1
  AB_list.pop(0)
  
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()))
c = 0
while a[0] < k or b[0] < k:
    if a[0] < b[0]:
        k -= a.pop(0)
        c += 1
    if a[0] > b[0]:
        k -= b.pop(0)
        c += 1
print(c)

Submission status: Runtime Error

Solution code:
n,m,k = list(map(int,input().split()))
a = list(map(int,input().split()))[::-1]
b = list(map(int,input().split()))[::-1]
cnt_book=0
cnt_time=0

while cnt_time<k:
  if not a or a[-1]>=b[-1]:
    cnt_time+=b.pop()
  else:
    cnt_time+=a.pop()
  if cnt_time>k:
    break    
  cnt_book+=1

print(cnt_book)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10)

def whichDesk(a, b):
    if len(a) == 0:
        return 2
    elif len(b) == 0:
        return 1
    elif a[0] > b[0]:
        return 2
    elif a[0] < b[0]:
        return 1
    else:
        return whichDesk(a[1:], b[1:])

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

t = 0
cnt = 0

while t <= k and (len(a) > 0 or len(b) > 0):
    if whichDesk(a, b) == 2:
        t += b[0]
        b.pop(0)
    else:
        t += a[0]
        a.pop(0)
    if t <= k:
        cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
from sys import stdin
input = stdin.readline
def Tsundoku(i,j,k):
    if (i,j) in dp:
        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])
    if j<m and k-b[j]>=0:
        ans2 = 1+Tsundoku(i,j+1,k-b[j])
    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 = dict()
ans = Tsundoku(0,0,k)
print(ans)

Submission status: Runtime Error

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    i, j = (0, 0)

    ans = 0
    cum = 0

    while True:
        if i < N and j < M:
            if A[i] <= B[j]:
                if cum + A[i] <= K:
                    cum += A[i]
                    i += 1
                    ans += 1
                else:
                    if cum + B[j] <= K:
                        cum += B[j]
                        j += 1
                        ans += 1
                    else:
                        break
            else:
                if cum + B[j] <= K:
                    cum += B[j]
                    j += 1
                    ans += 1
                else:
                    if cum + A[i] <= K:
                        cum += A[i]
                        i += 1
                        ans += 1
                    else:
                        break
        elif i < N:
            if cum + A[i] <= K:
                cum += A[i]
                i += 1
                ans += 1
            else:
                break
        elif j < M:
            if cum + B[j] <= K:
                cum += B[j]
                j += 1
                ans += 1
            else:
                break
        else:
            break

    print(ans)

main()
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    i, j = (0, 0)

    ans = 0
    cum = 0

    while True:
        if i < N and j < M:
            if A[i] <= B[j]:
                if cum + A[i] <= K:
                    cum += A[i]
                    i += 1
                    ans += 1
                else:
                    if cum + B[j] <= K:
                        cum += B[j]
                        j += 1
                        ans += 1
                    else:
                        break
            else:
                if cum + B[j] <= K:
                    cum += B[j]
                    j += 1
                    ans += 1
                else:
                    if cum + A[i] <= K:
                        cum += A[i]
                        i += 1
                        ans += 1
                    else:
                        break
        elif i < N:
            if cum + A[i] <= K:
                cum += A[i]
                i += 1
                ans += 1
            else:
                break
        elif j < M:
            if cum + B[j] <= K:
                cum += B[j]
                j += 1
                ans += 1
            else:
                break
        else:
            break

    print(ans)

main()

Submission status: Runtime Error

Solution code:
N, M, K = 3, 4, 240
ARR = [60, 90, 120]
BRR = [80, 150, 80, 150]


N,M,K = 3, 4, 730
ARR = [60, 90, 120]
BRR = [80, 150, 80, 150]


N,M,K = 5, 4, 1
ARR = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000]
BRR = [1000000000, 1000000000, 1000000000, 1000000000]

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


def calculate(n,m,k,arr,brr):
    arrIndex = 0
    brrIndex = 0

    total = 0
    result = 0

    while True:
        if (brrIndex >= m) and (arrIndex >= n):
            break

        if arrIndex >= n:
            if (total + brr[brrIndex]) <=k:
                total += brr[brrIndex]
                result += 1
                brrIndex += 1
                continue
            else:
                break

        if brrIndex >= m:
            if (total + brr[brrIndex]) <=k:
                total += brr[brrIndex]
                result += 1
                brrIndex += 1
                continue
            else:
                break

        if arr[arrIndex] < brr[brrIndex]:
            if (total + arr[arrIndex]) <= k:
                total += arr[arrIndex]
                result += 1
                arrIndex += 1
            else:
                break
        else:
            if (total + brr[brrIndex]) <=k:
                total += brr[brrIndex]
                result += 1
                brrIndex += 1
            else:
                break

    print(result)

calculate(N,M,K,ARR,BRR)

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 = []
while sum(time) < k:
    if len(a) > 0 and a[0] <= b[0]:
        time += [a.pop(0)]
    elif len(b) > 0:
        time += [b.pop(0)]

if sum(time) > k:
    time.pop(-1)

print(len(time))

Submission status: Runtime Error

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

books   = 0
minutes = 0

while True:
    if len(ai) != 0 and len(bi) != 0:
        if ai[0] <= bi[0]:
            if minutes + ai[0] > k: break

            books += 1
            minutes += ai[0]
            ai = ai[1:]
        else:
            if minutes + bi[0] > k: break

            books += 1
            minutes += bi[0]
            bi = bi[1:]

    else:
        if len(ai) == 0:
            if (minutes + bi[0]) > k: break

            books += 1
            minutes += bi[0]
            bi = bi[1:]
        else:
            if (minutes + ai[0]) > k: break

            books += 1
            minutes += ai[0]
            ai = ai[1:]

print(books)

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

A_index = 0
B_index = 0
time = 0
read = 0
for _ in range(N + M):
    if A_index < N and A[A_index] < B[B_index]:
        if time + A[A_index] > K:
            break
        time += A[A_index]
        A_index += 1
        read += 1
    else:
        if time + B[B_index] > K:
            break
        time += B[B_index]
        B_index += 1
        read += 1
print(read)

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.append(10**9+1)
b.append(10**9+1)

i,j,ans=0,0,0
while k>=0:
    print(i,j)
    if a[i]>10**9:
        k-=b[j]
        j+=1
    elif b[i]>10**9:
        k-=a[i]
        i+=1
    elif a[i]>b[j]:
        k-=b[j]
        j+=1
    else:
        k-=a[i]
        i+=1
    if k<0:
        break
    ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
#C
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
i = 0
j = 0
while n > i or m > j:
    # print(i,j,k)
    if n <= i:
        k -= b[j]
        j += 1
        ans+=1
        continue
    elif n <= j:
        k -= a[i]
        i += 1
        ans+=1
        continue
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k < 0:
        break
    ans += 1
    if k == 0:
        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
count = 0
for _ in range(n + m):
    if len(a) > 0 and len(b)>0:
        if a[0] < b[0]:
            time += a[0]
            a = a[1:]
        else:
            time += b[0]
            b = b[1:]
    elif len(a) == 0 and len(b) > 0:
        time += b[0]
        b = b[]
    elif len(b) == 0 and len(a) > 0:
        time += a[0]
        a = a[:-1]
    if time > k:
        break
    else:
        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()))
max_c = 0
a.reverse()
all = a + b

for i in range(n+1):
  cnt = 0
  t = 0
  j = i
  while t < k:
    t += all[j]
    cnt += 1
    j += 1
  if max_c<cnt-1:
    max_c = cnt-1
print(max_c)

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:
		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 = list(map(int, input().split()))
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 = deque(list(map(int,input().split())))
b = deque(list(map(int,input().split())))

ans = 0
time = 0
for _ in range(n+m):
    if len(a) == 0 and time + b[0] <= k:
        ans += 1
        time += b[0]
        b.popleft()
    elif len(b) == 0 and time + a[0] <= k:
        ans += 1
        time += a[0]
        a.popleft()       
    elif a[0] >= b[0] and time + b[0] <= k:
        ans += 1
        time += b[0]
        b.popleft()
    elif b[0] > a[0] and time + a[0] <= k:
        ans += 1
        time += a[0]
        a.popleft()
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(300000)

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


def calc(k, n):
    if k > K:
        return n - 1
    else:
        if len(A) and len(B):
            a = A.popleft()
            na = calc(k+a, n+1)
            A.appendleft(a)
            b = B.popleft()
            nb = calc(k+b, n+1)
            B.appendleft(b)
            return max(na, nb)
        elif len(A):
            a = A.popleft()
            na = calc(k + a, n + 1)
            A.appendleft(a)
            return na
        elif len(B):
            b = B.popleft()
            nb = calc(k + b, n + 1)
            B.appendleft(b)
            return nb
        else:
            return n


print(calc(0, 0))

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) + [10 ** 10]
b = list(map(int, input().split())) + [10 ** 10]


def dfs(ai, bi, time, finished=False):
    if finished:
        return ai + bi
    
    if ai == n-1 and bi == n-1:
        return ai + bi
    
    if time >= a[ai]:
        ret1 = dfs(ai + 1, bi, time - a[ai])
    else:
        ret1 = dfs(ai, bi, time, finished=True)

    if time >= b[bi]:
        ret2 = dfs(ai, bi + 1, time - b[bi])
    else:
        ret2 = dfs(ai, bi, time, finished=True)

    return max(ret1, ret2)


print(dfs(0, 0, k))

Submission status: Runtime Error

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

count=0
ans=0

i=0
j=0
while i<N or j<M:
    if i==N:
        count+=B[j]
        j+=1
        ans+=1
    elif j==N:
        count+=A[i]
        i+=1
        ans+=1
    elif A[i]<=B[j]:
        count+=A[i]
        i+=1
        ans+=1
    else:
        count+=B[j]
        j+=1
        ans+=1

    if count>K:
        ans-=1
        break

print(ans)

Submission status: Runtime Error

Solution code:
#n = int(input())
n,m,k = map(int,input().split())
a_L = list(map(int,input().split()))
b_L = list(map(int,input().split()))

#a_L = list(reversed(a_L))
#b_L = list(reversed(b_L))

ans = 0
dp = [float("inf")] * (max(n,m)+1)

i = 0
hun = 0
for i in range(max(m,n)):

    if a_L[-1] >= b_L[-1]:
        hun += b_L[-1]
        b_L.pop()
    else:
        hun += a_L[-1]
        a_L.pop()

    ans += 1


    if hun >= k:
        print(ans)
        exit()

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
import heapq
from collections import defaultdict, deque
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;la=len(a);lb=len(b);cnt=0
while (i<la or j<lb) and k>0:
    if i==la:
        if b[j]<=k:
            cnt+=1
            k-=b[j]
            j+=1
        else:
            break
    elif i==lb:
        if a[i]<=k:
            cnt+=1
            k-=a[i]
            i+=1
        else:
            break
    elif a[i]<=b[j]:
        if a[i]<=k:
            k-=a[i]
            cnt+=1
            i+=1
        else:
            break
    else:
        if b[j]<=k:
            k-=b[j]
            cnt+=1
            j+=1
        else:
            break
print(cnt)

Submission status: Runtime Error

Solution code:
a = [input() for l in range(0,3)]
a1 = [int(p) for p in a[0].split()]
a2 = [int(p) for p in a[1].split()]
a3 = [int(p) for p in a[2].split()]
a4 = a2 + a3
b = sorted(a4)
s = 0
t = 0
while s <= a1[2]:
    s = s + b[t]
    t = t + 1
print(t-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()))

# まずaを全部読む
# 全部読めたならbも読む
# 最後の本を捨てて、それがなければどこまでいけるかを試す

ans = 0
cost = 0
b_i = 0
while cost <= k:
  ans += 1
  if ans >= n:
    cost += b[n-ans]
    b_i += 1
  else:
    cost += a[ans]

max_ans = ans
if b_i == 0:
  for i in range(ans-1,-1,-1):
    ans -= 1
    cost -= a[i]
    while cost <= k and b_i < m:
      ans += 1
      cost += b[b_i]
      b_i += 1
    max_ans = max(max_ans, ans)
else:
  for i in range(n-1,-1,-1):
    ans -= 1
    cost -= a[i]
    while cost <= k and b_i < m:
      ans += 1
      cost += b[b_i]
      b_i += 1
    max_ans = max(max_ans, ans)
    
if not a[0] >= k and b[0] >= k:
  print(max_ans)
else:
  print(0)

Submission status: Runtime Error

Solution code:
#C
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
i = 0
j = 0
while n > i or m > j:
    # print(i,j,k)
    if n <= i:
        k -= b[j]
        j += 1
        ans+=1
        continue
    elif n <= j:
        k -= a[i]
        i += 1
        ans+=1
        continue
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k < 0:
        break
    ans += 1
    if k == 0:
        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()))

s=0
count=0

while s <= k:
  if a[0] <= b[0]:
    s=s+a[0]
    count=count+1
    del(a[0])
  else:
    s=s+b[0]
    count=count+1
    del(b[0])
    
print(count-1)

Submission status: Runtime Error

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

idxA = idxB = 0
readTime :int = 0
readBooks :int = 0
while readTime < k:
    if arA[idxA] <= arA[idxB] and idxA < n:
        if k < readTime + arA[idxA]:
            break
        readTime += arA[idxA]
        idxA += 1
    else:
        if k < readTime + arB[idxB]:
            break
        readTime += arB[idxB]
        idxB += 1
    readBooks += 1
    if idxA >= n and idxB >= m:
        break

print(readBooks)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, M, K = MI()
    A = deque(LI())
    B = deque(LI())

    ans = 0
    sm = 0
    while 1:
        #print(A, B, sm)
        if A and B:
            a = A.popleft()
            b = B.popleft()
            if a < b:
                if a + sm <= K:
                    sm = sm + a
                    ans += 1
                    B.appendleft(b)
                else:
                    break
            elif b < a:
                if b + sm <= K:
                    sm = sm + b
                    ans += 1
                    A.appendleft(a)
                else:
                    break
            else:   # a == b
                if a + sm > K: break

                for a2, b2 in zip(A, B):
                    if a2 == b2: continue
                    elif a2 < b2:
                        sm = sm + a
                        ans += 1
                        B.appendleft(b)
                    elif b2 < a2:
                        sm = sm + b
                        ans += 1
                        A.appendleft(a)
        elif A:
            a = A.popleft()
            if sm + a <= K:
                sm = sm + a
                ans += 1
            else:
                break
        elif B:
            b = B.popleft()
            if sm + b <= K:
                sm = sm + b
                ans += 1
            else:
              break
        else:
            break
    print(ans)

if __name__ == '__main__':
    solve()

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
spend = 0
ia = 0
ib = 0
while spend < k:
  if ia < n and A[ia] < B[ib]:
    spend += A[ia]
    ia += 1
    if ia == n:
      ia = n-1
  elif ib < m and A[ia] >= B[ib]:
    spend += B[ib]
    ib += 1
    if ib < m:
      ib = m-1
  elif ia == n-1:
    spend += B[ib]
    ib += 1
    if ib < m:
      ib = m-1
  elif ib == m-1:
    spend += A[ia]
    ia += 1
    if ia == n:
      ia = n-1
    
  count += 1
print(count-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()))
#A = [20, 10, 10, 30]
#B = [30, 100, 10, 50]
#K = 60
count = 0
book = 0
while True:
  if A[0] < B[0]:
    count += A[0]
    A.pop(0)
  elif A[0] > B[0]:
    count += B[0]
    B.pop(0)
  elif A[0] == B[0]:
    if len(A) == 1:
      count += B[0]
      B.pop(0)
    elif len(B) == 1:
      count += A[0]
      A.pop(0)
    else:
      if A[1] < B[1]:
        count += A[0]
        A.pop(0)
      elif A[1] > B[1]:
        count += B[0]
        B.pop(0)
  if count > K:
    break
  book += 1
print(book)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))[::-1]
b=list(map(int,input().split()))[::-1]
c=0
while k>0:
    if len(a)==0:
        k-=b[-1]
        b.pop()
        c+=1
    elif len(b)==0:
        k-=a[-1]
        a.pop()
        c+=1
    elif a[-1]>b[-1]:
        k-=b[-1]
        b.pop()
        c+=1
    elif a[-1]<b[-1]:
        k-=a[-1]
        a.pop()
        c+=1
    else:
        if a[-2]<=b[-2]:
            k-=a[-1]
            a.pop()
            c+=1
        else:
            k-=b[-1]
            b.pop()
            c+=1
    if k<0:
        c-=1
print(c)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a.reverse()
b.reverse()
ans = 0

for i in range(n + m):
    cost = 0
    if a[-1] > b[-1]:
        cost = b[-1]
        b.pop(-1)
    else:
        cost = a[-1]
        a.pop(-1)
    
    k -= cost
    if k <= 0:
        break
    else:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
a = [input() for l in range(0,3)]

a1 = [int(p) for p in a[0].split()]
a2 = [int(p) for p in a[1].split()]
a3 = [int(p) for p in a[2].split()]
a4 = a2 + a3

b = sorted(a4)

s = 0
t = 0
while s <= a1[2]:
    s = s + b[t]
    t = t + 1
print(t-1)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = [int(e) for e in input().split()]
b = [int(f) for f in input().split()]

ans = 0
while True:
    if a[0] <= b[0]:
        k -= a[0]
        if k < 0:
            print(ans)
            break
        else:
            ans += 1
            a.pop(0)
    else:
        k -= b[0]
        if k < 0:
            print(ans)
            break
        else:
            ans += 1
            b.pop(0)

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()))
cnt = 0
ans = 0
for i in range(n+m):
    if len(a) == 0 or a[0] >= b[0]:
        cnt += b[0]
        b.popleft()
        if cnt > k:
            break
    elif len(b) == 0 or a[0] < b[0]:
        cnt += a[0]
        a.popleft()
        if cnt > k:
            break
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
c = 0
while 1:
    a1 = a[0]
    b1 = b[0]
    if a1 < b1:
        a.pop(0)
        k -= a1
        c += 1
    else:
        b.pop(0)
        k -= b1
        c += 1
    if k <= 0:
        break
print(c-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()))
ans = 0
while k > 0:
  i, j = 0
  if a[i] <= b[j]:
    k -= a[i]
    if k >= 0
      i += 1
      ans += 1
  else:
    k -= b[j]
    if k >= 0
      j += 1
      ans += 1
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(' ')))
l=0
for i in range(max(N,M)):
    if i>=min(N,M) and N>M:
        K=K-A[i]
    if i>=min(N,M) and N<M:
        K=K-B[i]
    else:
        K=K-min(A[i],B[i])
    if K<0:
        print(l)
        exit()
    else:
        l=l+1
    if i==max(N,M)-1:
        print(N+M)

Submission status: Runtime Error

Solution code:
n,m,k,*ab = map(int, open(0).read().split())
aa = ab[:n]
bb = ab[m-1:]
ans = 0
i = j = 0
while True:
    if n <= i and m <= j:
        break
    elif n > i and m > j:
        a = aa[i]
        b = bb[j]
        if a < b:
            if a > k:
                break
            i += 1
            ans += 1
            k -= a
        else:
            if b > k:
                break
            j += 1
            ans += 1
            k -= b
    elif n > i:
        a = aa[i]
        if a > k:
            break
        i += 1
        ans += 1
        k -= a
    else:
        b = bb[j]
        if b > k:
            break
        j += 1
        ans += 1
        k -= b
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
if sum(A) + sum(B) <= K:
  print(len(A)+len(B))
else:
  A = deque(A)
  B = deque(B)
  time = 0
  ans = 0
  while time < K:
      if A[0] <= B[0]:
          time += A[0]
          ans += 1
          A.popleft()
      else:
          time += B[0]
          ans += 1
          B.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()))

a_i = 0
b_i = 0 
c = 0
time = 0
for i in range(m+n):
    if a_i >= n:
        time += b[b_i]
        b_i += 1
        c += 1
    
    elif b_i >= m:
        time += a[a_i]
        a_i += 1
        c += 1
    
    elif a[a_i] < b[b_i]:
        time += a[a_i]
        a_i += 1
        c += 1
    
    elif a[a_i] > b[b_i]:
        time += b[b_i]
        b_i += 1
        c += 1
    
    elif a[a_i] == b[b_i]:
        if a[a_i+1] > b[b_i+1]:
            time += b[b_i]
            b_i += 1
            c += 1
        
        else:
            time += a[a_i]
            a_i += 1
            c += 1
    
    
    
    if time > k:
        c -= 1
        break
        
print(c)

Submission status: Runtime Error

Solution code:
n,m,k = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
x=0
t=0
a=0
for i in range (n):
    a+=A[i]
for i in range(m):
    a+=B[i]
if a<=k:
    print(n+m)
else:
    for i in range (max(n,m)):
        t=t+1
        x+=min(A[0],B[0])
        if A[0]<=B[0]:
           A.pop(0)
        else:
            B.pop(0)
        if x>k:
            print(t-1)
            break