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:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    c=0
    ans=n+m
    for i in range(n+m):
        if len(a)==0:
            c+=b[0]
            b.pop(0)
        elif len(b)==0:
            c+=a[0]
            a.pop(0)
        elif a[0]<=b[0]:
            c+=a[0]
            a.pop(0)
        else:
            c+=b[0]
            b.pop(0)
        if c>k:
            ans=i
            break
    print(ans)


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    main()

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=[0]
B=[0]
for i in range(n):
    A.append(A[-1]+a[i])
for i in range(m):
    B.append(B[-1]+b[i])
ans=0
for i in range(n+1):
    index=bisect.bisect_right(B,k-A[i])-1
    index=max(0,index)
    ans=max(ans,i+index)
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()))
A=[0]
B=[0]
for i in range(n):
    A.append(A[-1]+a[i])
for i in range(m):
    B.append(B[-1]+b[i])
ans=0
for i in range(n+1):
    index=bisect.bisect_right(B,k-A[i])-1
    ans=max(ans,i+index)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K =[int(i) for i in input().split()]
An = [int(i) for i in input().split()]
Bn = [int(i) for i in input().split()]

qa = deque(An[::-1])
qb = deque(Bn[::-1])

max_val = 10**9
ans = 0
tmp_A = []
tmp_B = []

# 読める本がなくなるか、Kが0以下になるまでずっと本を読む
while True:
    # no time
    if(K < 0):
        break
    # AかBどっちかの本を読む
    if(len(qa)==0 and len(qb)==0):
        break
    # Aが空の場合
    elif(len(qa) == 0):
        tmp_B.append(qb.pop())
        for i in tmp_B:
            K -= tmp_B[0]
            if(K < 0):
                break
            ans += 1
        tmp_B = []
        continue
    # Bが空の場合
    elif(len(qb) == 0):
        tmp_A.append(qa.pop())
        for i in tmp_A:
            K -= tmp_A[0]
            if(K < 0):

                break
            ans += 1
        tmp_A = []
        continue
    # それ以外
    else:
        tmp_A.append(qa.pop())
        tmp_B.append(qb.pop())
    # 以降でtmp内の比較
    # Aのほうが必要な時間が短い場合
    if(sum(tmp_A) < sum(tmp_B)):
        # Bに本を戻す
        for _ in tmp_B:
            qb.append(_)
        tmp_B = []
        # 1冊ずつ
        for i in tmp_A:
            K -= tmp_A[0]
            if(K < 0):
                tmp_A = []
                break
            ans += 1
        tmp_A = []
    # Bのほうが必要な時間が短い場合
    elif(sum(tmp_A) > sum(tmp_B)):
        # Aに本を戻す
        for _ in tmp_A:
            qa.append(_)
        tmp_A = []
        # 1冊ずつ
        for i in tmp_B:
            K -= tmp_B[0]
            if(K < 0):
                tmp_B = []
                break
            ans += 1
        tmp_B = []
    else:
        continue


print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a = deque(A)
b = deque(B)

cnt = 0
book = 0
while cnt < K:
    if(cnt >= K):
        break    
    if(len(a)):
        aa = a.popleft()
    if(len(b)):
        bb = b.popleft()
    
    if(len(a) < 1):
        if(cnt+bb <= K):
            cnt += bb
            book += 1
            continue
        else:
            break
    elif(len(b) < 1):
        if(cnt+aa <= K):
            cnt += aa
            book += 1
            continue
        else:
            break
    
    if(aa >= bb and cnt+bb <= K):
        cnt += bb
        a.appendleft(aa)
    elif(aa < bb and cnt+aa <= K):
        cnt += aa
        b.appendleft(bb)
    book += 1
    
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()))

ans=0

totalofa=[]
for j in range(N+1):
    tt=sum(A[i] for i in range(j))
    if tt<=K:
        totalofa.append(tt)
maxofa=len(totalofa)

totalofb=[]
for j in range(M+1):
    tt2=sum(B[i] for i in range(j))
    if tt2<=K:
        totalofb.append(tt2)
maxofb=len(totalofb)

ans=max(maxofa,maxofb)

for i in range(maxofa): 
    temp=totalofa[i]
    for j in range(maxofb):
        ss=temp+totalofb[j]
        if ss>K:
            ans=max(ans,j+i-1)
            break

if totalofa[maxofa-1]+totalofb[maxofb-1]<=K:

    ans=maxofa+maxofb-2

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a = deque(A)
b = deque(B)

cnt = 0
book = 0
while cnt < K:
    if(cnt >= K):
        break    
    if(len(a)):
        aa = a.popleft()
    if(len(b)):
        bb = b.popleft()
    
    if(len(a) < 1):
        if(cnt+bb <= K):
            cnt += bb
            book += 1
            continue
        else:
            break
    elif(len(b) < 1):
        if(cnt+aa <= K):
            cnt += aa
            book += 1
            continue
        else:
            break
    
    if(aa >= bb and cnt+bb <= K):
        cnt += bb
        a.appendleft(aa)
    elif(aa < bb and cnt+aa <= K):
        cnt += aa
        b.appendleft(bb)
    else:
        break
    book += 1
    
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()))
t=sum(b)
j=m
ans=0
for i in range(n):
  t+=a[i]
  while j>0 and t>k:
    t-=b[j-1]
    j-=1
  if t>k:
    break
  if j+i+1>ans:
    ans=j+i+1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a = deque(A)
