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
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b


class cumsum1d:  # 一次元累積和クラス
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        # もとの配列lsにおける[i,j)の中合計
        return self.ls_accum[j] - self.ls_accum[i]


def two_pointers(ls: list):
    '''すべてのlに対して、条件is_okをみたすls[l:r]の中で
    r - lが最大になるような(l,r)の集合を返す'''
    n_ls = len(ls)
    ret = []

    def append(r, pre_states):
        '''状態にls[r]を考慮して更新する'''
        # 問題によって自分で定義
        return pre_states + ls[r]
        # 掛け算の例 return pre_state*ls[r]

    def pop(l, pre_states):
        '''状態からls[l]を抜く更新をする'''
        # 問題によって自分で定義
        # 掛け算の例 return pre_state//ls[l]
        return pre_states - ls[l]

    def is_ok(r, pre_states):
        # 問題によって自分で定義
        states = append(r, pre_states)
        # 114以下の最長の範囲が知りたい例 return states<=114
        return states <= K

    r = 0
    states = 0
    for l in range(n_ls):
        while r < n_ls and is_ok(r, states):
            # 更新式
            states = append(r, states)
            r += 1
        ret.append((l, r))
        # 抜けるときの更新
        states = pop(l, states)
    return ret


# K冊の組み合わせを試せば良い
# 合計は累積和で高速化
N, M, K = ints()
A = ints()
B = ints()
AB = list(reversed(A)) + B
# AB_cum = cumsum1d(AB)

ans = 0
for l, r in two_pointers(AB):
    if l <= N - 1 and N <= r:
        ans = max(ans, r - l)
print(ans)

Submission status: Wrong Answer

Solution code:
m,n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = a + b
d = sorted(c)
count = 0
x = 0
for i in range(len(c)):
  if c[0]>k:
    x = 0
  elif k >= count:
    count = count + c[i]
    x = x+1
  else:
    pass
print(x)

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
T=K
while(K>0):
  if(A[0]<B[0]):
    read=A[0]
    del A[0]
    A.append(T)
  else:
    read=B[0]
    del B[0]
    B.append(T)
  last=K-read
  if(last>=0):
    K=last
    cnt+=1
  else:
    break
print(cnt)

Submission status: Wrong Answer

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

l = []
for i in range(n + m):
  if len(a) == 0:
    l.append(b.pop(0))
  elif len(b) == 0:
    l.append(a.pop(0))
  
  else:
    if a[0] <= b[0]:
      l.append(a.pop(0))
    else:
      l.append(b.pop(0))


total = 0
cnt = 0
for i in range(n+m):
  if total+l[i] > k:
    break
  total += l[i]
  cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc172/abc172_c
import sys
from collections import deque
from bisect import bisect_left

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, M, K = i2nn()
    A = i2nn()
    B = i2nn()
    dpA = [0] * (N+1)
    dpB = [0] * (M+1)
    for i in range(N):
        dpA[i+1] = dpA[i] + A[i]
    for i in range(M):
        dpB[i+1] = dpB[i] + B[i]
    
    #print(dpA)
    #print(dpB)
    
    r = 0
    for i in range(M+1):
        n = i
        t = K - dpB[i]
        if t < 0:
            break
        n += (bisect_left(dpA, t) - 1)
        r = max(r, n)
        #print(i, n)

    print(r)
    return

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

total = sum(A) + sum(B)
count = N + M
flag = 0


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

    elif len(A) == 0:
        tmp = total - B[-1]
        flag = 1

    elif len(B) == 0:
        tmp = total - A[-1]

    elif A[-1] < B[-1]:
        tmp = total - A[-1]

    else:
        tmp = total - B[-1]
        flag = 1

    if tmp < K:
        count = count - 1
        break

    else:
        total = tmp
        count = count - 1
        if flag == 0:
            A.pop(-1)
        else:
            B.pop(-1)
            flag = 0

print(count)

Submission status: Wrong Answer

Solution code:
import sys

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()
b = nl()
ans = 0
sum_a = [0]*(n+1)
sum_b = [0]*(m+1)
for i in range(1, n+1):
    sum_a[i] = sum_a[i-1] + a[i-1]
for i in range(1, m+1):
    sum_b[i] = sum_b[i-1] + b[i-1]
ans = 0
bef_j = m
for i in range(n+1):
    if sum_a[i] >= k:
        break
    for j in range(m, -1, -1):
        if sum_a[i] + sum_b[j] <= k:
            ans = max(ans, i+j)
            bef_j = j
            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()))


