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:
from collections import defaultdict
n, m, k = map(int, input().split())
A = defaultdict(lambda: {"score": 0, "time": 10**7})
B = defaultdict(lambda: {"score": 0, "time": 10**7})
*tA, = map(int, input().split())
*tB, = map(int, input().split())
for i, a in enumerate(tA):
    A[i]["score"] = 1
    A[i]["time"] = a
for i, b in enumerate(tB):
    B[i]["score"] = 1
    B[i]["time"] = b
tmp = 0
cnt = 0
i, j = 0, 0
while tmp + A[i]["time"] <= k or tmp + B[j]["time"] <= k:
    if A[i]["time"] <= B[j]["time"]:
        tmp += A[i]["time"]
        cnt += A[i]["score"]
        i += 1
    else:
        tmp += B[j]["time"]
        cnt += B[j]["score"]
        j += 1
print(cnt)

Submission status: Wrong Answer

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

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

counter = 0
time = 0

only_a_max = 0
only_b_max = 0

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

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

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


aindex = 0
bindex = 0

while True:


    target = 0

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

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

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


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

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



ans = max(only_b_max, only_a_max, counter)
print(ans)

Submission status: Wrong Answer

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

time = 0
count = 0
a_i = 0
b_i = 0

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

bn = [0] * n
bm = [0] * m

a.extend(bm)
b.extend(bn)

for _ in range(n+m):
    if a[a_i] != 0 and b[b_i] != 0:
        av = a[a_i]
        bv = b[b_i]
        if av <= bv:
            time += av
            tmp = av
            a_i += 1

        else:
            time += bv
            b_i += 1
            tmp = bv
        count += 1
    else:
        if a[a_i] == 0:
            bv = b[b_i]
            time += bv
            tmp = bv
            b_i += 1
        else:
            av = a[a_i]
            time += av
            tmp = av
            a_i += 1
        count += 1

    if time > k:
        time -= tmp
        count -= 1
        break
print(count)

Submission status: Wrong Answer

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

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

    while(k>0):

        
        if count == n+m:
            break

        if i < n and j < m:

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

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

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

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

    print(count)



            

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
m, n, k = map(int,input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
t = 0
r = 0
ai = 0
bi = 0
la = len(a)
lb = len(b)
while t < k:
    while(t < k and ai < la and bi < lb and a[ai] == b[bi]):
        t += a[ai]
        r += 1 if t < k else 0
        ai += 1
        bi += 1
    if t >= k:
        break
    if ai < la and bi >= lb or ai < la and bi < lb and a[ai] < b[bi]:
        t += a[ai]
        ai += 1
        r += 1 if t < k else 0
    elif bi < lb and ai >= la or ai < la and bi < lb and a[ai] > b[bi]:
        t += b[bi]
        bi += 1
        r += 1 if t < k else 0
print(r)

Submission status: Wrong Answer

Solution code:
from statistics import mean
N,M,K=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
k=0
c=0
if sum(a) + sum(b) <= K:
    print(N+M)
else:
    while k <= K:
        if len(a) > 0 and len(b)>0:
            if mean(a[:2]) < mean(b[:2]) and ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)
            elif mean(a[:2]) > mean(b[:2]) and ((k+b[0]) <=K):
                k+=b[0]
                c+=1
                b.pop(0)
            else:
                if (a[0] < b[0]) and ((k+a[0]) <=K):
                    k+=a[0]
                    c+=1
                    a.pop(0)

                elif (a[0] > b[0]) and ((k+b[0]) <=K):
                    k+=b[0]
                    c+=1
                    b.pop(0)
                else:
                    break
        elif (len(a) > 0) and (len(b) ==0):
            if ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)
            else:
                break
        elif (len(b) > 0) and (len(a) ==0):
            if ((k+b[0]) <=K):
                k+=b[0]
                c+=1
                b.pop(0)
            else:
                break
        else:
            break
    print(c)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
An = []
Bn = []
for n in range(N):
    if n == 0:
        An.append(A[n])
    else:
        An.append(An[n-1]+A[n])
    if An[n] > K:
        break
N = n+1

for n in range(M):
    if n == 0:
        Bn.append(B[n])
    else:
        Bn.append(Bn[n-1]+B[n])
    if Bn[n] > K:
        break

M = n+1

old = 0

for n in range(N):
    if An[n] > K:
        break

