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: Wrong Answer

Solution code:
import sys
import copy
from collections import deque
import collections
stdin = sys.stdin

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m, k = na()

a = na()
b = na()

num_books = 0
sum_time = 0
for i in a:
    if sum_time+i > k:
        break
    num_books += 1
    sum_time += i
# print(num_books)


res = num_books
ia = num_books - 1
ib = 0
while ia >= 0 and ib < len(b):
    if sum_time < k:
        sum_time += b[ib]
        ib += 1
        num_books += 1
    else:
        sum_time -= a[ia]
        ia -= 1
        num_books -= 1
    if sum_time <= k:
        res = max(res, num_books)

print(res)

Submission status: Wrong Answer

Solution code:
import sys

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

count_a=0
count_b=0
total=0

if k < a[0] and k < b[0] :
    print(0) 
    sys.exit()

while(0<=k) :
    
    try:
        ai=a[count_a]
    except :
        ai=None
    try:
        bi=b[count_b]
    except :
        bi=None
    
    if ai is not None and bi is not None :
        if ai < bi :
            k -= ai 
            count_a+=1
        elif bi<ai:
            k-=bi
            count_b+=1
        
        else :
            if count_a > count_b :
                k-= ai 
                count_a+=1
            else :
                k-=bi
                count_b+=1
            
        total+=1
    
    
    elif ai is not None and bi is None :
        
        k-= ai 
        count_a+=1
        
        total+=1
    
    elif ai is None and bi is not None :
        k-=bi
        count_b+=1
        total+=1
        
    else :

        break

if k < 0 :
    print(total-1)
else :
    print(total)

Submission status: Wrong Answer

Solution code:
import itertools

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

aa = [0] + list(itertools.accumulate(A))
bb = [0] + list(itertools.accumulate(B))

