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

i, j = 0, 0
readed = 0
count = 0
tmp1 = []
tmp2 = []
for i in range(n):
    for j in range(m):
        tmp1.append(sum(a[:i])+sum(b[:j]))
        tmp2.append(i+j)
tmp1 = np.array(tmp1)
tmp = sorted(list(np.where(tmp1 > k)[0]), reverse=True)
if len(tmp) != 0:
    for i in tmp:
        tmp2.pop(i)
if len(tmp2) == 0:
    print(0)
else:
    print(max(tmp2))

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()))
 
a, b = 0, 0
a2, b2 = 0, 0
while a2 < n and b2 < m and As[a2] == Bs[b2]:
  a2 += 1
  b2 += 1
  
while k > 0:
  if a < n and b < m:
    if As[a] < Bs[b]:
      if k < As[a]:break
      k -= As[a]
      a += 1
    elif As[a] > Bs[b]:
      if k < Bs[b]:break
      k -= Bs[b]
      b += 1
    else:
      if k >= As[a] + Bs[b]:
        k -= As[a] + Bs[b]
        a += 1
        b += 1
      else:
        if a2 < n and b2 < b:
          if As[a2] < Bs[b2]:
            if k < As[a]:break
            k -= As[a]
            a += 1
          else:
            if k < Bs[b]:break
            k -= Bs[b]
            b += 1
        elif a2 < n:
          if k < As[a]:break
          k -= As[a]
          a += 1
        elif b2 < m:
          if k < Bs[b]:break
          k -= Bs[b]
          b += 1
        else:
          break
  elif a < n:
    if k < As[a]:break
    k -= As[a]
    a += 1
  elif b < m:
    if k < Bs[b]:break
    k -= Bs[b]
    b += 1
  else:
    break
    
print(a+b)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if sum(A)+sum(B)<=K:
  print(len(A)+len(B))
elif sum(A)<K and sum(A)+sum(B)>K:
  ans=len(A)
  time=sum(A)
  for i in range(len(B)):
    time+=B[i]
    if time>=K:
      print(ans)
      exit()
    else:
      ans+=1
elif sum(B)<K and sum(A)+sum(B)>K:
  ans=len(B)
  time=sum(B)
  for i in range(len(A)):
    time+=A[i]
    if time>=K:
      print(ans)
      exit()
    else:
      ans+=1
else:
  ans=0
  time=0
  while time>K :
    time+=min(A[0],B[0])
    ans+=1
    if A[0]<B[0]:
      del A[0]
    else:
      del B[0]
  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()))

l = []
for i in range(len(a) + len(b)):
    if len(a) > 0 and len(b) > 0:
        if a[0] <= b[0]:
            l.append(a[0])
            a.remove(a[0])
        else:
            l.append(b[0])
            b.remove(b[0])
    elif len(a) > 0 and len(b) == 0:
        l.append(a[0])
        a.remove(a[0])
    elif len(a) == 0 and len(b) > 0:
        l.append(b[0])
        b.remove(b[0])
    elif len(a) == 0 and len(b) == 0:
        break

count = 0
time = 0
for i in range(len(l)):
    time += l[0]
    if time > k:
        break
    else:
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import bisect

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

A = list(accumulate(A))
B = list(accumulate(B))

a_in = bisect.bisect_left(A,K)
b_in = bisect.bisect_left(B,K)
a_in_b = bisect.bisect_left(A,K-B[b_in-1])
b_in_a = bisect.bisect_left(B,K-A[a_in-1])

ans_a = a_in + b_in_a
ans_b = b_in + a_in_b
print(max(ans_a,ans_b))

Submission status: Wrong Answer

Solution code:
N, M, K = (int(i) for i in input().split())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]

read_time = 0
read_book_num = 0
read_books = []
while (As != [] and read_time + As[0] < K ):
    time = As.pop(0)
    read_books.append(time)
    read_time += time
    read_book_num += 1

while (Bs != [] and read_time + Bs[0] < K):
    read_time += Bs.pop(0)
    read_book_num += 1
   
max_read_time = read_time
max_read_book_num = read_book_num
while read_books != []:
    # 取り出す
    read_time -= read_books.pop(0)
    read_book_num -= 1
    # 新たにいれる
    while(Bs != [] and read_time + Bs[0] < K):
        read_time += Bs.pop(0)
        read_book_num += 1

    if max_read_time < read_time:
        max_read_time = read_time
        max_read_book_num = read_book_num