for nn in reversed(range(n+1)):
    for m in range(M):
        if A[0] <= K and Bn[m] + An[nn] <= K:
            if old < m+nn+2:
                old = m+nn+2
        elif A[0] > K:
            if Bn[m] <= K:
                if old < m+1:
                    old = m+1


print(old)

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.sort()
b.sort()
s,c = 0,0
if a[0] < k or b[0] < k:
    for i in range(m+n):
        if i< len(a):
            if s < k and s+a[i]<=k:
                s += a[i]
                c += 1
        if i<len(b):
            if s<k and s+b[i] <=k:
                s += b[i]
                c += 1
else:
    c = 0
print(c)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=0
alen=len(A)
b=0
blen=len(B)

while (K>0):
    if ((a<alen) & (b<blen)):
        if (A[a]<=B[b]):
            if (K<A[a]):
                break
            else:
                K=K-A[a]
                a=a+1
        elif (A[a]>B[b]):
            if (K<B[b]):
                break
            else:
                K=K-B[b]
                b=b+1
    elif ((a<alen) & (b>=blen)):
        if (K<A[a]):
            break
        else:
            K=K-A[a]
            a=a+1
    elif ((a>=alen) & (b<blen)):
        if (K<B[b]):
            break
        else:
            K=K-B[b]
            b=b+1

print(a+b)

Submission status: Wrong Answer