ans = 0
for i in range(N+1):
    tmp_K = K-aa[i]
    if tmp_K == 0:
        ans = max(ans, i)
    elif tmp_K < 0:
        continue

    if i + M <= ans:
        continue

    if ans != 0 and bb[(ans-i+1)] >= tmp_K:
        continue

    if tmp_K >= bb[-1]:
        ans = max(ans, i+M)
        continue

    mn = 0
    mx = M
    while True:
        if mn == mx:
            if tmp_K >= bb[mn]:
                ans = max(ans, i+mn)
            break

        mid = (mn+mx//2)
        if bb[mid] <= tmp_K:
            if mn == mid:
                mn = mid+1
            else:
                mn = mid

        else:
            if mx == mid:
                mx = mid-1
            else:
                mx = mid
            if i+mx <= ans:
                break
print(ans)

Submission status: Wrong Answer

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

t = 0
ans = 0
i = 0
j = 0
while t < k:
    if i < n and j < m:
        if a[i] < b[j]:
            if t + a[i] <= k:
                t += a[i]
                i += 1
            else:
                break
        elif a[i] < b[j]:
            if t + b[j] <= k:
                t += b[j]
                j += 1
            else:
                break
        else:
            if a[i] + t > k:
                break
            x = i
            y = j
            while a[x] == b[y] and x < n-1 and y < m-1:
                x += 1
                y += 1
            if a[x] < b[y]:
                while i < x and t < k:
                    if t + a[i] <= k:
                        t += a[i]
                        i += 1
                        n += 1
                    else:
                        break
            elif a[x] > b[y]:
                while j < y and t < k:
                    if t + b[j] <= k:
                        t += b[j]
                        j += 1
                        n += 1
                    else:
                        break
            else:
                if y+1 == m:
                    if t + a[i] <= k:
                        t += a[i]
                        i += 1
                    else:
                        break
                else:
                    if t + b[j] <= k:
                        t += b[j]
                        j += 1
                    else:
                        break
    elif i < n:
        if t + a[i] <= k:
            t += a[i]
            i += 1
        else:
            break
    elif j < m:
        if t + b[j] <= k:
            t += b[j]
            j += 1
        else:
            break
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
#C
time = 0
books = 0
count_N = 0
count_M = 0

line1 = input().split()
N = int(line1[0])
M = int(line1[1])
K = int(line1[2])

Tukue1 = input().split()
Tukue2 = input().split()


while True:
  try:
    if int(Tukue1[count_N]) < int(Tukue2[count_M]):
      #机Aを読む
      time = time + int(Tukue1[count_N])
      count_N = count_N + 1
    else:
      #机Bを読む
      time = time + int(Tukue2[count_M])
      count_M = count_M + 1

    if time > K:
      break
    
    books = books + 1
      
  except IndexError:
    break
    
while True:
  try:
    if len(Tukue1) == count_N:
      #机Bを読む
      time = time + int(Tukue2[count_M])
      count_M = count_M + 1
    elif len(Tukue2) == count_M:
      #机Aを読む
      time = time + int(Tukue1[count_N])
      count_N = count_N + 1
    else:
      break

    if time > K:
      break

    books += 1
    
  except IndexError:
    break

print(books)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
 
reading = 0
read_book = 0
i = 0
while reading <= K and i+1 <= max([N ,M]):
  if i+1 <= N:
    reading += A[i]
    if reading <= K:
      read_book += 1
    else:
      break
  if i+1 <= M:
    reading += B[i]
    if reading <= K:
      read_book += 1
    else:
      break
  i += 1
 
print(read_book)

Submission status: Wrong Answer

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


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    weightA = 0
    tmp_A = N
    for idx, a in enumerate(A):
        if weightA + a > K:
            tmp_A = idx
            break
        else:
            weightA += a
    max_count = current_count  = tmp_A
    len_A = tmp_A

    weightB = 0
    terminated = False
    for b in B:
        if terminated == True and weightA + weightB > K:
            break

        if len_A==0 and weightA + weightB + b > K:
            break

        if weightA + weightB + b <= K:
            weightB += b
            current_count += 1

        elif terminated==False:
            while weightA + weightB + b > K:
                if len_A>0:
                    weightA -= A[len_A-1]
                    len_A -= 1
                    current_count -= 1
                else:
                    terminated=True

        max_count = max(max_count, current_count)

    print(max_count)
    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
input1 = list(map(int, input().split()))
N = input1[0]
M = input1[1]
K = input1[2]

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

num_a = 0
num_b = 0

book_count = 0
time_count = 0

flag_a = True
flag_b = True

tmp = 0

def timecheck(time_count):
    return(True if time_count < K else False )


while (num_a < N and num_b < M):
    if(A[num_a] < B[num_b]):
        time_count += A[num_a]
        num_a += 1
    else:
        time_count += B[num_b]
        num_b += 1
    
    if(timecheck(time_count) == False):
        break




while (timecheck(time_count) == True and num_a < N):
    time_count += A[num_a]
    num_a += 1
    if(timecheck(time_count) == False):
        break

while (timecheck(time_count) == True and num_b < M):
    time_count += B[num_b]
    num_b += 1
    if(timecheck(time_count) == False):
        break

print(num_a + num_b - 1 if time_count > K else num_a + num_b)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for _ in range(m):
    A.append(10 ** 9 + 1)
for _ in range(n):
    B.append(10 ** 9 + 1)
A.sort(reverse=True)
B.sort(reverse=True)
total = 0
count = 0
for i in range(n + m):
    time = min(A[-1], B[-1])
    if A[-1] == time:
        total += A.pop()
    elif B[-1] == time:
        total += B.pop()
    if total > k:
        break
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
A, B, t = map(int, input().split())

desk_A = list(map(int, input().split()))
desk_B = list(map(int, input().split()))

i = 0
j = 0
count = 0

while(i > A and j > B):
    if desk_A[i] < desk_B[j]:
        if t >= desk_A[i]:
            t -= desk_A[i]
            i += 1
        else:
            break
    else:
        if t >= desk_B[j]:
            t -= desk_B[j]
            j += 1
        else:
            break
    count += 1

while(i < A and t > 0):
    if t >= desk_A[i]:
        t -= desk_A[i]
        i += 1
        count += 1
    else:
        break
  
while(j < B and t > 0):
    if t >= desk_B[j]:
        t -= desk_B[j]
        j += 1
        count += 1
    else:
        break
    
print(count)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int, input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
cnt=0
temp=0
Ai=0
Bi=0
for i in range(n+m):
  if Ai==n and Bi==m:
    exit()
  elif Ai==n and Bi<m:
    if temp+B[Bi]<=k:
      temp+=B[Bi]
      cnt+=1
      Bi+=1
    else:
      exit()
  elif Bi==m and Ai<n:
    if temp+A[Ai]<=k:
      temp+=A[Ai]
      cnt+=1
      Ai+=1
    else:
      exit()
  elif Ai<n and Bi<m:
    if A[Ai]<=B[Bi]:
      if temp+A[Ai]<=k:
        temp+=A[Ai]
        cnt+=1
        Ai+=1
      else:
        exit()
    elif A[Ai]>B[Bi]:
      if temp+B[Bi]<=k:
        temp+=B[Bi]
        cnt+=1
        Bi+=1
      else:
        exit()
print(cnt)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
t = 0
ia = 0
ib = 0
ans = 0
#qa = deque()
#qb = deque()
while t <= K:
    #print('ans=' + str(ans), ia,ib)
    if ia == N and ib == M:
        ans = N+M
        t = sum(A)+sum(B)
        break
    if ia < N and ib < M:
        if A[ia] <= B[ib]:
            t += A[ia]
            ia += 1
            ans += 1
        else:
            t += B[ib]
            ib += 1
            ans += 1
    elif ia == N:
        t += B[ib]
        ib += 1
        ans += 1
    elif ib == M:
        t += A[ia]
        ia += 1
        ans += 1
    #print(ans)
#print(t,ans)
ans = ia+ib
if t > K:
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

time = K
ans = 0
a = 0
b = 0
while True:
    if a >= N:
        if b >= M-1:
            if B[b] == time:
                ans += 1
            break
        time -= B[b]
        b += 1
    elif b >= M:
        if a >= N-1:
            if A[a] == time:
                ans += 1
            break
        time -= A[a]
        a += 1
    else:
        if A[a] > K or B[b] > K:
            break
        if(A[a] < B[b]):
            time -= A[a]
            a += 1
        else:
            time -= B[b]
            b += 1
    if time <= 0:
        if time == 0:
            ans += 1
        break
    ans += 1

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
len_A = len(A)
len_B = len(B)
it = 200000
k = 0
count = 0
select = list()
choice = 'A'
for i in range(it):
  if len_A == 0 and len_B == 0:
    break
  elif len_A == 0 and len_B != 0:
    # Bの本のみ
    select = B
    choice = 'B'
  elif len_B == 0 and len_A != 0:
    # Aの本のみ
    select = A
    choice = 'A'
  elif A[0] <= B[0]:
    # BよりもAの方が早く読める(同じも含む)
    select = A
    choice = 'A'
  elif A[0] > B[0]:
    select = B
    choice = 'B'
  k += select[0]
  if k <= K:
    count += 1
  else:
    break
  if choice == 'A':
    len_A -= 1
  else:
    len_B -= 1
  select.pop(0)
print(count)

Submission status: Wrong Answer

Solution code:
import sys
n, m, k = map(int, input().split())
s = list(map(int, input().split())) 
t = list(map(int, input().split())) 
time = 0
book = 0
for i in range(n + m + 2):
    if s != [] and t != []: 
        u = min(s[0], t[0])
    if s != [] and t == []:
        u = s[0]
    if s == [] and t != []:
        u = t[0]
    if s == t == []:
        print(book)
        sys.exit()
    if time + u <= k:
        time += u
        if u in s:
            s.remove(u)
        else:
            t.remove(u)
        book += 1
    if time + u > k:
        print(book)
        sys.exit()

Submission status: Wrong Answer

Solution code:

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

time = 0
cnt = 0
b = []

for i in B:
    if i+time>K:
        break
    time += i
    cnt += 1
    b.append(i)

b.reverse()
# print(b)
# print(cnt)
bi = 0
for i in range(N):
    if time+A[i] <= K:
        cnt += 1
        time += A[i]
    else:
        if len(b)-1 >= bi:
            if A[i] <= b[bi]:
                time -= b[bi]-A[i]
                bi += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)

