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

tmp = 0
ans = 0
idx = 0
for i in range(N):
    if tmp + A[i] > K:
        break
    tmp += A[i]
    ans += 1
    idx += 1

idx -= 1
max_ans = ans

for i in range(M):
    if tmp + B[i] <= K:
        ans += 1
        max_ans = max(ans, max_ans)
        tmp += B[i]
        continue
    else:
        if idx >= 0:
            tmp -= A[idx]
            ans -= 1
        if tmp + B[i] <= K:
            ans += 1
            tmp += B[i]
            max_ans = max(ans, max_ans)
        else:
            break

print(max_ans)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
from functools import lru_cache
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


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

    if sum(A) + sum(B) <= K:
        print(N + M)
        return

    SB = [0] * (M + 1)
    for i in range(M):
        SB[i + 1] = SB[i] + B[i]
    res = V(max, 0)
    for i in range(N):
        j = bisect.bisect_right(SB, K) - 1
        res.ud(j + i)
        K -= A[i]
        if K < 0:
            break
    print(res)


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

numA = 0
numB = 0
count = 0
time = 0
for i in range(N+M):
  if numA < N and numB < M:
    if A[numA] > B[numB] and time + B[numB] <= K:
      count += 1
      time += B[numB]
      numB += 1
    elif A[numA] <= B[numB] and time + A[numA] <= K:
      count += 1
      time += A[numA]
      numA += 1
  elif numA >= N and numB < M and time + B[numB] <= K:
    count += 1
    time += B[numB]
    numB += 1
  elif numB >= M and numA < N and time + A[numA] <= K:
    count += 1
    time += A[numA]
    numA += 1
  else:
    break
print(count)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m,k = readInts()
A = readInts()
B = readInts()
dA = deque(A)
dB = deque(B)
ks = 0
cnt = 0
while ks < k:
    if len(dA) == 0 and len(dB) == 0:
        break
    if len(dA) != 0 and len(dB) != 0:
        a =  dA.popleft()
        b =  dB.popleft()
        if a < b:
            ks += a
            if ks > k:
                break
            cnt += 1
            dB.appendleft(b)
        else:
            ks += b
            if ks > k:
                break
            cnt += 1
            dA.appendleft(a)
    elif len(dA) != 0 and len(dB) == 0:
        a = dA.popleft()
        ks += a
        if ks > k:
            break
        cnt += 1
    elif len(dB) != 0 and len(dA) == 0:
        b = dB.popleft()
        ks += b
        if ks > k:
            break
        cnt += 1
    #print(ks)
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()))

t = 0
n = 0

while True:
    la = len(A)
    lb = len(B)
    if la != 0 and lb != 0:
        if A[0] < B[0]:
            t+= A.pop(0)
            n+=1
        else:
            t += B.pop(0)
            n+=1
    elif la != 0:
        t+=A.pop(0)
        n+=1
    elif lb != 0:
        t+=B.pop(0)
        n+=1
    else:
        break

    if t>K:
        n-=1
        break

print(n)

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
cur = 0
len_A = len(A)
len_B = len(B)
i = 0
j = 0
while True:
  a_i = A[i] if i < len_A else 1e10
  b_j = B[j] if j < len_B else 1e10
  if a_i < b_j:
    cur += a_i
    i += 1
  else:
    cur += b_j
    j += 1
  if cur > K:
    break
  total += 1
  if i >= len_A and j >= len_B:
    break
    
print(total)

Submission status: Wrong Answer

Solution code:

def resolve():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    A += list(map(int, input().split()))
    A.sort(reverse=True)
    ans = 0
    time = 0
    while A:
        a = A.pop()
        if time+a > K:
            break
        time += a
        ans += 1
    print(ans)

if __name__ == "__main__":
    resolve()

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

ind_A = 0
ind_B = 0
ans = 0
time = 0

while time <= K:
    if ind_A >= N and ind_B >= M:
        ans += 1
        break

    if (ind_B >= M and ind_A < N): # Aの本を読む
        time += A[ind_A]
        ind_A += 1
        ans += 1
    elif (ind_A >= N and ind_B < M) : # Bの本を読む
        time += B[ind_B]
        ind_B += 1
        ans += 1
    elif  A[ind_A] <= B[ind_B]: #Aの本
        time += A[ind_A]
        ind_A += 1
        ans += 1
    elif A[ind_A] > B[ind_B]:
        time += B[ind_B]
        ind_B += 1
        ans += 1
    

