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 math,bisect,collections
n,m,k=map(int,input().split())
stack1=list(map(int,input().split()))
stack2=list(map(int,input().split()))
stack1.reverse()
stack2.reverse()
count=k
ans=0
while stack1 and stack2 and count>stack1[-1] and count>stack2[-1] :
    
    if stack1 and stack1[-1]<stack2[-1]:
        count-=stack1[-1]
        stack1.pop()
        ans+=1
        
    elif stack2 and stack1[-1]>stack2[-1]:
        count-=stack2[-1]
        stack2.pop()
        ans+=1

    else:
        count-=stack1[-1]
        stack1.pop()
        ans+=1


while stack1 and count>=stack1[-1]:
    count-=stack1[-1]
    ans+=1
    stack1.pop()
while stack2 and count>=stack2[-1]:
    count-=stack2[-1]
    ans+=1
    stack2.pop()
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

flag = True
que = list()

a = b = val = -1

while flag:
    if len(A) > 0:
        a = A.pop(0)
    if len(B) > 0:
        b = B.pop(0)

    if a < 0 and b < 0:
        flag = False
        break

    else:
        if a < b:
            val = a
            B.insert(0, b)
        else:
            val = b
            A.insert(0, a)

    if sum(que) + val <= K:
        que.append(val)
    else:
        flag = False
        break

print(len(que))

Submission status: Wrong Answer

Solution code:
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import bisect


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


def main():
    N, M, K = LI()
    AArray = LI()
    BArray = LI()

    aSum = [AArray[0]]
    bSum = [BArray[0]]

    cnt = 0

    for i in range(1, N):
        aSum.append(aSum[i-1]+AArray[i])
    for i in range(1, M):
        bSum.append(bSum[i-1]+BArray[i])

    for i in range(N):
        a = aSum[i]
        if a > K:
            if i == 0:
                b1 = bisect.bisect_right(bSum, K)
                cnt = max(cnt, b1)
            break
        if a == K:
            cnt = max(cnt, i+1)
            break
        sa = K-a
        b = bisect.bisect_right(bSum, sa)
        cnt = max(cnt, i+1+b)

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

i,j=0,0
lA,lB=len(A),len(B)

while True:
  if K<=0:
    break
  if A[i]<=B[j] and i<lA:
    K-=A[i]
    i+=1  
  elif B[j]<=A[i] and j<lB:
    K-=B[j]
    j+=1
  if i>=lA and K>0:
    while j<lB and K>=0:
      K-=B[j]
      j+=1
  elif j>=lB and K>0:
    while i<lA and K>=0:
      K-=A[i]
      i+=1

count=max(0,i+j-1) if K<0 else i+j
print(count)

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())))
read_time = 0
cnt = 0
while a or b:
    if not a:
        read_time += b.popleft()
        if read_time > k:
            break
        cnt += 1
    elif not b:
        read_time += a.popleft()
        if read_time > k:
            break
        cnt += 1
    else:
        if a[0] < b[0]:
            read_time += a.popleft()
            if read_time > k:
                break
            cnt += 1
        elif a[0] > b[0]:
            read_time += b.popleft()
            if read_time > k:
                break
            cnt += 1
        else:
            if len(a) > 1 and len(b) > 2:
                if a[1] > b[1]:
                    read_time += b.popleft()
                    if read_time > k:
                        break
                    cnt += 1
                elif a[1] < b[1]:
                    read_time += a.popleft()
                    if read_time > k:
                        break
                    cnt += 1
                else:
                    read_time += a.popleft()
                    if read_time > k:
                        break
                    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
a, b, c = list(map(int, input().split()))
d = list(map(int, input().split()))
e = list(map(int, input().split()))
e.extend(d)
e.sort()
n = 1
   
while True:
  if sum(e[:n]) >= c:
    if sum(e[:n]) - c < 0:
      n -=  1
    break
  n += 1
 
