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:
a = input()
n = input()
m = input()
first = a.split(" ")
N = n.split(" ")
M = m.split(" ")

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)

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

r = 0
time = 0
c = 0


while time <= k:
  if not a:
    if not b:
      break
    else:
      time += b[0]
      if time <= k:
        r += 1
        b.pop(0)
      else:
        break
  elif not b:
    if not a:
      break
    else:
      time += a[0]
      if time <= k:
        r += 1
        a.pop(0)
      else:
        break
  # 初めの値を比較 aの方が小さい場合
  elif a[0] <= b[0]:
    time += a[0]
    if time <= k:
      r += 1
      a.pop(0)
    else:
      break
  # bの方が小さい場合
  else:
    time += b[0]
    if time <= k:
      r += 1
      b.pop(0)
    else:
      break
      
print(r)

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
ans = 0
i = 0
j = 0
while t < k:
    if i < n and j < m:
        if a[i] < b[j]:
            if t + a[i] <= k:
                t += a[i]
                i += 1
            else:
                break
        elif a[i] < b[j]:
            if t + b[j] <= k:
                t += b[j]
                j += 1
            else:
                break
        else:
            if a[i] + t > k:
                break
            f = 0
            x = i
            y = j
            while a[x] == b[y] and x < n-1 and y < m-1:
                x += 1
                y += 1
            if a[x] < b[y]:
                if t + a[i] <= k:
                    t += a[i]
                    i += 1
                else:
                    break
            elif a[x] > b[y]:
                if t + b[j] <= k:
                    t += b[j]
                    j += 1
                else:
                    break
            else:
                if y+1 == m:
                    if t + a[i] <= k:
                        t += a[i]
                        i += 1
                    else:
                        break
                else:
                    if t + b[j] <= k:
                        t += b[j]
                        j += 1
                    else:
                        break
    elif i < n:
        if t + a[i] <= k:
            t += a[i]
            i += 1
        else:
            break
    elif j < m:
        if t + b[j] <= k:
            t += b[j]
            j += 1
        else:
            break
    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()))
s = 0
x = 0
for i in range(N+M+1):
	if len(A) != 0 and len(B) != 0:
		if A[0]<=B[0]:
			s += A[0]
			x += 1
			if s>K:
				s - A[0]
				x = x - 1
				break
			A.remove(A[0])
		elif A[0]>B[0]:
			s += B[0]
			x += 1
			if s>K:
				s - B[0]
				x = x -1
				break
			B.remove(B[0])
	elif len(A) != 0 and len(B) == 0:
		s += A[0]
		x += 1
		if s>K:
			s - A[0]
			x = x - 1
			break
		A.remove(A[0])
	elif len(A) == 0 and len(B) != 0:
		s += B[0]
		x += 1
		if s>K:
			s - B[0]
			x = x -1
			break
		B.remove(B[0])
	elif len(A)==0 and len(B)==0:
		break
print(x)

Submission status: Wrong Answer

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

A_ = list(itertools.accumulate(A))
B_ = list(itertools.accumulate(B))

ans = 0
l = len(A_)