A.append(K)
B.append(K)

sumA = []
sumB = []

sumAi = 0
for i in range(N+1):
    sumAi += A[i]
    if sumAi > K:
        break
    else:
        sumA.append(sumAi)
AA = A[:i]

sumBj = 0
for j in range(M+1):
    sumBj += B[j]
    if sumBj > K:
        break
    else:
        sumB.append(sumBj)
BB = B[:j]

NN = len(AA)
MM = len(BB)

#Ares = [K-x for x in sumA]
#Bres = [K-x for x in sumB]

#invsumA=[]
#invsum=0
#for x in AA[::-1]:
#    invsum += x
#    invsumA.append(invsum)

#ans = NN

#timearray = np.zeros((NN,MM))


        
if NN == 0:
    ans = MM
else:
    ans = NN+MM
    time = sumA[-1]+sumB[-1]
    if ans > K:
        for i in range(NN+MM):
            if AA[-1]>BB[-1]:
                nozoku = AA.pop(-1)
                time -= nozoku
                ans -= 1
            else:
                nozoku = BB.pop(-1)
                time -= nozoku
                ans -= 1
            if time <= K:
                break
        
    #    res = K-sumA[-1]
    #    resA = AA
    #    kakuB = []
    #    resN = len(resA)
    #    
    #    for j in range(MM):
    #        if BB[j] <= res:
    #            kakuB.append(BB[j])
    #            res-=BB[j]
    #        else:
    #            break
    #    kakuM = len(kakuB)
    #    
    #    process=True
    #    while process:
    #        invsum = 0
    #        for i in range(max(resN,kakuM)-1):
    #            invsum += resA[-i-1]
    #            if sumB[i+1] - invsum <= res:
    #                resA = resA[:-i-1]
    #                kakuB = BB[:i+2]
    #                resN = len(resA)
    #                kakuM = len(kakuB)
    #                break
    #            if i == max(resN,kakuM)-2:
    #                process= False
    #    
    #    ans = resN+kakuM
print(ans)

Submission status: Wrong Answer

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

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))[::-1]
B = list(map(int, readline().split()))[::-1]

cnt = 0
tmp = 0
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
ans = cnt

cnt = 0
tmp = 0

for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
ans = max(ans, cnt)

tot = 0
cnt = 0

while True:
    if len(A) > 0:
        acc_a = A.pop()
    else:
        acc_a = sys.maxsize
    if len(B) > 0:
        acc_b = B.pop()
    else:
        acc_b = sys.maxsize
    if acc_a == acc_b == sys.maxsize:
        break
    acc = min(acc_a, acc_b)

    if tot + acc <= K:
        cnt += 1
    else:
        break

    tot += acc
    if acc_a <= acc_b:
        B.append(acc_b)
    else:
        A.append(acc_a)

ans = max(ans, cnt)

print(ans)

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()))
a.append(10**9+1)
b.append(10**9+1)
cnt = 0
while k > 0 and n + m > 0:
    v = -1
    if a[0] < b[0]:
        v = a.popleft()
        n -= 1
    elif a[0] > b[0]:
        v = b.popleft()
        m -= 1
    elif a[1] < b[1]:
        v = a.popleft()
        n -= 1
    elif a[1] > b[1]:
        v = b.popleft()
        m -= 1
    else:
        v = a.popleft()
        n -= 1
    
    k -= v
    if k >= 0:
        cnt += 1

print(cnt)

Submission status: Wrong Answer

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]

i, j, ans = 0, 0, 0
while K > 0:
    if i == N and j == M:
        break
    elif i == N:
        K -= B[j]
        j += 1
    elif j == M:
        K -= A[i]
        i += 1
    if A[i] == B[j]:
        k, clear = 1, False
        while i+k < N and j+k < N:
            if A[i+k] == B[j+k]:
                k += 1
            elif A[i+k] < B[j+k]:
                K -= A[i]
                i += 1
                clear = True               
                break
            elif A[i+k] < B[j+k]:
                K -= B[j]
                j += 1
                clear = True               
                break
        if not clear:
            K -= A[i]
            i += 1
    elif A[i] < B[j]:
        K -= A[i]
        i += 1
    else:
        K -= B[j]
        j += 1
    ans += 1

if K < 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
I = list(map(int,input().split()))

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

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