print(max_read_book_num)

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import numpy as np

def resolve():
    n, m, k = map(int, input().split())
    A = [0] + list(map(int, input().split()))
    B = [0] + list(map(int, input().split()))

    cum_A = np.cumsum(A)
    cum_B = np.cumsum(B)

    # print(cum_A)
    # print(cum_B)

    ans = [0 for _ in range(n + 1)]

    for i in range(len(cum_A)):
        if k < cum_A[i]:
            pass
        elif k == cum_A[i]:
            ans[i] = i
        else:
            tmp_k = k - cum_A[i]

            left = 0
            right = len(cum_B) - 1
            # print(tmp_k)

            while 1:
                mid = (left + right) // 2 # 真ん中より左
                # print(left, right, mid)
                if mid == left or mid == right:
                    break
                else:
                    if tmp_k >= cum_B[mid]:
                        left = mid
                    else:
                        right = mid

            ans[i] = i + mid

    # print(ans)
    print(max(ans))



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 4 240
60 90 120
80 150 80 150"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 4 730
60 90 120
80 150 80 150"""
        output = """7"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

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()))
Acount = -1
Bcount = -1
tmpA = []
tmpB = []
tmp = 0
for i in range(N):
    tmp += A[i]
    if tmp > K:
        break
    tmpA.append(tmp)
tmp = 0
for i in range(M):
    tmp += B[i]
    if tmp > K:
        break
    tmpB.append(tmp)

if len(tmpA) > 0 and len(tmpB) > 0:
    res = 0
    Bcount = -1
    Acount = len(tmpA) - 1
    while Acount > -1 and Bcount < M:
        for i in range(max(Bcount, 0), len(tmpB)):
            if tmpA[Acount] + tmpB[i] > K:
                break
            Bcount = i
        tmpRes = Acount + Bcount + 2 if Bcount > -1 else Acount + 1
        if res < tmpRes:
            res = tmpRes
        Acount -= 1
    print(res)
elif len(tmpA) > 0:
    print(len(tmpA))
elif len(tmpB) > 0:
    print(len(tmpB))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
def main():
    n, m, k, *ab = map(int, open(0).read().split())
    a = ab[:n]
    b = ab[n:]

    if min(a[0], b[0]) > k:
        print(0)
        exit()

    if sum(a) + sum(b) <= k:
        print(n + m)
        exit()

    a += [float("Inf")] * (m + 1)
    b += [float("Inf")] * (n + 1)

    i = 0
    j = 0
    total = 0
    for _ in range(n + m + 1):
        x = a[i]
        y = b[j]
        if min(x, y) + total > k:
            break

        if x == y:
            if a[i + 1] <= b[j + 1]:
                i += 1
            else:
                j += 1
            total += x

        elif x < y:
            total += x
            i += 1
        else:
            total += y
            j += 1

    print(i + j)


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()))
ans = n + m
while sum(a) + sum(b) > k:
    if len(a) == 0:
        a.append(0)
    if len(b) == 0:
        b.append(0)
    if sum(a) < sum(b):
        del b[-1]
    else:
        del a[-1]
    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()))
temp=0
an,bn=0,0
ans=0
while temp<=k:
    if an>=n and bn>=m: break
    if an>=n:
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        temp+=a[an]
        if temp>k: break
        an+=1
    else:
        if a[an]<b[bn]:
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else: #同じとき
            cnt=0
            while True:
                if an==n:
                    an-=cnt
                    ans-=1
                    break
                if bn==m:
                    bn-=cnt
                    ans-=1
                    break
                if a[an]<b[bn]:
                    temp+=a[an]
                    an+=1
                    bn-=cnt+1
                    break
                if a[an]>b[bn]:
                    temp+=b[bn]
                    bn+=1
                    an-=cnt+1
                    break
                temp+=a[an]
                if temp>k:
                    break
                an+=1
                bn+=1
                ans+=1
                cnt+=1
            if temp>k:
                break
    ans+=1
print(ans)

