Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Wrong Answer

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

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

if total > K:
    cnt -= 1
print(cnt)

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(float("INF"))
b.append(float("INF"))

ans = 0
na = nb = 0
while k >= 0:
    x = a[na]
    y = b[nb]
    if y >= x:
        if k >= x:
            ans += 1
            na += 1
            k -= x
        else:
            break
    else:
        if k >= y:
            ans += 1
            nb += 1
            k -= y
        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()))
time = 0
count = 0
while True:
    if len(A) == 0 and len(B)==0:
        break
    elif len(A) == 0:
        time += B[0]
        B.pop(0)
    elif len(B) == 0:
        time += A[0]
        A.pop(0)
    elif A[0] > B[0]:
        time += B[0]
        B.pop(0)
    else:
        time += A[0]
        A.pop(0)
    
    if time >K:
        break
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
def main():
    n, m, k = list(map(int, input().split()))
    INF = 1e+10
    A = [INF, ] + list(map(int, input().split()))[::-1]
    B = [INF, ] + list(map(int, input().split()))[::-1]
    a = A.pop()
    b = B.pop()
    s = 0
    ans = 0
    while s <= k:
        if a <= b:
            s += a
            a = A.pop()
        else:
            s += b
            b = B.pop()
        ans += 1
        if a == b == INF:
            break
    else:
        ans -= 1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
m,n,k=map(int,input().split())
l=list(map(int,input().split()))
l.extend(list(map(int,input().split())))
l.sort()
z=0
s=0
for i in range(m+n):
  z+=l[i]
  if z>k:
    break
  s+=1
print(s)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=list(accumulate(a))
B=list(accumulate(b))

ans=0
for i in range(n):
    if k-A[i]>=0:
        c = bisect.bisect_right(B, k-A[i])
        ans = max(i+c+1,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

t = 0
ia = 0
ib = 0
ans = 0

while t < k:
    if ia >= n and ib < m:
        ans += 1
        t += b[ib]
        ib+=1
        continue

    if ia < n and ib >= m:
        ans += 1
        t += a[ia]
        ia+=1
        continue

    if ia>=n and ib>=m:
        break

    if a[ia]<=b[ib]:
        ans += 1
        t += a[ia]
        ia+=1
        continue
    else:
        ans += 1
        t += b[ib]
        ib+=1
        continue

if t <= k:
    print(ans)
else:
    print(max(ans-1,0))

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
while k > 0:
    if a[0] <= b[0]:
        tmp = a[0]
        a.pop(0)
        a.append(10**10)
    else:
        tmp = b[0]
        b.pop(0)
        b.append(10**10)
    if k - tmp >= 0:
        ans += 1
    k -= tmp
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(' ')))

ans=0

while len(a)>0 and len(b)>0 and k>=min(a[0], b[0]):
    
    if a[0]>b[0]:
        k-=b.pop(0)
    else:
        k-=a.pop(0)
    ans+=1

if k>0:
    if len(b)>0:
        while len(b)>0 and k>=b[0]:
            k-=b.pop(0)
            ans+=1
    elif len(a)>0:
        while len(a)>0 and k>=a[0]:
            k-=a.pop(0)
            ans+=1
        

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
a=deque(a)
b=list(map(int,input().split()))
b=deque(b)
ans=0
while k>0 or a or b:
    if a and b:
        if a[0]>=b[0]:
            k-=b.popleft()
            if k>=0:
                ans+=1
        elif a[0]<b[0]:
            k-=a.popleft()
            if k>=0:
                ans+=1
    elif a:
        k-=a.popleft()
        if k>=0:
            ans+=1
    elif b:
        k-=b.popleft()
        if k>=0:
            ans+=1
print(ans)

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()))
S = [0]*N
T = [0]*M
S[0] = A[0]
for k in range(N):
    S[k] = S[k-1] + A[k]
T[0] = B[0]
for k in range(M):
    T[k] = T[k-1] + B[k]

ans = bisect.bisect_right(T,K)
for k in range(N):
    if K-S[k] >= 0:
        ans = max(ans,k+1+bisect.bisect_right(T,K-S[k]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


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

    ans = 0
    i_A = 0
    i_B = 0
    time = 0
    while True:
        if i_A == N:
            for i in range(i_B, M):
                time += B[i]
                if time <= K:
                    ans += 1
                else:
                    break
            break
        if i_B == M:
            for i in range(i_A, N):
                time += A[i]
                if time <= K:
                    ans += 1
                else:
                    break
            break

        if A[i_A] < B[i_B]:
            time += A[i_A]
            i_A += 1
        else:
            time += B[i_B]
            i_B += 1

        if time <= K:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
A.sort()
B.sort()
ina = 0
inb = 0
T = K
while ina != -1 or inb != -1:
    if ina != -1 and inb != -1:
        if A[ina] < B[inb]:
            if T > A[ina]:
                T -= A[ina]
                ans += 1
                ina += 1
                if ina == N:
                    ina = -1
            else:
                break
        elif A[ina] >= B[inb]:
            if T > B[inb]:
                T -= B[inb]
                ans += 1
                inb += 1
                if inb == M:
                    inb = -1
            else:
                break
    elif ina == -1:
        if T > B[inb]:
            ans += 1
            inb += 1
            if inb == M:
                inb = -1
    else:
        if T > A[ina]:
            ans += 1
            ina += 1
            if ina == N :
                ina = -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()))
