Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Wrong Answer

Solution code:
import sys
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(1000000000)
B.append(1000000000)
a = 0
b = 0
l = n + m
dp = [0] * (l + 1)
for i in range(l):
    dp[i + 1] = dp[i] + min(A[a], B[b])
    if dp[i + 1] > k:
        print(i)
        sys.exit()
    if A[a] <= B[b]:
        a = a + 1
    elif B[b] < A[a]:
        b = b +1
print(n + m)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
T = 0
a = 0
b = 0
Flag = True
while Flag == True:
  if a < N and b < M:
    if A[a] < B[b]:
      if T + A[a] <= K:
        T += A[a]
        a += 1
      elif T+A[a] > K:
        Flag = False
        
    elif A[a] >= B[b]:
      if T + B[b] <= K:
        T += B[b]
        b += 1
      elif T+B[b] > K:
        Flag = False
        
  elif a == N and b < M:
    if T + B[b] <= K:
      T += B[b]
      b += 1
    elif T + B[b]>K:
      Flag = False
  elif a < N and b ==M:
    if T + A[a] <= K:
      T += A[a]
      a += 1
    elif T + A[a] >K:
      Flag = False
      
  elif a == N and b == M:
    Flag = False
    
print(a+b)

Submission status: Wrong Answer

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

A.append(1000000001)
B.append(1000000001)
i,j = 0,0
for k in range(N+M):
    if A[i] <= B[j]:
        K -= A[i]
        i += 1
    else:
        K -= B[j]
        j += 1
    if K < 0:
        break
    result += 1
print(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()))

if len(A) < len(B):
    A = A + [0] * ( len(B) - len(A) )
if len(B) < len(A):
    B = B + [0] * ( len(A) - len(B) )

L = []
for a, b in zip(A, B):
    L = L + [min(a, b)]
    L = L + [max(a, b)]
for i in range(L.count(0)):
    L.remove(0)

cnt = 0
for l in L:
    K = K - l
    if K <= 0:
        if K == 0:
            cnt = cnt + 1
        break
    cnt = cnt + 1

print(cnt)

Submission status: Wrong Answer

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF = 10**20
def main():
    N,M,K=mi()
    A=deque(mi())
    B=deque(mi())

    k = 0
    ans = 0
    while k <= K and (A or B):
        a = A.popleft() if len(A) > 0 else INF
        b = B.popleft() if len(B) > 0 else INF

        if a == b and len(A) > 0 and len(B) > 0:
            if A[0] > B[0]:
                k += b
                A.appendleft(a)
            else:
                k += a
                B.appendleft(b)
                
        elif a < b:
            k += a
            if b != INF:
                B.appendleft(b)
        else:
            k += b
            if a != INF:
                A.appendleft(a)

        if k <= K:
            ans += 1

    print(ans)



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

ai=0
bi=0
time=0

while ai < n and bi < m:
  if a[ai]<b[bi]:time+=a[ai];ai+=1;print(time)
  else:time+=b[bi];bi+=1;print(time)
    
  if time >= k:break
    
if n > m:
  while ai < n:
    time+=a[ai];ai+=1;print(time)
    if time >= k:break

if n < m:
  while bi < m:
    time+=b[bi];bi+=1;print(time)
    if time >= k:break
      
print(ai+bi)

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 = 0
for s in range(n+m):
    if i>k:
        if a[0]>=b[0]:
            i = i + a[0]
            del a[0]
        else:
            i = i + b[0]
            del a[0]
    else:
        print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = deque(list(map(int, input().split())))
B = deque(list(map(int, input().split())))
time = 0
ans = 0
while ans < N + M:
    if len(A) > 0 and len(B) > 0:
        if A[0] <= B [0]:
            time += A.popleft()
            if time <= K:
                ans += 1
            else:
                break
        else:
            time += B.popleft()
            if time <= K:
                ans += 1
            else:
                break
    elif len(A) == 0:
        time += B.popleft()
        if time <= K:
            ans += 1
        else:
            break
    elif len(B) == 0:
        time += A.popleft()
        if time <= K:
            ans += 1
        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()))
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
print(cnt+cnt2)

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
ab_list = []
for _ in range(len(a) + len(b)):
    if len(a) >0 and len(b) > 0:
        if a[0] >= b[0]:
            ab_list.append(b.pop(0))
        else:
            ab_list.append(a.pop(0))
    elif len(a) == 0:
        ab_list.append(b.pop(0))
    elif len(b) == 0:
        ab_list.append(a.pop(0))