Submission status: Wrong Answer

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

    sum_a = 0
    ret = 0
    for i in range(0, len(a)):
        if i+1 > k: break
        sum_a += a[i]
        sum_b = 0
        for j in range(0, len(b)):
            if i+j+2 > k: break
            sum_b = b[0]
            if sum_a+sum_b > ret: ret = sum_a+sum_b

    print(ret)

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict, Counter
N, M, K = (int(i) for i in input().split())  
A = list(map(int, input().split()))
B = list(map(int, input().split()))
da = deque(A)
db = deque(B)
 
ans = 0
total = 0
while True:
  if len(da) > 0 and len(db) > 0:
    if da[0] >= db[0]:
      total += db.popleft()
    else:
      total += da.popleft()
  elif len(db) > 0:
    total += db.popleft()
  elif len(da) > 0:
    total += da.popleft()
  else:
    break
 
  if total >= K:
    if total == K:
      ans += 1
    print(ans)
    exit()
  else:
    ans += 1
 
print(N+M)

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(" ")))
d=0
i=0
j=0
c=0
t=0
while(d<=k):
    if(i<n and j<m):
        if(a[i]<b[j]):
            d=d+a[i]
            i=i+1
            c=c+1
        else:
            if(a[i]>=b[j]):
                d=d+b[j]
                j=j+1
                c=c+1
    else:
        if(i>=n):
            if(j<m):
                d=d+b[j]
                j=j+1
                c=c+1
            else:
                t=t+1
                print(c)
        elif(j>=m):
            d=d+a[i]
            i=i+1
if(t==0):
    if(d>k):
        print(c-1)
    else:
        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()))
ans = 0
book = 0
for i in range(n):
  if ans < k:
    for j in range(m):
      if a[i] > b[j]:
        if ans+a[i] <= k:
          ans += a[i]
          book += 1
          continue
      elif a[i] < b[j]:
        if ans+b[j] <= k:
          ans += b[j]
          book += 1
          continue

print(book+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()))

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[b_i]
      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:
    b_i = 0
    for i in range(ans-1,-1,-1): # 2から
      # 今のままで足せる？
      while 1:
        if b_i >= m:
          break
        _next = b[b_i]
        if cost + _next > k:
          break
        cost += _next
        ans += 1
        b_i  += 1
        max_ans = max(max_ans, ans)
      ans -= 1
      cost -= a[i]
      while 1:
        if b_i >= m:
          break
        _next = b[b_i]
        if cost + _next > k:
          break
        cost += _next
        ans += 1
        b_i  += 1
      max_ans = max(max_ans, ans)
  else:
    b_i += 1
    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)

  if a[0] <= k:
    print(max_ans)
  else:
    ans = 0
    cost = 0
    _next = 0
    while 1:
      if ans >= m:
        break
      _next = b[ans]
      if cost + _next > k:
        break
      cost += _next
      ans += 1
    print(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(" ")))


time=0


ok_list=[]
for i in range(1,N+1):
  for j in range(1,M+1):
    if sum(A[:i])+sum(B[:j]) <=K:
      ok_list.append(i+j)

if len(ok_list)==0:
  print(0)
else:
   print(max(ok_list))

Submission status: Wrong Answer

Solution code:
from sys import stdin
An, Bn, t = [int(x) for x in stdin.readline().rstrip().split()]

A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]

idxA = idxB = cnt = 0

for i in range(An + Bn):
	# if t < 0 : break



	if An > idxA :
		calc = t - A[idxA]
		if calc < 0 : 
			idxA += 1
			continue
		t -= A[idxA]
		cnt += 1
		idxA += 1


	if Bn > idxB :
		calc = t - B[idxB]
		if calc < 0 :
			idxB += 1
			continue
		t -= B[idxB]
		cnt += 1
		idxB += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    n, m, k = map(int, input().split())
    a = deque(map(int, input().split()))
    b = deque(map(int, input().split()))
    asum = sum(a)
    bsum = sum(b)
    rk = asum + bsum - k
    time = 0
    bn = 0
    while (a or b):
        if time >= rk:
            break
        if a and b:
            if a[-1] > b[-1]:
                time += a.pop()
                bn += 1
                if time >= rk:
                    break
            else:
                time += b.pop()
                bn += 1
                if time >= rk:
                    break
        elif a:
            time += a.pop()
            bn += 1
            if time >= rk:
                break
        else:
            time += b.pop()
            bn += 1

            if time  >= rk:
                break
                

    r = n + m - bn
    print(r)


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()))
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
else:
    d = 0
    A = deque(A)
    B = deque(B)
    while sum(A) + sum(B) > K:
        if len(A) == 0:
            B.pop()
            d += 1
        elif len(B) == 0:
            A.pop()
            d += 1
        else:
            if A[-1] <= B[-1]:
                B.pop()
                d += 1
            else:
                A.pop()
                d += 1
    print(N + M - d)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = 0
