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:
from collections import deque


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

if sum(A) + sum(B) <= k:
    print(n + m)
    exit()

ans = 0
time = 0
while time <= k:
    a = A[0] if A else float("inf")
    b = B[0] if B else float("inf")
    if a < b:
        time += a
        A.popleft()
        ans += 1
    else:
        time += b
        B.popleft()
        ans += 1

print(ans - 1)

Submission status: Wrong Answer

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

  A.reverse()
  B.reverse()

  elapsed = 0
  ans = 0

  desk = []

  while elapsed <= K:
    al = len(A)
    bl = len(B)
    
    if al == 0 and bl == 0:
      break
    
    if al == 0 :
      desk = B
    
    elif bl == 0:
      desk = A

    elif A[-1] >= B[-1]:
      desk = B

    elif A[-1] < B[-1]:
      desk = A

    else:
      break

    time = desk.pop()
    elapsed += time

    if elapsed > K:
      break

    ans += 1
      
  print(ans)


if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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

time = 0
ans = 0
while True:
  al = len(A)
  bl = len(B)
  if al > 0 and bl > 0 and (time + A[0] <= K or time + B[0] <= K):
    pass
  elif al > 0 and bl == 0 and (time + A[0] <= K):
    pass
  elif al == 0 and bl > 0 and (time + B[0] <= K):
    pass
  else:
    break
  if al > 0 and bl > 0:
    if A[0] >= B[0]:
      time += B.pop(0)
    else:
      time += A.pop(0)
  elif al > 0 and bl == 0:
    time += A.pop(0)
  elif al == 0 and bl > 0:
    time += B.pop(0)
  ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

num_books = 0
remaining_time = K

while True:
    consumption = 0

    if len(A) == 0 and len(B) == 0: break

    if len(A) == 0:
        consumption = B.popleft()
    elif len(B) == 0:
        consumption = A.popleft()
    elif A[0] <= B[0]:
        consumption = A.popleft()
    else:
        consumption = B.popleft()
    # print(consumption)
    
    remaining_time -= consumption
    if remaining_time < 0: break

    num_books += 1

print(num_books)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
sub = 0
a_ls_cnt = 0
a_ls_len = len(a_ls)
now_time = 0
#aのみを選択した場合
#このwhile終了後はa_ls_cntは読んだaの本数になる
while now_time < k:
    now_time+=a_ls[a_ls_cnt]
    if now_time > k:
        now_time-=a_ls[a_ls_cnt]
        break
    a_ls_cnt+=1
    if a_ls_len == a_ls_cnt:
        break
#print(a_ls_cnt)
b_ls_cnt = 0
b_ls_len = len(b_ls)
#a全部をよんだ段階で，bで読めるだけ
while now_time < k:
    now_time+=b_ls[b_ls_cnt]
    if now_time > k:
        now_time-=b_ls[b_ls_cnt]
        break
    b_ls_cnt+=1
    if b_ls_len == b_ls_cnt:
        break

sub = a_ls_cnt+b_ls_cnt
break_flg = 0
#print(sub)
#少しずつaで読んだ本数削る
while a_ls_cnt >= 1:
    if break_flg == 1:
        break
    now_time-=a_ls[a_ls_cnt-1]
    a_ls_cnt-=1
    while now_time < k:
        #print("b_ls",b_ls_cnt, b_ls_len)
        if b_ls_cnt == b_ls_len:
            break_flg = 1
            sub = max(sub, a_ls_cnt+b_ls_cnt)
            break
        now_time+=b_ls[b_ls_cnt]
        if now_time > k:
            now_time-=b_ls[b_ls_cnt]
            break
        b_ls_cnt+=1
    #sub = max(sub, a_ls_cnt+b_ls_cnt)
    #print(a_ls_cnt, b_ls_cnt, sub)
print(sub)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M , W = MAP()
A = LIST()
B = LIST()
A = deque(A)
B = deque(B)