for i in ab_list:
    k -= i
    if k < 0:
        print(ans)
        exit()
    elif k ==0:
        print(ans+1)
        exit()
    else:
        ans+=1
print(ab_list)

Submission status: Wrong Answer

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

a= list(map(int,input().split())) #n冊

b= list(map(int,input().split())) #m冊
#ab=a+b
#print(ab)

if n>m:
    q=0
    while q<=(n-m):
        b.append(100000000000)
        q+=1
elif m>n:
    p=0
    while p<=(m-n):
        a.append(100000000000)
        p+=1
b.append(100000000000)
a.append(100000000000)
ab=[110000000000]*(n+m)
ans=0
count=0
i=0
A=0
B=0
if sum(a)+sum(b)<=k:
    ans=n+m
    print(ans)
else:
    while i<(n+m) and A<=n and B<=m:
        ab[i]=min(a[A],b[B])

        if a[A]>=b[B]:
            if A==n+1 or B==m+1:
                i+=1
            else:  
                B+=1
                i+=1
        else :
            if A==n+1 or B==m+1:
                i+=1
            else:
                A+=1
                i+=1
    for i in range(n+m):
        ans+=ab[i]
        if ans>k:
            break
        else:
            count+=1
    print(count)

Submission status: Wrong Answer

Solution code:
def merge(x, y, a):
    i = 0
    j = 0
    while i < len(x) or j < len(y):
        if i == len(x):
            a[i+j] = y[j]
            j += 1
        elif j == len(y):
            a[i+j] = x[i]
            i += 1
        elif x[i] <= y[j]:
            a[i+j] = x[i]
            i += 1
        else:
            a[i+j] = y[j]
            j += 1
    return a
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[0]*(N+M)
C=merge(A,B,C)
for i in range(N+M):
    K-=C[i]
    if K==0:
        print(i+1)
        break
    elif K<0:
        print(i)
        break

Submission status: Wrong Answer

Solution code:
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')


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

    i, j = 0, 0
    mins = 0
    while (mins < k):
        if (i < n and (j == m or a[i] <= b[j])):
            mins += a[i]
            i += 1
        elif (j < m and (i == n or b[j] < a[i])):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

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
hon = 0
if min(a) > k and min(b) > k:
    print("0")
    quit()
while ans <= k :

    if  a and b:
        if a[0] > b[0]:
            ans += a[0]
            a.pop(0)
        else:
            ans += b[0]
            b.pop(0)
    elif not a and b:
        ans += b[0]
        b.pop(0)
    elif a and not b:
        ans += a[0]
        a.pop(0)
    else:
        break
    hon += 1
print(hon)

Submission status: Wrong Answer

Solution code:
N,M,K=input().split()
N=int(N)
M=int(M)
K=int(K)
tmp=input().split()
A=[]
Atime=[]
for i in range(len(tmp)):
  A.append(int(tmp[i]))
  if i==0: Atime.append(A[0])
  else: Atime.append(Atime[i-1]+A[i])
tmp=input().split()
B=[]
Btime=[]
for i in range(len(tmp)):
  B.append(int(tmp[i]))
  if i==0: Btime.append(B[0])
  else: Btime.append(Btime[i-1]+B[i])

Blim=len(Btime)
for i in range(len(Btime)):
  if Btime[i]>K:
    Blim=i
    break
if Blim==0: result=0
else: result=Blim
#print(Atime)
for i in range(len(Atime)):
  while Blim>0:
    if Atime[i]+Btime[Blim-1]<=K:
      if Blim+i+1>result: result=Blim+i+1
      break
    Blim-=1
print(result)

Submission status: Wrong Answer

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