print(ans - 1)

Submission status: Wrong Answer

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

S = 0
a_idx = 0
for a in A:
    if S + a > K:
        break
    S += a
    a_idx += 1

b_idx = 0
for b in B:
    if S + b > K:
        break
    S += b
    b_idx += 1

ans = a_idx + b_idx
while a_idx >= 0:
    a_idx -= 1
    while b_idx < M and S + B[b_idx] <= K:
        S += B[b_idx]
        b_idx += 1
    ans = max(ans, a_idx + b_idx)
print(ans)

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()))
time = 0
ans = 0

def judge(data):
    if len(data) == 0:
        return 10 ** 10
    return data[0]
while time + min(judge(a), judge(b)) <= k:
    ans += 1
    if judge(a) < judge(b):
        time += a[0]
        a.popleft()
    else:
        time += b[0]
        b.popleft()
    if len(a) == len(b) == 0:
        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()))


maxNum = 0
cnt = 0


for i in a:
    if k - i > 0:
        k -= i
        cnt += 1
    else:
        break

    for i in b:
        if k - i > 0:
            k -= i
            cnt += 1
        else:
            break
    
    maxNum = max(maxNum, cnt)


print(maxNum)

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

k2 = 0
a_c = 0
b_c = 0
t_c = 0
while True:
    if a_c == len(a):
        if b_c == len(b):
            break
        else:
            k2 += b[b_c]
            if k2 > k:
                break
            else:
                b_c += 1
                t_c += 1
            continue
    elif b_c == len(b):
        k2 += a[a_c]
        if k2 > k:
            break
        else:
            a_c += 1
            t_c += 1
        continue

    if a[a_c] <= b[b_c]:
        k2 += a[a_c]
        if k2 > k:
            break
        else:
            a_c += 1
            t_c += 1
    else:
        k2 += b[b_c]
        if k2 > k:
            break
        else:
            b_c += 1
            t_c += 1
print(t_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()))
C = A + B
C.sort()
S = 0
ctr = 0
for i in C:
  if S + i <= K:
    S += i
    ctr += 1
  else:
    break
print(ctr)

Submission status: Wrong Answer

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


ia = 0
ib = 0
count = 0
bCount = 0
while count <= k:
    if ia < len(listA) and ib < len(listB):
        if listA[ia] <= listB[ib]:
            if count + listA[ia] <= k:
                count += listA[ia]
                bCount += 1
                ia+=1
            else:
                break
        else:
            if count + listB[ib] <= k:
                count += listB[ib]
                bCount += 1
                ib += 1
            else:
                break
    elif ia < len(listA):
        if count + listA[ia] <= k:
            count += listA[ia]
            bCount += 1
            ia += 1
        else:
            break
    elif ib < len(listB):
        if count + listB[ib] <= k:
            count += listB[ib]
            bCount += 1
            ib += 1
        else:
            break
    else:
        break
print(bCount)

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(0)
b.append(0)
cnt=0
ans=0
flag=True
c_a=0
c_b=0
for i in range(n+m):
  if c_b==m or a[c_a]<=b[c_b]:
    cnt+=a[c_a]
    if c_a==n:
      c_a=n
    else:
      c_a+=1
  elif c_a==n or a[c_a]>b[c_b]:
    cnt+=b[c_b]
    if c_b==m:
      c_b=m
    else:
      c_b+=1
  if cnt>k:
    print(ans)
    flag=False
    break
  else:
    ans+=1
if flag:
  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()))
A.append(10**9+1)
A.append(10**9+1)
B.append(10**9+1)
B.append(10**9+1)
count=0
time=0
a_current=0
b_current=0
while time<=K:
    if A[a_current]<=B[b_current]:
        time+=A[a_current]
        a_current=a_current+1
    else:
      time+=B[b_current]
      b_current+=1
    count+=1
print(count-1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect
n,k,m = map(int,readline().split())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]

aa = [0]*(n+1)
bb = [0]*(k+1)
for i in range(n):
  aa[i+1] = aa[i]+a[i]
for i in range(k):
  bb[i+1] = bb[i]+b[i]
  