time2 = 0
cnt2 = 0
a = []

for i in A:
    if i+time>K:
        break
    time2 += i
    cnt2 += 1
    a.append(i)

a.reverse()
# print(b)
# print(cnt)
ai = 0
for i in range(M):
    if time2+B[i] <= K:
        cnt2 += 1
        time2 += B[i]
    else:
        if len(a)-1 >= ai:
            if B[i] <= a[ai]:
                time2 -= a[ai]-B[i]
                ai += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)


print(max(cnt, cnt2))

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))
book = n + m
nokori = k
Ans = 0
for i in range(book):
    if an[0] <= bn[0]:
        nokori = nokori - an[0]
        del an[0]
        an.append(1000000000)
    elif an[0] > bn[0]:
        nokori = nokori - bn[0]
        del bn[0]
        bn.append(1000000000)
    if nokori < 0:
        break
    Ans = Ans + 1
print(Ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
time = 0
book_a = 0
book_b = 0

for i in range(len(a)):
    if time > k:
        break
    for j in range(len(b)):
        if time > k:
            break
        elif a[i] < b[j]:
            time = time + a[i]
            book_a = book_a + 1
            break
        else:
            time = time + b[j]
            book_b = book_b + 1

bb = b[book_b:]
if time <= k:
    for i in bb:
        if time > k:
            break
        else:
            time = time + i
            book_b = book_b + 1

if book_a + book_b == len(a) + len(b):
    print(book_a + book_b)
else:
    print(book_a + book_b -1)

Submission status: Wrong Answer

Solution code:
N, M, K = (int(s) for s in input().rstrip().split())

A = [int(s) for s in input().rstrip().split()]
B = [int(s) for s in input().rstrip().split()]

i, j = (0, 0)
sum, count = (0, 0)

while sum < K:
  if (i < N) and (j < M):
    if A[i] <= B[j]:
      sum += A[i]
      i += 1
      count += 1
    else:
      sum += B[j]
      j += 1
      count += 1
  elif i < N:
    sum += A[i]
    i += 1
    count += 1
  elif j < M:
    sum += B[j]
    j += 1
    count += 1
  else:
    count += 1
    break

if sum > K:
  count -= 1

print(count)

Submission status: Wrong Answer

Solution code:
a = input().split(" ")
N = int(a[0])
M = int(a[1])
K = int(a[2])
Ai = input().split(" ")
Bi = input().split(" ")
Ai = [int(s) for s in Ai]
Bi = [int(s) for s in Bi]
time_ = int(K)
time = 0
acount = 0
bcount = 0

for i in range(10000000000000000):
    try:
        time += Ai[i]
        if time > time_:
            time -= Ai[i]
            break
        acount += 1
    except IndexError:
        break
atime = time
time = 0
for i in range(10000000000000000):
    try:
        time += Bi[i]
        if time > time_:
            time -= Bi[i]
            break
        bcount += 1
    except IndexError:
        break
btime = time
allc = bcount
if bcount > acount:
    hic = bcount
else:
    hic = acount
# AiからBiに移す
cip = 0
for i in range(acount):
    ci = i + cip
    cip = 0
    try:
        while time + Ai[ci] <= time_:
            time += Ai[ci]
            allc += 1
            ci += 1
            cip += 1
        if hic < allc:
            hic = allc
    except IndexError:
        continue
    try:
        time -= Bi[bcount - 1]
        bcount -= 1
        allc -= 1
    except IndexError:
        pass
    try:
        while time + Ai[ci] <= time_:
            time += Ai[ci]
            allc += 1
            ci += 1
            cip += 1
        if hic < allc:
            hic = allc
    except IndexError:
        break

print(hic)

Submission status: Wrong Answer

Solution code:
import sys
 
n, m, k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))