A_top = 0
B_top = 0
time = 0
result = 0

A_over = -1
B_over = -1

Asum = 0
Bsum = 0

for iter in range(len(A)):
    Asum += A[iter]
    if Asum > K:
        A_over = iter
        break

for iter in range(len(B)):
    Bsum += B[iter]
    if Bsum > K:
        B_over = iter
        break


while time < K:
    if A_over == A_top and B_over == B_top:
        break



    if B_top >= len(B) and A_top < len(A) and time + A[A_top] <= K:
        time += A[A_top]  
        A_top += 1
        continue
    elif B_top >= len(B) and A_top < len(A) and time + A[A_top] > K:
        break

    if A_top >= len(A) and B_top < len(B) and time + B[B_top] <= K:
        time += B[B_top]
        B_top += 1
        continue
    elif A_top >= len(A) and  B_top < len(B) and time + B[B_top] > K:
        break
    
    if A[A_top] <= B[B_top] and time + A[A_top] <= K:
        time += A[A_top]
        A_top += 1
        continue

    if A[A_top] >= B[B_top] and time + B[B_top] <= K:
        time += B[B_top]
        B_top += 1
        continue


    
    break

result = A_top + B_top
# if time > K and result > 0:
#     result -= 1

print(result)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as ia 
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = set(ia(A))
b = set(ia(B))
na = 0
nb = 0
ans = 0
for i in a:
  if i > K:
    break
  else:
    na += 1
    nb = 0
    nb = len([i + j for j in b if i + j <= K])
  ans = max(ans, na + nb)
if ans == 0:
  for i in b:
    if i <= 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()))
AS=[0]
BS=[0]
r=0
for i in A:
  if i+AS[-1]>k:
    break
  AS.append(i+AS[-1])
AS.reverse()
for i in B:
  if i+BS[-1]>k:
    break
  BS.append(i+BS[-1])
BS.reverse()
for i in range(len(AS)):
  for j in range(len(BS)):
    if AS[i]+BS[j]<=k:
      if i+j>r:
        r=len(AS)-i+len(BS)-j-2
      break
    
print(r)

Submission status: Wrong Answer

Solution code:

def count_book(NMK,A,B):
  i=0
  j=0
  sum = 0
  count = 0
  I = len(A)
  J = len(B)
  while sum<=NMK[2]:
    if i < I and j < J:
      if A[i]<B[j]:
        sum = sum + A[i]
        print(A[i])
        i = i + 1
        if sum<=NMK[2]:
         count = count + 1
         continue
      else:
        sum = sum + B[j]
        print(B[j])
        j = j + 1
        if sum<=NMK[2]:
         count = count + 1
         continue
 
    if i < I and j >= J:
      sum = sum + A[i]
      print(A[i])
      i = i + 1
      if sum<=NMK[2]:
        count = count + 1
        continue

    if i >= I and j < J:
      sum = sum + B[j]
      print(B[j])
      j = j + 1
      if sum<=NMK[2]:
        count = count + 1
        continue

    if i >= I and j >= J:
      break
      
  print(count)

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

r = 0

A_len = N #len(A_list)
B_len = M #len(B_list)

total_page = sum(A_list) + sum(B_list)
total_num = A_len + B_len

if total_page <= K:
  print(total_num)
  
else:
  import queue
  q_A = queue.LifoQueue()
  q_B = queue.Queue()
  q_AB = queue.Queue()
  for A_i in A_list:
    q_A.put(A_i)
    q_AB.put(A_i)
  for B_i in B_list:
    q_B.put(B_i)
    q_AB.put(B_i)

  
  AB_total = 0
  AB_max_count = 0
  while AB_total < K:
    AB_F = q_AB.get()
    AB_total += AB_F
    AB_max_count += 1
  initial_r = AB_max_count - 1
  initial_total = AB_total - AB_F
  
  result = initial_r
  tmp_total = initial_total
  tmp_r = initial_r
  
  while q_A.qsize() > 0:
    A_L = q_A.get()  
    tmp_total = tmp_total - A_L
    count_B = 0
    while tmp_total < K and q_B.qsize() > 0:
      B_F = q_B.get()
      tmp_total += B_F
      count_B += 1
      
      if tmp_total > K:
        count_B -= 1
        q_B.put(B_F)
        for c in range(q_B.qsize() - 1):
          tmp = q_B.get()
          q_B.put(tmp)
          
    tmp_total -= B_F
    tmp_r += count_B

    if result < tmp_r:
      result = tmp_r
    tmp_r -= 1    
  print(result)

