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:
s = [input() for i in range(3)]
list1 = list(map(int, s[0].split()))
n = list1[0]
m = list1[1]
k = list1[2]

list2 = list(map(int, s[1].split()))
list3 = list(map(int, s[2].split()))

counter_l1 = 0
counter_l2 = 0

for i in range(n+m):
    if counter_l1 < n and counter_l2 < m:
        if list2[counter_l1] < list3[counter_l2]:
            k = k - list2[counter_l1]
            if k <= 0:
                break
            counter_l1 +=1
            continue
        if list2[counter_l1] >= list3[counter_l2]:
            k = k - list3[counter_l2]
            if k <= 0:
                break
            counter_l2 +=1
    else:
        if counter_l1 >= n:
            k = k - list3[counter_l2]
            if k <= 0:
                break
            counter_l2 +=1
        if counter_l2 >= m:
            k = k - list2[counter_l1]
            if k <= 0:
                break
            counter_l1 +=1
print(counter_l1+counter_l2)

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

tmp=k
alpha=0
bata=0

ans=0

cnt1=0
cnt2=0
cost1=0
cost2=0

for i in range(n):
    cost1+=a[i]
    if cost1<=k:
        cnt1+=1
    else:
        cost1-=a[i]
        break

for j in range(m):
    cost2+=b[j]
    if cost2<=k:
        cnt2+=1
    else:
        cost2-=b[j]
        break

if cnt1>cnt2:
    ans+=cnt1
    k-=cost1
    for i in range(m):
        k-=b[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)

elif cnt2>cnt1:
    ans+=cnt2
    k-=cost2
    for i in range(n):
        k-=a[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)

else:
    alpha+=cnt1
    tmp-=cost1
    for i in range(m):
        tmp-=b[i]
        if tmp>=0:
            alpha+=1
        else:
            break
    
    bata+=cnt2
    k-=cost2
    for i in range(n):
        k-=a[i]
        if k>=0:
            bata+=1
        else:
            break

    ans=max(alpha, bata)

    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import sys
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=[]
A=a.pop(0)
B=b.pop(0)
cnt=0
while True:
    if A<=B:
        l.append(A)
        if len(a)>0:
            A=a.pop(0)
    else:
        l.append(B)
        if len(b)>0:
            B=b.pop(0)
    if len(a)==0 or len(b)==0:
        cnt+=1
        if cnt==2:
            if len(a)>0:
                l.append(A)
            else:
                l.append(B)
            break
if len(a)>0:
    l.extend(a)
if len(b)>0:
    l.extend(b)
cumsum=list(accumulate(l))
print(bisect.bisect_right(cumsum,k))

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 = [1000000000000] * 200000
count = 0

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
a=deque()
b=deque()
nmk=[int(i) for i in input().split()]
for i in input().split():
  a.append(int(i))
for i in input().split():
  b.append(int(i))
k=nmk[2]
cnt=0
while k>=0:
  if len(b)==0:
    k-=a.popleft()
    cnt+=1
  elif len(a)==0:
    k-=b.popleft()
    cnt+=1
  elif a[0]<=b[0]:
    k-=a.popleft()
    cnt+=1
  elif a[0]>b[0]:
    k-=b.popleft()
    cnt+=1
#  print(a,b,k)
  if k<0:
    cnt-=1
  if len(a)==0 and len(b)==0:
    break
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))
total_1 = 0
total_2 = 0
a_ruiseki = [0]*n
b_ruiseki = [0]*m
a = 0
b = 0
for i in range(n):
    total_1 += a_li[i]
    a_ruiseki[i] = total_1
    if a_ruiseki[i] > 1000000000:
        break
for i in range(m):
    total_2 += b_li[i]
    b_ruiseki[i] = total_2
    if b_ruiseki[i] > 1000000000:
        break
for i in range(n):
    if a_ruiseki[i] > k:
        a = i
        break
else:
    a = i+1
for i in range(m):
    if b_ruiseki[i] > k:
        b = i
        break
else:
    b = i+1



for i in range(m):
    if b_ruiseki[i] > k-a_ruiseki[a-1]:
        a += i
        break
else:
    a += i+1

for i in range(n):
    if a_ruiseki[i] > k-b_ruiseki[b-1]:
        b += i
        break