time = 0
cnt = 0
ai = 0
bi = 0
while time<k:
  if ai<len(a) and bi<len(b):
    if a[ai]<b[bi]:
      time += a[ai]
      ai += 1
      cnt += 1
    else:
      time += b[bi]
      bi += 1
      cnt += 1
  elif ai<len(a) and bi>=len(b):
    time += a[ai]
    ai += 1
    cnt += 1
  elif ai>=len(a) and bi< len(b):
    time += b[bi]
    bi += 1
    cnt += 1
  else:
    break
if time>k:
  print(cnt-1)
else:
  print(cnt)

Submission status: Wrong Answer

Solution code:

def main():
    N_A, M_B, K = map(int, input().split())

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

    C = A + B
    C.sort()

    ans = 0
    summ = 0

    for i in range(N_A + M_B):
        summ += C[i]
        
        if (summ <= K):
            ans += 1
        else:
            break

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200000)


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


def solve(N, M, K, A, B, INF=10**9+1):
    A += [INF]
    B += [INF]
    i = 0
    j = 0
    vsum = 0
    ans = 0
    while True:
        v = INF
        if A[i] < B[j]:
            v = A[i]
            i += 1
        else:
            v = B[j]
            j += 1
        if v == INF:
            return ans
        if vsum + v > K:
            return ans
        vsum += v
        ans += 1
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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)
book = 0
flag = True
while flag:
    flag = False
    if A[0] <= B[0] and K >= A[0]:
        K -= A[0]
        book += 1
        A.pop(0)
        flag = True
    elif B[0] < A[0] and K >= B[0]:
        K -= B[0]
        book += 1
        B.pop(0)
        flag = True
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()))
num=0
for i in range(n+m):
    if len(b)!=0:
        if len(a)!=0:
            if a[0]>b[0]:
                num+=b[0]
                del b[0]
            else:
                num+=a[0]
                del a[0]
        else:
            num+=b[0]
            del b[0]
    if len(b)==0:
        if len(a)==0:
            ans=n+m
            break
        else:
            num+=a[0]
            del a[0]
    if num>k:
        ans=i
        break
if num<=k:
    ans=n+m
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

#DIV = (10**9) + 7
DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    N,M,K = isRaw()
    As = isRaw()
    Bs = isRaw()
    ai = 0
    bi = 0
    ans = 0
    c = 0
    while c < K:
        if ai!=len(As) and (bi==len(Bs) or As[ai]<=Bs[bi]):
            c += As[ai]
            if c>K:
                break
            ai+=1
        elif bi!=len(Bs):
            c += Bs[bi]
            if c>K:
                break
            bi+=1
        else:
            break
    print(ai+bi)
            



main()

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

dq_A = deque(A)
dq_B = deque(B)

cnt = 0
while K > 0:
    if len(dq_A) > 0 and len(dq_B) > 0:    
        if dq_A[0] > dq_B[0]:
            tmp = dq_B.popleft()
            K -= tmp
            cnt += 1
        else:
            tmp = dq_A.popleft()
            K -= tmp
            cnt += 1
    elif len(dq_A) > 0:
        tmp = dq_A.popleft()
        K -= tmp
        cnt += 1
    elif len(dq_B) > 0:
        tmp = dq_B.popleft()
        K -= tmp
        cnt += 1

if K == 0:
    print(cnt)
else:
    print(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()))
# k分以内
ans=a+b
ans.sort()
i=0
kotae=0
while True:
    if ans[i]+kotae>k:
        break
    else:
        kotae+=ans[i]
    i+=1
    if i>=len(ans):
        print(len(ans))
        quit()
print(i)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def solve():
      input = sys.stdin.readline
      N, M, K = map(int, input().split())
      A = [int(a) for a in input().split()]
      B = [int(b) for b in input().split()]
      ad, bd = deque(), deque()
      for a in A: ad.append(a)
      for b in B: bd.append(b)

      count = 0
      for i in range(N + M):
            ah, bh = 10 ** 10, 10 ** 10
            if ad: ah = ad.popleft()
            if bd: bh = bd.popleft()

            if ah > K and bh > K: break
            count += 1
            if ah <= bh:
                  K -= ah
                  if bh < 10 ** 10: bd.appendleft(bh)
            else:
                  K -= bh
                  if ah < 10 ** 10: ad.appendleft(ah)
      print(count)

      return 0

