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:
# -*- coding: utf-8 -*-
import sys
N,M,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
B=map(int, sys.stdin.readline().split())

L=[]

ai=0
bi=0
while ai<N or bi<M:
    if ai==N:   #A列が最後の値からはみ出していたら
        b=B[bi]
        L.append(b) #bを1列に加え
        bi+=1   #Bを1つ進める
    elif bi==M:   #B列が最後の値からはみ出していたら
        a=A[ai]
        L.append(a) #aを1列に加え
        ai+=1   #Aを1つ進める
    else:       #どっちもまだ列の最後の値に達していなければ
        a=A[ai]
        b=B[bi]
        if a<b:
            L.append(a) #aを1列に加え
            ai+=1   #Aを1つ進める
        elif b<a:
            L.append(b) #bを1列に加え
            bi+=1   #Bを1つ進める
        elif a==b:  #同じだったら
            L.append(a) #aを1列に加え
            ai+=1   #Aを1つ進める
            L.append(b) #bを1列に加え
            bi+=1   #Bを1つ進める

total=0
for i,x in enumerate(L):
    total+=x
    if K<total:
        print i
        quit()
else:
    print i+1

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

ans = 0
time = 0

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

    if len(a) < 1:
        b_pop = b.popleft()
        time += b_pop
        if time > k:
            break
        ans += 1
        continue

    if len(b) < 1:
        a_pop = a.popleft()
        time += a_pop
        if time > k:
            break
        ans += 1
        continue

    if a[0] < b[0]:
        a_pop = a.popleft()
        time += a_pop

    elif a[0] == b[0]:
        if len(a) != 1 and len(b) != 1:
            if a[1] < b[1]:
                a_pop = a.popleft()
                time += a_pop
                if time > k:
                    break
                ans += 1
                continue
            else:
                b_pop = b.popleft()
                time += b_pop
                if time > k:
                    break
                ans += 1
                continue
        else:
            b_pop = b.popleft()
            time += b_pop
            if time > k:
                break
            ans += 1
            continue

    else:
        b_pop = b.popleft()
        time += b_pop

    if time > k:
        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]

Sum = 0


for i in range(N):
  K = K - A[i]
  if K >= 0:
    Sum += 1
  else:
    K = K + A[i]
    break
S.append(Sum)

for j in range(M):
  K = K - B[j]
  if K >= 0:
    Sum += 1
  else:
    if i == 0:
      break
    for k in range(i):
      K = K + A[i-k-1]
      Sum = Sum - 1
      if K >= 0:
        i = i-k-1
        break
    else:
      i = 0
      continue
    if K >= 0:
      Sum += 1
    
  S.append(Sum)
print(max(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()))
ans=0
for i in range(n+m+1):
  for a in range(i+1):
    b=i-a
    t=sum(A[:a])+sum(B[:b])
    if t>k:continue
    ans=max(ans,a+b)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
x=[int(x) for x in input().split()]
y=[int(x) for x in input().split()]
c=0
cost=0
f,t=0,0
for i in range(n):
    if i<n and cost<k and cost+x[i]<=k and f==0:
        cost+=x[i]
        c+=1
    else:f=1
    if i<m and  cost<k and cost+y[i]<=k and t==0:
        cost+=y[i]
        c+=1
    else:t=1
i+=1
if m>n and t==0:
    while(i<(m)):
        if cost+y[i]<=k and cost<k:
            cost+=y[i]
            c+=1
        i+=1
c1=0
cost=0
for i in range(n):
    if cost+x[i]<=k:
        cost+=x[i]
        c1+=1
c2=0
cost=0
for i in range(m):
    if cost+y[i]<=k:
        cost+=y[i]
        c2+=1
print(max(c1,c2,c))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import random

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

'''
testcount = 0
while True:
    testcount += 1
    if testcount > 100:
        break
    N = random.randint(1,200000)
    M = random.randint(1,200000)
    K = random.randint(1,1000000000)
    A = [random.randint(1,1000000000) for a in range(N)]
    B = [random.randint(1,1000000000) for a in range(M)]
    A.sort()
    B.sort()
'''
index_A = 0
index_B = 0
now = 0
count = 0

while True:
    if A[index_A] >= B[index_B]:
        if now + B[index_B] <= K:
            now += B[index_B]
            index_B += 1
            count += 1
        else:
            break
    else:
        if now + A[index_A] <= K:
            now += A[index_A]
            index_A += 1
            count += 1
        else:
            break
    if index_A > len(A)-1 or index_B > len(B)-1:
        break