for idx, i in enumerate(A_):
  if i <= K:
          ans = max(idx+1 + bisect.bisect_right(B_, K-i), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n, m, k = i_map()
    a_list = i_list()
    b_list = i_list()

    a_ruisekiwa = list(accumulate(a_list))
    b_ruisekiwa = list(accumulate(b_list))

    # print(a_ruisekiwa)
    # print(b_ruisekiwa)

    max_count = 0

    for i in range(n):
        if k <= a_ruisekiwa[i]:
            break
        insert_num = k - a_ruisekiwa[i]
        insert_index = bisect_right(b_ruisekiwa, insert_num)
        max_count = max(max_count, i + insert_index +1)

    print(max_count)
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
i=j=count=0
lA=len(A)
lB=len(B)
lim=100000000000
while(k>0):
    ans=min(A[i],B[j])
    if k>=ans:
        if i==lA-1:
            A[i]=lim
        if j==lB-1:
            B[j]=lim
        count+=1
        k-=ans
        if ans==A[i] and i<lA-1:
            i+=1
        
        elif ans==B[j] and j<lB-1:
            j+=1
    
    else:
        break
print(count)

Submission status: Wrong Answer

Solution code:
i1 = input().split(" ")
n = int(i1[0])
m = int(i1[1])
k = int(i1[2])
i2 = input().split(" ")  # desk A
i3 = input().split(" ")  # desk B

t = 0
at =0
bt =0

def isWithinBudget(val, k):
    if k - val < 0:
        return False
    else:
        return True

for i in range(len(i2 + i3)):
    if bt  >= len(i3)and at  >= len(i2):
        break
    elif bt  >= len(i3):
        # pick form desk A
        val = int(i2[at])
        at += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    elif at  >= len(i2):
        val = int(i3[0])
        bt += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    else:
        if int(i3[bt]) >= int(i2[at]):
            # pick form desk A
            val = int(i2[at])
            at += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break
        else:
            val = int(i3[bt])
            bt += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break

    if k < 0:
        break
print(t)

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

from itertools import accumulate
from collections import deque
A=deque(list(accumulate(A)))
B=deque(list(accumulate(B)))
A.appendleft(0)
B.appendleft(0)
ans1=0
for i in range(N+1):
    if A[i]>K:
        ans1=i
        break
else:
    ans1=N
ans2=0
ans3=0
for i in range(ans1):
    while i+ans2<=M and A[ans1-i]+B[i+ans2]<=K:
        ans2+=1
    ans3=max(ans3,ans1+ans2-1)
print(ans3)

Submission status: Wrong Answer

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

A_ind = 0
B_ind = 0
count = 0
ans = 0

for i in range(N+M): 
  if A_N[A_ind] < B_M[B_ind]:
    if count + A_N[A_ind] <= K:
      count += A_N[A_ind]
      ans += 1
      if A_ind == N-1:
        A_N[A_ind] = 1000000001
      else:
        A_ind += 1
    else:
      break
  elif A_N[A_ind] >= B_M[B_ind]:
    if count + B_M[B_ind] <= K:
      count += B_M[B_ind]
      ans += 1
      if B_ind == M-1:
        B_M[B_ind] = 1000000001
      else:
        B_ind += 1
    else:
      break
    if A_N[A_ind] == 1000000001:
      if B_M[B_ind] == 1000000001:
        break
print(ans)

Submission status: Wrong Answer

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

AB = list()
a = 0
b = 0
for i in range(N+M):
  if ((a < N) and (b < M)):
    if(A[a] <= B[b]):
      AB.append(A[a])
      a += 1
    else:
      AB.append(B[b])
      b += 1
  elif(a >= N):
    for j in range(M-b):
      AB.append(B[b])
      b += 1
    break
  elif(b >= M):
    for j in range(N-a):
      AB.append(A[a])
      a += 1
    break

ans = 0
while K >= 0:
  K -= AB[ans]
  ans += 1
  if(ans == (N+M)):
    print(N+M)
    sys.exit()
  
print(ans-1)

Submission status: Wrong Answer

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot,
                  log2, pi, radians, sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


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


n, m, t = invr()
a = inlist()
b = inlist()
MAX = 1000000001
pref1 = []
pref2 = []
for i in range(n):
    if i == 0:
        pref1.append(a[i])
    else:
        pref1.append(pref1[i-1] + a[i])
for i in range(m):
    if i == 0:
        pref2.append(b[i])
    else:
        pref2.append(pref2[i-1] + b[i])
i = j = 0
read = 0
count = 0

it = min(m, n)
found = -1
for i in range(it):
    if pref2[i] < pref1[i]:
        found = i

is_b_done = False
if found == -1:
    # read all a
    for i in range(n):
        if read + a[i] <= t:
            read += a[i]
            count += 1
        else:
            break
else:
    # read b
    is_b_done = True
    for i in range(m):
        if read + b[i] <= t:
            count += 1
            read += b[i]
        else:
            break

if is_b_done == False:
    for i in range(m):
        if read + b[i] <= t:
            count += 1
            read += b[i]
        else:
            break
else:
    for i in range(n):
        if read + a[i] <= t:
            count += 1
            read += a[i]
        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()))