c = 0
if sum(a) + sum(b) <= k:
    print(n+m)
    quit()
for i in range(min(n, m)-1):
    if t + a[i]> k:
        print(c)
        quit()
    t += a[i]
    c += 1
    if t + b[i]> k:
        print(c)
        quit()
    t += b[i]
    c += 1
    
if t < k:
    if max(n, m) == n:
        d = a
        e = b
    else:
        d = b
        e = a
    for i in range (len(e)-1, len(d)-1):
        if t + d[i] > k:
            print(c)
            quit()
        t += d[i]
        c += 1
    print(c)
else:
    print(c)

Submission status: Wrong Answer

Solution code:
def main():
    N,M,K = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    
    A_sum = 0
    A_max = N
    tmp_sum = 0
    if A[0] > K:
        flag = False
    else:
        flag = True
    for i in range(N):
        tmp_sum += A[i]
        if tmp_sum > K:
            A_max = i
            break
        else:
            A_sum = tmp_sum
        
    ans = A_max
    tmp_ans = ans
    #B_sum = 0
    B_i = 0
    time = K - A_sum
    count = 0
    for i in range(-1,A_max):
        if i != -1 and flag:
            A_i = A_max -1 -i
            time += A[A_i]
            count = -1
            
        if i == -1 or flag:
            tmp_time = time
            next_B_i = B_i
            for j in range(B_i,M):
                tmp_time -= B[j]
                if tmp_time >= 0:
                    next_B_i += 1
                    count += 1
                    time = tmp_time
                else:
                    break
                    
            B_i = next_B_i
            tmp_ans += count
            if count >= 0:
                ans = tmp_ans
            
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=map(int,input().split())
A=list(A)
B=map(int,input().split())
B=list(B)
time=int(0)
book=int(0)
ia=0 #iaは1~N冊
jb=0 #jbは1~M冊
while time<K and ia<N and jb<M:
    if A[ia]<=B[jb]:
        time += A[ia]
        book += 1
        ia+=1
        if ia==N:
            break
    else:
        time += B[jb]
        book +=1
        jb +=1
        if jb==M:
            break
if time<K and ia==N:
    while time<K and jb<M:
        time+=B[jb]
        book+=1
        jb+=1
        if jb==M:
            break
elif time<K and jb==M:
    while time<K and ia<N:
        time+=A[ia]
        book+=1
        ia+=1
        if ia==N:
            break
if time<=K:
    print(book)
else:
    print(book-1)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]


N, M, K = map(int, input().split())
dp = [[]]
#A = deque([int(i) for i in input().split()])
#B = deque([int(i) for i in input().split()])
A = [int(i) for i in input().split()]
AA = [0]
B = [int(i) for i in input().split()]
BB = [0]
for i in range(1,N+1):
    AA.append(AA[i-1]+A[i-1])
for i in range(1,M+1):
    BB.append(BB[i-1]+B[i-1])
ans = []
A_idx = 0
B_idx = M

for i in range(N+M+10):
    if(K<=AA[A_idx]+BB[B_idx]):
        if(B_idx>0):
            B_idx -= 1
        if(K>=AA[A_idx]+BB[B_idx]):
            ans.append(A_idx + B_idx)
    else:
        if(A_idx<N):
            A_idx += 1
        if(K>=AA[A_idx]+BB[B_idx]):
            ans.append(A_idx + B_idx)
        else:
            ans.append(A_idx + B_idx -1)


print(max(ans))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

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

sA=[A[0]]
for i in range(1,n):
    sA.append(sA[-1]+A[i])
sB=[B[0]]
for i in range(1,m):
    sB.append(sB[-1]+B[i])

ans=0
for i in range(n):
    temp=k-sA[i]
    if temp<0:
        break
    add=bisect_right(sB,temp)
    ans=max(ans,i+1+add)

print(min(ans,m+n))

Submission status: Wrong Answer

Solution code:
import queue

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

Aq = queue.Queue()
Bq = queue.Queue()
for i in A:
    Aq.put(i)