if index_A > len(A)-1:
    while True:
        if now+B[index_B] <= K:
            now += B[index_B]
            count += 1
            index_B += 1
            if index_B > len(B)-1:
                break
        else:
            break
else:
    while True:
        if now + A[index_A] <= K:
            now += A[index_A]
            count += 1
            index_A += 1
            if index_A > len(A)-1:
                break
        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()))
cntA = 0
ans = 0
for i in range(n):
  if cntA+a[i] > k:
    break
  ans += 1
  cntA += a[i]
indA = ans-1
nc = indA+1
for j in range(indA+1):
  temp = 0
  adds = 0
  for v in b:
    if cntA + adds + v > k:
      break
    adds += v
    temp += 1
  cntA -= a[nc-1-j]
  ans = max(ans,indA+1+temp)
  indA -= 1
print(ans)

Submission status: Wrong Answer

Solution code:

import bisect
import numpy

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

cost_a = numpy.insert(numpy.cumsum(a), 0, 0)
cost_b = numpy.insert(numpy.cumsum(b), 0, 0)

left_max = bisect.bisect_left(cost_a, k)
right_max = bisect.bisect_left(cost_b, k)

ans = 0
j = right_max - 1
for i in range(left_max):
    for j in range(j , -1, -1):
        if cost_a[i] + cost_b[j] <= k:
            ans  = i + j if ans < i + j else ans
            break
print(ans)

Submission status: Wrong Answer

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

    c = a + b
    sum = 0
    for i in range(len(c) + 1):
        if len(a)==0 and len(b)==0:
            return print(i)
        elif not a:
            sum += b[0]
            del b[0]
            if k < sum:
                return print(i)
        elif not b:
            sum += a[0]
            del a[0]
            if k < sum:
                return print(i)
        elif a[0] <= b[0]:
            sum += a[0]
            del a[0]
            if k < sum:
                return print(i)
        elif a[0] > b[0]:
            sum += b[0]
            del b[0]
            if k < sum:
                return print(i)
       
main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools as it
input = sys.stdin.readline

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

AS = list(it.accumulate(A))
BS = list(it.accumulate(B))

max = 0
for i in range(len(AS)):
    a = AS[i]
    for j in range(len(BS)):
        b = BS[j]
        if a + b <= K:
            if max < i + j + 2:
                max = i + j + 2

print(max)

Submission status: Wrong Answer

Solution code:
n,m,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
an = 0
bn = 0
num = 0
time = 0

while(True):
  if an == n and bn == m:
    break
  elif an == n:
    t = b[bn]
    bn += 1
  elif bn == m:
    t = a[an]
    an += 1
  elif a[an] <= b[bn]:
    t = a[an]
    an += 1
  else:
    t = b[bn]
    bn += 1
  
  if time + t > k:
    break
  else:
    time += t
    num += 1
    
print(num)

Submission status: Wrong Answer

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

a.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
print(c)

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

a.append(10 ** 10)
b.append(10 ** 10)
na = a.popleft()
nb = b.popleft()

cnt = 0
min = 0
while min <= k:
    if na == 10 ** 10 and nb == 10 ** 10:
        cnt += 1
        break
    if na < nb:
        cnt += 1
        min += na
        na = a.popleft()
    elif na > nb:
        cnt += 1
        min += nb
        nb = b.popleft()
    else:
        cnt += 2
        min += na
        min += nb
        if min > k:
            cnt -= 1
            break
        na = a.popleft()
        nb = b.popleft()

print(cnt-1)

Submission status: Wrong Answer

Solution code:
import sys

def dfs(a, b, k):
    global cnt
    seen[a][b] = True
    na = a + 1
    nb = b + 1
    if cnt < N + M:
        if na <= N and seen[na][b] is False and k >= book_a[na]:
            k -= book_a[na]
            cnt = max(cnt, na + b)
            dfs(na, b, k)
        if nb <= M and seen[a][nb] is False and k >= book_b[nb]:
            k -= book_b[nb]
            cnt = max(cnt, a + nb)
            dfs(a, nb, k)