if __name__ == "__main__":
      solve()

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()))
asu=[]
s=0
for i in a:
  s+=i
  asu.append(s)
bsu=[]
s=0
for i in b:
  s+=i
  bsu.append(s)
  
ac=n-1
bc=0
maxc=0

while (bc!=m)and(ac>=0):
  while (bc!=m) and (asu[ac]+bsu[bc]<=k):
    bc+=1
  maxc=max(maxc,ac+bc+1)
  ac-=1
print(maxc)

Submission status: Wrong Answer

Solution code:
production = True

import sys, math, collections

def input(input_format = 0, multi = 0):

    if multi > 0: return [input(input_format) for i in range(multi)]
    else:
        next_line = sys.stdin.readline()[:-1]

        if input_format >= 10:
            use_list = False
            input_format = int(str(input_format)[-1])
        else: use_list = True

        if input_format == 0: formatted_input = [next_line]
        elif input_format == 1: formatted_input = list(map(int, next_line.split()))
        elif input_format == 2: formatted_input = list(map(float, next_line.split()))
        elif input_format == 3: formatted_input = list(next_line)
        elif input_format == 4: formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
        elif input_format == 5: formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
        elif input_format == 6: formatted_input = next_line.split()
        else: formatted_input = [next_line]

        return formatted_input if use_list else formatted_input[0]


def out(output_line, output_format = 0, newline = True):

    formatted_output = ""

    if output_format == 0: formatted_output = str(output_line)
    elif output_format == 1: formatted_output = " ".join(map(str, output_line))
    elif output_format == 2: formatted_output = "\n".join(map(str, output_line))

    print(formatted_output, end = "\n" if newline else "")


def log(*args):
    if not production:
        print("$$$", end = "")
        print(*args)

enum = enumerate

#
#   >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#


def solve():

    n, m, k = input(1)
    a = input(1)
    b = input(1)

    a.reverse()
    b.reverse()

    c = -1

    while k >= 0:
        c += 1
        if a and b:
            if a[-1] < b[-1]:
                n = a.pop()
            else:
                n = b.pop()
        elif a:
            n = a.pop()
        elif b:
            n = b.pop()
        else:
            break

        k -= n

    out(c)

    return


#for i in range(input(11)): solve()
solve()

#
#   >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#

Submission status: Wrong Answer

Solution code:
a,b,k=map(int,input().split())
s=list(map(int,input().split()))
z=list(map(int,input().split()))
res=0

while len(s)!=0 and len(z)!=0 and k>0:
    if s[-1]<z[-1] and s[-1]<=k:
        res+=1
        k-=s[-1]
        s.pop()
    elif z[-1]<s[-1] and z[-1]<=k:
        res+=1
        k-=z[-1]
        z.pop()
    else:
        break

while len(s)!=0 and k>0:
    if s[-1]<=k:
        k-=s[-1]
        res+=1
        s.pop()
    else:
        break
    

while len(z)!=0 and k>0:
    if z[-1]<=k:
        k-=z[-1]
        res+=1
        z.pop()
    else:
        break

print(res)

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()))
MAX = 10**9+1
time = k
res = 0
a_idx = 0
b_idx = 0
while k > 0 and (a_idx < n or b_idx < m):
    next_a = MAX
    next_b = MAX
    if a_idx < n:
        next_a = a[a_idx]
    if b_idx < m:
        next_b = b[b_idx]
    if next_a < next_b:
        if k - next_a < 0:
            break
        a_idx += 1
        k -= next_a
    else:
        if k - next_b < 0:
            break
        b_idx += 1
        k -= next_b
print(a_idx+b_idx)

Submission status: Wrong Answer

Solution code:
from  collections import deque

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

A = deque(A)
B = deque(B)

ans = 0
while True:
  if A:
    a = A[0]
  else:
    a = 10**9+7
  if B:
    b = B[0]
  else:
    b = 10**9+7
  if min(a, b) > K:
    break
  else:
    if a <  b:
      a = A.popleft()
      K -= a
      ans += 1
    else:
      b = B.popleft()
      K -= b
      ans += 1
  #print(ans,  K)
      
      