time = 0
a_cnt = 0
cnt_list = []
for a_i in a:
    time_tmp = time + a_i
    if time_tmp <= k:
        time = time_tmp
        a_cnt += 1
    else:
        break
cnt_list.append(a_cnt)

right_outer = 0
for left_outer in range(a_cnt, 0, -1):
    time -= a[left_outer-1]
    for b_idx in range(right_outer, len(b)+1):
        if b_idx >= len(b):
            break
        time_tmp = time + b[b_idx]
        if time_tmp <= k:
            time = time_tmp
            right_outer += 1
        else:
            break
    cnt_list.append(left_outer + right_outer)
    print(left_outer, right_outer)

print(max(cnt_list))

Submission status: Wrong Answer

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

# N = [3 ,4, 0]
# A = [60, 90, 120]
# B = [80, 150, 80, 150]

M = N[1]
K = N[2]
N = N[0]

s = 0
c1 = 0
c2 = 0

s1 = [A[0]]
s2 = [B[0]]
for i in range(N-1):
    s1.append(s1[i]+A[i+1])
for i in range(M-1):
    s2.append(s2[i]+B[i+1])

k = 0
C = [k]
for i in range(N):
    if s1[i] > K:
        break
    while s2[k] <= K-s1[i]:
        k += 1
        if k == M:
            break
    k -= 1
    C.append(k+i+2)