Solution code:
N, M, K=(int(x) for x in input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

A_i = 0
B_i = 0

A_i_sub = 0
B_i_sub = 0

next_A = A_list[A_i]
next_B = B_list[B_i]

count_read = 0

while(K >= next_A or K >= next_B):
    if(next_A <= next_B and A_i < N):
        K = K - next_A
        count_read += 1
        if(A_i < N-1):
            A_i += 1
            next_A = A_list[A_i]
        elif(A_i == N-1):
            next_A = 10000000000
    elif(next_B < next_A and B_i < M):
        K = K - next_B
        count_read += 1
        if(B_i < M-1):
            B_i += 1
            next_B = B_list[B_i]
        elif(B_i == M-1):
            next_B = 10000000000

print(count_read)

Submission status: Wrong Answer

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

sumA = []
for i in range(len(listA)):
    if i == 0:
        sumA.append(listA[i])
    else:
        if sumA[i-1]+listA[i] > k:
            break
        else:
            sumA.append(sumA[i-1]+listA[i])
sumB = []
for i in range(len(listB)):
    if i == 0:
        sumB.append(listB[i])
    else:
        if sumB[i-1]+listB[i] > k:
            break
        else:
            sumB.append(sumB[i-1]+listB[i])
if sumB[-1]+sumA[-1] <= k:
    print(n+m)
else:
    maxB = 0
    for iA in range(len(sumA)):
        if sumA[iA] > k:
            break
        for iB in range(len(sumB)):
            if sumA[iA] + sumB[iB] > k:
                break
            else:
                if maxB < iA+1+iB+1:
                    maxB = iA+1+iB+1
    print(maxB)

Submission status: Wrong Answer

Solution code:
str1 = input().strip().split()
str_a = input().strip().split()
str_b = input().strip().split()

a_kazu = int(str1[0])
b_kazu = int(str1[1])
jikan_ = int(str1[2])

str_a_count = 0
str_b_count = 0
count = 0

print(a_kazu + b_kazu)
for num in range(a_kazu + b_kazu - 1):
    try:
      if int(str_a[str_a_count]) < int(str_b[str_b_count]):
        jikan_ = jikan_ - int(str_a[str_a_count])
        str_a_count = str_a_count + 1
        count = count + 1
        if jikan_ < 0:
          break
      else:
        jikan_ = jikan_ - int(str_b[str_b_count])
        str_b_count = str_b_count + 1
        count = count + 1
        if jikan_ < 0:
          break
    except:
      try:
        jikan_ = jikan_ - int(str_a[str_a_count])
        str_a_count = str_a_count + 1
        count = count + 1
        if jikan_ < 0:
          break
      except:
        jikan_ = jikan_ - int(str_b[str_b_count])
        str_b_count = str_b_count + 1
        count = count + 1
        if jikan_ < 0:
          break
if count == (a_kazu + b_kazu):
  print(count)
else:
  print(count - 1)

Submission status: Wrong Answer

Solution code:
# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    P = X[X <= k]
    Q = Y[Y <= k]
    n = len(P)
    m = len(Q)
    # print(P,Q)

    cnt = 0
    if P[-1] + Q[-1] >= k:
        print(n + m - 2)
        exit()

    # Aをi個、Bをj個選ぶ
    for i in range(n)[::-1]:
        if i + m <= cnt:
            break
        else:
            for j in range(m)[::-1]:
                #print(i,j,X[i],Y[j])
                if X[i] + Y[j] <= k:
                    cnt = max(cnt, i + j)
                    break
    print(cnt)



if __name__ == "__main__":
    main()

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

A_acum = list(itertools.accumulate(A))
B_acum = list(itertools.accumulate(B))

ans = 0
for i in range(N-1, -1, -1):
    if A_acum[i] > K:
        continue
    else:
        j = M - 1
        p = A_acum[i] + B_acum[j]
        while p > K:
            j -= 1
            if j == -1:
                break
            p = A_acum[i] + B_acum[j]
        ans = max(ans, i+j+2)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case #{}: {}".format(i, o))


def solve(n, m, k, a, b):
    from_row_1 = 0
    left_1 = k
    for item in a:
        left_1 -= item
        if left_1 < 0:
            left_1 += item
            break
        from_row_1 += 1
    from_row_2 = 0
    left_2 = k
    for item in b:
        left_2 -= item
        if left_2 < 0:
            left_2 += item
            break
        from_row_2 += 1

    result = 0
    if from_row_1 > from_row_2 or (from_row_1 == from_row_2 and left_1 > left_2):
        p1 = from_row_1 - 1
        p2 = 0
        result += from_row_1
        while left_1 > 0:
            if p2 < len(b) and b[p2] <= left_1:
                result += 1
                left_1 -= b[p2]
                p2 += 1
            elif p1 >= 0 and p2 < len(b) and a[p1] > b[p2]:
                left_1 += a[p1] - b[p2]
                p1 -= 1
                p2 += 1
            else:
                break
        if left_1 < 0:
            result -= 1
    else:
        p1 = 0
        p2 = from_row_2 - 1
        result += from_row_2
        while left_2 > 0:
            if p1 < len(a) and a[p1] <= left_2:
                result += 1
                left_2 -= a[p1]
                p1 += 1
            elif p1 < len(a) and p2 >= 0 and b[p2] > a[p1]:
                left_2 += b[p2] - a[p1]
                p1 += 1
                p2 -= 1
            else:
                break
        if left_2 < 0:
            result -= 1
    return result



if __name__ == "__main__":
    n, m, k = arr_in()
    a = arr_in()
    b = arr_in()
    out(solve(n, m, k, a, b))

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()))
#----------
def ShouldReadAs4(As,Bs):
    if As==Bs:
        return True
    
    AsFirst= As+Bs
    BsFirst= Bs+As
 
    for i in range(1,len(AsFirst)+1):
        if As[:i]==Bs[:i]:
            continue
        else:
            if AsFirst[i-1]<BsFirst[i-1]:
                return True
            elif AsFirst[i-1]>BsFirst[i-1]:
                return False
#----------
count=0
 
while (K>0):
    if ShouldReadAs4(As,Bs):
        K-=As.pop(0)
    else:
        K-=Bs.pop(0)
    count+=1
    
if len(As)>0 or len(Bs)>0:
    print(count-1)
else:
    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.append(K + 1)
B.append(K + 1)
ia, ib = 0, 0
time = 0
flag = True
while flag:
    flag = False
    if A[ia] <= B[ib] and time + A[ia] <= K:
        time += A[ia]
        ia += 1
        flag = True
    elif B[ib] < A[ia] and time + B[ib] <= K:
        time += B[ib]
        ib += 1
        flag = True