b = deque(B)

cnt = 0
book = 0
while cnt <= K:
    if(cnt >= K):
        break    
    if(len(a)):
        aa = a.popleft()
    if(len(b)):
        bb = b.popleft()
    
    if(len(a) < 1):
        if(cnt+bb <= K):
            cnt += bb
            book += 1
            continue
    elif(len(b) < 1):
        if(cnt+aa <= K):
            cnt += aa
            book += 1
            continue
    
    if(aa >= bb and cnt+bb <= K):
        cnt += bb
        a.appendleft(aa)
    elif(aa < bb and cnt+aa <= K):
        cnt += aa
        b.appendleft(bb)
    else:
        break
    book += 1
    
print(book)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect
    from itertools import accumulate

    import sys
    input = sys.stdin.readline

    n, m, k = map(int, input().split())
    a = list(accumulate([0] + list(map(int, input().split()))))
    b = list(accumulate([0] + list(map(int, input().split()))))

    i1 = bisect(a, k) - 1
    j1 = bisect(b, max(0, k - a[i1])) - 1
    i2 = bisect(b, k) - 1
    j2 = bisect(a, max(0, k - b[i2])) - 1

    print(max(i1 + j1, i2 + j2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    l=[]
    while len(a) or len(b):
        if len(a) and len(b):
            l.append(min(a[0],b[0]))
            if min(a[0],b[0])==a[0]:
                a=a[1:]
            else:
                b=b[1:]
        elif len(a):
            l.append(a[0])
            a=a[1:]
        else:
            l.append(b[0])
            b=b[1:]
    #print(*l)
    c=0
    ans=n+m
    for i in range(n+m):
        c+=l[i]
        if c>k:
            ans=i
            break
    print(ans)


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    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()))

aa=0
bb=0

c=len(a)
d=len(b)
books = [0]*(n+m)

for i in range(n+m):
    if aa<c and bb<d and a[aa]<b[bb]:
        books[i]=a[aa]
        aa+=1
    elif aa<c and bb<d and a[aa]>b[bb]:
        books[i]=b[bb]
        bb+=1
    elif aa<c:
        books[i]=a[aa]
        aa+=1
    elif bb<d:
        books[i]=b[bb]
        bb+=1

ans=0
read=0

for i in books:
    read+=i
    if read>k:
        break
    else:
        ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    l=[]
    while len(a) or len(b):
        if len(a) and len(b):
            l.append(min(a[0],b[0]))
            if min(a[0],b[0])==a[0]:
                a.pop(0)
            else:
                b.pop(0)
        elif len(a):
            l.append(a[0])
            a.pop(0)
        else:
            l.append(b[0])
            b.pop(0)
    #print(*l)
    c=0
    ans=n+m
    for i in range(n+m):
        c+=l[i]
        if c>k:
            ans=i
            break
    print(ans)


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    main()

Submission status: Wrong Answer

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    l=[]
    for i in range(min(n,m)):
        if a[i]<=b[i]:
            l.append(a[i])
            l.append(b[i])
        else:
            l.append(b[i])
            l.append(a[i])
    x=abs(len(a)-len(b))
    if len(a)>len(b):
        l.extend(a[min(n,m):])
    else:
        l.extend(b[min(n,m):])
    c=0 
    ans=n+m
    #print(*l)
    for i in range(n+m):
        c+=l[i]
        if c>k:
            ans=i
            break
    print(ans)


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    main()

Submission status: Wrong Answer

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    l=[]
    for i in range(min(n,m)):
        l.append(a[i])
        l.append(b[i])
    x=abs(len(a)-len(b))
    if len(a)>len(b):
        l.extend(a[min(n,m):])
    else:
        l.extend(b[min(n,m):])
    c=0 
    ans=n+m
    #print(*l)
    for i in range(n+m):
        c+=l[i]
        if c>k:
            ans=i
            break
    print(ans)


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    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()))
if sum(A)+sum(B)<=K:
    print(len(A)+len(B))
else:
    ans=0
    count=0
    A.reverse()
    B.reverse()
    while count<K and len(A)!=0 and len(B)!=0:
        m=min(A[0],B[0])
        count+=m
        if m==A[0]:
            A.popleft()
        else:
            B.popleft()
        ans+=1
        if count>K:
            ans-=1
    if count<K and len(A)==0:
        while count<K:
            m=B[0]
            count+=m
            B.popleft()
            ans+=1
    if count<K and len(B)==0:
        while count<K:
            m=A[0]
            count+=m
            A.popleft()
            ans+=1
    print(ans)