else:
    b += i+1
print(max(a,b))

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=[]
B=[]
A=list(map(int,input().split()))
B=list(map(int,input().split()))
goukei=[[0 for i in range(M+1)] for i in range(N+1)]
maxmax=0
for i in range(1,N+1):

    for j in range(1,M+1):
        if(j==1):
            goukei[i][j-1]=goukei[i-1][j-1]+A[i-1]
        goukei[i][j]=goukei[i][j-1]+B[j-1]
        if(K<goukei[i][j]):

            break
        else:
            maxmax=i+j

print(maxmax)

Submission status: Wrong Answer

Solution code:
import sys
N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

ans = 0
for n in range(N):
    for m in range(M):
        if K < sum(A_list[:n+1]):
            print(ans)
            sys.exit()
        
        if K >= sum(A_list[:n+1])+sum(B_list[:m+1]):
            ans = max(ans, n+m+2)
        else:
            break
            
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()))
a=[0]*N
b=[0]*M
a[0]=A[0]
b[0]=B[0]
for i in range(N-1):
    a[i+1]=a[i]+A[i]
for i in range(M-1):
    b[i+1]=b[i]+B[i]
diff=2*10**9
count=0
for i in range(N):
    j=bisect.bisect_left(b,K-a[i])-1
    if K==A[i]+b[j]:
        diff=0
        count=max(count,(i+1)+(j+1))
    elif 0<=K-(a[i]+b[j])<=diff:
        diff=K-(a[i]+b[j])
        count=max(count,(i+1)+(j+1))
print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
#N=int(input())
import numpy
N,M,K=map(int, input().split())
AList=list(map(int, input().split()))
BList=list(map(int, input().split()))

#if sum(AList)+sum(BList)<=K:
#    print(N+M)
    #print("aa")
#    quit()
    

SUM=0
cnt=0
AI=0
BI=0
for i in range(200007):
    if AI<N and BI<M:
        if AList[AI]<=BList[BI] and SUM+AList[AI]<=K:
            SUM+=AList[AI]
            #print("AListC",AList[AI],AI)
            AI+=1
            cnt+=1
        elif AList[AI]>=BList[BI] and SUM+BList[BI]<=K:
            SUM+=BList[BI]
            #print("BListD",BList[BI],BI)
            BI+=1
            cnt+=1
        else:
            break
    elif AI>=N and BI<M:
        if SUM+BList[BI]<=K:
            SUM+=BList[BI]
            #print("BListA",BList[BI],BI)
            BI+=1
            cnt+=1
        else:
            break
    elif BI>=M and AI<N:
        if AI<N and SUM+AList[AI]<=K:
            SUM+=AList[AI]
            #print("AListB",AList[AI],AI)
            AI+=1
            cnt+=1
        else:
            break
    elif BI>=M and AI>=N:
        break
    else:
        print(UNCHI)

print(cnt)

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

sa = sum(A)
sb = sum(B)
A.reverse()
C = A + B
# print(C)
ans = 0
left_wa = 0
right_wa = 0


ruiseki = C.copy()
# ruiseki = [0] + ruiseki
for i in range(len(ruiseki)-1):
    ruiseki[i+1] = ruiseki[i] + ruiseki[i+1]
# print(ruiseki)
# ruiseki = ruiseki + [ruiseki[-1]]
left = 0
# for right in range(1, len(C)+1):
#     ruiseki[left:right]
idx = 0
wa = 0
ans = 0
# print(len(ruiseki))
if sum(A) + sum(B) <= W:
    print(N + M)
    exit()
for right in range(len(A), len(ruiseki)):
    # idx += 1
    # print("right:", right)
    wa = ruiseki[right] - ruiseki[left]
    # if
    while wa > W:
        left += 1
        wa = ruiseki[right] - ruiseki[left]
    if left < len(A):
        tmp = right - left
        # print(tmp)
        ans = max(tmp, ans)
        # print("left, right:", left, right)
    
    else:
        break
    # print("left, right, :", left, right)

print(ans)        






exit()
for right in range(1, N+1):
    # sum(A[left:right])
    right_wa += C[right-1]
    wa = right_wa - left_wa
    if wa <= K:
        while wa <= K:
            if left < N:
                left_wa += C[left]
                wa = right_wa - left_wa
            left += 1
    ans += left
    