t_a=[]
t_b=[]
l=0
for i in range(N):
  l+=A[i]
  t_a.append(l)
l=0
for i in range(M):
  l+=B[i]
  t_b.append(l)
def bi_re(i):
  x=K-t_a[i]
  l=0;r=M-1
  while l<=r:
    m=(l+r)//2
    q=t_b[m]
    if q==x:
      return m+1
    elif q<x:
      l=m+1
    else:
      r=m-1
  for j in range(r-1,l+2):
    if j>=0 and j<=M-1:
      if t_b[j]>x:
        return j
  return M
  
ans=0
for i in range(N):
  if t_a[i]==K:
    ans=max(i+1,ans)
  elif t_a[i]<K:
    ans=max(bi_re(i)+i+1,ans)
print(ans)

Submission status: Wrong Answer

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

a_count = 0
b_count = 0
ans = 0
time = 0
flag = True
while flag:
    if a_count < n and b_count < m:
        if a[a_count] < b[b_count]:
            time += a[a_count]
            a_count+=1
            ans += 1

        else:
            time += b[b_count]
            b_count+=1
            ans += 1

    elif a_count < n and b_count >= m:
        time += a[a_count]
        a_count+=1
        ans += 1
    elif a_count >= n and b_count <m:
        time += b[b_count]
        b_count += 1
        ans += 1
    else:
        flag = False
    if time>k:
        flag = False
        ans -= 1

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

ans = 0
pile = 0

aq = deque(a)
bq = deque(b)

while(pile < k):
    ans += 1
    if len(aq) == 0 and len(bq) == 0:
        # cant pop
        break

    elif len(aq) == 0 and len(bq) != 0:
        #pop form b
        pile += bq.popleft()

    elif len(aq) != 0 and len(bq) == 0:
        #pop from a
        pile += aq.popleft()

    elif aq[0] > bq[0]:
        #pop form b
        pile += bq.popleft()

    elif aq[0] <= bq[0]:
        #pop form a
        pile += aq.popleft()

print(ans-1)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m,k = readInts()
A = readInts()
B = readInts()
dA = deque(A)
dB = deque(B)

ans = []
if sum(A) + sum(B) <= k:
    print(n+m)
    exit()
else:
    # pre
    now = 0
    while True:
        if len(dA) == 0 and len(dB) == 0:
            break
        if len(dA) != 0 and len(dB) != 0:
            a =  dA.popleft()
            b =  dB.popleft()
            if a < b:
                now += a
                ans.append(now)
                dB.appendleft(b)
            else:
                now += b
                ans.append(now)
                dA.appendleft(a)
        elif len(dA) != 0 and len(dB) == 0:
            a = dA.popleft()
            now += a
            ans.append(now)
        elif len(dB) != 0 and len(dA) == 0:
            b = dB.popleft()
            now += b
            ans.append(now)
#print(ans)
import bisect
print(bisect.bisect_left(ans,k+1))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

acc1 = list(accumulate(A))
acc2 = list(accumulate(B))

# 判定用関数 満たすなら1を,満たさないなら0を返す
def is_ok(a, b):
    if acc1[a] + acc2[b] <= K:  return 1
    return 0

# left = 左端 - 1, right = 右端 + 1 
# 例: left = -1, right = len(k)
def binary_search(i, left, right):
    while (abs(right - left) > 1):
        mid = (left + right) // 2
        if is_ok(i, mid):  right = mid
        else:  left = mid
    return right

ans = 0
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  continue
    else:  ans = max(ans,  i + ret + 2)
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()))

sumbook = len(A + B)
A.append(10 ** 9 + 7)
B.append(10 ** 9 + 7)
m = 0
cnt = 0