ans = 0
for num,i in enumerate(bb):
  M = m-i
  ind = bisect(aa,M)
  ans = max(ans,num+ind-1)
  
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A.append(1000000001)
B.append(1000000001)
a=0
b=0
count=0
time=0
while True:
    if A[a]<B[b]:
        time+=A[a]
        a+=1
        if time>K:
            break
        count+=1
    else:
        time+=B[b]
        b+=1
        if time>K:
            break
        count+=1

print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
#from heapq import heappop, heappush
#from collections import OrderedDict, defaultdict
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
from collections import deque
#import numba

def run():
    N, M, K = map(int, sysread().split())
    A = deque(list(map(int, sysread().split())))
    B = deque(list(map(int, sysread().split())))


    ret = 0
    time = 0
    INF = 1 << 50
    while True:
        if A and B:
            if A[0] > B[0]:
                val = B.popleft()
                time += val
                if time > K:
                    print(ret)
                    return None
                ret += 1
            else:
                val = A.popleft()
                time += val
                if time > K:
                    print(ret)
                    return None
                ret += 1
        elif A:
            val = A.popleft()
            time += val
            if time > K:
                print(ret)
                return
            ret += 1
        elif B:
            val = B.popleft()
            time += val
            if time > K:
                print(ret)
                return None
            ret += 1
        else:
            print(ret)
            return None


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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


As = list(accumulate(A))
Bs = list(accumulate(B))

ans = 0
idx = M-1
for i in range(N):    
    while idx != -1 and As[i] + Bs[idx] > K:
        idx -= 1
    num = i + idx + 2
    if A[i] > K:  
        num = 0
    ans = max(ans, num)

if Bs[M-1] <= K:
    ans = max(ans, M)
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()))
 
pa, pb = 0, 0
 
ans = 0
fa, fb = False, False
while 1:
  if pa >= N and pb < M:
    if K - B[pb] >= 0:
      ans += 1
      pb += 1
    else:
      break
  elif pb >= M and pa < N:
    if K - A[pa] >= 0:
      ans += 1
      pa += 1
    else:
      break
  elif pa >= N and pb >= M:
    break
  elif A[pa] < B[pb]:
    if K - A[pa] >= 0:
      ans += 1
      pa += 1
    else:
      break
  else:
    if K - B[pb] >= 0:
      ans += 1
      pb += 1
    else:
      break
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
i=0
j=0
check=0
suma=0
count=0
while(i<N and j<M):
  if(A[i]<B[j]):
    temp=suma+A[i]
    if(temp>K):
      break
    else:
      suma=suma+A[i]
      count+=1
      i+=1
  else:
    temp=suma+B[j]
    if(temp>K):
      break
    else:
      suma+=B[j]
      count+=1
      j+=1
temp=0
if(i==N and suma<K):
    temp=1
    while(j<M):
        temp=suma+B[j]
        if(temp>K):
            break
        else:
            count+=1
            suma+=B[j]
            j+=1
if(j==M and suma<K and temp==1):
    while(i<N):
        temp=suma+A[i]
        if(temp>K):
            break
        else:
            count+=1
            suma+=A[i]
            i+=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()))
ni = 0
mi = 0
now = 0
cnt= 0
while True:
    if ni < n and mi < m:
        if a[ni] <= b[mi] and now + a[ni] <= k:
            now += a[ni]
            ni += 1
            cnt += 1
        elif a[ni] >= b[mi] and now + b[mi] <= k:
            now += b[mi]
            mi += 1
            cnt += 1
        else:
            break
    elif ni < n:
        if now + a[ni] <= k:
            now += a[ni]
            ni += 1
            cnt += 1
        else:
            break
    elif mi < m:
        if now + b[mi] <= k:
            now += b[mi]
            mi += 1
            cnt += 1
        else:
            break
    else:
        break
    print(now)
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

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

i, j = 0, 0
ok = False
while i < N or j < M:
    a = b = 10 ** 10
    if i < N:
        a = A[i]
    if j < M:
        b = B[j]
    if K < min(a, b):
        break
    if a <= b:
        K -= a
        i += 1
        while i < N and A[i] <= b:
            a = A[i]
            if K < a:
                ok = True
                break
            K -= a
            i += 1
    else:
        K -= b
        j += 1
        while j < M and B[j] <= a:
            b = B[j]
            if K < b:
                ok = True
                break
            K -= b
            j += 1
    if ok is True:
        break