exit()
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:
import numpy as np
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())))
    
    cost = K
    count = 0
    while True:
        if len(A) ==0 and len(B) == 0:
            break
        elif len(A) == 0:
            cost -= B.popleft()
        elif len(B) == 0:
            cost -= A.popleft()

        else:
            a = A[0] 
            b = B[0] 
            if a < b:
                cost -= A.popleft()
            else:
                cost -= B.popleft()

        if cost < 0:
            break
        count += 1
    
    print(count)


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

suma = [ -1 for i in range(n) ]
sumb = [ -1 for i in range(m) ]
suma[0] = a[0]
sumb[0] = b[0]
nn = n
mm = m
for i in range(1,n):
  suma[i] = suma[i - 1] + a[i]
  if suma[i] > k:
    nn = i
    break
for i in range(1,m):
  sumb[i] = sumb[i - 1] + b[i]
  if sumb[i] > k:
    mm = i
    break

c = 0
t = 0
cc = 0
for i in range(nn):
  if suma[i] <= k:
    t = suma[i]
    c = i + 1
    cc = max(cc,c)
  else:
    c = 0
  for j in range(mm):
    tt = t + sumb[j]
    if tt <= k:
      cc = max(cc,c + j + 1)
    else:
      break

print(cc)

Submission status: Wrong Answer

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

minute = 0
i = 0
j = 0

if alist[i] > k and blist[j] > k:
  print(0)

else:
  while i < n and j < m:
    if alist[i] < blist[j]:
      minute += alist[i]
      if minute > k:
        break
      i += 1
    else:
      minute += blist[j]
      if minute > k:
        break
      j += 1

  if minute > k:
    print(i+j)
  elif i < n:
    while i < n:
      minute += alist[i]
      if minute > k:
        break
      i += 1
    print(i+j)
  else:
    while j < m:
      minute += blist[j]
      if minute > k:
        break
      j += 1  
    print(i+j)

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_time = 0
num = 0
num_a = 0
num_b = 0

for i in range(n+m):  
  if num_a >= n and num_b >= m:
    break
  elif num_a >= n:
    time = b[num_b]
    num_b += 1
  elif num_b >= m:
    time = a[num_a]
    num_a += 1
  elif a[num_a] > b[num_b]:
    time = b[num_b]
    num_b += 1
  else:
    time = a[num_a]
    num_a += 1
  total_time += time
  if total_time > k:
    break
  num += 1 
  
print(num)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
costA=list(map(int,input().split()))
costB=list(map(int,input().split()))
time=0
count=0
cost=sorted(costA+costB)
while time<=K:
    time+=cost[0]
    count+=1
    del cost[0]
    if len(cost)==0:
        break
print(count-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()))

count=0
while True:
    if A!=[] and B!=[]:
        if A[0] < B[0]:
            K -= A[0]
            count+=1
            A.pop(0)
        else:
            K -= B[0]
            count+=1
            B.pop(0)
    elif A==[]  and B!=[]:
        K -= B[0]
        count+=1
        B.pop(0)
    elif A!=[] and  B==[]:
        K -= A[0]
        count+=1
        A.pop(0)
    else:
        count+=1
        break
    
    if 0 > K:
        break
print(count-1)

Submission status: Wrong Answer

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

# m, n, k = 3, 4, 240
# a = [60, 90, 120]
# b = [80, 150, 80, 150]

# m, n, k = 3, 4, 730
# a = [60, 90, 120]
# b = [80, 150, 80, 150]

# m, n, k = 5, 4, 1
# a = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000]
# b = [1000000000, 1000000000, 1000000000, 1000000000]

# 組み合わせを考えること
# aだけをk分読むと何冊か
# bだけをk分読むと何冊か
# 上記で、組み合わせの数を限定することができる

# aの限界
max_a = 0
i = 0
for t in a:
  if max_a + t <= k:
    max_a += t
    i += 1
  else:
    break

# bの限界
max_b = 0
j = 0
for t in b:
  if max_b + t <= k:
    max_b += t
    j += 1
  else:
    break

# print(max_a)
# print(i)