print(ia + ib)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
c = 0
while K > 0:
    if not A == [] and not B == []:
        if A[0] <= B[0]:
            K = K - A[0]
            A.remove(A[0])
            if K >= 0:
                c += 1
        else:
            K = K - B[0]
            B.remove(B[0])
            if K >= 0:
                c += 1
    if A == [] and B != []:
        K = K - B[0]
        B.remove(B[0])
        if K >= 0:
            c += 1
    if B == [] and A != []:
        K = K - A[0]
        A.remove(A[0])
        if K >= 0:
            c += 1
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
ai=0
bi=0
for i in range(n+m):
  ans+=1
  if n-ai==0:
    k-=b[bi]
    bi+=1
  elif m-bi==0:
    k-=a[ai]
    ai+=1
  elif a[ai]>b[bi]:
    k-=b[bi]
    bi+=1
  elif b[bi]>a[ai]:
    k-=a[ai]
    ai+=1
  else:
    if n-ai-1==0:
      k-=b[bi]
      bi+=1
    elif m-bi-1==0:
      k-=a[ai]
      ai+=1
    elif a[ai+1]>=b[bi+1]:
      k-=b[bi]
      bi+=1
    elif a[ai+1]<b[bi+1]:
      k-=a[ai]
      ai+=1
  if k<0:
    ans-=1
    break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline


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

    return AA


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

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

    M = 0

    left = 0
    right = len(BB) - 1

    print(AA, BB)

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

    print(M)


main()

Submission status: Wrong Answer

Solution code:
import math

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

time = 0
a_i = 0
b_i = 0
while time <= k:
    if a_i >= n:
        cur_a = math.inf
    else:
        cur_a = alist[a_i]

    if b_i >= m:
        cur_b = math.inf
    else: 
        cur_b = blist[b_i]

    if cur_a == math.inf and cur_b == math.inf:
        break

    if cur_a <= cur_b:
        time += alist[a_i]
        a_i += 1
    else:
        time += blist[b_i]
        b_i += 1

if time <= k:
    print(a_i+b_i)
else:
    print(a_i+b_i-1)

Submission status: Wrong Answer

Solution code:
n,m,rem=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
j=0
k=0
c=list()
count=0
while k<n+m:
	if a[i]<=b[j]:
		c.append(a[i])
		k=k+1
		i=i+1
	else:
		c.append(b[j])
		k=k+1
		j=j+1
	if i==n:
		while j<m:
			c.append(b[j])
			k=k+1
			j=j+1
	if j==m:
		while i<n:
			c.append(a[i])
			k=k+1
			i=i+1
i=0
while i<len(c) and rem>=c[i] :
	rem=rem-c[i]
	i=i+1
	count=count+1
print(count)

Submission status: Wrong Answer

Solution code:
a,b,c = map(int,input().split())
d = list(map(int,input().split()))
e = list(map(int,input().split()))
count = 0

while c > 0:
  if(len(d) > 0):
    if((c-d[0]) >= 0):
      c = c - d[0]
      del d[0]
      count += 1
  if(len(e) > 0):
    if((c-e[0]) >= 0):
      c = c - e[0]
      del e[0]
      count += 1
  if(c == c):
    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()))

SA = [0]*N
SB = [0]*M
SA[0] = A[0]
SB[0] = B[0]
for i in range(1,N):
    SA[i] = SA[i-1] + A[i]
for i in range(1,M):
    SB[i] = SB[i-1] + B[i]
j = 0
k = 0
ans = 0
for j in range(N):
    for k in range(M):
        if SA[j] + SB[k] <= K:
            ans = max(ans, j +1+ k+1)
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
l = input().split()
N, M, K = int(l[0]), int(l[1]), int(l[2])
A = input().split()
B = input().split()

cnt = 0
t = 0
i = j = 0
while cnt <= N + M:
    if i < N:
        a = int(A[i])
    if i >= N:
        a = 0
    if j < M:
        b = int(B[j])
    if j >= M:
        b = 0
    subt = t
    if a >= b and b != 0:
        subt += b
        j += 1
    if a < b and a != 0:
        subt += a
        i += 1
    if subt > K or cnt >= N+M:
        break
    if subt <= K:
        t = subt
        cnt += 1

print(cnt)

Submission status: Wrong Answer

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



Kc=K
bookc=0

Ai=0
Bi=0

while Kc>0:
    if Ai==N and Bi==M:
        break
    if Ai<N:
        if Bi<M:
            if A[Ai]<=B[Bi] and A[Ai]<=Kc:
                bookc+=1
                Kc-=A[Ai]
                Ai+=1

            elif B[Bi]<=Kc:
                bookc+=1
                Kc-=B[Bi]
                Bi+=1
            else:
                break
        elif A[Ai]<=Kc:
            bookc+=1
            Kc-=A[Ai]
            Ai+=1
        else:
            break
    elif Bi<M:
        if B[Bi]<=Kc:
            bookc+=1
            Kc-=B[Bi]
            Bi+=1
        else:
            break