ctime = k
c = 0
ac = 0
bc = 0
time = 0

while True:
    if time > k:
        print(c - 1)
        break
    elif n == ac and m == bc:
        print(c)
        break
    else:
        c += 1
        if n == ac:
            # print(b_list[bc], bc)
            time += b_list[bc]
            ctime -= b_list[bc]
            bc += 1
        elif m == bc:
            # print(a_list[ac], ac)
            time += a_list[ac]
            ctime -= a_list[ac]
            ac += 1
        else:
            all_a = 0
            for i in a_list[ac:]:
                all_a += i
                if all_a > ctime:
                    all_a -= i
                    break
            all_b = 0
            for j in b_list[bc:]:
                all_b += j
                if all_b > ctime:
                    all_b -= j
                    break
            if all_a == 0:
                all_a = 10**10
            if all_b == 0:
                all_b = 10**10
            # print(all_a, all_b, ctime)
            if all_a < all_b:
                # print(a_list[ac], ac)
                time += a_list[ac]
                ctime -= a_list[ac]
                ac += 1
            else:
                # print(b_list[bc], bc)
                time += b_list[bc]
                ctime -= b_list[bc]
                bc += 1

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
  t = sum(A[:i+1])
  if t > K:
    break
  else:
    n = i + 1
  for j in B:
    if t + j > K:
      break
    else:
      t += j
      n += 1
  ans = max(ans, n)
print(ans)

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 < len(A):
    am = A[ai]
  else:
    am = K
  if bj < len(B):
    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:
n,m,k=map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

spend = 0
ia = 0
ib = 0
tmpa = A[0]
tmpb = B[0]

total = sum(A)+sum(B)
if total <= k:
  print(n+m)
  exit()

for count in range(1,n+m+1):
#while spend < k:
  if tmpa < tmpb:
    spend += tmpa
    ia += 1
    if ia == n:
      tmpa = 10**9
    else:
      tmpa = A[ia]
  else:
    spend += tmpb
    ib += 1
    if ib == m:
      tmpb = 10**9
    else:
      tmpb = B[ib]
  if spend > k:
    count -= 1
    break

print(count)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())  
A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))
K = K/1000
A_list = [A/1000 for A in A_list]
B_list = [B/1000 for B in B_list]
read_time = 0
books = 0
while True:
    if B_list == []:
        if A_list == []:
            break
        read_time += A_list[0]
        if read_time > K:
            break
        books += 1
        del A_list[0]
    elif A_list == []:
        read_time += B_list[0]
        if read_time > K:
            break
        books += 1
        del B_list[0]
    elif A_list[0] < B_list[0]:
        read_time += A_list[0]
        if read_time > K:
            break
        books += 1
        del A_list[0]
    elif A_list[0] >= B_list[0]:
        read_time += B_list[0]
        if read_time > K:
            break
        books += 1
        del B_list[0]

print(books)

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 a_i in range(0,len(an)):


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

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


def Find(a, b):
    if a[-1] + b[-1] <= k:
        return len(a) - 1 + len(b) - 1

    if a[-1] > b[-1]:
        rest = k - a[-1]
        for i in range(len(b)):
            if rest <= b[-i]:
                return len(a) - 1 + len(b) - 1 - i

    if b[-1] > a[-1]:
        rest = k - b[-1]
        for i in range(len(a)):
            if rest <= a[-i]:
                return len(b) - 1 + len(a) - 1 - i

    return Find(a[0:len(a)-1], b[0:len(b)-1]) + 1


print(Find(accum_a, accum_b))

Submission status: Wrong Answer

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

time0 = 0
cnt = 0
while(time0<=K):
    if len(A) == 0 and len(B) != 0:
        time0 += B.pop(0)
        cnt += 1
    elif len(B) == 0 and len(A) != 0:
        time0 += A.pop(0)
        cnt += 1
    elif len(A) == 0 and len(B) == 0:
        cnt += 1
        break
    else:
        if A[0] < B[0]:
            time0 += A.pop(0)
            cnt += 1
        elif A[0] > B[0]:
            time0 += B.pop(0)
            cnt += 1
        else:
            if len(A) >= len(B):
                time0 += A.pop(0)
                cnt += 1
            else:
                time0 += B.pop(0)
                cnt += 1
            
