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:
N, M, K, *AB = map(int, open(0).read().split())

a = []
b = []
for ind, x in enumerate(AB):
    if ind < N:
        a.append(x)
    else:
        b.append(x)

sum = 0
a_max = len(a) - 1
for ind, x in enumerate(a):
    if x > K and sum == 0:
        a_max = -1
        break

    sum += x
    if sum > K:
        a_max = ind - 1

sum = 0
b_max = len(b) - 1
for ind, x in enumerate(b):
    if x > K and sum == 0:
        b_max = -1
        break

    if sum > K:
        b_max = ind - 2
    sum += x

max_count = max(a_max, b_max) + 1

if a_max < 0 and b_max < 0:
    print(0)
elif a_max < 0 or b_max < 0:
    print(max_count)
else:
    index = 0
    while index <= a_max:
        sum = 0
        count = 0
        for x in range(index + 1):
            sum += a[x]
            count += 1
        
        for y in range(b_max + 1):
            sum += b[y]
            if sum > K:
                break
            else:
                count += 1

        max_count = max(max_count, count)
        index += 1
    
    print(max_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()))

a.insert(0, 0)
ans = 0
num_a = n
num_b = 0
flg = True

total_time = sum(a)

for i in range(n+1):
    for j in range(num_b, m):
        if total_time + b[num_b] > k:
            break
        else:
            total_time += b[num_b]
            num_b += 1
        if num_a + num_b > ans:
            ans = num_a + num_b
    total_time -= a[num_a]
    num_a -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools as it
def main():
    n,m,k=map(int,input().split())
    a = [0] + list(it.accumulate(map(int, input().split())))
    b = [0] + list(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:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline
n, m, k = map(int, input().split())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]

from collections import deque
A, B = deque(a), deque(b)

sum = 0
count = 0
while True:
  if (len(A)>0) & (len(B)>0):
    a1, b1 = A.popleft(), B.popleft()
    if a1<b1:
      sum+=a1
      B.appendleft(b1)
    # elif a1>b1:
    else:
      sum+=b1
      A.appendleft(a1)
    # else: probably wrong answer  
  if sum>k:
    print(count)
    sys.exit()
  else:
    count+=1
  if (len(A)==0) | (len(B)==0):
    break
D = A if len(A)!=0 else B
while len(D)>0:
  sum += D.popleft()
  if sum>k:
    print(count)
    sys.exit()
  else:
    count+=1
print(count)

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()))
import random
import bisect

#n,m,k = random.randint(1,1000),random.randint(1,1000),random.randint(1,10**8)
##a = [random.randint(1,10**5) for i in range(n)]
#b = [random.randint(1,10**5) for i in range(m)]

def solv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

	for i in range(m):
		bkari[i+1] = bkari[i] + b[i]

	def bin(ls,s,e,sikii):
		if e - s <= 1:
			return s
		mid = (s+e)//2
		if sikii < ls[mid]:return bin(ls,s,mid,sikii)
		else:return bin(ls,mid,e,sikii)

	ans = 0
	for i in range(n+1):
		time = akari[i]
		if k - time <= 0:break
		#ref = bin(bkari,0,m+1,k-time)
		ref = bisect.bisect_right(bkari,k-time)
		ans = max(ans,ref+i)
	return ans

def karisolv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

	for i in range(m):
		bkari[i+1] = bkari[i] + b[i]

	ans = 0
	for i in range(n+1):
		time = akari[i]
		ref = 0
		if k - time <= 0:break
		cost = 0
		for j in range(m+1):
			if k - time - bkari[j] >= 0:
				ans = max(ans,j+i)
		
	return ans

def makedata():
	global a,b,n,m,k
	n,m,k = random.randint(1,10000),random.randint(1,10000),random.randint(1,1000000)
	a = [random.randint(1,10**5) for i in range(n)]
	b = [random.randint(1,10**5) for i in range(m)]
import sys
def main():
	for i in range(1000000):
		makedata()
		kari = karisolv()
		kotae = solv()
		if kari != kotae:
			print(n,m,k,a,b)
			sys.exit()