print(i + j)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
arr1 = [int(j) for j in input().split()] + [10**10]
arr2 = [int(j) for j in input().split()] + [10**10]

ind1, ind2 = 0, 0
cur,ans = 0, 0
while ind1 != n and ind2 != m:

	if cur+arr1[ind1] > k and cur+arr2[ind2] > k:
		break

	if arr1[ind1] < arr2[ind2]:
		cur += arr1[ind1]
		ind1 += 1
		ans += 1
	elif arr1[ind1] == arr2[ind2]:
		x = ind1+1
		y = ind2+1
		if arr1[x] <= arr2[y]:
			cur += arr1[ind1]
			ind1 += 1
			ans += 1
		else:
			cur += arr2[ind2]
			ind2 += 1
			ans += 1
	else:
		cur += arr2[ind2]
		ind2 += 1
		ans += 1

while ind1 != n:
	if cur + arr1[ind1] > k:
		break

	cur += arr1[ind1]
	ind1 += 1
	ans += 1

while ind2 != m:
	if cur + arr2[ind2] > k:
		break

	cur += arr2[ind2]
	ind2 += 1
	ans += 1

print(ans)

Submission status: Wrong Answer

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

for i in range(N + M):
    tmp += A[i]
    if tmp > K:
        break
    res += 1
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()))

A.reverse()
B.reverse()

count = 0
time = 0

while count < K and time < N + M:
    try:
        if A[-1] <= B[-1]:
            count += A.pop()
        else:
            count += B.pop()
    
    except:
        if A:
            count += A.pop()
        else:
            count += B.pop()
    
    time += 1
   
if count > K:
  time -= 1

print(time)

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())))
ans = 0
t = 0
while t < K:
    if A and B:
        if A[0] < B[0]:
            x = A.popleft()
        elif A[0] > B[0]:
            x = B.popleft()
        else:
            i = 1
            while True:
                if len(A) > i and len(B) > i:
                    if A[i] == B[i]:
                        i += 1
                        continue
                    if A[i] > B[i]:
                        x = B.popleft()
                        break
                    else:
                        x = A.popleft()
                        break
                elif len(A) > i:
                    x = A.popleft()
                    break
                elif len(B) > i:
                    x = B.popleft()
                    break
                else:
                    x = A.popleft()
                    break
    elif A:
        x = A.popleft()
    elif B:
        x = B.popleft()
    else:
        break
    if t + x <= K:
        t += x
        ans += 1
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
# template by 3xC and starkizard.
# contributors: 

#####################################################################################
from __future__ import division, print_function 

import sys
import os
from collections import Counter, deque, defaultdict
import itertools
import math
import io


"""Uncomment modules according to your need"""


# from bisect import bisect_left, bisect_right, insort
# from heapq import heappop, heapify, heappush
# from random import randint as rn
# from Queue import Queue as Q
# from copy import deepcopy
# from decimal import *
# import re
# import operator

#####################################################################################
# this enables you to write python3 code with PyPy2 (Python 2)
if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip

#####################################################################################

"""value of mod"""
MOD = 998244353
mod = 10**9 + 7

"""Uncomment next 4 lines if doing huge recursion"""
# import threading
# threading.stack_size(1<<27)
# sys.setrecursionlimit(10000

def prepare_factorial(mod=mod):
    """ returns two lists, factorial and inverse factorial modulo argument by default 10**9 +7 """
    # Comment code out when you don't need inverse factorial or vice versa
    fact = [1]
    for i in range(1, 200005):
        fact.append((fact[-1] * i) % mod)

    ifact = [0] * 200005
    ifact[200004] = pow(fact[200004], mod - 2, mod)
    for i in range(200004, 0, -1):
        ifact[i - 1] = (i * ifact[i]) % mod

    return fact, ifact

def modinv(n, p):
    """ returns N inverse modulo p """
    return pow(n, p - 2, p)

def ncr(n, r,  fact, ifact):
    """ takes 4 arguments: n , r and factorial and inverse factorial lists"""
    t = (fact[n] * (ifact[r]*ifact[n-r]) % MOD)% MOD
    return t


def get_n(Sum):    
    """this function returns the maximum n for which Summation(n) <= Sum"""
    ans = (-1 + sqrt(1 + 8*Sum))//2
    return ans

def sieve(n):
    """ returns a list of prime numbers till n """
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r