print(max(C))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    time, ans = 0, 0
    a, b = 0, 0
    for _ in range(n+m+5):
        if time > k:
            print(ans-1)
            return
        if a < n and b < m:
            if A[a] < B[b]:
                time += A[a]
                a += 1
            else:
                time += B[b]
                b += 1
        elif a == n and b < m:
            time += B[b]
            b += 1
        elif a < n and b == m:
            time += A[a]
            a += 1
        else:
            print(ans)
            return
        ans += 1

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
N, M, K = [int(x) for x in input().split()]
A = [float(x) for x in input().split()]
B = [float(x) for x in input().split()]

AB = list()
a = 0
b = 0
for i in range(N+M):
  if ((a < N) and (b < M)):
    if(A[a] == B[b]):
      AB.append(A[a])
      a += 1
      AB.append(B[b])
      b += 1
    elif(A[a] < B[b]):
      AB.append(A[a])
      a += 1
    else:
      AB.append(B[b])
      b += 1
  elif(a >= N):
    for j in range(M-b):
      AB.append(B[b])
      b += 1
    break
  elif(b >= M):
    for j in range(N-a):
      AB.append(A[a])
      a += 1
    break

ans = 0
while K >= 0:
  K -= AB[ans]
  ans += 1
  if(ans == (N+M)):
    print(N+M)
    sys.exit()
  
print(ans-1)

Submission status: Wrong Answer

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

def main():
    sum = 0
    num = 0

    while sum < k:
        if len(a) == 0:
            sum += b[0]
            num += 1
            b.pop(0)
        
        elif len(b) == 0:
            sum += a[0]
            num += 1
            a.pop(0)

        elif a[0] <= b[0]:
            sum += a[0]
            num += 1
            a.pop(0)
    
        elif b[0] < a[0]:
            sum += b[0]
            num += 1
            b.pop(0)
    
    if sum > k:
        print(num-1)
    elif sum == k:
        print(num)

main()

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = 0
a_i = 0
b_i = 0
while(t <= k):
  if(10**9 < t):
    break
  if(a_i < len(a) and t + a[a_i] <= 10**9 and (b_i == len(b) or a[a_i] <= b[b_i])):
    t += a[a_i]
    a_i += 1
  elif(b_i < len(b) and t + b[b_i] <= 10**9 and (a_i == len(a) or b[b_i] < a[a_i])):
    t += b[b_i]
    b_i += 1
  else:
    break
print(a_i + b_i)

Submission status: Wrong Answer

Solution code:
import heapq
N,M,V = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
S=list(map(int,A+B))
heapq.heapify(S)
heapq.heapify(A)
heapq.heapify(B)
ans=0
time=0
if sum(S)<=V:
  print(N+M)
  exit()
else:
  for i in range(N+M):
   
    if time+min(S)<V:
      if min(A)<min(B):
        time+=heapq.heappop(A)
        ans+=1
      else:
        time+=heapq.heappop(B)
        ans+=1
    else:
      print(ans)
      exit()

Submission status: Wrong Answer

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

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

sumA=[A[0]]
sumB=[B[0]]


for i in range(1,N):
  sumA.append(sumA[-1]+A[i])

for a in range(len(sumA)):
  if sumA[a]>K:
    lessthanA=a-1
    break
if sumA[-1]<=K:
  lessthanA=N-1
 
for i in range(1,M-1):
  sumB.append(sumA[-1]+B[i])

totalBook=0
time=0

if A[0]>K and B[0]>K:
  totalBook=0
elif A[-1]+B[-1]<=K:
  totalBook=N+M

else:
  for a in range(lessthanA,-1,-1):
    #print(a)
    for b in range(len(sumB)):
      if sumB[b]>=K-sumA[a]:
        break
      
    if a+b>=totalBook:
      #print("totalBook")
      #print(a)
      #print(b)
      totalBook=a+b+1

print(totalBook)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
An = []
Bn = []
for n in range(N):
    if n == 0:
        An.append(A[n])
    else:
        An.append(An[n-1]+A[n])

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


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

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

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