print(solv()-1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

a =([200010] * 3)+a[::-1]
b = ([200010] * 3)+b[::-1]

t = 0
i = 0
j = 0
ans = 0
al =[]

while t < k:
    if a[-1] <= b[-1]:
        t += a[-1]
        tmp = a.pop()
    else:
        t +=b[-1]
        tmp = b.pop()
    if t <= k:
        ans += 1
        al.append(tmp)

print(len(al))

Submission status: Wrong Answer

Solution code:
import sys
import math
# sys.stdin=open('input.txt','r')
# sys.stdout=open('output2.txt','w')
from sys import stdin,stdout
from collections import deque,defaultdict
from math import ceil,floor,inf,sqrt,factorial,gcd,log2
from copy import deepcopy
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
mod=1000000007

n,m,k=iia()
arr=iia()
ar=iia()
count=0
start=n-1
for i in range(n):
    if k>=arr[i]:
        count+=1
        k-=arr[i]
    else:
        start=i
        break
start2=0
ma=count
if k>=0:
    for j in range(m):
        if k>=ar[j]:
            count+=1
            ma=max(ma,count)
            k-=ar[j]
        else:
            start2=j
            break
for i in range(start2,m):
    while start>=0 and k<ar[i] :
        k+=arr[start]
        start-=1
        count-=1
        ma=max(count,ma)
    if k>=ar[i]:
        count+=1
        k-=ar[i]
        ma=max(count,ma)
print(ma)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,k = map(int,input().split())
a = deque([int(i) for i in input().split()])
b = deque([int(i) for i in input().split()])
ans = 0
count = 0
while ans < k:
    if count % 2 == 0:
        try:
            ans1 = a.popleft()
        except Exception as e:
            pass
        ans += ans1
        count += 1
    else:
        try:
            ans2 = b.popleft()
        except Exception as e:
            pass
        ans += ans2
        count += 1

if ans > k:
    print(count-1)
else:
    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()))
c = 0
while k > 0:
    if c == m+n:
        break;
    if len(a) == 0:
        d = b.popleft()
    elif len(b) == 0:
        d = a.popleft()
    else:
        if a[0] < b[0]:
            d = a.popleft()
        elif a[0] > b[0]:
            d = b.popleft()
        else:
            if len(a) == 1:
                d = b.popleft()
            elif len(b) == 1:
                d = a.popleft()
            else:
                if a[1] < b[1]:
                    d = a.popleft()
                else:
                    d = b.popleft()
    if d <= k:
        c += 1
        k -= d
    else:
        break;
print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import exit

N, M, K = [int(x) for x in input().split()]

a_book = [0]
for index, i in enumerate(input().split(), 1):
    a_book.append(a_book[index-1] + int(i))

b_book = [0]
for index, i in enumerate(input().split(), 1):
    b_book.append(b_book[index-1] + int(i))

if a_book[1] > K and b_book[1] > K:
    print(0)
    exit()

if a_book[-1] + b_book[-1] < K:
    print(N + M)
    exit()

search = deque()
check = {}


def search_count(v):
    search.appendleft(v)
    while search:
        v = search.popleft()

        if not v in check:
            check[v] = v[0] + v[1]

            if not (v[0] + 1, v[1]) in check:
                if v[0] + 1 <= N:
                    if a_book[v[0] + 1] + b_book[v[1]] <= K:
                        search.appendleft((v[0] + 1, v[1]))

            if not (v[0], v[1] + 1) in check:
                if v[1] + 1 <= M:
                    if a_book[v[0]] + b_book[v[1] + 1] <= K:
                        search.appendleft((v[0], v[1] + 1))


search_count((0, 0))

print(check)

print(max(check.values()))

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):
        for j, b in enumerate(B0):
            if a + b <= K:
                soln = max(soln, i+1+j+1)
    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 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ad = [0]*n
bd = [0]*m

ca = 0
cb = 0
count = 0
ea = False
eb = False
for i in range(n):
  if i == 0:
    ad[i] = a[i]
  else:
    ad[i] = a[i] + ad[i-1]
for i in range(m):
  if i == 0:
    bd[i] = b[i]
  else:
    bd[i] = b[i] + bd[i-1]

while k > 0:
  if ea and eb:
    break
  elif ea:
    if b[cb] <= k:
        k = k - b[cb]
        count += 1
        cb += 1
        if(cb == m):
          eb = True
    else:
      break
  elif eb:
    if a[ca] <= k:
        k = k - a[ca]
        count += 1
        ca += 1
        if(ca == n):
          ea = True
    else:
      break
  else:
    if ad[ca] <= bd[cb]:
      if ca != 0:
        k = k + ad[ca-1]   
      if ad[ca] <= k:
        k = k - ad[ca]
        count += 1
        ca += 1
        if(ca == n):
          ea = True
      else:
        break
    else:
      if cb != 0:
        k = k + bd[cb-1]   
      if bd[cb] <= k:
        k = k - bd[cb]
        count += 1
        cb += 1
        if(cb == m):
          eb = True
      else:
        break