print(ans)

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()))
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:
        ans = max(ans,i+1 + bisect.bisect_right(b,k-a[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]
ans = 0
curr_T = 0
while True:
    next_dT = 0
    if not A and not B:
        break
    elif not A:
        next_dT = B.pop()
    elif not B:
        next_dT = A.pop()
    else:
        if A[-1] < B[-1]:
            next_dT = A.pop()
        else:
            next_dT = B.pop()
    if curr_T + next_dT <= K:
        curr_T += next_dT
        ans += 1
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    i, j = (0, 0)

    ans = 0
    cum = 0

    while True:
        if i < N and j < M:
            if A[i] <= B[j]:
                if cum + A[i] <= K:
                    cum += A[i]
                    i += 1
                    ans += 1
                else:
                    break
            else:
                if cum + B[j] <= K:
                    cum += B[j]
                    j += 1
                    ans += 1
                else:
                    break
        elif i < N:
            if cum + A[i] <= K:
                cum += A[i]
                i += 1
                ans += 1
            else:
                break
        elif j < M:
            if cum + B[j] <= K:
                cum += B[j]
                j += 1
                ans += 1
            else:
                break
        else:
            break

    print(ans)

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

A = A[::-1]
B = B[::-1]

cnt = 0
while K >= 0:
    if A and B:
        a = A.pop()
        b = B.pop()
        if a <= b:
            K -= a
            cnt += 1
            B.append(b)
        else:
            K -= b
            cnt += 1
            A.append(a)
    elif A:
        a = A.pop()
        K -= a
        cnt += 1
    elif B:
        b = B.pop()
        K -= b
        cnt += 1
    else:
        cnt += 1
        break

print(cnt - 1)

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()))
    indb = M-1
    now = sum(B)
    count = M
    while now > K:
        now -= B[indb]
        count -= 1
        indb -= 1

    ans = count
    for a in A:
        # print(ans, now)
        now += a
        count += 1
        while now > K and indb > 0:
            now -= B[indb]
            count -= 1
            indb -= 1
        if now <= K:
            if ans < count:
                ans = count
    print(ans)
if __name__ == '__main__':
    main()

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:
        a = A.popleft() if len(A) > 0 else INF
        b = B.popleft() if len(B) > 0 else INF

        if a < b:
            k += a
            B.appendleft(b)
        else:
            k += b
            A.appendleft(a)

        if k <= K:
            ans += 1

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    N, M, K = map(int, input().split())

    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    count = 0
    total = 0

    while total <= K:

        if len(As) + len(Bs) == 0:
            count += 1
            break
        elif len(Bs) == 0:
            to_read = As.pop(0)
        elif len(As) == 0:
            to_read = Bs.pop(0)
        elif As[0] <= Bs[0]:
            to_read = As.pop(0)
        else:
            to_read = Bs.pop(0)

        total += to_read
        count += 1

    return count - 1


def main():
    print(solve())


if __name__ == "__main__":
    main()

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(10**9+7)
B.append(10**9+7)
ans = 0

while True:
    if A[0] > B[0]:
        K = K- B[0]
        B = B[1:]
        ans +=1
    else:
        K = K-A[0]
        A = A[1:]
        ans +=1
    if K <0:
        break
print(ans-1)

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_cum = [0 for _ in range(n+1)]
b_cum = [0 for _ in range(m+1)]

for i in range(n):
    a_cum[i+1] = a_cum[i] + a[i]
for i in range(m):
    b_cum[i+1] = b_cum[i] + b[i]

ans = 0
for i in range(n+1):
    if a_cum[i] >= k:
        break
    x = bisect.bisect_right(b_cum, k - a_cum[i])
    ans = max(ans, i + x - 1)

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
A_ind = N - 1
B_ind = M - 1
ans = 0
time = K
while True:
    if A_ind < 0:
        if time < B[B_ind]:
            break
        else:
            ans += 1
            time -= B[B_ind]
            B_ind -=1
    elif B_ind < 0:
        if time < A[A_ind]:
            break
        else:
            ans += 1
            time -= A[A_ind]
            A_ind -=1
    else:
        if A[A_ind] < B[B_ind]:
            if time < A[A_ind]:
                break
            else:
                ans += 1
                time -= A[A_ind]
                A_ind -=1
        else:
            if time < B[B_ind]:
                break
            else:
                ans += 1
                time -= B[B_ind]
                B_ind -=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()))
i=j=0
ans=0
while i<n and j<m and k>0 and (k>=a[i] or k>=b[j]):
  if(a[i]<=b[j]):
    k-=a[i]
    i+=1
  else:
    k-=b[j]
    j+=1
  ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n,m,k = tuple([int(t)for t in input().split()])

    a = [int(t)for t in input().split()][::-1]
    b = [int(t)for t in input().split()][::-1]

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

    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()))
book = sorted(A + B)
#print(book)
count = 0
ans = 0
for bo in book:
    if count + bo <= K:
        #print(count)
        count += bo
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M,K=list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
a = deque(a_list)
b = deque(b_list)

book = 0
k = 0
while len(a) > 0 or len(b) > 0:
  if len(a) == 0:
    k += b.popleft()
  elif len(b) == 0:
    k += a.popleft()
  elif a[0]<b[0]:
    k += a.popleft()
  else:
    k += b.popleft()
  if k<=K:
    book+=1
  else:
    break
print(book)