while cnt <= sumbook:
    tmp = A.pop(0) if A[0] <= B[0] else B.pop(0)
    if m + tmp > K:
        break
    m += tmp
    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    cost = K
    count = 0
    while True:
        if len(A) ==0 and len(B) == 0:
            break
        elif len(A) == 0:
            b = B[0]
            if cost -b < 0:
                break
            cost -= b
            B = B[1:]
        elif len(B) == 0:
            a = A[0]
            if cost -a < 0:
                break
            cost -= a
            A = A[1:]

        else:
            a = A[0] 
            b = B[0] 
            if a < b:
                if cost -a < 0:
                    break
                cost -= a
                A = A[1:]
            else:
                if cost -b < 0:
                    break
                cost -= b
                B = B[1:]

        count += 1
    
    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
aa = 0
bb = 0
p = k

for i in range(n):
    if p - a[i] <0:
        break
    else:
        p -= a[i]
        aa += 1
ans = aa
#print(ans)

for i in range(m):
    if p - b[i] >= 0:
        p -= b[i]
        bb += 1
        if ans < aa + bb:
            ans = aa + bb
    else:
        while aa >= 0:
            aa -= 1
            #print(aa)
            p += a[aa]
            if p - b[i] >= 0:
                p -= b[i]
                bb += 1
        if ans < aa + bb:
            ans = aa + bb
        if aa <= 0:
            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()))

time = 0
a = 0
b = 0
while True:
    if time > K:
        break
    if time == K:
        break
    if a == N and B == M:
        break
    if a == N:
        time += B[b]
        b += 1
    elif b == M:
        time += A[a]
        a += 1
    elif A[a] <= B[b]:
        time += A[a]
        a += 1
    elif B[b] < A[a]:
        time += B[b]
        b += 1
    
print(a+b if time == K else a+b-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, M, K = map(int, input().split())
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))
    CNT = 0
    while K > 0:
        if len(A) > 0 and len(B) > 0:
            a = A[0]
            b = B[0]
            if a >= b:
                if K >= b:
                    K -= b
                    B.popleft()
                    CNT += 1
                else:
                    break
            else:
                if K >= a:
                    K -= a
                    A.popleft()
                    CNT += 1
                else:
                    break

        elif len(A) == 0:
            b = B[0]
            if K >= b:
                K -= b
                B.popleft()
                CNT += 1
            else:
                break
        else:
            if K >= a:
                K -= a
                A.popleft()
                CNT += 1
            else:
                break
    print(CNT)

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

tmp = 0
count = 0
sum = 0
while 1:
    if n  == 0 and m == 0:
        break
    elif n == 0:
        tmp = b.pop(0)
        m -= 1
    elif m == 0:
        tmp = a.pop(0)
        n -= 1
    elif a[0] > b[0]:
        tmp = b.pop(0)
        m -= 1
    elif a[0] <= b[0]:
        tmp = a.pop(0)
        n -= 1

    sum += tmp
    if sum  > k:
        break
    count += 1
print(count)

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()))
C = N + M
cnt = 0
res = 0
A = deque(A)
B = deque(B)
for c in range(C):
    if A:
        a = A.popleft()
    else:
        a = 10**10+1
    if B:
        b = B.popleft()
    else:
        b = 10**10+1
    if a >= b:
        cnt += b
        A.appendleft(a)
    elif a <= b:
        cnt += a
        B.appendleft(b)

    if cnt <= K:
        res += 1
    else:
        break
print(res)

Submission status: Wrong Answer

Solution code:
def main():
    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.append(10**9+1)
    b.append(10**9+1)
    cnt_a = 0
    cnt_b = 0
    cumsum = 0

    while cumsum <= k:
        if a[cnt_a] > b[cnt_b]:
            cumsum += b[cnt_b]
            if cumsum > k:
                break
            else:
                cnt_b += 1
        else:
            cumsum += a[cnt_a]
            if cumsum > k:
                break
            else:
                cnt_a += 1

        if cumsum == k:
            break
    print(cnt_a + cnt_b)
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def readInt():
    return list(map(int, input().split()))
    