print(old)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n, m, k = nm()
a = nl()[::-1]
b = nl()[::-1]
a = [10**9+1]*(10**6) + a
b = [10**9+1]*(10**6) + b
cnt = 0
time = 0
while True:
    if a[-1] < b[-1]:
        tmp= a.pop()
    else:
        tmp = b.pop()
    
    if tmp + time <= k:
        cnt += 1
        time += tmp
    else:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
num = N+M
ans = 0
for i in range(N):
    for j in range(M):
        total = sum(A[0:(i+1)]) + sum(B[0:(j+1)])
        if K >= total:
            if i+j+2 > ans:
                ans = i+j+2
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
n = input()
m = input()
first = a.split(" ")
N = n.split(" ")
M = m.split(" ")
overflow = 0
for i in N:
    if int(i) > 10**9:
        overflow = 1
        break
        
for i in M:
    if int(i) > 10**9:
        overflow = 1
        break
if overflow == 0:
    totaltime = 0
    count = 1
    for i in range(int(first[1]) + int(first[0])):
      if i == 6:
        break
      if  len(N) != 0 and len(M) != 0:
        if totaltime + int(N[0]) > int(first[2]) or totaltime + int(M[0]) > int(first[2]):
          break
      if len(N) == 0:
        totaltime += int(M[0])
        M.remove(M[0])
        count += 1
        if totaltime > int(first[2]):
          break
      elif len(M) == 0:
        totaltime += int(N[0])
        N.remove(N[0])
        count += 1
        if totaltime > int(first[2]):
          break
      elif 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)
else:
    print(0)

Submission status: Wrong Answer

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

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

ans = 0
time = 0
while True:

    if not a and not b:
        break
    elif a and not b:
        mt = a[0]
        a.pop(0)
    elif not a and b:
        mt = b[0]
        b.pop(0)
    else:
        mt = min(a[0], b[0])
        if mt == a[0]:
            a.pop(0)
        if mt == b[0]:
            b.pop(0)
    
    time += mt
    if time > k:
        break

    ans += 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N,M,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    # dp[i] := i冊読み、Aをえらんだ回数がa回としたときの(a,最小時間)
    INF = float('inf')
    dp = [INF]*(N+M+1)
    dpa = [0]*(N+M+1)
    dp[0] = 0
    ans = 0
    for i in range(1,N+M+1):
        # i冊目がA
        if dpa[i-1]<N:
            if dp[i]>dp[i-1]+A[dpa[i-1]]:
                dpa[i] += 1
                dp[i] = dp[i-1]+A[dpa[i-1]]

        # i冊目がB
        prevb = (i-1)-dpa[i-1]
        if prevb<M:
            dp[i] = min(dp[i], dp[i-1]+B[prevb])

        if dp[i]>K:
            print(i-1)
            return
    print(i)



main()

Submission status: Wrong Answer

Solution code:
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
list = []
tmp = 0
count = 0
while len(A) != 0 and len(B) != 0:
  if A[0] < B[0]:
    list.append(A.pop(0))
  else:
    list.append(B.pop(0))
  if len(A) == 0:
    list.extend(B)
  elif len(B) == 0:
    list.extend(A)
  if sum(list) > K:
    break
    
while len(list) != 0:
  tmp += list.pop(0)
  if tmp > K:
    break
  count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
a.append(1000000000)
b.append(1000000000)
nc = 0
mc = 0
cnt = 0
t = 0
while cnt + a[nc] <= k or cnt + b[mc] <= k:
    if a[nc] > b[mc]:
        cnt += b[mc]
        mc += 1
    else:
        cnt += a[nc]
        nc += 1
    t += 1
print(t)

Submission status: Wrong Answer

Solution code:

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

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

AL = []
BL = []
a = 0
for i in A:
    a += i
    AL.append(a)
a = 0
for i in B:
    a += i
    BL.append(a)

#print(AL)
#print(BL)

a = 0
for i in range(len(AL)):
    if AL[i] > k:
        a = i
        break
else:
    a = len(AL)

b = 0
for i in range(len(BL)):
    if BL[i] > k:
        b = i
        break
else:
    b = len(BL)

#print(a,b)

m = a
u = 0
for j in reversed(range(a)):
#    print(j,u)
    t = k-AL[j]
    for i in range(u,b):
        if t-BL[i]>=0:
            if m < j+1 + i +1:
                m = j+1 + i +1
#                print(m)
        else:
            u = i
            break
        
    if j+1+b<=m:
        break
if a == 0 and b > 0:
    m = b
print(m)