print(n - 1)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = []
B = []
A = input().split()
B = input().split()
XA = 0
XB = 0
TI = 0
for i in range(N+M):
  if XA == N:
    if (TI + int(B[XB]))<= K:
      TI = TI + int(B[XB])
      XB = XB +1
  elif XB == M:
    if (TI + int(A[XA]))<= K:
      TI = TI + int(A[XA])
      XA = XA +1
  else:
    if int(A[XA]) <= int(B[XB]):
      if (TI + int(A[XA]))<= K:
        TI = TI + int(A[XA])
        XA = XA +1
      else:
        break
    else:
      if (TI + int(B[XB]))<= K:
        TI = TI + int(B[XB])
        XB = XB +1
      else:
        break
print(XA+XB)

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=0
b=0
t=0
while t<=k:
  if a<=n-1 and b<=m-1:
    if A[a]<B[b]:
      t+=A[a]
      a+=1
    elif A[a]>B[b]:
      t+=B[b]
      b+=1
    else:
      t+=A[a]
      a+=1
  elif a>=n and b<=m-1:
    t+=B[b]
    b+=1
  elif b>=m and a<=n-1:
    t+=A[a]
    a+=1
  else:
    print(n+m)
    exit()
print(a+b-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()))
books = 0
i,j = 0,0
bsum = 0
while i<n and j<m:
    if a[i]<b[j]:
        if bsum+a[i]<=k:
            books += 1
            bsum += a[i]
        i += 1
    else:
        if bsum+b[j]<=k:
            books += 1
            bsum += b[j]
        j += 1
while i<n:
    if bsum + a[i] <= k:
        books += 1
        bsum += a[i]
    i += 1
while j<m:
    if bsum + b[j] <= k:
        books += 1
        bsum += b[j]
    j += 1
print(books)

Submission status: Wrong Answer

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

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

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

ans = 0
cost = 0
b_i = 0
if not (a[0] >= k and b[0] >= k):
  while 1:
    if ans >= n:
      if ans-n>=m:
        break
      _next = b[n-ans]
      b_i += 1
    else:
      _next = a[ans]
    if (cost + _next) <= k:
      ans += 1
      cost += _next
    else:
      b_i -= 1
      break

  max_ans = ans
  if b_i == 0 or b_i == -1:
    for i in range(ans-1,-1,-1): # 2から
      ans -= 1
      cost -= a[i]
      while cost < k and b_i < m:
        ans += 1
        cost += b[b_i]
        b_i += 1
      max_ans = max(max_ans, ans)
  else:
    for i in range(n-1,-1,-1):
      ans -= 1
      cost -= a[i]
      while cost < k and b_i < m:
        ans += 1
        cost += b[b_i]
        b_i += 1
      max_ans = max(max_ans, ans)

  print(max_ans)
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

time = 0
books = 0
if min(A[0], B[0]) > a[2]:
    books = 0
else:
    while True:
        if time > a[2]:
            books -= 1
            break
        if len(A) == 0 and len(B) == 0:
            break
        if len(A) == 0 and len(B) != 0:
            books += 1
            time += B[0]
            B.pop(0)
        elif len(A) != 0 and len(B) == 0:
            books += 1
            time += A[0]
            A.pop(0)
        else:
            time += min(A[0], B[0])
            books += 1
            if A[0] < B[0]:
                A.pop(0)
            elif A[0] > B[0]:
                B.pop(0)

    
print(books)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

b_q = deque(b_list)
k_q = deque()

ans = 0

for a in a_list:
    if k - a >=0:
        k_q.append(a)
        k -= a
        ans += 1
while True:
    if b_q:
        b = b_q.popleft()
        if k - b >=0:
            k -= b
            ans += 1
        else:
            break
    else:
      break
el_num = len(k_q)
final_ans = ans
for i in range(el_num):
    ans -= 1
    k += k_q.pop()
    while b_q and True:
        if k - b_q[0] >= 0:
            ans += 1
            b_q.popleft()
        else:
            break
    final_ans = max(final_ans, ans)
print(final_ans)

Submission status: Wrong Answer

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