for i in B:
    Bq.put(i)

total = 0
cnt = 0

while total <= K:
    if Aq.empty() and Bq.empty():
        cnt += 1
        break
    elif Aq.empty():
        total += Bq.get()
    elif Bq.empty():
        total += Aq.get()
    elif Aq.get() < Bq.get():
        total += Aq.get()
    else:
        total += Bq.get()
    cnt += 1

print(cnt-1)

Submission status: Wrong Answer

Solution code:
def main():
  N, M, K = map(int,input().split())
  A = list(map(int,input().split()))
  B = list(map(int,input().split()))
  if sum(A) + sum(B) <= K:
    return N + M
  elif A[0] > K or B[0] > K:
    return 0
  count = 0
  prev = 0
  for i in range(N):
    A[i] += prev
    prev = A[i]
  prev = 0
  for j in range(M):
    B[j] += prev
    prev = B[j]
  for i in range(N):
    for j in range(M):
      if A[i] + B[j] <= K:
        count = max(count, i + j + 2)
  return count

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

count =0
i = 0
j = 0
flag = False
while count <= N+M and flag != True and (i!=N or j !=M):
        if flag != True and A[i] <= B[j] and K >= A[i] :
            K = K - A[i]
            print(i,j,K)
            i += 1
            count += 1
            if i == N:
                for p in range(j,M):
                    if K >= B[p]:
                        K = K - B[p]
                        print(i,p,K)
                        p += 1
                        count += 1
                        
                    else:
                        flag = True
                        break
                    
        if i == N and j == M:
            break
                
        if i == N:
            i -= 1
            
        if flag != True and B[j] <= A[i] and K >= B[j]:
            K = K - B[j]
            print(i,j,K)
            j += 1
            count += 1
            if j == M:
                for q in range(i,N):
                    if K >= A[q]:
                        K = K - A[q]
                        print(q,j,K)
                        q += 1
                        count += 1
                    else:
                        flag = True
                        break
                    
        if i == N and j == M:
            break
        
        if j == M:
            j -= 1
        

        
print(count)

Submission status: Wrong Answer

Solution code:
"""
AtCoder :: Beginner Contest 172 :: C :: Tsundoku
https://atcoder.jp/contests/abc172/tasks/abc172_c

"""
import sys
from itertools import accumulate


def solve(A, B, K):
    """Solve puzzle."""
    A0 = list(accumulate(A))
    B0 = list(accumulate(B))
    soln = 0
    for i, a in enumerate(A0):
        if a > K:
            break
        for j, b in enumerate(B0):
            if a + b <= K:
                soln = max(soln, i+1+j+1)
            else:
                break
    return soln


def main():
    """Main program."""
    N, M, K = (int(i) for i in sys.stdin.readline().split())
    A = [int(i) for i in sys.stdin.readline().strip().split()]
    B = [int(i) for i in sys.stdin.readline().strip().split()]
    print(solve(A, B, K))


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=[]
while True:
  if len(A)==0 or len(B)==0:
    break
  if A[0]<B[0]:
    a.append(A[0])
    A.pop(0)
  elif A[0]>B[0]:
    a.append(B[0])
    B.pop(0)
  elif A[0]==B[0]:
    a.append(A[0])
    a.append(B[0])
    A.pop(0)
    B.pop(0)
if len(A)!=0:
  a.extend(A)
elif len(B)!=0:
  a.extend(B)

books=0
count =0
for i in range(N+M):
  books += a[i]
  if books<=K:
    count+=1
  elif books > 0:
    break
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

Acum = np.cumsum(np.array(A))
Bcum = np.cumsum(np.array(B))

found = False
num1 = 0
for n in range(N,-1,-1):
    if n>0:
        tA = Acum[n-1]
    else:
        tA = 0
    if tA>K:
        continue
    
    for m in range(M,-1,-1):
        if m>0:
            tB = Bcum[m-1]
        else:
            tB = 0
        if tA+tB<=K:
            num1 = n+m
            found = True            
            break
    if found:
        break

found = False
num2 = 0
for m in range(M,-1,-1):
    if m>0:
        tB = Bcum[m-1]
    else:
        tB = 0
    if tB>K:
        continue
    
    for n in range(N,-1,-1):
        if n>0:
            tA = Acum[n-1]
        else:
            tA = 0
        if tA+tB<=K:
            num2 = n+m
            found = True            
            break
    if found:
        break