Submission status: Wrong Answer

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

total_A_minutes = sum(A_minutes)
total_B_minutes = sum(B_minutes)

# すべて読める場合
if total_A_minutes + total_B_minutes <= K:
    print(N + M)
    exit()


# Aの合計が超えた場合
# ⇒Aだけで何冊読めるかの冊数を算出
# Aの合計が超えない場合
# ⇒Aの合計冊数でを算出　この場合Aを減らす必要なし（ただし、Bの１番上が超えちゃうなら減らす必要あり）
# Aを1札減らしてBを1札増やす （１）
# もし　Kを超えないなら
#    Bを1冊増やす
# もし　Kを超えるなら
# （１）へ戻る

# これらをAがなくなるかBの一番下まで到達するまでやる

# 冊数
ans = 0
# Kを超える最小のi
limitover_A_index = 0
# Aの合計が超えた場合
if total_A_minutes > K:
    tmp_total_A = 0
    for i in range(N):
        tmp_total_A = tmp_total_A + A_minutes[i]
        if tmp_total_A > K:
            limitover_A_index = i
            ans = i
            break
# Aの合計が超えない場合
else:
    limitover_A_index = N
    ans = N
    
print('*******ループ前*********')
print(ans)

hikaku = ans
    
while limitover_A_index >= 0:
    tmp_minute = sum(A_minutes[:limitover_A_index])
    tmp_ans = limitover_A_index + 1
    for B_minute in B_minutes:
        # Bの冊数を足したことでK分を超えるなら
        print('*******tmp_minute + B_minute*********')
        print(tmp_minute + B_minute)
        if tmp_minute + B_minute > K:
            break

        tmp_minute = tmp_minute + B_minute
        tmp_ans = tmp_ans + 1
        print('****************')
        print(tmp_ans)

    # 最大冊数の記録を超えたら更新する
    if hikaku < tmp_ans:
        hikaku = tmp_ans
        print('******最大冊数を超えた**********')
        print(hikaku)

    limitover_A_index = limitover_A_index - 1

print(hikaku)

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from decimal import Decimal
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,M,K=LI()
A = LI()
B = LI()

#print(N,M,K,A,B)

ans_a,nn=0,0

for n in range(N):
    if ans_a +A[n] > K:
        nn+=1
        break
    ans_a += A[n]
    nn+=1
#print(ans_a,nn,A[:nn])
ans = 0
for i in range(nn):
    ans_b = sum(A[:nn-i])
    jj=0
    for j in range(M):
        if ans_b+B[j] > K:
            jj+=1
            break
        ans_b += B[j]
        jj+=1
    #print(ans_b,j,nn-i+jj)
    ans = max(ans,nn,nn-i+jj)
print(ans)

Submission status: Wrong Answer

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

rui_A = [0 for _ in range(N)]
rui_B = [0 for _ in range(M)]

rui_A[0] = A[0]
for i in range(N-1):
    rui_A[i+1] = rui_A[i] + A[i]

rui_B[0] = B[0]
for i in range(M-1):
    rui_B[i+1] = rui_B[i] + B[i]

max_book = 0
for i in range(len(rui_A)):
    if rui_A[i] <= K:
        nokori = K - rui_A[i]
        B_posi = bisect.bisect_right(rui_B,nokori)
        if max_book < B_posi + i + 1:
            max_book = B_posi + i + 1
print(max_book)

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


accA = list(itertools.accumulate(a))
accB = list(itertools.accumulate(b))


cnt = 0
ans = 0
for i in range(len(accA)):
    for j in range(len(accB)):
        if k >= accA[i]+accB[j]:
            cnt = i+j
        
        ans = max(cnt, ans)


print(ans)

Submission status: Wrong Answer

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

count = 0
ans = 0
l = n + m
ac = 0
bc = 0

if k >= sum(a) + sum(b):
    print(len(a) + len(b))