A_np = np.array(A)
B_np = np.array(B)

C_np = np.append(A_np,B_np)
C_np = np.sort(C_np)
y=0
for i in range(len(C_np)):
    if K >= C_np[i]:
        K -= C_np[i]
        y += 1
    else:
        break
print(y)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
lt = []
x = 0
a_max = 0
t = 0
for i in range(n):
  t += la[i]
  if t > k:
    a_max = i-1
    t -= la[i]
    x = i
    lt.append(x)
    break
  if i == n-1:
    x = i+1
    lt.append(x)

j = 0
for i in range(m):
  t += lb[i]
  x += 1
  if t > k:
    lt.append(x-1)
    if j <= a_max:
      t -= la[a_max-j]
      x -= 1
      j += 1
    else:
      break
  if i == m-1:
    lt.append(x)

print(max(lt))

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 = [0]
B = [0]
A.extend(a)
B.extend(b)
time = sum(A)
ai = len(A)-1
bi = 0
con = 0
flg = None
while True:
    #print("---------")
    #print(str(ai)+":"+str(bi))
    if time > K:
        if flg is not None:
            con = max(con,flg-1)
            flg = None
        
        if ai == 0:
            break
        time -=A[ai]
        ai -= 1
    else:
        #print("b")
        flg = ai+bi
        time +=B[bi]
        bi += 1
        if bi == len(B):
            if time > K:
                pass
            else:
                con = max(con,ai+bi-1)
            break
    #print(flg)
print(con)

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

from itertools import accumulate
a_acc=list(accumulate(a))
b_acc=list(accumulate(b))

cnt=0

import bisect
for i in range(n):
    nokori=k-a_acc[i]
    if nokori<b_acc[0]:
        break
    b_i=bisect.bisect_right(b_acc,nokori)
    cnt=max(cnt,(i+1)+b_i)
    # print(nokori,i,b_i)

print(cnt)

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


arr = []
counter = 0

while True:


    if len(a) > 0 and len(b) > 0:
        if a[0] <= b[0]:
            arr.append(a.pop(0))
        else:
            arr.append(b.pop(0))

    elif len(a) > 0 and len(b) == 0:
        arr.append(a.pop(0))

    elif len(b) > 0 and len(a) == 0:
        arr.append(b.pop(0))


    time = sum(arr)
    if time <= k:
        counter += 1
    else:
        break

    if len(a) == 0 and len(b) == 0:
        break

print(counter)

Submission status: Wrong Answer

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

i = 0
j = 0
ans = 0
time = 0
while (time <= K):
    if i < N and j < M:
        if A[i] <= B[j]:
            if (time+A[i]) <= K:
                time += A[i]
                ans +=1
                i +=1
            else:
                break
        else:
            if (time+B[j]) <= K:
                time += B[j]
                ans +=1
                j +=1
            else:
                break
    elif i < N and j >= M:
        if (time+A[i]) <= K:
            time += A[i]
            ans +=1
            i +=1
        else:
            break
    elif i >= N and j < M:
        if (time+B[j]) <= K:
            time += B[j]
            ans +=1
            j +=1
        else:
            break
    else:
        break
print(ans)

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(' ')))
l=0
while (A !=[] or B != []) and K>0:
    if A==[]:
        K=K-B[0]
        B.pop(0)
        continue
    if B==[]:
        K=K-A[0]
        A.pop(0)
        continue
    if A[0]<=B[0]:
        K=K-A[0]
        A.pop(0)
    else:
        K=K-B[0]
        B.pop(0)
if A ==[] and B == [] and K>=0:
    print(N+M)
else:
    print(N+M- len(A)-len(B)-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()))
cnt=0

while True:
    if not (a or b):
        print(cnt)
        break
    elif not a:
        k -= b[0]
        b.remove(b[0])
    elif not b:
        k -= a[0]
        a.remove(a[0])
    elif a[0]>=b[0]:
        k-=b[0]
        b.remove(b[0])
    else:
        k-=a[0]
        a.remove(a[0])
        
    if k<0:
        print(cnt)
        break
    else:
        cnt+=1

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]+list(map(int,input().split()))
C=[0]
cnt=0
a=0
ans=0
for i in A:
    a+=i 
    if a<=K:
        C.append(a)
        cnt+=1
    else:break