print(max(num1,num2))

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M, K = map(int,input().split())
al = deque(list(map(int,input().split())))
bl = deque(list(map(int,input().split())))

cnt = 0
time = 0
for i in range(N+M):
    if len(al) > 0 and len(bl) > 0:
        book = min(al[0], bl[0])
        if time + book > K:
            print(cnt)
            exit()
        else:
            time += book
            cnt += 1
            if al[0] < bl[0]:
                al.popleft()
            else:
                bl.popleft()
    elif len(al) == 0 and len(bl) > 0:
        book = bl[0]
        if time + book > K:
            print(cnt)
            exit()
        else:
            time += book
            cnt += 1
            bl.popleft()
    elif len(al) > 0 and len(bl) == 0:
        book = al[0]
        if time + book > K:
            print(cnt)
            exit()
        else:
            time += book
            cnt += 1
            al.popleft()
    else:
        print(cnt)
        exit()

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
time = 0
ares = 0
bres = 0
bsatu = int()
time2 = 0
res = []
for i in range(0,n): #Aで最高何冊読めるか
    if sum(a[n-i:]) < k and k <= sum(a[n-i-1:]):
        ares = i
for i in range(0,ares+1): #0からaresまで
    asatu = ares - i
    bsatu = 0
    time2 = sum(a[n-asatu:])
    bsatu += i
    if time >= k:
        break
    res += [asatu + bsatu]
print(max(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()))
count_a = 0
count_b = 0
total = 0
minutes = 0
while count_a < n and count_b < m and minutes < k:
  if a[count_a] < b[count_b] and minutes + a[count_a] <= k:
    total += 1
    minutes += a[count_a]
    count_a += 1
  elif a[count_a] > b[count_b] and minutes + a[count_a] <= k:
    total += 1
    minutes += b[count_b]
    count_b += 1
while count_a < n and  minutes + a[count_a] <= k:
  minutes += a[count_a]
  total += 1
  count_a += 1
while count_b < m and  minutes + b[count_b] <= k:
  minutes += b[count_b]
  total += 1
  count_b += 1
print(total)

Submission status: Wrong Answer

Solution code:
import itertools as it
def main():
    n,m,k=map(int,input().split())
    a = [0]
    a.extend(it.accumulate(map(int, input().split())))
    b = [0]
    b.extend(it.accumulate(map(int, input().split())))
    AA = calc(k,n,m,a,b)
    BB = calc(k,m,n,b,a)
    print(max(AA,BB))

def calc(k,n,m,a,b):
    for A in range(n,-1,-1):
        aa = a[A]
        Kaa = k-aa
        for B in range(m,-1,-1):
            if b[B] <= Kaa:
                return A+B
    return 0

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()))
sa = [0 for _ in range(n)]
sb = [0 for _ in range(m)]
x = 0
for i in range(n):
  x += a[i]
  sa[i] = x
y = 0
for j in range(m):
  y += b[j]
  sb[j] = y
si = -1
for i in range(n):
  if sa[n-1-i] <= k:
    si = n-1-i
    break
if si == -1:
  ej = -1
  for j in range(m):
    if sb[j] >k:
      ej = j
      break
  if ej == -1:
    print(m)
    exit()
  print(ej)
  exit()
else:
  ans = si+1
  ej = 0
  while True:
    while sa[si]+sb[ej] <= k:
      if ans < si + ej + 2:
        ans = si + ej + 2
      ej += 1
      if ej == m:
        break
    if si == 0 or ej == m:
      break
    else:
      si -= 1
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

l1 = str(input()).split()

N = int(l1[0])
M = int(l1[1])
K = int(l1[2])

aq = deque([int(v) for v in str(input()).split()])
bq = deque([int(v) for v in str(input()).split()])

t = 0
count = 0
while N > 0 or M > 0:
    if M == 0:
        t = aq.popleft()
        N -= 1
    elif N == 0:
        t = bq.popleft()
        M -= 1
    else:
        ta = aq.popleft()
        tb = bq.popleft()
        if ta < tb:
            t = ta
            bq.appendleft(tb)
            N -= 1
        else:
            t = tb
            aq.appendleft(ta)
            M -= 1

    if (K - t) < 0:
        break

    K -= t
    count += 1

print(count)