cnt -= 1

print(cnt)

Submission status: Wrong Answer

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

a.append(pow(10,10))
b.append(pow(10,10))

ans = 0

ai = 0
bi = 0

while True:
  t = 0
  if a[ai] > b[bi]:
    t = b[bi]
    bi += 1
  elif a[ai] < b[bi]:
    t = a[ai]
    ai += 1
  else:
    if len(a) == ai + 1:
      t = b[bi]
      bi += 1
    elif len(b) == bi + 1:
      t = a[ai]
      ai += 1
    else:
      if a[ai + 1] > b[bi + 1]:
        t = b[bi]
        bi += 1
      else:
        t = a[ai]
        ai += 1
  if k >= t:
    k -= t
    ans += 1
    if k == 0:
      break
  else:
    break

print(str(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_time = [0]
time = 0
for i in range(N):
    time += A[i]
    A_time.append(time)
B_time = [0]
time = 0
for i in range(M):
    time += B[i]
    B_time.append(time)

def binarySearch(key):
    left = -1
    right = len(B_time)
    
    while right - left > 1:
        mid = left + (right - left)//2
        if B_time[mid] > key:
            right = mid
        else:
            left = mid
    return left

Max = 0
for i in range(len(A_time)):
    j = binarySearch(K - A_time[i])
    Max = max(Max, i+j)

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

sum_min = 0
a_index = 0
b_index = 0
cnt = 0
while sum_min < K:
    if a_index == len(A):
        sum_min += B[b_index]
        b_index += 1
        if sum_min > K:
            break
        cnt += 1
    elif b_index == len(B):
        sum_min += A[a_index]
        a_index += 1
        if sum_min > K:
            break
        cnt += 1
    else:
        if A[a_index] < B[b_index]:
            sum_min += A[a_index]
            a_index += 1
            if sum_min > K:
                break
            cnt += 1
        else:
            sum_min += B[b_index]
            b_index += 1
            if sum_min > K:
                break
            cnt += 1
    
    if a_index == len(A) and b_index == len(B):
        break

print(cnt)

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

count = 0
for i in range(N + M):
  if len(A) == 0:
    book = B.popleft()
  elif len(B) == 0:
    book = A.popleft()
  else:
    if A[0] < B[0]:
      book = A.popleft()
    else:
      book = B.popleft()
  
  if K >= book:
    count += 1
    K -= book
  else:
    break

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, m, k = map(int, input().split())
a = np.cumsum(list(map(int, input().split())))
b = np.cumsum(list(map(int, input().split())))

ans = 0
if a[0] > k and b[0] > k:
    print(ans)
    exit()
l = 0
for i in range(n - 1, -1, -1):
    if a[i] > k:
        continue
    tmp = i + 1
    for j in range(l, m):
        if a[i] + b[j] <= k:
            tmp += 1
        else:
            break
        ans = max(ans, tmp)
        l = j - 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()))
M=0
count=0
while M<k:
  if len(A)==0:
    M+=B.pop(0)
    if M>k:
        break
    print(M,'B')
    count+=1
    continue
  if len(B)==0:
    M+=A.pop(0)
    if M>k:
        break
    print(M,'A')
    count+=1
    continue
  if min(A[0],B[0])==A[0]:
    M+=A.pop(0)
    if M>k:
        break
    print(M,'A')
    count+=1
  else:
    M+=B.pop(0)
    if M>k:
        break
    print(M,'B')
    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()]

count=0
time=0
for i in range(1,n+m+1):
    if ((time>k)|((A[0]==0)&(B[0]==0))):
        break
    if A[0]>B[0]:
        count+=1
        time+=A[0]
        A=A[1:]
    elif A[0]<B[0]:
        count+=1
        time+=B[0]
        B=B[1:]
    if A==[]:
        A.append(0)
    if B==[]:
        B.append(0)
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()))