print(count)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(99999)
B.append(99999)
Sum = 0
i = 0
j = 0
while True:
  if A[i] <= B[j] and Sum + A[i] <= K:
    Sum += A[i]
    i += 1
  elif A[i] > B[j] and Sum + B[j] <= K:
    Sum += B[j]
    j += 1
  else:
    break
print(i + j)

Submission status: Wrong Answer

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

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

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

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

ans=i_a


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


print(ans)

Submission status: Wrong Answer

Solution code:
#C
from itertools import accumulate
from collections import deque
import bisect

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

cnt = 0

a_c = list(accumulate(list(a)))
b_c = list(accumulate(list(b)))
result = []

idx_a = bisect.bisect_left(a_c,k)
idx_a_b = bisect.bisect_left(b_c,k-list(a_c)[idx_a-1]+1)
result.append(idx_a + idx_a_b)

idx_b = bisect.bisect_left(b_c,k)
idx_b_a = bisect.bisect_left(a_c,k-list(b_c)[idx_b-1]+1)
result.append(idx_b + idx_b_a)

for i in range(n+m):
    if len(a) > 0 and len(b) > 0:
        s,t = a.popleft(),b.popleft()
    elif len(a) > 0:
        s = a.popleft()
        t = float('inf')
    elif len(b) > 0:
        s = float('inf')
        t = b.popleft()
    else:
        break
    if s >= t:
        k -= t
        a.appendleft(s)
    else:
        k -= s
        b.appendleft(t)
    if k < 0:
        break
    else:
        cnt += 1
        #print('a:',a)
        #print('b:',b)
    
result.append(cnt)

#print(result)
print(max(result))

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

i, j = 0, 0
count = 0
while k > 0:
    if i == n and j == m:
        break

    if i < n and j < m:
        if A[i] < B[j]:
            k -= A[i]
            i += 1
        else:
            k -= B[j]
            j += 1
    else:
        if i == n:
            k -= B[j]
            j += 1
        else:
            k -= A[i]
            i += 1

    if k < 0:
        break

    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
AA = [0]
BB = [0]
r = 0
for i in A:
    r += i
    AA.append(r)
r = 0
for i in B:
    r += i
    BB.append(r)
ans = 0
a = N
b = 0
while AA[a]+BB[b] > K:
    a -= 1
ans = a+b

while a > 0:
    while b < M:
        if AA[a]+BB[b+1] <= K:
            b += 1
        else:
            break
    print(a,b,AA[a]+BB[b])
    if ans < a+b:
        ans = a+b
    if b == M:
        break
    a -= 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()))

A_sum = 0
A_max = 0
B_sum = 0
B_max = 0
best_num = 0
A_max = N
flag = True # Aだけ読んで時間が尽きるかどうか

# Aを最大まで読んだ時の冊数と時間
for i in range(N):
    A_sum += A[i]
    if A_sum > K:
        A_sum -= A[i]
        A_max = i
        flag = False
        break
A = A[0:A_max]
now_num = A_max # 今の読んだ数
mixsum = A_sum # A,Bを合わせた時間
b_num = 0 # bを読んだ数
if flag:
    now_num += M
    b_num = M
    for i in range(M):
        mixsum += B[i]
        if mixsum > K:
            mixsum -= B[i]
            now_num += i
            b_num = i  #i冊読んだので次取るならi番目から
            break
A.reverse()
best_num = now_num
for i in range(A_max):
    mixsum -= A[i]
    now_num -= i
    for j in range(b_num, M):
        mixsum += B[j]
        now_num += 1
        if mixsum > K:
            mixsum -= B[j]
            now_num -= 1
            break
    if now_num > best_num:
        best_num = now_num
print(best_num)

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(10**9 + 1)
B.append(10**9 + 1)
ans = 0
C = A + B
C.sort()
for i in range(len(C)):
  if K >= C[i]:
    K = K - C[i]
    ans += 1
  else:
    break
print(ans)

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):
    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)
    if(K>=AA[A_idx]+BB[B_idx]):
        if(A_idx<N):
            A_idx += 1
        if(K>=AA[A_idx]+BB[B_idx]):
            ans.append(A_idx + B_idx)


print(max(ans))

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)
elif j-1 <= 0:
  print(i)
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:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
import math
from decimal import *
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
sum = 0
acount = 0
bcount = 0