# print(max_b)
# print(j)

if i-1 <= 0 and j-1 <= 0:
  print(0)
elif i-1 <= 0:
  print(j-1)
elif j-1 <= 0:
  print(i-1)
else:
  # i-1とj-1が1より大きいと、
  # 限定されたところまで組み合わせの数を求める
  combis = []
  count = []
  sum_a = 0
  sum_i = 0
  for l, t in enumerate(a[:i]):
    if sum_a + t > k:
      break
    else:
      sum_a += t
      sum_i += 1

      sum_b = 0
      sum_j = 0
      for m, u in enumerate(b[:j]):
        if sum_a + sum_b + u > k:
          break
        else:
          sum_b += u
          sum_j += 1
          combis.append(sum_a+sum_b)
          count.append(sum_i+sum_j)

  # print(combis)
  # print(count)
  print(max(count))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

a = input().split()
np_a=np.array(a, dtype='int64')
b = input().split()
np_b=np.array(b, dtype='int64')

ia=0
ib=0
time=0
hon=n+m
for i in range(0,n+m):
  if (ia == n): 
    time+=np_b[ib]
    ib+=1
  elif (ib == m): 
    time+=np_a[ia] 
    ia+=1
  elif (ia < n or ib < m): 
    if (np_a[ia] <= np_b[ib]):
      time+=np_a[ia]
      ia+=1
    else:
      time+=np_b[ib]
      ib=+1
  else:
    print ("NO")
    
  if (time > k): 
    hon=i
    break

print (hon)

Submission status: Wrong Answer

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


sa = []
i = 0
sa.append(0)
while i < n:
    sa.append(sa[-1] + a[i])
    i += 1
    if sa[-1] > k:
        sa.pop(-1)
        break

#print(sa)
sb = []
i = 0
sb.append(0)
while i < m:
    sb.append(sb[-1] + b[i])
    i += 1
    if sb[-1] > k:
        sb.pop(-1)
        break
#print(sb)

max = -1
ans = 0
for i in range(1,len(sa)):
    for j in range(1,len(sb)):
        x = sa[i] + sb[j]
        if x > k:
            j = len(sb) + 1
        
        if x <= k:
            if x > max:
                max = x
                ans = i + j
                #print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import os
import sys
from collections import defaultdict, Counter, deque
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    
    n,m,k = map(int, input().split())
    n_list = list(map(int, input().split()))
    m_list = list(map(int, input().split()))
    i,j,count = 0,0,0

    while(k>=0):

        
        if count == n+m:
            break

        if i < n and j < m:

            if n_list[i] < m_list[j]:
                k -= n_list[i]
                i += 1
            else:
                k -= m_list[j]
                j += 1
              
            if k < 0:
                break
            else:
                count += 1

        else:
            if i >= n:
                if j >= m:
                    break
                k -= m_list[j]
                j += 1

            elif j >= m:
                if i >= n:
                    break
                k -= n_list[i]
                i += 1

            if k < 0:
                break
            else:
                count += 1

    print(count)



            

if __name__ == "__main__":
    main()

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

# book = sorted(A + B)
now = 0
ans = 0

while now <= K:
    if len(A) != 0:
        a = A.popleft()
    else:
        a = -1
    if len(B) != 0:
        b = B.popleft()
    else:
        b = -1

    if a == -1 and b == -1:
        break
    elif a != -1 and b != -1:
        if a <= b:
            now += a
            B.appendleft(b)
            if now > K:
                break
            ans += 1
        else:
            now += b
            A.appendleft(a)
            if now > K:
                break
            ans += 1
    elif a != -1:
        now += b
        if now > K:
            break
        ans += 1
    elif b != -1:
        now += a
        if now > K:
            break
        ans += 1
print(ans)

Submission status: Wrong Answer

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

DP = [[0 for i in range(N+1)] for j in range(M+1)]
index = []
flag = True

for i in range(0,M+1):
    for j in range(0,N+1):
        if i == 0 and j == 0:
            continue
        elif j == 0:
            DP[i][j] = int(DP[i-1][0]) + int(B[i-1])
            if DP[i][j] > K:
                break
        else:
            DP[i][j] = DP[i][j-1] + int(A[j-1])
        if DP[i][j] > K:
            if j != 0:
                index.append(i+j-1)
                break
            else:
                if flag is True:
                    index.append(i+j-1)
                    flag = False
                    break