# C = A + B
cnt = 0
while W >= 0 and cnt != N + M:
    if len(A) != 0 and len(B) != 0:
        a = A[0]
        b = B[0]
        if a <= b:
            W -= a
            A.popleft()
        else:
            W -= b
            B.popleft()
    elif len(A) != 0:
        # a = INF
        a = A.popleft()
        W -= a
    elif len(B) != 0:
        # a = INF
        b = B.popleft()
        W -= b
    # print(W)
    if W >= 0:
        cnt += 1


print(cnt)



# 3 4 240
# 60 90 120
# 80 150 80 150
# 出力例 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()))
t = 0
cnt = 0
i = 0
j = 0

while True:
  if i < N:
    a = A[i]
  else:
    a = 10 ** 10
  if j < M:
    b = B[j]
  else:
    b = 10 ** 10
  if t + a > K and t + b > K:
    break
  
  if a < b:
    t += a
    i += 1
  else:
    t += b
    j += 1
  cnt += 1
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()))

max_book_num = 0
for right_book_num in range(len(B)):
    total_time = 0
    total_book_num = 0
    right_book_total_time = 0
    for i in range(right_book_num + 1):
        right_book_total_time += B[i]
        total_book_num += 1
    #print()
    #print(right_book_num, right_book_total_time)
    if right_book_total_time > k:
        break
    else:
        #total_book_num = right_book_num
        total_time = right_book_total_time
        for j in range(len(A)):
            #print("c", A[j] + total_time)
            if A[j] + total_time > k:
                break
            else:
                total_time += A[j]
                total_book_num += 1
                #print(total_book_num, total_time)
                max_book_num = max(max_book_num, total_book_num)
                #print("max", max_book_num, total_time)
print(max_book_num)

Submission status: Wrong Answer

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

    now_A = 0
    now_B = 0
    ans = 0
    minute = 0

    for _ in range(N + M):
        AA = A[now_A] if now_A < N else 10 ** 9
        BB = B[now_B] if now_B < M else 10 ** 9

        minute += min(AA, BB)
        if (minute > K):
            break

        ans += 1
        if (now_A < N and AA < BB):
            now_A += 1
        if (now_B < M and AA >= BB):
            now_B += 1

    print(ans)

resolve()

Submission status: Wrong Answer

Solution code:
n,m,k=map(int, input().split())
a=list(map(int,input().split()))+[10**9+1]*2
b=list(map(int,input().split()))+[10**9+1]+[1]
i=0
j=0
cnt=0
while(True):
    if(a[i]<b[j]):
        k=k-a[i]
        i+=1
    elif(a[i]>b[j]):
        k=k-b[j]
        j+=1
    else:
        tmp=0
        while(True):
            if(a[i+tmp]<b[j+tmp]):
                k=k-a[i]
                i+=1
                break
            elif(a[i+tmp]>b[j+tmp]):
                k=k-b[j]
                j+=1
                break
            else:
                tmp+=1
    if(k<0):
        break
    cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
import itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,M,K = LI()
A = LI()
B = LI()
accumulate_A = list(itertools.accumulate(A))
accumulate_B = list(itertools.accumulate(B))
ans = 0
for i in range(N):
    if accumulate_A[i]>K:
        break
    for j in range(M):
        if accumulate_B[j]>K:
            break
        if accumulate_A[i]+accumulate_B[j]>K:
            break
        ans = max(ans,i+j+2)
print(ans)

Submission status: Wrong Answer

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

a_i = 0
b_i = 0
time = 0
counter = 0

while time < k:
    if a_i >= len(a_list):
        time += b_list[b_i]
        if time > k:
            break
        counter += 1
        b_i += 1
    elif b_i >= len(b_list):
        time += a_list[a_i]
        if time > k:
            break
        counter += 1
        a_i += 1
    elif a_list[a_i] <= b_list[b_i]:
        time += a_list[a_i]
        if time > k:
            break
        counter += 1
        a_i += 1
    else:
        time += b_list[b_i]
        if time > k:
            break
        counter += 1
        b_i += 1