while(sum < k):
    if acount == len(a) and bcount == len(b):
        break

    elif acount == len(a):
        mi = b[bcount]
        sum = Decimal(sum) + Decimal(mi)
        ans = Decimal(ans) + Decimal(1)
        bcount += 1

    elif bcount == len(b):
        mi = a[acount]
        sum = Decimal(sum) + Decimal(mi)
        ans = Decimal(ans) + Decimal(1)
        acount += 1

    else:
        if a[acount] < b[bcount]:
            mi = a[acount]
            sum = Decimal(sum) + Decimal(mi)
            ans = Decimal(ans) + Decimal(1)
            acount += 1
        else:
            mi = b[bcount]
            sum = Decimal(sum) + Decimal(mi)
            ans = Decimal(ans) + Decimal(1)
            bcount += 1

if k < sum:
    print(ans - 1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import itertools,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,M,K = LI()
A = LI()
B = LI()
accumulate_A = list(itertools.accumulate(A))
accumulate_B = list(itertools.accumulate(B))
countA,countB,_sumA,_sumB = 0,0,0,0
for i in range(N):
    if accumulate_A[i]>K:
        if i!=0:
            _sumA = accumulate_A[i-1]
        break
    countA += 1
for j in range(M):
    if accumulate_B[j]>K:
        if j!=0:
            _sumB = accumulate_B[i-1]
        break
    countB += 1
for y in accumulate_B:
    if _sumA+y>K:
        break
    countA += 1
for x in accumulate_A:
    if _sumB+x>K:
        break
    countB += 1
print(max(countA,countB))

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()))
ctr = 0
T = 0
while T <= K:
  try:
    k = min(A[0], B[0])
    if k == A[0]:
      A.remove(A[0])
    else:
      B.remove(B[0])
  except IndexError:
      if A == [] and B != []:
          k = B[0]
          B.remove(B[0])
      elif B == [] and A != []:
          k = A[0]
          A.remove(A[0])
  T += k
  if T <= K:
    ctr += 1
  else:
    break
print(ctr)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n,m,k = map(int,input().split())
a =[0] + list(map(int,input().split())) 
b =[0] + list(map(int,input().split()))

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

asum = [a[0]]
bsum = [b[0]]

for i in range(1,n+1):
    ain = asum[-1]+a[i]
    asum.append(ain)
for i in range(1,m+1):
    bsum.append(bsum[-1]+b[i])

for i in range(n+1):
    if asum[i] > k:
        aind = i
        break
else:
    aind = n

for i in range(m+1):
    if bsum[i] > k:
        bind = i
        break
else:
    bind = m
# print(aind,bind)
while asum[aind] + bsum[bind] > k:
    bind -= 1
    if bind == 0:
        break


ans = aind + bind
for i in range(aind-1,-1,-1):
    while asum[i] + bsum[bind] <= k:
        bind += 1
        if bind == m+1:
            bind -= 1
            ans = max(ans,i+bind)
            break
    else:
        ans = max(ans,i+bind-1)
print(ans)

Submission status: Wrong Answer

Solution code:
import random
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(" ")]

while True:
  if (N>10**8) | (M>10**8) |(K>10**8):
      if random.random()>0.5:
          print(0)
          break
  else:
      time=0
      a_idx=0
      b_idx=0
      count=0
      while time<K:
          count+=1
          try:
              if a_idx+1==N:
                  time+=b[b_idx]
                  b_idx+=1
                  if time>K:
                      count-=1
                  continue
              elif b_idx+1==M:
                  time+=a[b_idx]
                  a_idx+=1
                  if time>K:
                      count-=1
                  continue    

              if a[a_idx]<b[b_idx]:
                  time+=a[a_idx]
                  a_idx+=1

              elif a[a_idx]>b[b_idx]:
                  time+=b[b_idx]
                  b_idx+=1
              else:
                  try:
                      a_com=a_idx
                      b_com=b_idx
                      while True:
                          if a[a_com]>b[b_com]:
                              time+=a[a_idx]
                              a_idx+=1
                              break
                          elif a[a_com]<b[b_com]:
                              time+=b[b_idx]
                              b_idx+=1
                              break
                          a_com+=1
                          b_com+=1
                  except:
                      time+=a[a_idx]
                      a_idx+=1
              if time>K:
                  count-=1            
          except:
              break
      print(count)
      break

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

from collections import deque

sum_a = sum(a)
sum_b = sum(b)

time = sum_a+sum_b
ans = len(a)+len(b)

a = deque(a[::-1])
b = deque(b[::-1])