for j in range(cnt+1):
    b=C[j]
    for k in range(M):
        b+=B[k]
        if b<=K:
            ans=max(ans,j+k)
        else:break
print(ans)

Submission status: Wrong Answer

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


a_acc = [0]
b_acc = [0]

for i in range(n):
    a_acc.append(a_acc[-1] + a[i])
for i in range(m):
    b_acc.append(b_acc[-1] + b[i])
ans = 0
#print(len(b_acc))
#0冊からn冊まで
for i in range(n+1):
    criteria = k - a_acc[i]
    if criteria < 0:
        break
    #b_accについて2分探索
    left = 0
    right = m
    #print(i,criteria)
    while left != right-1:
        #print(left,right)

        tmp = (left + right) // 2
        if criteria == b_acc[right]:
            left = right
            break
        elif criteria < b_acc[tmp]:
            right = tmp
        else:
            left = tmp
    ans = max(ans,i + left)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,k = map(int, input().split())
a = deque(map(int, list(input().split())))
b = deque(map(int, list(input().split())))
t = 0
c = 0
while True:
    if c < n+m:
        if len(b) == 0 and t + a[0] <= k:
            t = t + a[0]
            a.popleft()
            c = c + 1
        elif len(a) == 0 and t + b[0] <= k:
            t = t + b[0]
            b.popleft()
            c = c + 1
        elif len(a) > 0 and len(b) > 0:
            if a[0] <= b[0] and a[0] <= 1000000000 and b[0] <= 1000000000 and t + a[0] <= k:
                t = t + a[0]
                a.popleft()
                c = c + 1
            elif a[0] > b[0] and a[0] <= 1000000000 and b[0] <= 1000000000 and t + b[0] <= k:
                t = t + b[0]
                b.popleft()
                c = c + 1
            else:
                break
        else:
            break
    else:
        break
print(c)

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

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

x.pop(0)

pos = bisect.bisect_right(x, k)
print(pos)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
pa = abs(N-M) + 1
A = list(map(int, input().split()))+ [1000000010]
B = list(map(int, input().split()))+ [1000000010]
c = 0
while K >= A[0] or K >= B[0]:
    if A[0] < B[0]:
        K -= A.pop(0)
        c += 1
    elif A[0] > B[0]:
        K -= B.pop(0)
        c += 1
    elif A[0] + B[0] == 1000000010*2:
        break
print(c)

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 = a[::-1]
b = b[::-1]
sum, ans = 0, 0
while(len(a)>0 and len(b)>0):
    sum+=min(a[-1],b[-1])
    if sum>k:
        break
    else:
        ans+=1
        if(a[-1] < b[-1]):
            a.pop()
        else:
            b.pop()
if(sum<k and len(a)!=0):
    for i in range(len(a)):
        sum+=a[-1]
        if sum>k:
            break
        else:
            ans+=1
            a.pop()
if(sum<k and len(b) !=0):
    for i in range(len(b)):
        sum+=b[-1]
        if sum>k:
            break
        else:
            ans+=1
            b.pop()
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(0)
B.append(0)
ans = 0
for i in range(N+1):
  tmp = sum(A[:i])
  if K - tmp < 0:
    break
  for j in range(M+1):
    tmp += sum(B[:j])
    if K - tmp < 0:
      break
    ans = max(ans, i+j+2)
print(ans)

Submission status: Wrong Answer

Solution code:
n, m ,k = map(int,input().split())
a_list = [int(a) for a in input().split()]
b_list = [int(b) for b in input().split()]
time = 0
a_num = 0
b_num = 0
count = 0