sys.setrecursionlimit(1_000_000)
N, M, k = map(int, input().split())
book_a = [int(x) for x in input().split()]
book_b = [int(x) for x in input().split()]
book_a.insert(0, 0)
book_b.insert(0, 0)
cnt = 0
seen = [[False] * (M + 1) for _ in range(N + 1)]
dfs(0, 0, k)
print(cnt)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate as ia 
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = set(ia(A))
b = set(ia(B))
na = 0
nb = 0
ans = 0
for i in a:
  if i > K:
    break
  else:
    na += 1
    nb = 0
  for j in b:
    if i + j > K:
      break
    else:
      nb += 1
  ans = max(ans, na + nb)
if ans == 0:
  for i in b:
    if i <= K:
      ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
aes = list(map(int, input().split()))
bs = list(map(int, input().split()))
r_aes = [0]*n
for i in range(1,n):
    aes[i] += aes[i-1]
for i in range(1,m):
    bs[i] += bs[i-1]
cnt_a = 0
for i in range(n):
    if aes[i] > k:
        break
    cnt_a += 1
cnt_b = 0
res = cnt_a
#cnt = 5
while True:
    #print("cnt_a: "+str(cnt_a))
    #print("cnt_b: " + str(cnt_b))
    if cnt_b >= m or bs[cnt_b] > k:
        break
    if cnt_a >= 1 and k-aes[cnt_a-1]>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_a+cnt_b)
        continue
    if cnt_a < 1 and k>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_b)
        continue
    cnt_a -= 1
    if cnt_a < 1:
        break
    #if cnt<= 0:
        #break
print(res)

Submission status: Wrong Answer

Solution code:
import random
N, M, K=[int(i) for i in input().split(" ")]
a=[int(i) for i in input().split(" ")]
b=[int(i) for i in input().split(" ")]

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

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

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

Submission status: Wrong Answer

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

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

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

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

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

if k < sum:
    print(ans - 1)
else:
    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 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, k1):
    if a[-1] + b[-1] <= k1:
        return len(a) - 1 + len(b) - 1

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

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

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

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


print(Find(accum_a, accum_b, k))

Submission status: Wrong Answer

Solution code:
def ABC172_C():
    import sys
    sys.setrecursionlimit(4100000)

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

    minamo = []
    def saiki(i,j,sum,mx):
        if sum >+ k:
            mx = min(i+j,mx)
            return mx

        if i+j >= mx:
            return mx

        if i+1 <= n:
            mx = saiki(i+1,j,sum+a[i],mx)
        if j+1 <= m:
            mx = saiki(i,j+1,sum+b[j],mx)

        return mx

    mls = saiki(0,0,0,pow(10,5))
    print(mls)

ABC172_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_ruiseki = [0]
for i in range(n):
    if A_ruiseki[-1]+A[i] <= k :
        A_ruiseki.append(A_ruiseki[-1]+A[i])
    else:
        break

B_ruiseki = [0]
for i in range(m):
    if B_ruiseki[-1]+B[i] <= k :
        B_ruiseki.append(B_ruiseki[-1]+B[i])
    else:
        break

ans = len(A_ruiseki)-1

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i, 0)

    if len(B_ruiseki) <= expected:
        continue

    if time + B_ruiseki[expected] > k:
        continue

    if expected > 0:
        time += B_ruiseki[expected-1]

    for j in range(expected, len(B)):
        if time + B[j] > k:
            break

        time += B[j]
        ans =i+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()))
amax = len(a)
bmax = len(b)
res = 0
tim = 0

if sum(a)+sum(b) < k:
    res = amax + bmax
else:
    for _ in range(amax+bmax):
        if len(a) == 0:
            tim += b[0]
            b.pop(0)
        elif len(b) == 0:
            tim += a[0]
            a.pop(0)
        elif a[0] <= b[0]:
            tim += a[0]
            a.pop(0)
        else:
            tim += b[0]
            b.pop(0)
        res += 1
        if tim > k:
            res -= 1
            break
print(res)

Submission status: Wrong Answer

Solution code:


def main():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    counter = 0
    a_num = 0
    b_num = 0
    if (sum(aa)+sum(bb)) <= k:
        return n+m

    while time_sum <= k:
        if len(bb) == b_num and len(aa) == a_num:
            break
        if len(bb) == b_num:
            time_sum += aa[a_num]
            a_num += 1
        elif len(aa) == a_num:
            time_sum += bb[b_num]
            b_num += 1
        elif aa[a_num] > bb[b_num]:
            time_sum += bb[b_num]
            b_num += 1
        else:
            time_sum += aa[a_num]
            a_num += 1

        if time_sum > k:
            break
        counter += 1

    return counter