print(bookc)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,M,K = map(int,input().split())
Ali = list(map(int,input().split()))
Bli = list(map(int,input().split()))

r = 0
s = 0
t = 0

for i in range(N):
  if Ali[i] <= K-t:
    r += 1
    t += Ali[i]
  else:
    break
    
for i in range(M):
  if Bli[i] <= K-t:
    r += 1
    t += Bli[i]
  else:
    break
    
for i in range(M):
  if Bli[i] <= K-t:
    s += 1
    t += Bli[i]
  else:
    break
    
for i in range(N):
  if Ali[i] <= K-t:
    s += 1
    t += Ali[i]
  else:
    break
    
print(max(r,s))

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
ans = 0
while True:
  if A == [] and B == []:
    break
  elif A == []:
    s += B[0]
    B.pop(0)
    if s < k:
      ans += 1
    else:
      break
  elif B == []:
    s += A[0]
    A.pop(0)
    if s < k:
      ans += 1
    else:
      break
  else: 
    if A[0] >= B[0] :
      s += B[0]
      B.pop(0)
      if s < k:
        ans += 1
      else:
        break
    else :
      s += A[0]
      A.pop(0)
      if s < k:
        ans += 1
      else:
        break
print(ans)

Submission status: Wrong Answer

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

import sys
import collections

def input():
    return sys.stdin.readline()

def greedy_reading(n, m, k, a_list, b_list):

    max_books = len(a_list) + len(b_list)

    k_remain = k
    count = 0
    for i in range(max_books):
        if len(a_list) > 0 and len(b_list) > 0:
            if a_list[0] <= b_list[0]:
                if k_remain < a_list[0]:
                    break
                k_remain -= a_list.popleft()
                count += 1
            else:
                if k_remain < b_list[0]:
                    break
                k_remain -= b_list.popleft()
                count += 1
        elif len(a_list) > 0:
            if k_remain < a_list[0]:
                break
            k_remain -= a_list.popleft()
            count += 1
        elif len(b_list) > 0:
            if k_remain < b_list[0]:
                break
            k_remain -= b_list.popleft()
            count += 1

        #print(k_remain)

    return count


def resolve():
    n, m, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    a_list = collections.deque(a_list)
    b_list = collections.deque(b_list)
    ans = greedy_reading(n, m, k, a_list, b_list)


    a_sum = 0
    b_sum = 0
    a_num = 0
    b_num = 0
    a_remain = 0
    b_remain = 0
    # Aから
    for i in range(len(a_list)):
        a_sum += a_list[i]
        if a_sum > k:
            a_sum -= a_list[i]
            a_num = i
            break
    if a_num == 0 and a_sum == 0:
        print(ans)
        exit()
    elif a_num == 0:
        a_num = len(a_list)

        a_remain = k - a_sum

    # B
    for i in range(len(b_list)):
        b_sum += b_list[i]
        if b_sum > k:
            b_sum -= b_list[i]
            b_num = i
            break

    if b_num == 0 and b_sum == 0:
        print(ans)
        exit()
    elif b_num == 0:
        b_num = len(b_list)
        b_remain = k - b_sum


    # Aオーバーしない
    a_b_sum = a_sum
    a_b_num = a_num

    for i in range(len(b_list)):
        a_b_sum += b_list[i]
        if a_b_sum > b_remain:
            a_b_sum -= b_list[i]
            a_b_num += i
            break
    # bオーバーしない
    b_a_sum = b_sum
    b_a_num = b_num
    for i in range(len(a_list)):
        b_a_sum += a_list[i]
        if b_a_num > a_remain:
            b_a_sum -= a_list[i]
            b_a_num += i
            break

    #print(a_b_num, b_a_num, ans)
    if ans < a_b_num and b_a_num <= a_b_num:
        print(a_b_num)
        exit()

    if ans < b_a_num and a_b_num <= b_a_num:
        print(b_a_num)
        exit()

    print(ans)











if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
N, M, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
a = [0]*N
b = [0]*M
a[0] = A[0]
b[0] = B[0]
ae = N-1
be = M-1
e = 0
for i in range(1, N):
    a[i] = a[i-1] + A[i]
    if(a[i] > K):
        ae = i-1
        break