n,m,k = readInt()
a = readInt()
b = readInt()
i = 0
j = 0
t = 0
judge = True
ans = 0
while((t+a[i]<=k or t+b[j]<=k) and ans < n+m):
    if i < n and j < m:
        if a[i] < b[j]:
            t += a[i]
            if i < n-1: i += 1
        else:
            t += b[j]
            if j < m-1: j += 1
    elif i < n and j >= m:
        t += a[i]
        if i < n-1: i += 1
    elif i >= n and j < m:
        t += b[j]
        if j < m-1: j += 1
    else: break
    ans += 1
    # print(ans)
print(ans)

Submission status: Wrong Answer

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

from collections import deque

an = list(map(int, input().split()))

bn = list(map(int, input().split()))

an = an
bn = bn

vMax = 10**9+10

import itertools

accum_a = [0]
accum_b = [0]



candidate = []


for b_i in range(0,len(bn)):
    i_ = accum_b[-1] + bn[b_i]
    if i_ > k:
        break
    accum_b.append(i_)


countMax = 0
for a_i in range(0,len(an)):


    sumCandidate = accum_a[-1] + an[a_i]
    if sumCandidate > k:
        break
    accum_a.append(sumCandidate)

    rest = k - sumCandidate

    bIndex = 0
    for j, accB in enumerate(accum_b):
        if accB > rest:
            bIndex = max(0, j - 1)
            break
        bIndex = j

    countMax = max(countMax, a_i+1 + bIndex)



print(countMax)

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_a=0
cnt_b=0
ans=0
num_a=0
num_b=0
for i in range(n):
    num_a+=i
    if num_a>k:
        num_a=i
    if i==n-1:
        num_a=n
for i in range(m):
    num_b+=i
    if num_b>k:
        num_b=i
    if i ==n-1:
        num_b=n
num=max(num_a,num_b)
for i in range(200000):
    if cnt_a<n and cnt_b<m:
        if a[cnt_a]<=b[cnt_b]:
            k-=a[cnt_a]
            if k<0:
                break
            ans+=1
            cnt_a+=1
        else:
            k-=b[cnt_b]
            if k<0:
                break
            ans+=1
            cnt_b+=1
    elif cnt_a<n and cnt_b>=m:
        k-=a[cnt_a]
        if k<0:
            break
        ans+=1
        cnt_a+=1
    elif cnt_a>=n and cnt_b<m:
        k-=b[cnt_b]
        if k<0:
            break
        ans+=1
        cnt_b+=1
    else:
        break
print(max(num,ans))

Submission status: Wrong Answer

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            ans = max(ans,anow+bmax)
if Bc[len(B)-1] <= K:
    ans = max(ans, len(B))
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()))

def 操作(A,B,N,M):
    MAX=0
    s=0
    for i in range(N):
        s+=A[i]
        if s>=K:
            break
        S=s
        for j in range(M):
            S+=B[j]
            if S<=K:
                if i+j+2>MAX:
                    MAX=i+j+2
                else:
                    break
        for k in range(i+1,N):
            S+=A[k]
            if S>K:
                break
            MAX=k+j+2
    return MAX

if N>=M:
    print(操作(A,B,N,M))
else:
    print(操作(B,A,M,N))

Submission status: Wrong Answer

Solution code:
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
LS=lambda:list(map(str,input().split()))

import sys
input=sys.stdin.readline
n,m,k=L()
a=L()
b=L()
ans=0
maxi=n+m
time=0
ai=0
bi=0
dp=[]
while time<k and ans<maxi:
    if ai>=n:
        time+=b[bi]
        bi+=1
        if time<=k:
            ans+=1
        continue
        
    elif bi>=m:
        time+=a[ai]
        ai+=1
        if time<=k:
            ans+=1
        continue
        
    if a[ai]<b[bi]:
        time+=a[ai]
        ai+=1
        
    else:
        time+=b[bi]
        bi+=1
        
    if time<=k:
        ans+=1