if __name__ == '__main__':
    ans = main()
    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()))

ia = 0
ib = 0
book = 0
minute = 0

while minute < k:
    if a[ia] > b[ib]:
        minute += b[ib]
        book += 1
        if minute > k:
            book -= 1
            break
        ib += 1
        if ib == m:
            ib = m - 1
            b[ib] = float('inf')
    else:
        minute += a[ia]
        book += 1
        if minute > k:
            book -= 1
            break
        ia += 1
        if ia == n:
            ia = n - 1
            a[ia] = float('inf')


print(book)

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()))
 
reading = 0
read_book = 0
i = 0
j = 0


while reading < K:
  
  if i+1 <= N and j+1 <= M:
    if A[i] <= B[j]:
      reading += A[i]
      i += 1
      if reading <= K:
        read_book += 1
    else:
      reading += B[j]
      j += 1
      if reading <= K:
        read_book += 1
  elif i+1 <= N and j+1 > M:
    reading += A[i]
    i += 1
    if reading <= K:
      read_book += 1
  elif i+1 > N and j+1 <= M:
    reading += B[j]
    j += 1
    if reading <= K:
      read_book += 1
  else:
    break
    


 
print(read_book)

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 t < K:
    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:
from collections import deque
n,m,k=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
time=0
ans=0
if sum(a)+sum(b)<=k:
    print(n+m)
    exit()
a=deque(a)
b=deque(b)
a0=a.popleft()
b0=b.popleft()
while True:
  if a0>=b0:
    time+=b0
    if time>k:
      print(ans)
      exit()
    else:
      ans+=1
      if not b:
        while True:
          time+=a0
          if time>k:
            print(ans)
            exit()
          else:
            ans+=1
            a0=a.popleft()
      else:
        b0=b.popleft()
  else:
    time+=a0
    if time>k:
      print(ans)
      exit()
    else:
      ans+=1
      if not a:
        while True:
          time+=b0
          if time>k:
            print(ans)
            exit()
          else:
            ans+=1
            b0=b.popleft()
      else:
        a0=a.popleft()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
nums=input().split()
a=int(nums[0])
b=int(nums[1])
n=int(nums[2])
lista=input().split()
listb=input().split()

acount=0
bcount=0
count=0
time=0
atime=0
btime=0
for i in lista:
  val=int(i)
  if val+atime>n:
    break
  atime+=val
  acount+=1

count=acount
btime=atime
for j in listb:
  valb=int(j)
  if valb+btime>n:
    break
  btime+=valb
  count+=1

for i in range(acount):
  bcount=acount-i
  btime-=int(lista[acount-i-1])
  time=btime
  for j in listb:
    valb=int(j)
    if valb+time>n:
      break
    time+=valb
    bcount+=1
  if count<bcount:
    count=bcount
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()))
i=j=0
ans=0
h=k
s1=sum(a)
s2=sum(b)
while k>=0:
    if i<len(a) and j<len(b):
        if a[i]<=b[j]:
            k=k-a[i]
            i=i+1
        elif a[i]>b[j]:
            k=k-b[j]
            j=j+1
    else:
        if i<len(a):
            k=k-a[i]
            i=i+1
        elif j<len(b):
            k=k-b[j]
            j=j+1
        else:
            break
    if k>=0:
        ans=ans+1
a.reverse()
b.reverse()
for i in a:
    if s1<=h:
        break
    else:
        s1=s1-i
for i in b:
    if s2<=h:
        break
    else:
        s2=s2-i
print(max(max(s1,s2),ans))

Submission status: Wrong Answer

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

ans = 1
c = [0]
d = [0]
for i in range(1,n+1):
    c.append(c[i-1] + a[i-1])
    if(c[i] > k):
        c.pop()
        break
for i in range(1,m+1):
    d.append(d[i-1] + b[i-1])
    if(d[i] > k):
        d.pop()
        break


leng = len(c)
lend = len(d)
ans = leng - 1
temp = 0
for i in range(leng):
    p = c[leng-i-1]
    t = temp
    while(t < lend and p+d[temp] <= k):
        t += 1
        if(t==lend):
            print(leng-i-2+t)
            sys.exit()
    ans = max(ans,leng-i-2+t)
    temp = t

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()))
#asum=sum(a)
b=list(map(int,input().split()))
#bsum=sum(b)
 