for i in range(1, M):
    b[i] = b[i-1] + B[i]
    if(b[i] > K):
        be = i-1
        break
ans = be+1
for i in range(ae, -1, -1):
    ans = max(ans, i+1)
    for j in range(e, be+1):
        if a[i] + b[j] > K:
            e = max(j-1, 0)
            break
        ans = max(ans, i+j+2)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k =map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if sum(a)+sum(b) <= k:
  print(n+m)
  exit(0)
a.append(10**9+1)
b.append(10**9+1)
cnt = 0
cnt2 = 0
mo = 0
while mo <= k:
  if a[cnt] >= b[cnt2]:
    if cnt > 0 and a[cnt-1]<b[cnt2]:
      mo = mo - a[cnt-1] + b[cnt2]
      if mo > k:
        print(cnt+cnt2)
        exit(0)
      cnt -= 1
      cnt2+= 1
    else:
      mo += b[cnt2]
      if mo > k:
        print(cnt+cnt2)
        exit(0)
      cnt2+=1
  else:
    if cnt2 > 0 and a[cnt]>b[cnt2-1]:
      mo = mo - b[cnt2-1] + a[cnt]
      if mo > k:
        print(cnt+cnt2)
        exit(0)
      cnt2 -= 1
      cnt+= 1
    else:
      mo += a[cnt]
      if mo > k:
        print(cnt+cnt2)
        exit(0)
      cnt += 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()))
c = [10000000001]
count = 0

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

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

Submission status: Wrong Answer

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

time = 0
ai = 0
bj = 0
am = 0
bm = 0

while time <= K:
  if ai < M:
    am = A[ai]
  else:
    am = K
  if bj < N:
    bm = B[bj]
  else:
    bm = K

  if am < bm:
    if (time + am) > K:
      break
    else:
      time += am
      ai += 1
  else:
    if (time + bm) > K:
      break
    else:
      time += bm
      bj += 1

print(ai + bj)

Submission status: Wrong Answer

Solution code:
from statistics import mean
N,M,K=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
k=0
c=0
if sum(a) + sum(b) <= K:
    print(N)
else:
    while k <= K:
        if len(a) > 0 and len(b)>0:
            if mean(a[:2]) < mean(b[:2]) and ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)
            elif mean(a[:2]) > mean(b[:2]) and ((k+b[0]) <=K):
                k+=b[0]
                c+=1
                b.pop(0)
            else:
                if (a[0] < b[0]) and ((k+a[0]) <=K):
                    k+=a[0]
                    c+=1
                    a.pop(0)

                elif (a[0] > b[0]) and ((k+b[0]) <=K):
                    k+=b[0]
                    c+=1
                    b.pop(0)
                else:
                    break
        elif (len(a) > 0) and (len(b) ==0):
            if ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)
            else:
                break
        elif (len(b) > 0) and (len(a) ==0):
            if ((k+b[0]) <=K):
                k+=b[0]
                c+=1
                b.pop(0)
            else:
                break
        else:
            break
    print(c)

Submission status: Wrong Answer

Solution code:
str1 = input().strip().split()
str_a = input().strip().split()
str_b = input().strip().split()

a_kazu = int(str1[0])
b_kazu = int(str1[1])
jikan_ = int(str1[2])

str_a_count = 0
str_b_count = 0
count = 0

print(a_kazu + b_kazu)
for num in range(a_kazu + b_kazu- 1):
    try:
      if int(str_a[str_a_count]) < int(str_b[str_b_count]):
        jikan_ = jikan_ - int(str_a[str_a_count])
        str_a_count = str_a_count + 1
        count = count + 1
        if jikan_ < 0:
          break
      else:
        jikan_ = jikan_ - int(str_b[str_b_count])
        str_b_count = str_b_count + 1
        count = count + 1
        if jikan_ < 0:
          break
    except:
      try:
        jikan_ = jikan_ - int(str_a[str_a_count])
        str_a_count = str_a_count + 1
        count = count + 1
        if jikan_ < 0:
          break
      except:
        jikan_ = jikan_ - int(str_b[str_b_count])
        str_b_count = str_b_count + 1
        count = count + 1
        if jikan_ < 0:
          break
if count == (a_kazu + b_kazu):
  print(count)
else:
  print(count - 1)