if len(index) == 0:
    print(N+M)
else:
    print(max(index))

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()))
M=0
count=0
while M<k:
  if len(A)==0:
    M+=B.pop(0)
    #print(M,'B')
  elif len(B)==0:
    M+=A.pop(0)
    #print(M,'A')
  elif min(A[0],B[0])==A[0]:
    M+=A.pop(0)
    #print(M,'A')   
  elif min(A[0],B[0])==B[0]:
    M+=B.pop(0)
    #print(M,'B')
  else:
    M+=A.pop(0)
    #print(M,'A')
    
  if M>k:
    break
  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()))

ans = 0
a_ind = 0
b_ind = 0
while k > 0:
    if n > a_ind and m > b_ind:
        if a[a_ind] == b[b_ind]:
            nex = 0
            a_ind_ = a_ind
            b_ind_ = b_ind
            while a[a_ind_] == b[b_ind_]:
                if n > a_ind_+1:
                    a_ind_ += 1
                else:
                    nex = -1
                    break
                if m > b_ind_+1:
                    b_ind_ += 1
                else:
                    nex = 1
                    break
            if nex == 0:
                if a[a_ind_] < b[b_ind_]:
                    nex = 1
                else:
                    nex = -1
            if nex == 1:
                if k >= a[a_ind]:
                    k -= a[a_ind]
                    a_ind += 1
                    ans += 1
                else:
                    break
            else:
                if k >= b[b_ind]:
                    k -= b[b_ind]
                    b_ind += 1
                    ans += 1
                else:
                    break
        elif a[a_ind] < b[b_ind]:
            if k >= a[a_ind]:
                k -= a[a_ind]
                a_ind += 1
                ans += 1
            else:
                break
        else:
            if k >= b[b_ind]:
                k -= b[b_ind]
                b_ind += 1
                ans += 1
            else:
                break
    elif n > a_ind and m == b_ind:
        if k >= a[a_ind]:
            k -= a[a_ind]
            a_ind += 1
            ans += 1
        else:
            break
    elif n == a_ind and m > b_ind:
        if k >= b[b_ind]:
            k -= b[b_ind]
            b_ind += 1
            ans += 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:
            K -= a
        elif not a_is_available and b_is_available:
            K -= b
        elif a <= b:
            K -= a
            B.appendleft(b)
        else:
            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:
n, m, k = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ans = 0


def solve(n_, l, r):
    m_ = (l + r) // 2
    s = sum(Bs[: m_ + 1])
    if (s == n_):
        return m_
    else:
        if (r == l + 1):
            s_ = sum(Bs[: m_ + 1])
            if (s_ < k):
                return r
            else:
                return l
        elif (s > n_):
            r = m_
            return solve(n_, l, r)
        else:
            l = m_
            return solve(n_, l, r)
    

for i, a in enumerate(As):
    k_ = k - sum(As[: i + 1])
    if (k_ > 0):
        if (sum(Bs) <= k_):
            ans_ = (i+1) + m
        else:
            m_ = solve(k_, 0, m - 1)
            ans_ = (i + 1) + m_
        ans = max(ans, ans_)

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()))
num = min(N,M)
C= [None]*(num*2)
C[::2] = A[:num]
C[1::2] = B[:num]
C.extend(A[num:])
C.extend(B[num:])
  
if min(C[0],C[1])>K:
  print('0')
  exit()
k=0
ans=0
while k<=K:
  key=min(C[0],C[1])
  k+=key
  ans+=1
  C.remove(key)
  if len(C)==1:
    k+=C[0]
    ans+=1
    break
if k==K:
  print(ans)