if len(A) < len(B):
    A = A + [0] * ( len(B) - len(A) )
if len(B) < len(A):
    B = B + [0] * ( len(A) - len(B) )

L = []
for a, b in zip(A, B):
    L = L + [min(a, b)]
    L = L + [max(a, b)]
for i in range(L.count(0)):
    L.remove(0)
print(L)

cnt = 0
for l in L:
    K = K - l
    if K <= 0:
        if K == 0:
            cnt = cnt + 1
        break
    cnt = cnt + 1

print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin
#, setrecursionlimit, stdout
#setrecursionlimit(1000000)
#from collections import deque
#from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
#from heapq import heapify, heappop, heappush, heappushpop, heapreplace
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())
#mod=1000000007
res=['NET', 'DA']
############# CODE STARTS HERE #############

def bin_search(a, val):
    pos, l, r=0, 0, len(a)-1
    while l<=r:
        m=(l+r)//2
        if a[m]<=val:
            pos=m+1
            l=m+1
        else:
            r=m-1
    return pos


test_case=1
while test_case:
    test_case-=1


    n, m, k=mi()
    a=li()
    b=li()
    for i in range(1, n):
        a[i]+=a[i-1]
    for i in range(1, m):
        b[i]+=b[i-1]
    ans=0
    for i in range(n):
        if a[i]<=k:
            c=bin_search(b, k-a[i])
            ans=max(ans, c+i+1)
    print(ans)

Submission status: Wrong Answer

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

total = 0
ai = 0
bi = 0
count = 0
while total < p:
    if ai < n and bi < m:
        if a[ai] < b[bi]:
            total += a[ai]
            ai += 1
        elif a[ai] > b[bi]:
            total += b[bi]
            bi += 1
        else:
            a_min = min(a[ai:])
            b_min = min(b[bi:])
            if a_min < b_min:
                total += a[ai]
                ai += 1
            else:
                total += b[bi]
                bi += 1

    elif ai >= n and bi < m:
        total += b[bi]
        bi += 1
    elif ai < n and bi >= m:
        total += a[ai]
        ai += 1
    else:
        break

    # print(total)
    if total <= p:
        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()))
ans1=0
ans2=0
k2=k
a2=a
b2=b
for _ in range(4*10**5):
  if a[0] < b[0]:
    if k-a[0] >= 0:
      ans1+=1
      k-=a[0]
      a.pop(0)
      if len(a)==0:
        a.append(10**10)
    else:
      break
  elif a[0] > b[0]:
    if k-b[0] >= 0:
      ans1+=1
      k-=b[0]
      b.pop(0)
      if len(b)==0:
        b.append(10**10)
    else:
      break
  else:
    if k-a[0] >= 0:
      ans1+=1
      k-=a[0]
      a.pop(0)
      if len(a)==0:
        a.append(10**10)
    else:
      break
print(ans1)

Submission status: Wrong Answer

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

books_total = 0
possible_combo_c = {}
possible_combo_c[(0, 0)] = 0
while True:
    print(possible_combo_c)
    hit = False
    possible_combo_n = {}
    for combo in possible_combo_c.keys():
        if(combo[0] < n):
            combo_n_a = (combo[0]+1, combo[1])
            time = possible_combo_c[combo]+a[combo[0]]
            if time <= k:
                possible_combo_n[combo_n_a] = time
                hit = True
        if(combo[1] < m):
            combo_n_b = (combo[0], combo[1]+1)
            time = possible_combo_c[combo]+b[combo[1]]
            if time <= k:
                possible_combo_n[combo_n_b] = time
                hit = True
    if hit > 0:
        books_total += 1
        possible_combo_c = possible_combo_n
    else:
        break

ans = books_total
print(ans)

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
n=i[0]
m=i[1]
k=i[2]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a=b=0 #それぞれの読んだ数
cal=0 #読んだ時間
while True:
    if a!=n and b!=m:
        if A[a]<B[b]:
            cal+=A[a]
            if cal>=k:
                break
            a+=1
        elif B[b]<A[a]:
            cal+=B[b]
            if cal>=k:
                break
            b+=1
        elif B[b]==A[a]:
            cal+=A[a]
            if cal>=k:
                break
            a+=1
    elif a==n and b==m:
        break
    elif a==n:
        cal+=B[b]
        if cal>=k:
            break
        b+=1
    elif b==m:
        cal+=A[a]
        if cal>=k:
            break
        a+=1