while True:
    
    if (len(a_list) < a_num + 1) and (len(b_list) < b_num + 1): 
        break

    elif  (len(a_list) < a_num + 1) and(len(b_list) >= b_num + 1):
        time += b_list[b_num]      
        b_num += 1
     
    elif (len(a_list) >= a_num + 1) and (len(b_list) < b_num + 1):
        time += a_list[a_num]      
        a_num += 1
        
    else:
        if a_list[a_num] < b_list[b_num]:
            time += a_list[a_num]
            a_num += 1
        else:
            time += b_list[b_num]      
            b_num += 1
    
    if time <= k:
        count += 1
    else:
        break

print(count)

Submission status: Wrong Answer

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

class Cumulative_Sum:
    def __init__(self, a):
        la = len(a)
        self.s = [0] * (la + 1)
        for i in range(la):
            self.s[i+1] = self.s[i] + a[i]
    # s[l]からs[r]までの和
    def sum(self, l, r):
        return self.s[r] - self.s[l]
csa = Cumulative_Sum(a)
csb = Cumulative_Sum(b)
#print(csa.s)
#print(csb.s)
ans = 0
for i in range(n+1):
    t = csa.s[i]
    if t > k:
        continue
    p = k - t
    bi = bisect.bisect_left(csb.s, p)
    if csb.s[m] <= p:
        ans = max(ans, i + m)
    else:
        ans = max(ans, i + bi-1)

print(ans)

Submission status: Wrong Answer

Solution code:
input = raw_input

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

ai = 0
bi = 0
cost = 0
flag = True
count = 0
a = As[ai]
b = Bs[bi]

cur = 0
a_cumsum = []
for i in range(n):
    cur += As[i]
    if cur > k:
        n = i
        break
    else:
        a_cumsum.append(cur)

cur = 0
b_cumsum = []
for i in range(m):
    cur += Bs[i]
    if cur > k:
        m = i
        break
    else:
        b_cumsum.append(cur)
#print("n", n)
#print("m", m)
#print(a_cumsum)
#print(b_cumsum)

out = 0
end = False
for i in reversed(range(n)):
    a_count = i +1
    cur = a_cumsum[i]
    cur_count = None
    
    for j in reversed(range(m)):
        b_count = j + 1
        cur2 = cur + b_cumsum[j]
        if cur2 <= k:
            if b_count == m:
                end = True
            cur_count = a_count + b_count
            #print(i, a_count, b_count)
            break
    
    if cur_count is None:
        cur_count = a_count
        #print(i, a_count, 0)
    out = max(out, cur_count)

    if end:
        break

print(out)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

atop = 0
btop = 0
zikan = 0
satu = 0
yomu = 0

def ruiseki(list):
    cnt = 0
    for i in list:
        cnt += i
    return i

while True:
    if atop >= n and btop >= m:
        break
    if a[atop] < b[btop]:
        yomu = a[atop]
        atop += 1
    elif a[atop] > b[btop]:
        yomu = b[btop]
        btop += 1
    else:
        if ruiseki(a[atop:n]) <= ruiseki(b[btop:m]):
            yomu = a[atop]
            atop += 1
        else:
            yomu = b[btop]
            btop += 1
    if zikan + yomu <= k:
        zikan += yomu
        satu += 1
    else:
        break
print(satu)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = deque([int(i) for i in readline().split()])
    B = deque([int(i) for i in readline().split()])

    sig = 0
    cnt = 0
    while (len(A) > 0) or (len(B) > 0):
        if (len(A) > 0) and (len(B) == 0):
            a = A.popleft()
            sig += a
            if sig <= K:
                cnt += 1
        elif (len(A) == 0) and (len(B) > 0):
            b = B.popleft()
            sig += b
            if sig <= K:
                cnt += 1
        else:
            a = A[0]
            b = B[0]
            if a <= b:
                sig += a
                A.popleft()
                if sig <= K:
                    cnt += 1
            else:
                sig += b
                B.popleft()
                if sig <= K:
                    cnt += 1
    print(cnt)
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()))
A.reverse()
B.reverse()