time=0
count=0
while time<=k:
  if len(a)==0 and len(b)==0:
    break
  elif len(a)==0:
    if time+b[0]>k:
      break
    time+=b.pop(0)
    count+=1
  elif len(b)==0:
    if time+a[0]>k:
      break
    time+=a.pop(0)
    count+=1
    
    
  else:
    if a[0]<b[0]:
      if time+a[0]>k:
        break
      time+=a.pop(0)
      count+=1
    elif a[0]>b[0]:
      if time+b[0]>k:
        break
      time+=b.pop(0)
      count+=1
    else:
      if sum(a)<=sum(b):
        if time+a[0]>k:
          break
        time+=a.pop(0)
        count+=1
      else:
        if time+b[0]>k:
          break
        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()))
t = 0
n = 0
a = 0
b = 0
 
while K > t :
    if a < N and b < M :
        if A[a] <= B[b] :
            t += A[a]
            n += 1
            a += 1
            if K < t :
                n -=1
                break
 
        else :
            t +=B[b]
            n += 1
            b += 1
            if K < t :
                n -=1
                break
    
    elif a == N :
        while b < M :
            t += B[b]
            n += 1
            b += 1
            if K < t :
                n -=1
                break
        
        else :
            break
 
    else :
        while a < N :
            t += A[a]
            n += 1
            a += 1
            if K < t :
                n -=1
                break
 
print(n)

Submission status: Wrong Answer

Solution code:
N, M, timelimit = map(int, input().split())

A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

book_count = 0
a_current = 0
b_current = 0
sum_time = 0

while True:
    if M <= b_current or (N > a_current and A[a_current] <= B[b_current]):
        if N<=a_current or timelimit < sum_time + A[a_current]:
            break
        else:
            sum_time += A[a_current]
            a_current += 1
            book_count += 1
    
    else:
        if M<= b_current or  timelimit < sum_time + B[b_current]:
            break
        else:
            sum_time += B[b_current]
            b_current += 1
            book_count += 1

print(book_count)

Submission status: Wrong Answer

Solution code:
#from collections import deque
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
A = A[::-1]
B = list(map(int, input().split()))

book = A + B
sum_a = sum(A)
sum_b = sum(B)

l = 0
r = len(A)
time = sum_a
ans = 0
#print(book)
while ((r < len(book))&(l <= len(A))):
    if time + book[r] <= K:
        time += book[r]
        ans = max(ans,len(book[l:r])+1)
        r += 1
    elif r == l:
        r += 1
        l += 1
    else:
        time -= book[l]
        l += 1

    #print(l, r)
    #print(ans)
    #print()
    


print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import *
def solve():
    ans = 0
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))+[float('inf')]
    B = list(map(int, input().split()))+[float('inf')]
    cumA = [0]*(N+1)
    cumB = [0]*(M+1)
    for i in range(N):
        cumA[i+1] = cumA[i]+A[i]
    for i in range(M):
        cumB[i+1] = cumB[i]+B[i]
    for i in range(N+1):
        rest = K-cumA[i]
        ind = bisect_right(cumB,rest)
        if ind>1:
            ans = max(ans,i+ind-1)
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = deque([int(i) for i in input().split()])
B = deque([int(i) for i in input().split()])
ans = 0
inf = 10**10
a = A.popleft()
b = B.popleft()

while K >= 0 or (A or B):
    if a < b:
        K -= a
        if K >= 0:
            ans += 1
        try:
            a = A.popleft()
        except:
            a = inf

    else:
        K -= b
        if K >= 0:
            ans += 1
        try:
            b = B.popleft()
        except:
            b = inf

print(ans)

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()]
s = 0
ans = 0
ca = 0
for i in range(n):
  if k >= s + a[i]:
    s += a[i]
    ans += 1
    ca += 1
  else:
    break
cb = 0
for i in range(m):
  if k >= s + b[i]:
    s += b[i]
    ans += 1
    cb += 1
  else:
    break
if ans == n + m or ans == 0:
  print(ans)
else:
  l = []
  l.append(ans)
  for i in range(m):
    if ca > 0:
      s - a[ca - 1]
      ca -= 1
      ans -= 1
    if k >= s + b[cb - 1]:
      s += b[cb - 1]
      cb += 1
      ans += 1
      l.append(ans)
    if cb >= m:
      break
  print(max(l))

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 k < a[0] and k < b[0]:
    print(0)
    exit()

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()

i = 0
j = 0
c = []

while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(len(c))
        exit()