print(counter)

Submission status: Wrong Answer

Solution code:
def main():
    from builtins import int, map, list, print, len
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = (lambda: (sys.stdin.readline()).rstrip())
    input_list = (lambda: input().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    N, M, K = input_number_list()
    A = input_number_list()
    B = input_number_list()
    idx_a = 0
    idx_b = 0
    ans = 0
    time = 0
    while True:
        if time > K:
            ans -= 1
            break
        if idx_a == len(A) and idx_b == len(B) or time == K:
            break
        if idx_a < len(A) and idx_b < len(B):
            if A[idx_a] < B[idx_b]:
                time += A[idx_a]
                idx_a += 1
                ans += 1
            else:
                time += B[idx_b]
                idx_b += 1
                ans += 1
        elif idx_a < len(A):
            time += A[idx_a]
            idx_a += 1
            ans += 1
        elif idx_b < len(B):
            time += B[idx_b]
            idx_b += 1
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

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]"):
    cur = sum(A)
    for i in range(N-1,-1,-1):
        if cur>K: cur -= A[i]
        else:break
    if cur==0: max_k = 0
    else: max_k = i+1
    p = 0
    # print(cur,max_k)
    for q in range(i,-1,-1):
        while(p<M):
            if cur+B[p] <= K:
                cur+=B[p]
                max_k = max(i+1+p+1, max_k)
                p+=1
            else:
                break
        cur -= A[q]
    print(max_k)





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

ai = 0
bi = 0
cnt = 0
fin = ''
while k > 0:
    if fin == 'a':
        if k - b[bi] < 0: break
        cnt += 1
        k -= b[bi]
        bi += 1
        if bi >= m:
            break
    elif fin == 'b':
        if k - a[ai] < 0: break
        cnt += 1
        k -= a[ai]
        ai += 1
        if ai >= n:
            break
    
    
    elif fin != 'a' and a[ai] < b[bi]:
        if k - a[ai] < 0: break
        cnt += 1
        k -= a[ai]
        ai += 1
        if ai >= n:
            fin = 'a'
            ai -= 1
    elif fin != 'b' and a[ai] > b[bi]:
        if k - b[bi] < 0: break
        cnt += 1
        k -= b[bi]
        bi += 1
        if bi >= m:
            fin = 'b'
            bi -= 1
    else:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
d1 = list(map(int,input().split()))
d2 = list(map(int,input().split()))
count = 0
while k > 0 :
    if len(d1) == 0 and len(d2) == 0 :
        break
    if len(d2) == 0:
        if d1[0] <= k :
            count += 1
            k -= d1[0]
            del d1[0]
        else :
            break
    elif len(d1) == 0 :
        if d2[0] <= k :
            count += 1
            k -= d2[0]
            del d2[0]
        else :
            break
    elif d1[0] <= d2[0] :
        if d1[0] <= k :
            count += 1
            k -= d1[0]
            del d1[0]
        else :
            break
    else :
        if d2[0] <= k :
            count += 1
            k -= d2[0]
            del d2[0]
        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()))

ans = 0
total = 0
idx = -1

for i in range(N):
  t = total + A[i]
  if t > K:
    break
  total = t
  ans += 1
  idx = i

for j in range(M):
  t = total + B[j]
  if t > K:
    # print(idx)
    for k in range(idx, -1, -1):
      t -= A[k]
      if t <= K and total < t:
        total = t
        ans -= (idx - k - 1)
        idx = k - 1
  else:
    total = t
    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()))