jikan=0
count=0
while True:
    if len(A)==0 and len(B)==0:
        count+=1
        break

    elif len(A)==0:
        jikan+=B[-1]
        count+=1
        del B[-1]
        if jikan>K:
            break

    elif len(B)==0:
        jikan+=A[-1]
        count+=1
        del A[-1]
        if jikan>K:
            break
    else:
        if A[-1] <= B[-1]:
            jikan+=A[-1]
            count+=1
            del A[-1]
            if jikan>K:
                break
        else:
            jikan+=B[-1]
            count+=1
            del B[-1]
            if jikan>K:
                break
    #print('A,B:', A,B)

print(count-1)

Submission status: Wrong Answer

Solution code:
N ,M, K = map(int, input().split())
list_A = [int(i) for i in input().split()]
list_B = [int(i) for i in input().split()]

total_books = 0

total_a = 0
max_len_a = 0
total_b = 0
max_len_b = 0

for i in list_A:
  if total_a < K:
    total_a += i
    max_len_a += 1
  else:
    break
    
for i in list_B:
  if total_b < K:
    total_b += i
    max_len_b += 1
  else:
    break

for i in range(max_len_a-1):
  for j in range(max_len_b-1):
    temp_a = sum(list_A[:(max_len_a - i)])
    temp_b = sum(list_B[:(max_len_b - j)])
    temp_books = (max_len_a - i) + (max_len_b - j)
    
    if K < temp_a + temp_b:
      continue
    else:
      if temp_books > total_books:
        total_books = temp_books
      break
      
print(total_books)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

#DIV = (10**9) + 7
DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    N,M,K = isRaw()
    As = isRaw()
    Bs = isRaw()
    sAs = [0]
    for a in As:
        t = a + sAs[-1]
        if t>K:
            break
        sAs.append(t)

    sBs=[0]
    for b in Bs:
        t = b + sBs[-1]
        if t>K:
            break
        sBs.append(t)

    ai = len(sAs)-1
    bi = len(sBs)-1
    while sAs[ai]+sBs[bi] > K:
        if ai==0 and bi==0:
            break
        dA = 10**9+1
        if ai!=0:
            dA = sAs[ai]-sAs[ai-1]
        dB = 10**9+1
        if bi!=0:
            dB = sBs[bi]-sBs[bi-1]
        if dA >dB:
            ai=ai-1
            if bi+1!=len(sBs) and sAs[ai]+sBs[bi+1]<sAs[ai+1]+sBs[bi]:
                bi+=1
        else:
            bi=bi-1
            if ai+1 != len(sAs) and sAs[ai+1]+sBs[bi] < sAs[ai]+sBs[bi+1]:
                ai += 1
    print(ai+bi)


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

max_num = 0

for a in range(N):
    for b in range(M):
        if sum(A[:a]+B[:b]) <= K:
            if max_num < a+b:
                max_num = a+b
        else:
            break
        
        



print(int(max_num))

Submission status: Wrong Answer

Solution code:

N,M,K = map(int,input().split())
A_lis = list(map(int,input().split()))
B_lis = list(map(int,input().split()))





if A_lis[0]>B_lis[0]:
    status=B_lis[0]
elif  A_lis[0]<B_lis[0]:
    status=A_lis[0]
else:
    status=A_lis[0]
    
    

count=0



for i in range(len(A_lis)+len(B_lis)):
    if K>status:
        if len(A_lis)>0 and len(B_lis)>0:
            if A_lis[0]<B_lis[0]:
                pd=A_lis.pop(0)
                status+=pd
                count+=1
            elif A_lis[0]>B_lis[0]:
                pd=B_lis.pop(0)
                status+=pd
                count+=1
            elif A_lis[0]==B_lis[0]:
                pd=A_lis.pop(0)
                status+=pd
                count+=1
        elif len(A_lis)==0:
            pd=B_lis.pop(0)
            status+=pd
            count+=1
        elif len(B_lis)==0:
            pd=A_lis.pop(0)
            status+=pd
            count+=1
print(count)