print(a+b)

Submission status: Wrong Answer

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

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

# 累積和
CA = A
CB = B
sA = sum(A)
sB = sum(B)
for i in range(N-1):
    CA[i+1] = CA[i] + A[i+1]
for i in range(M-1):
    CB[i+1] = CB[i] + B[i+1]

def countmax(CA, N, K):
    for i in range(N):
        if CA[i] > K:
            return i
    return N

# Dont need count
if sA + sB <= K:
    print(N + M)
    exit(0)


if CA[-1] <= K: # 全読み可
    AyomiA = N
    AyomiB = countmax(CB, M, K - sA)
else:
    AyomiA = countmax(CA, N, K)
    AyomiB = countmax(CB, M, K-CA[AyomiA-1])

## Bの全読み
if CB[-1] <= K: # 全読み可
    ByomiB = M
    ByomiA = countmax(CA, N, K - sB)
else:
    ByomiB = countmax(CB, M, K)
    ByomiA = countmax(CA, N, K-CB[ByomiB-1])


# Bでカウントする時
if AyomiA - ByomiA > ByomiB - AyomiB:
    count = 0
    for b in range(AyomiB, ByomiB + 1):
        count = max(count, b + countmax(CA, N, K - CB[b-1]))
else:
# Aでカウントする時
    count = 0
    for a in range(ByomiA, AyomiA + 1):
        count = max(count, a + countmax(CB, M, K - CA[a-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()))

ans = 0
if min(A[0],B[0]) > K:
    ans = 0
elif min(A[0],B[0]) == K:
    ans = 1
else:
    while True:
        K = K - min(A[0],B[0])
        if K <= 0:
            break
        ans += 1
        if min(A[0],B[0])==A[0]:
            A.pop(0)
            if A == []:
                A.append(10**9+1)
        else:
            B.pop(0)
            if B == []:
                B.append(10**9+1)
                
print(ans)

Submission status: Wrong Answer

Solution code:
def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

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

    max_num_A, total_time_A = count_books(A, K)

    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    m = 0
    total_time_B = 0
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n]

        while m < M and total_time_B + B[m] < remain:
            total_time_B += B[m]
            m += 1
        num_B = m

        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
main()

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

cnt = 0
time = 0
a = A.popleft()
b = B.popleft()
judge = 'A'
while time <= K:
    if a == b == 10**9 + 1:
        break

    if a == b:
        for i in range(min(len(A), len(B))):
            if A[i] < B[i]:
                judge = 'A'
                break
            elif A[i] > B[i]:
                judge = 'B'
                break

        if judge == 'A':
            time += a
            try:
                a = A.popleft()
            except:
                a = 10 ** 9 + 1
        else:
            time += b
            try:
                b = B.popleft()
            except:
                b = 10 ** 9 + 1
    elif a < b:
        time += a
        try:
            a = A.popleft()
        except:
            a = 10**9 + 1
    else:
        time += b
        try:
            b = B.popleft()
        except:
            b = 10**9 + 1

    if time > K:
        break

    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
import bisect

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

a_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

if a_sum[0] > k and b_sum[0] > k:
  print(0)
  exit()

ans = 0
for i in range(len(a)):
  for j in range(len(b)):
    if a_sum[i] + b_sum[j] <= k:
      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()))
c=a+b
ans=0
while k>min(c):
    if a==[]:
        k-=b[0]
        b.remove(b[0])
        ans+=1
    elif b==[]:
        k-=a[0]
        a.remove(a[0])
        ans+=1
    else:                
        if a[0]<b[0]:
            k-=a[0]
            a.remove(a[0])
            ans+=1
        else:
            k-=b[0]
            b.remove(b[0])
            ans+=1
print(ans)