count = 0
time = 0
i = 0
j = 0
while True:
   if (not a) and (not b):
      break
   if a and not b:
      time+= a.pop(0)
   elif b and not a:
      time+=b.pop(0)
   else:
      if a[0] > b[0]:
         time+=b.pop(0)
      else:
         time+=a.pop(0)
   if time > k:
      break
   else:
      count+=1
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()))
no=10**10
zikan=0
ans=0
for i in range(n+m):
  if a[0]<=b[0]:
    if zikan+a[0]<=k:
      zikan+=a[0]
      ans+=1
      del a[0]
      if a==[]:
        a.append(no)
  else:
    if zikan+b[0]<=k:
      zikan+=b[0]
      ans+=1
      del b[0]
      if b==[]:
        b.append(no)
print(ans)

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

ans=0
T=0
for i in range(N):
    T+=A[i]
    if T > K:
        T-=A[i]
        break
    else:
        ans+=1
R=[]
R.append(ans)
n=ans-1
m=0
while(1):
    while(1):
        if m>=M: break
        T+=B[m]
        
        if T > K:
            T-=B[m]
            break
        else:
            ans+=1
            R.append(ans)
        m+=1
            
    n-=1
    if n<0: break
    T-=A[n]
    ans-=1
    

print(max(R))

Submission status: Wrong Answer

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

t = 0
i = 0
j = 0
while (t <= k):
  #print(i,j,t)
  if i < n:
    ai = a[i]
  else:
    ai = 10**10
  if j < m:
    bi = b[j]
  else:
    bi = 10**10
  if ai < bi:
    i += 1
    t += ai
  else:
    j += 1
    t += bi
print(i+j-1)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
time = 0
coun = 0
while time < k:
    c = 0
    if As and Bs:
        if As[0] <= Bs[0]:
            c = As.pop(0)
        else:
            c = Bs.pop(0)
    elif As:
        c = As.pop(0)
    else:
        c = Bs.pop(0)
    if time+c<=k:
        time +=c
        coun +=1
    else:
        break
print(coun)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
data = list(map(int,input().split()))
numOfBooksA=data[0]
numOfBooksB=data[1]
time=data[2]

nextA=0
nextB=0
count=0
overAflg=0
overBflg=0

booksA=list(map(int,input().split()))
booksB=list(map(int,input().split()))

for i in range(numOfBooksA+numOfBooksB):
    if(time <= 0):
        count = count - 1
        break
    
    if(overAflg==1 and overBflg==0):
        time = time - booksB[nextB]
        if(len(booksB) > nextB+1):
            nextB = nextB + 1
        else:
            overBflg=1
        count = count + 1
        continue
    
    if(overAflg==0 and overBflg==1):
        time = time - booksA[nextA]
        if(len(booksA) > nextA+1):
            nextA = nextA + 1
        else:
            overAflg=1
        count = count + 1
        continue
    
    
    if (booksA[nextA] <= booksB[nextB]):
        time = time - booksA[nextA]
        if(len(booksA) > nextA+1):
            nextA = nextA + 1
        else:
            overAflg=1
        count = count + 1
    
    elif (booksA[nextA] > booksB[nextB]):
        time = time - booksB[nextB]
        if(len(booksB) > nextB+1):
            nextB = nextB + 1
        else:
            overBflg=1
        count = count + 1

print(count)

Submission status: Wrong Answer

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


    cnt = -1
    su = 0
    while su <= k:
        if a and b:
            su += min(a[0], b[0])
            if a[0]<=b[0]:
                a.pop(0)
            else:
                b.pop(0)
            cnt += 1
        elif not a and not b:
            break
        elif not a:
            su += b[0]
            b.pop(0)
            cnt += 1
        elif not b:
            su += a[0]
            a.pop(0)
            cnt += 1

    if su <= k: cnt += 1
    return cnt


print(solve())