ai=0
bi=0
time=0
ans2=0
while time<k and ans2<maxi:
    if ai>=n:
        time+=b[bi]
        bi+=1
        if time<=k:
            ans2+=1
        continue
        
    elif bi>=m:
        time+=a[ai]
        ai+=1
        if time<=k:
            ans2+=1
        continue
        
    if a[ai]<=b[bi]:
        time+=a[ai]
        ai+=1
        
    else:
        time+=b[bi]
        bi+=1
        
    if time<=k:
        ans2+=1
print(max(ans,ans2))

Submission status: Wrong Answer

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

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

R = K
A_index = 0
B_index = 0
ans = 0
while N > 0:
    if R >= A[A_index]:
        R = R - A[A_index]
        ans += 1
        A_index += 1
        N -= 1
    else:
        break
if N == 0:
    A_index -= 1

while M > 0:
    if R >= B[B_index]:
        R = R - B[B_index]
        ans += 1
        B_index += 1
        M -= 1
    else:
        break

if M == 0:
    B_index -= 1

if N == 0 and M == 0:
    print(ans)
    sys.exit()

max = ans

while True:
    if A_index >= 0:
        R += A[A_index]
        A_index -= 1
        ans -= 1
        while B_index < len(B):
            if R >= B[B_index]:
                R -= B[B_index]
                B_index += 1
                ans += 1
            else:
                break
        
        if ans > max:
            max = ans
    else:
        break

print(max)

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
count = 0
while time < k:
  # 両方空の場合
  if len(a) == 0 and len(b) == 0:
    break
  
  # どちらか片方が空の場合
  if len(a) == 0:
    if b[0] + time > k:
      break
    else:
      time += b.pop(0)
      count += 1
      continue
      
  if len(b) == 0:
    if a[0] + time > k:
      break
    else:
      time += a.pop(0)
      count += 1
      continue
  
  # 両方少なくとも1冊はある場合
  if a[0] < b[0]:
    # 超過するなら読まない（bのほうが大きいのでbはチェックしなくていい）
    if a[0] + time > k:
      break
    else:
      time += a.pop(0)
      count += 1
  elif a[0] > b[0]:
    if b[0] + time > k:
      break
    else:
      time += b.pop(0)
      count += 1
  else:
	# a[0] == b[0]の場合は、次の本の読む時間が短いほうを選びたい（次の本がない場合もある）
    # でもその前に超過チェック(bは同じだからチェックしなくていい)
    if a[0] + time > k:
      break
      
    if len(b) == 0:
      time += a.pop(0)
      count += 1
    else:
      time += b.pop(0)
      count += 1

print(count)

Submission status: Wrong Answer

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

t = 0
cnt = 0
c1, c2 = A.pop(), B.pop()
while True:
    
    if t >= K-1 or (t + c1 >= K and t + c2 >= K):
        break
    if c1 <= c2:
        if t + c1 < K:
            t += c1
            cnt += 1
            c1 = A.pop() if bool(A) else 100000000000
    else:
        if t + c2 < K:
            t += c2
            cnt += 1
            c2 = B.pop() if bool(B) else 100000000000

print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-



def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    Tot = sum(A)+sum(B)
    NM = N+M
    a = N-1
    b = M-1

    while Tot > K:
        if a>=0 and b>=0:
            if A[a]<=B[b]:
                Tot-=B[b]
                b-=1
                NM -= 1
            elif A[a]>B[b]:
                Tot-=A[a]
                a-=1
                NM -= 1
        elif a>=0:
            Tot-=A[a]
            a-=1
            NM -= 1
        elif b>=0:
            Tot-=B[b]
            b-=1
            NM -= 1
        else:
            break
        if a+1<N:
            if Tot+A[a+1]<=K:
                NM+=1
        elif b+1<M:
            if Tot+B[b+1]<=K:
                NM+=1

    print(NM)

if __name__ == '__main__':
    main()