while time>k:
    if  len(a)!=0 and (len(b)==0 or (sum_a>sum_b or (sum_a==sum_b and a[0]>b[0]))):
        time-=a[0]
        sum_a-=a[0]
        ans-=1
        if ans==0:
            break
        a.popleft()
    elif len(b)!=0 and (len(a)==0 or (sum_a<sum_b or (sum_a==sum_b and a[0]<=b[0]))):
        time-=b[0]
        sum_b-=b[0]
        ans-=1
        if ans==0:
            break
        b.popleft()
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(300000)
b.append(300000)
c = []
e = 0
f = 0
for i in range(len(a)):
    b.append(a[i])
while sum(c) < k:
    if a[e] < b[f]:
        c.append(a[e])
        e += 1
    else:
        c.append(b[f])
        f += 1
        
if 300000 in c:
    c.remove(300000)
if sum(c) == k:
    print(len(c))
else:
    print(len(c)-1)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

N,M,K=(int(x) for x in input().rstrip('\n').split())
As=[int(x) for x in input().rstrip('\n').split()]
Bs=[int(x) for x in input().rstrip('\n').split()]
res = 0
now = 0
now_a = 0
now_b = 0
for i in range(N+M):
  if now_a==len(As):
    if now_b==len(Bs):
      c=2
      break
    else:
      c=0
  elif now_b==len(Bs):
    c=1
  elif As[now_a]!=Bs[now_b]:
    if As[now_a]<Bs[now_b]:
      c=1
    else:
      c=0
  else:
    nex_a=now_a+1
    nex_b=now_b+1
    if all(x==y for x,y in zip(As[now_a:],Bs[now_b:]))==True:
      c=1
    else:
      while 5572>573:
        if nex_a==len(As):
          c=0
          break
        elif nex_b==len(Bs):
          c=1
          break
        else:
          if As[nex_a]<Bs[nex_b]:
            c=1
            break
          elif As[nex_a]>Bs[nex_b]:
            c=0
            break
          else:
            nex_a+=1
            nex_b+=1
  if c == 1:
    now += As[now_a]
    now_a += 1
  elif c>1:
    break
  else:
    now += Bs[now_b]
    now_b += 1
  if now>K:
    break
  res += 1
print(res)

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
print(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:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
a_now = 0
b_now = 0

while K >= 0:
    ans_time = 0
    if len(A) <= a_now and len(B) <= b_now:
        break
    elif len(A) <= a_now:
        ans_time += B[b_now]
        b_now += 1
    elif len(B) <= b_now:
        ans_time += A[a_now]
        a_now += 1
    else:
        if A[a_now] >= B[b_now]:
            ans_time += B[b_now]
            b_now += 1
        else:
            ans_time += A[a_now]
            a_now += 1

    if K >= ans_time:
        ans += 1
        ##print(K, ans_time, sep= " ")
        K -= ans_time
    else:
        break
print(ans)

Submission status: Wrong Answer

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

num = 0
L = []
while a and b:
    if a[0] <=b[0]:
        L.append(a[0])
        a.popleft()
    else:
        L.append(b[0])
        b.popleft()
if a:
    for i in range(len(a)):
        L.append(a[i])
if b:
    for i in range(len(b)):
        L.append(b[i])

for i in range(len(L)):
    num +=L[i]

    if num >k:
        print(i)
        exit()
    elif num == k:
        print(i+1)
        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()))
num=0
num2=-1
for i in range(n):
    num+=a[i]
    if num>k:
        num2=i
        break
if num2!=-1:
    a=a[:i+1]
n=len(a)
num=0
num2=-1
for i in range(m):
    num+=b[i]
    if num>k:
        num2=i
        break
if num2!=-1:
    b=b[:i+1]
m=len(b)
num=sum(a)+sum(b)
numa=0
numb=0
ans=-1
if num<=k:
    ans=m+n
else:
    num=0
    for i in range(n+m):
        if numa!=n:
            if numb!=m:
                if a[numa]<b[numb]:
                    if numb<m-1:
                        if a[numa]>(b[numb]+b[numb+1])/2 and num+b[numb]<k:
                            num+=b[numb]
                            numb+=1
                        else:
                            num+=a[numa]
                            numa+=1
                    else:
                        num+=a[numa]
                        numa+=1
                else:
                    if numa<n-1:
                        if b[numb]>(a[numa]+a[numa+1])/2 and num+a[numa]<k:
                            num+=a[numa]
                            numa+=1
                        else:
                            num+=b[numb]
                            numb+=1
                    else:
                        num+=b[numb]
                        numb+=1
            else:
                num+=a[numa]
                numa+=1
        else:
            if numb!=m:
                num-=b[numb]
                numb-=1
            else:
                ans=m+n
                break
        if num>k:
            ans=numa+numb-1
            break
if ans==-1:
    ans=m+n
print(ans)