else:
    while k >= count and l >= ans:
        if n > ac and m > bc:
            if k >= count + min(a[0], b[0]):
                count += min(a[0], b[0])
                ans += 1
                if b[0] >= a[0]:
                    a.popleft()
                    ac += 1
                else:
                    b.popleft()
                    bc += 1
            else:
                break
        elif n == ac and m > bc:
            if k >= count + b[0]:
                count += b[0]
                ans += 1
                b.popleft()
                bc += 1
            else:
                break
        elif m == bc and n > ac:
            if k >= count + a[0]:
                count += a[0]
                ans += 1
                a.popleft()
                ac += 1
            else:
                break
        else:
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, M, K = map(int, input().split())
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))
    ans = 0
    a_is_available = True
    b_is_available = True
    while K >= 0:
        lenA = len(A)
        lenB = len(B)
        if lenA == 0: a_is_available = False
        if lenB == 0: b_is_available = False
        if not a_is_available and not b_is_available:
            ans += 1
            break
        if a_is_available:a = A.popleft()
        if b_is_available:b = B.popleft()
        if a_is_available and not b_is_available:
            if a > K:
                ans += 1
                break
            K -= a
        elif not a_is_available and b_is_available:
            if b > K:
                ans += 1
                break
            K -= b
        elif a <= b:
            if a > K:
                ans += 1
                continue
            K -= a
            B.appendleft(b)
        else:
            if b > K:
                ans += 1
                continue
            K -= b
            A.appendleft(a)
        if K < 0:
            break
        ans += 1
    ans = min(N+M, ans)
    ans = max(0, ans)
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

amax = 0
time = 0
for a in A:
  if time + a > k: break
  time += a
  amax += 1
bmax,time = 0,0
for b in B:
  if time + b > k: break
  time += b
  bmax += 1
if amax == 0:
  print(bmax)
  exit()
if bmax == 0:
  print(amax)
  exit()
v = max(amax,bmax)
ans = v
#print(amax,bmax)
for i in range(v+1,k+1):
  ok = False
  for j in range(i+1):
    if j>n or i-j > m: continue
    t = sum(A[:i])+sum(B[:(i-j)])
    if t <= k:
      ans = i
      ok = True
      break
  if ok == False:
    print(ans)
    exit()
print(ans)

Submission status: Wrong Answer

Solution code:
[N,M,K]=list(map(int, input().split()))
alist=list(map(int, input().split()))
blist=list(map(int, input().split()))
clist=alist+blist
clist.sort()

time=0
ans=0

for c in clist:
    time+=c
    if time<=K:
        ans+=1
    else:
        break


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
data = sys.stdin

conversion_data = []
for l in data:
    conversion_data.append([int(i) for i in l.split()])

table_a = conversion_data[1]
table_b = conversion_data[2]

N = conversion_data[0][0]
M = conversion_data[0][1]
limit = conversion_data[0][2]
max = max([N, M])

total_time = 0
position_a = 0
position_b = 0

for i in range(max):
  if position_a <= (N - 1):
      a = table_a[position_a]
      if (a + total_time) <= limit:
          total_time += a
          position_a += 1
  
  if position_b <= (M - 1):
    b = table_b[position_b]
    if (b + total_time) <= limit:
      total_time += b
      position_b += 1
  
print(position_a + position_b)

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

# a_ls=[1,3,2,1,4]
# K=5

a_ruiseki=[0]
b_ruiseki=[0]
ans=0

a_time=0
i_a=0 
while a_time+a_ls[i_a]<=K:
	a_time+=a_ls[i_a]
	i_a+=1
	if i_a>=N:
		break

ans=i_a


b_time=0
i_b=0
reigai=0

if i_a==0:
	reigai=1

while a_time>0:
	while a_time+b_time+b_ls[i_b]<=K:
		b_time+=b_ls[i_b]
		i_b+=1
		if i_b>=M:
			break
	
	ans=max(ans,i_a+i_b)
	i_a-=1
	a_time=a_time-a_ls[i_a]
	if i_b>=M:
		break

if reigai==1:
	while b_time+b_ls[i_b]<=K:
		b_time+=b_ls[i_b]
		i_b+=1
		if i_b>=M:
			break
	
	ans=i_b



print(ans)

Submission status: Wrong Answer

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

max_books = len(A)+len(B)

A.append(1000000001)
B.append(1000000001)

res2 = 0
K2 = K
for a in A:
    K2 -= a
    if K2 < 0:
        break
    else:
        res2 += 1

res3 = 0
K3 = K
for b in B:
    K3 -= b
    if K3 < 0:
        break
    else:
        res3 += 1

res1 = -1
K1 = K
j = 0
k = 0
for i in range(max_books):
    if  K1 >= 0:
        if A[j] <= B[k]:
            K1 -= A[j]
            j += 1
        else:
            K1 -= B[k]
            k += 1
        res1 += 1
    else:
        break