Submission status: Wrong Answer

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, m, k = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    cnt_A = 0
    cnt_B = 0
    readtime = 0
    time_max = 10**9 + 1

    for i in range(n+m):
        readtime += min(A[cnt_A], B[cnt_B])
        if readtime > k:
            readtime -= min(A[cnt_A], B[cnt_B])
            break
        else:
            if A[cnt_A] <= B[cnt_B]:
                cnt_A += 1
                if cnt_A == n:
                    A.append(time_max)
            else:
                cnt_B += 1
                if cnt_B == m:
                    B.append(time_max)

        #
        # if len(A) > 0 and len(B) > 0:
        #     readtime += min(A[0], B[0])
        #     if readtime > k:
        #         readtime -= min(A[0], B[0])
        #         break
        #     else:
        #         if A[0] <= B[0]:
        #             cnt_A += 1
        #             del A[0]
        #         else:
        #             cnt_B += 1
        #             del B[0]
        #
        # elif len(A) == 0:
        #     readtime += B[0]
        #     if readtime > k:
        #         readtime -= B[0]
        #         break
        #     else:
        #         cnt_B += 1
        #         del B[0]
        #
        # else:
        #     readtime += A[0]
        #     if readtime > k:
        #         readtime -= A[0]
        #         break
        #     else:
        #         cnt_A += 1
        #         del A[0]

    print(cnt_A + cnt_B)


resolve()

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
lin = list(map(int,input().split()))
lim = list(map(int,input().split()))
 
li = lin + lim
li.sort()
 
t = 0
ans = 0
flg = 0
 
for i in li:
    t += i
    if t>k:
        print(ans)
        flg = 1
        break
    elif t==k:
        print(ans+1)
        flg = 1
        break
    else:
        ans+=1

if flg==0:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

count = 0
try:
    for _ in range(10 ** 9):
        if A[0] > K and B[0] > K:
            break
        elif A[0] > K:
            tmp = B.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif B[0] > K:
            tmp = A.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif not A:
            tmp = B.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif not B:
            tmp = A.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif A[0] > B[0]:
            tmp = B.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif A[0] < B[0]:
            tmp = A.popleft()
            if K >= tmp:
                K -= tmp
                count += 1
        elif A[0] == B[0]:
            if A[1] > B[1]:
                tmp = B.popleft()
                if K >= tmp:
                    K -= tmp
                    count += 1
            elif A[0] < B[0]:
                tmp = A.popleft()
                if K >= tmp:
                    K -= tmp
                    count += 1

except:
    print(count)
    
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()))
ans1=0
i=0
while i<N:
    ans1+=A[i]
    i=i+1
    if ans1>K :
        print(ans1)
        ans1=ans1-A[i-1]
        break
ans2=0
j=0
while j<M:
    ans2+=B[j]
    j=j+1
    if ans2>K :
        ans2=ans2-B[j-1]
        break
print(i+j)

Submission status: Wrong Answer

Solution code:
import itertools
import bisect

def main():
    N,M,K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AS = list(itertools.accumulate(A))
    BS = list(itertools.accumulate(B))
    Amaxin = bisect.bisect_right(AS, K)
    Bmaxin = bisect.bisect_right(BS, K)
    ans = max(Amaxin-1,Bmaxin-1)
    index = 0
    for i in reversed(range(0,Amaxin)):
        while index<M and AS[i] + BS[index] <=K:
            index+=1
        ans = max(i+index+1,ans)

    print(max(ans,0))








if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m,kk=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
k,c=0,[] 
i=0
j=0 

while i<len(a) and j<len(b):
	if a[i]<=b[j]:
		c.append(a[i])
		i+=1 
	else:
		c.append(b[j])
		j+=1 

while i<len(a):
	c.append(a[i])
	i+=1 
while j<len(b):
	c.append(b[j])
	j+=1 
#print(c)
i=0 
ans=-1
s=0
#print(kk)
while i<len(c):
	s=s+c[i]
	if s==kk:
		ans=i 
		break 
	if s>kk:
		ans=i-1 
		break 
	i+=1
if ans==-1:
	print(len(c))