def divs(n, start=1):
    """ returns a list of all divisors till n """
    divisors = []
    #rdivisors=[]
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors



def divn(n, primes):
    """ returns the number of divisors, two arguments n and the sieve till n """
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t



def lrfind(d, x, default=-1):
    """ Takes 2 arguments an iterable and an element. returns a tuple (firstoccurence,lastoccurence) -1 if not found """
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return default, default
    else:
        return left, right

def gcd(x, y): # math.gcd is slower
    """ returns greatest common divisor of x and y """
    while y:
        x, y = y, x % y
    return x

def check_sorted(a):
    ''' returns True/False '''
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return False
    return True
def ceil(n, k=1): return n // k + (n % k != 0) #returns math.ceil but protecting against floating inconsistencies
def input(): return sys.stdin.readline().strip()
def ii(): return int(input()) #inputs integer
def mi(): return map(int, input().split()) # inputting space seperated variables for example x,y,z
def li(): return list(map(int, input().split())) #inputting a space seperated list of integers
def lw(): return input().split() #inputting a space seperated list of strings
def lcm(a, b): return abs(a * b) // gcd(a, b) #returns LCM of two arguments
def prr(a, sep=' ', end='\n'): print(sep.join(map(str, a)), end=end) #For printing an iterable with seperator sep as optional second argument (default : " "), ending character (default: "\n") as optional third
def dd(): return defaultdict(int) #returns a dictionary with values defaulted to 0
def ddl(): return defaultdict(list) #returns a dictionary with values defaulted to []
def write(s): return sys.stdout.write(s)












###################################################################

def main():
    #code goes here
    n,m,k=mi()
    a=li()
    b=li()
    ptr1=0
    ptr2=0
    totaltime=0
    check=True
    count=0
    while(ptr1<n and ptr2<m):
        if a[ptr1]<b[ptr2]:
            if totaltime+a[ptr1]<=k:
                totaltime+=a[ptr1]
                count+=1
                ptr1+=1
            else:
                check=False
                break
        else:
            if totaltime+b[ptr2]<=k:
                totaltime+=b[ptr2]
                count+=1
                ptr2+=1
            else:
                check=False
                break
        
    if(check==False):
        print(count)
    else:
        while(ptr1<n):
            if totaltime+a[ptr1]<=k:
                totaltime+=a[ptr1]
                count+=1
                ptr1+=1
            else:
                check=False
                break
            
        while(ptr2<m):
            if totaltime+b[ptr2]<=k:
                totaltime+=b[ptr2]
                count+=1
                ptr2+=1
            else:
                check=False
                break
        print(count)

""" -------- Python 2 and 3 footer by Pajenegod and c1729 ---------"""

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192

class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill(): pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill();
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            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')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

""" main function"""

if __name__ == '__main__':
    main()
    # threading.Thread(target=main).start()

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

total = 0
a = 0
b = 0
ans = 0
while total <= K and (a < len(A) or b < len(B)):
    if a < len(A) and b < len(B):
        if A[a] <= B[b]:
            total += A[a]
            ans += 1
            a += 1
        else:
            total += B[b]
            ans += 1
            b += 1
    elif a >= len(A):
        total += B[b]
        ans += 1
        b += 1
    else:
        total += A[a]
        ans += 1
        a += 1
if total == K:
    print(ans)
else:
    print(ans-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, M, K = map(int, input().split())
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))
    ans = 0
    while K >= 0:
        a = 1000000001
        b = 1000000001
        lenA = len(A)
        lenB = len(B)
        if lenA > 0:a = A.popleft()
        if lenB > 0:b = B.popleft()
        if a < b:
            K -= a
            if lenB > 0:
                B.appendleft(b)
        else:
            K -= b
            if lenA > 0:
                A.appendleft(a)
        ans += 1
    ans -= 1
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
a=input().split()
A=[int(c) for c in a]
b=input().split()
B=[int(c) for c in b]
answer=0
numbers=[]
while A and B:
    if A[0]>=B[0]:
        numbers.append(B[0])
        del B[0]
    else:
        numbers.append(A[0])
        del A[0]
if A:
    for a in A:
        numbers.append(a)
else:
    for b in B:
        numbers.append(b)
whole=0
for a in range(N+M):
    whole+=numbers[a]
    if whole<=K:
        answer+=1
print(answer)