res = max(res1,res2,res3)
print(res)

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()))
s=0
t=0
i=n+m
while s==0:
    for j in range(n):
        if i-(j+1)<=m:
            t=sum(a[0:j])+sum(b[0:i-j])
        if 0<t<=k:
            s+=1
    i=i-1
    if i==-1:
        s+=1
print(i+1)

Submission status: Wrong Answer

Solution code:
from bisect import *
def solve():
    ans = 0
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))+[float('inf')]
    B = list(map(int, input().split()))+[float('inf')]
    cumA = [0]*(N+1)
    cumB = [0]*(M+1)
    for i in range(N):
        cumA[i+1] = cumA[i]+A[i]
    for i in range(M):
        cumB[i+1] = cumB[i]+B[i]
    for i in range(N+1):
        rest = K-cumA[i]
        if rest<0:
            break
        ind = bisect_right(cumB,rest)
        if ind>1:
            ans = max(ans,i+ind-1)
    return ans
print(solve())

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()))
countA=2
countB=2
shoyou=K
tot=0
sumA=[0,A[0]]
sumB=[0,B[0]]
mmm=True

for i in range(len(A)-1):
    sumA.append(A[countA-1]+sumA[countA-1])
    countA+=1
for i in range(len(B)-1):
    sumB.append(B[countB-1]+sumB[countB-1])
    countB+=1
for i in range(1,N+M+1):
    for j in range(0,min(i,len(sumB))):
        if 0<=i-j<=len(sumA)-1 and j<=len(sumB)-1:
            if sumA[i-j]+sumB[j]>K:
                mmm=False
                break
        elif i-j<0 or len(sumA)-1<i-j or j>=len(sumB):
            break
    if mmm==True:
        tot+=1
    elif mmm==False:
        print(tot+1)
        break
if mmm==True:
    print(tot)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

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

cnt = 0

c = deque(a)
d = deque(b)

i = c.popleft()
j = d.popleft()

while k >= 0:
    if i > j:
        k -= j
        if len(d) == 0:
            j = 10**10
        else:
            j = d.popleft()
    else:
        k -= i
        if len(c) == 0:
            i = 10**10
        else:
            i = c.popleft()

    if k < 0 or (i == 10**10 and j == 10**10):
        break

    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(map(int, input().split()))

import sys

N, M, K = MI()
A = LI()
B = LI()
C = A.copy()
D = B.copy()
a = N
b = M

if sum(A) + sum(B) <= K:
    print(N + M)
    sys.exit()

for i in range(1, N):
    A[i] += A[i - 1]
    if A[i] > K:
        a = i
        
for i in range(1, M):
    B[i] += B[i - 1]
    
    if B[i] > K:
        b = i
        

ans = max(a, b)


if ans == a:
    tmp = A[a - 1]
    i = 0
    if A[0] > K:
        ans = 0
        
    while True:
        tmp += D[i]
        
        if tmp > K:
            break
        elif tmp == K:
            ans += 1
            break
        
        ans += 1
        i += 1
        
        if i >= len(D):
            break
        
else:
    tmp = B[b - 1]
    j = 0
    
    if B[0] > K:
        ans = 0
    
    while True:
        tmp += C[j] 
        
        if tmp > K:
            break
        elif tmp == K:
            ans += 1
            break
        
        ans += 1
        j += 1
        
        if j >= len(C):
            break
        
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A=deque(A)
B=deque(B)
AC=[0]*(N+1+M)
WA=[0]*(M+1+N)
for i in range(N+M):
    if len(A)>0 and len(B)>0:
        if A[0]>B[0]:
            AC[i]=A[0]
            WA[i]=B[0]
            
        else:
            AC[i]=B[0]
            WA[i]=A[0]
        A.popleft()
        B.popleft()

    elif len(A)>0:
        AC[i]=A[0]
        A.popleft()
    elif len(B)>0:
        AC[i]=B[0]
        B.popleft
AC_WA=[0]
WA_wA=[0]
for i in range(len(AC)):
    AC_WA.append(AC_WA[-1]+AC[i])
for j in WA:
    WA_wA.append(j+WA_wA[-1])
count=0
for i in range(len(AC_WA)):
    if AC_WA[i]>K:
        count=max(count,i)
        break
for i in range(len(WA_wA)):
    if WA_wA[i]>K:
        count=max(count,i)
        break
print(count)