else:
	print(ans+1)

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]"):
    candidateA = []
    weightA = 0
    for a in A:
        if weightA + a > K:
            break
        else:
            candidateA.append(a)
            weightA += a
    max_count = current_count = len(candidateA)

    candidateB = []
    weightB = 0
    terminated=False
    for b in B:
        if len(candidateA)==0:
            break

        if weightA + weightB + b <= K:
            weightB += b
            candidateB.append(b)
            current_count += 1

        elif terminated==False:
            while weightA + weightB + b > K:
                if len(candidateA)>0:
                    weightA -= candidateA.pop()
                    current_count -= 1
                else:
                    terminated=True
            if terminated == True:
                continue
            else:
                weightB += b
                candidateB.append(b)
                current_count += 1
        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:
def main():
    n, m, k = map(int, input().split())
    alis = list(map(int, input().split()))
    blis = list(map(int, input().split()))
    time = 0
    ans = 0
    while time< k:
        #print(alis, blis, len(alis), k, time)
        if len(alis) == 0 and len(blis) == 0:
            break
        elif len(alis) == 0 and blis[0] <= k-time:
            time += blis[0]
            blis.pop(0)
            ans += 1
        elif len(blis) == 0 and alis[0] <= k-time:
            time += alis[0]
            alis.pop(0)
            ans += 1
        else:
            #print(alis, blis)
            if alis[0] <= blis[0] and time + alis[0] <= k:
                #print(time, alis[0], blis[0])
                time += alis[0]
                alis.pop(0)
                ans += 1
            elif alis[0] > blis[0] and time + blis[0] <= k:
                #print(time)
                time += blis[0]
                blis.pop(0)
                ans += 1
            else:
                break


    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

time = 0
x = N-1
y = N
answer = 0

for i in range(N):
    time += A[i]
    if time > K:
        time -= A[i]
        x = i-1
        y = i
        answer = i
        break

for i in range(x):
    y -= 1
    time -= A[x-i]
    for j in B:
        time += j
        y += 1
        if time > K:
            time -= j
            y -= 1
            if y > answer:
                answer = y
print(answer)

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = il() + [MAX]
    B = il() + [MAX]
    aq, bq = 0, 0
    time, ret = 0, 0

    for _ in range(N+M):
        if aq < N and A[aq] < B[bq] and A[aq] + time <= K:
            time += A[aq]
            ret += 1
            aq += 1
        elif bq < M and B[bq] + time <= K:
            time += B[bq]
            ret += 1
            bq += 1
        else:
            break
    print(ret)


if __name__ == '__main__':
    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()))
x = 0
c = a+b
c = sorted(c)
num = len(c)
for i in range(0,num):
    if k >= c[i]:
        k = k-c[i]
        x += 1
    else:
        break

print(x)

Submission status: Wrong Answer

Solution code:
a=[input() for i in range(3)]
a1=[int(i) for i in a[0].split()]
a2=[int(i) for i in a[1].split()]
a3=[int(i) for i in a[2].split()]

time=0
count=0

while time<=a1[2]:

	if a2[0]<=a3[0]:
		time+=a2[0]
		count+=1
		del a2[0]
		a2.append(1000000000)

	else:
		time+=a3[0]
		count+=1
		del a3[0]
		a3.append(1000000000)

print(count-1)

Submission status: Wrong Answer

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


#読んだ本の数
cnt=0
#消費した時間
time=0

while 1:
    #上から掘り当てていって相手が存在しない、または小さい方
    read=''
    for i in range(max(len(a),len(b))):
        if i==len(a):
            read='b'
        elif i==len(b):
            read='a'
        elif a[i]>b[i]:
            read='b'
        elif a[i]<b[i]:
            read='a'
        if read:
            break

    #読むほうをPOP
    time+=a.pop(0) if read=='a' else b.pop(0)
    #kを超えた場合ブレーク
    if time>k:
        break
    #カウントアップ
    cnt+=1
    #互いに超えた場合ブレーク
    if len(a)==0 and len(b)==0:
        break
print(cnt)