else:
  print(ans-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin,stdout
def ss(s,a):
    nn=len(a)
    for i in range(nn):
        if a[i]>s:return i
    return nn
n,m,s=map(int, stdin.readline().split());M=10**9+2
dn=list(map(int, stdin.readline().split()))#+[M]
dm=list(map(int, stdin.readline().split()))#+[M]
pn=[dn[0]];pm=[dm[0]]
for v in dn[1:]:
    pn+=[pn[-1]+v]
for v in dm[1:]:
    pm+=[pm[-1]+v]
p=ss(s,pn)
q=ss(0 if p-1<0 else pn[p-1],pm)
pp=ss(s,pm)
qq=ss(0 if pp-1<0 else pm[pp-1],pn)
print(max(p+q,pp+qq))

'''cn=0;cm=0;cs=0
for v in dn:
    cs+=v
    if cn>=n-1 or cs>s:
        f=1;cs-=v
        for v1 in dm:
            cs+=v1
            if cs>s:
                f=0
                break
            cn+=1
        if f==0:break
    #print(v)
    cn+=1
cs=0
for v in dm:
    cs+=v
    if cm>=m-1 or cs>s:
        f=1;cs-=v
        for v1 in dn:
            cs+=v1
            if cs>s:
                f=0
                break
            cm+=1
        if f==0:break
    cm+=1
print(max(cn,cm))'''

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()))
sum,count = 0,0
for _ in range(N+M+1):
    if sum>K:
        break
    else:
        if A and B:
            if A[0]<B[0]:
                sum+=A.popleft()
                count+=1
            else:
                sum+=B.popleft()
                count+=1
        elif not A and B:
            sum+=B.popleft()
            count+=1
        elif not B and A:
            sum+=A.popleft()
            count+=1
        else:
            print(count)
            exit()
print(count-1)

Submission status: Wrong Answer

Solution code:
temp = [int(i) for i in input().split(" ")]
n = temp[0]
m = temp[1]
k = temp[2]

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

counter = 0
time = 0

only_a_max = 0
only_b_max = 0

hoge = 0
for i in range(len(a)):

    hoge += a[i]
    if hoge <= k:
        only_a_max += 1
    else:
        break

hoge = 0
for i in range(len(b)):
    hoge += b[i]
    if hoge <= k:
        only_b_max += 1
    else:
        break


aindex = 0
bindex = 0

while True:


    target = 0

    if len(a) > aindex and len(b) > bindex:
        if a[aindex] <= b[bindex]:
            target = a[aindex]
            aindex += 1
        else:
            target = b[bindex]
            bindex += 1

    elif len(a) > aindex and len(b) == bindex:
        target = a[aindex]
        aindex += 1

    elif len(b) > bindex and len(a) == aindex:
        target = b[bindex]
        bindex += 1


    time += target
    if time <= k:
        counter += 1
    else:
        break

    if len(a) == aindex and len(b) == bindex:
        break



ans = max(only_b_max, only_a_max, counter)
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

while True:
    if not a:
        while b != []:
            k -= b[0]
            if k < 0: break
            del b[0]
            count += 1
        break
    elif not b:
        while a != []:
            k -= a[0]
            if k < 0: break
            del a[0]
            count += 1
        break
    elif a[0] <= b[0]:
        k -= a[0]
        if k < 0: break
        del a[0]
        count += 1
    else:
        k -= b[0]
        if k < 0: break
        del b[0]
        count += 1

print(count)

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()))
ans = 0
#print(a, b)
time = k
for i in range(400010):
    if time > 0:
        if not a:
            if not(not b):
                time -= b[0]
                b.popleft()
                ans += 1
               # print(a, b, time)
            else:
               # print(a, b, time)
                break
        elif not b and not(not a):
            time -= a[0]
            a.popleft()
            ans += 1
            #print(a, b, time)
        elif not(not a) and not(not b):
            if a[0] <= b[0]:
                time -= a[0]
                a.popleft()
                ans += 1
                #print(a, b, time)
            else:
                time -= b[0]
                b.popleft()
                ans += 1
                #print(a, b, time)
if time < 0:
    print(ans - 1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline


def SS(A, K):
    sA = 0
    AA = []
    for a in A:
        sA += a
        if sA > K:
            break
        AA.append(sA)

    return AA


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

    AA = SS(A, K)
    BB = SS(B, K)
    AA = AA[::-1]

    M = 0

    left = 0
    right = len(BB) - 1

    while left < len(AA):
        if AA[left] + BB[right] <= K:
            m = len(AA) - left + right + 1
            if m > M:
                M = m
            left += 1
        else:
            right -= 1
            if right < 0:
                break

    print(M)


main()