Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Wrong Answer

Solution code:
import bisect

def f(l):
    cost = [0]
    for i, v in enumerate(l):
        if i == 0:
            cost.append(l[i])
        else:
            cost.append(l[i] + cost[i])
    return cost

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

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

ans = 0
for i in range(left_max):
    for j in range(right_max):
        if cost_a[i] + cost_b[j] <= k:
            ans  = i + j if ans < i + j else ans
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()))

AL = []
BL = []
a = 0
for i in A:
    a += i
    AL.append(a)
a = 0
for i in B:
    a += i
    BL.append(a)

#print(AL)
#print(BL)

a = 0
for i in range(len(AL)):
    if AL[i] > k:
        a = i
        break
else:
    a = len(AL)

b = 0
for i in range(len(BL)):
    if BL[i] > k:
        b = i
        break
else:
    b = len(BL)

#print(a,b)

m = a
u = 0
for j in reversed(range(a)):
    t = k-AL[j]
    for i in range(u,b):
        if t-BL[i]>=0:
            if m < j+1 + i +1:
                m = j+1 + i +1
        else:
            u = i
            break
        
    if j+1+b<=m:
        break
if a == 0 and b > 0:
    m = b
print(m)

Submission status: Wrong Answer

Solution code:
MAX=10**10
def main(n, m, k, a_n, b_n):
    a_n.append(MAX)
    b_n.append(MAX)

    r = 0
    a = 0
    b = 0
    for _ in range(n+m):
        a_v = a_n[a]
        b_v = b_n[b]

        if a_v == MAX and b_v == MAX:
            return r

        if a_v < b_v:
            k -= a_v
            a += 1
        else:
            k -= b_v
            b += 1

        if k >= 0:
            r += 1

        if k <= 0:
            return r
    return r

if __name__ == "__main__":
    n, m, k = map(int, input().split())
    a_n = [int(x) for x in input().split()]
    b_n = [int(x) for x in input().split()]
    print(main(n, m, k, a_n, b_n))

Submission status: Wrong Answer

Solution code:
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(' '))))

    time = 0
    cnt = 0
    while True:
        if len(a) == 0:
            time += b.popleft()
        elif len(b) == 0:
            time += a.popleft()
        else:
            if a[0]<b[0]:
                time += a.popleft()
            else:
                time += b.popleft()
        if time > k:
            break
        cnt += 1
        if len(a) + len(b) == 0:
            break
    
    print(cnt)


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()))
count=0
a.reverse()
b.reverse()
if sum(a)+sum(b)<=k:
    count=len(a)+len(b)+1
else:
    while k>=0:
        if a==[]:
            k-=b[len(b)-1]
            del b[len(b)-1]
        elif b==[]:
            k-=a[len(a)-1]
            del a[len(a)-1]
        elif a[len(a)-1]<=b[len(b)-1]:
            k-=a[len(a)-1]
            del a[len(a)-1]
        elif a[len(a)-1]>b[len(b)-1]:
            k-=b[len(b)-1]
            del b[len(b)-1]
        count+=1
print(count-1)

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

As = [0] * (N + 1)
Bs = [0] * (M + 1)

for i in range(1, N + 1):
  As[i] = As[i - 1] + A[i - 1]

for i in range(1, M + 1):
  Bs[i] = Bs[i - 1] + B[i - 1]

n = 0

while True:
  time = float('inf')
  for i in range(n + 1):
    if i < len(As) and n + 1 - i < len(Bs):
      time = min(time, As[i] + Bs[n + 1 - i])
  if time <= K:
    n += 1
  else:
    break

#print(As, Bs, time)

print(n)

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


def dfs(x, y, read, z):
    while True:
        if len(x) == 0 and len(y) == 0:
            return read
        elif len(x) == 0:
            time = y.popleft()
        elif len(y) == 0:
            time = x.popleft()
        elif x[0] < y[0]:
            time = x.popleft()
        elif x[0] > y[0]:
            time = y.popleft()
        elif x[0] == y[0]:
            if dfs(deque(list(x.copy())[1:]), y.copy(), read, z) > dfs(x.copy(), deque(list(y.copy())[1:]), read, z):
                time = x.popleft()
            else:
                time = y.popleft()

        if z - time >= 0:
            z -= time
            read += 1
        else:
            return read


ans = dfs(a, b, 0, k)
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()))
count=0
while 1:
	if k==0:
		break
	if len(a)==0 and len(b)==0:
		break
	elif len(a)!=0 and len(b)!=0:
		if a[0]<b[0]:
			if a[0]<=k:
				k-=a[0]
				count+=1
				a.pop(0)
				
			else:
				break
		else:
			if b[0]<=k:
				k-=b[0]
				count+=1
				b.pop(0)
			else:
				break
	elif len(a)==0 and len(b)!=0:
		if b[0]>k:
			break
		else:
			for j in range(len(b)):
				if b[0]>k:
					break
				else:	
					k-=b[0]
					count+=1
					b.pop(0)
			break
	else:
		if a[0]>k:
			break
		else:
			for j in range(len(a)):
				if a[0]>k:
					break
				else:	
					k-=a[0]
					count+=1
					a.pop(0)
			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()))

usedTime = 0
readCount = 0
while True:
    if len(a) > 0 and len(b) > 0:
        if usedTime + a[0] <= k or usedTime + b[0] <= k:
            if a[0] < b[0]:
                usedTime += a.pop(0)
            else:
                usedTime += b.pop(0)
            readCount += 1
        else:
            break
    elif len(a) > 0:
        if usedTime + a[0] <= k:
            usedTime += a.pop(0)
            readCount += 1
        else:
            break
    elif len(b) > 0:
        if usedTime + b[0] <= k:
            usedTime += b.pop(0)
            readCount += 1
        else:
            break
    else:
        break

print(readCount)

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)
B.append(10**9+1)
run_time = K
count_A = 0
count_B = 0
#print(N, M ,K)
while(run_time > 0 or count_A + count_B >= N + M):
#	print(A[count_A] <= B[count_B] , run_time >= A[count_A] , count_A < N , "|" ,A[count_A] > B[count_B] , run_time >= B[count_B] , count_B < M)
	if(A[count_A] <= B[count_B] and run_time >= A[count_A] and count_A < N):
#		print(count_A, N)
#		print(N)
		run_time -= A[count_A]
		count_A += 1
	elif(A[count_A] > B[count_B] and run_time >= B[count_B] and count_B < M):
#		print(count_B , M)
		run_time -= B[count_B]
		count_B += 1
	else:
		break
print(count_A + count_B)

Submission status: Wrong Answer

Solution code:
import sys
import os
import itertools
import numpy as np
from collections import deque

def main():
    if os.getenv("ENV")=='LOCAL':
        sys.stdin = open(os.path.dirname(__file__)+'/input.txt', 'r')

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

    lA=len(A)-1
    Ap=0
    Bp=0
    n=0
    t=0

    for i in range(len(A)): #Aは可能な限り足す
        if t+A[i]>K:
            break
        Ap=i
        t+=A[i]
        n+=1
    for i in range(len(B)):
        if t+B[i]>K:
            break
        Bp=i
        t+=B[i]
        n+=1

    max=n #読める本の最大値
    # print(max)
    for i in range(Ap+1):
        # print("n="+str(n)+" t="+str(t)+" A="+str(Ap)+" B="+str(Bp))
        t-=A[Ap]
        n-=1
        Ap-=1
        for j in range(Bp+1,len(B)):
            # print(str(n)+" : "+str(t)+" j="+str(j))
            if t+B[j]>K:
                break
            Bp=j
            t+=B[j]
            n+=1
        if max<n:
            max=n

    print(max)

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

list = a + b
list = sorted(list)

l = n + m

m = 0
count = 0

for i in range(l):
    if count + list[i] <= k:
        count += list[i]
        m += 1
        
print(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から選んでみる
minutes_a = 0
count_a = 0
for i in range(N):
    if minutes_a + A[i] > K:
        break
    minutes_a += A[i]
    count_a += 1

minutes_b = 0
count_b = 0

for i in range(M):
    if minutes_a + minutes_b + B[i] > K:
        if count_a > 0 and minutes_a - A[count_a - 1] + minutes_b + B[i] <= K:
            minutes_a -= A[count_a - 1]
            count_a -= 1
            minutes_b += B[i]
            count_b += 1
            continue
        else:
            break
    minutes_b += B[i]
    count_b += 1

a_max = count_a + count_b

# 全部Bから選んでみる
minutes_b = 0
count_b = 0
for i in range(M):
    if minutes_b + B[i] > K:
        break
    minutes_b += B[i]
    count_b += 1

minutes_a = 0
count_a = 0

for i in range(N):
    if minutes_b + minutes_a + A[i] > K:
        if count_b > 0 and minutes_b - B[count_b - 1] + minutes_a + A[i] <= K:
            minutes_b -= B[count_b - 1]
            count_b -= 1
            minutes_a += A[i]
            count_a += 1
            continue
        else:
            break
    minutes_a += A[i]
    count_a += 1

print(max(a_max, count_a + count_b))

Submission status: Wrong Answer

Solution code:
N,M,K=list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
totalbook = 0
totaltime = 0
for a in a_list:
  if totaltime+a <= K:
    totaltime+=a
    totalbook+=1
  else:
    break
ans = totalbook
nowaind = totalbook-1 if totalbook > 0 else 0
nowbind = 0
for b in b_list:
  if totaltime+b<=K:
    totaltime+=b
    totalbook+=1
    nowbind+=1
  else:
    break
ans = max(ans,totalbook)
for b in range(nowbind,len(b_list)):
  while nowaind >= 0 and totaltime+b_list[b]>K:
    totalbook-=1
    totaltime-=a_list[nowaind]
    nowaind-=1
  totaltime+=b_list[b]
  if totaltime<=K:
    totalbook+=1

print(max(ans,totalbook))

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
i = 0
n = 0
while i < K:
    if a == [] and b == []:
        break
    elif a == []:
        i += b[0]
        del b[0]

    elif b == []:
        i += a[0]
        del a[0]

    elif a[0] < b[0]:
        i += a[0]
        del a[0]

    else:
        i += b[0]
        del b[0]

    n += 1

if i > K:
    print(n -1)
else:
    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()))
count = 0
minute = 0

while min(len(A),len(B)) > 0 and minute < K:
    if A[0] >= B[0]:
        minute += A[0]
        A.pop(0)
    else:
        minute += B[0]
        B.pop(0)
    count += 1


if minute > K:
    print(count - 1)
    exit()
if len(A) == 0:
    while len(B) > 0 and minute < K:
        minute += B[0]
        B.pop(0)
        count += 1
else:
    while len(A) > 0 and minute < K:
        minute += A[0]
        A.pop(0)
        count += 1

if minute > K:
    print(count -1)
else:
    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()))

a.append(10**9+1)
b.append(10**9+1)

a_time = 0
b_time = 0

a_count = 0
b_count = 0

for i in range(len(a)):
  a_time += a[i]
  if a_time > k:
    a_time -= a[i]
    break
  a_count += 1
  
for i in range(len(b)):
  b_time += b[i]
  if b_time > k:
    b_time -= b[i]
    break
  b_count += 1
  
for i in range(len(b)):
  a_time += b[i]
  if a_time > k:
    break
  a_count += 1
  
for i in range(len(a)):
  b_time += b[i]
  if b_time > k:
    break
  b_count += 1
  
if a_count > b_count:
  print(a_count)
else:
  print(b_count)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
n,m,k=getNM()
a=getList()
b=getList()
a_=[]
b_=[]
check=True
ans=[0]
for i in range(n):
    a_.append(sum(a[:i+1]))
    if a_[-1]>k:
        break
for j in range(m):
    b_.append(sum(b[:j+1]))
    if b_[-1]>k:
        break
for i in range(1,len(a_)+1):
    for j in range(1,len(b_)+1):
        if a_[-i]+b_[-j]<=k:
            ans.append(len(a_)+len(b_)-i-j+1+1)
            break

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

counter=0
sum_min=0
i,j=0,0
flag = True
while i<n and j<m:
    if a[i] <= b[j]:
        sum_min+=a[i]
        i+=1
    else:
        sum_min+=b[j]
        j+=1
    if flag and i==n:
        a[n-1]=10**9+1
        i=n-1
        flag=False
    if flag and j==m:
        b[m-1]=10**9+1
        j=m-1
        flag=False
    if sum_min <= k:
        counter+=1
    elif sum_min > k:
        print(counter)
        break
    # print(i,j)
    # print(sum_min)
else:
    print(counter)
# print(sum_min)

Submission status: Wrong Answer

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

from itertools import accumulate

N, M, K = map(int, input().split())
#A_input = list(map(int, input().split()))
#B_input = list(map(int, input().split()))
A = list(accumulate(list(map(int, input().split()))))
B = list(accumulate(list(map(int, input().split()))))

A.insert(0, 0)
B.insert(0, 0)
cnt = 0

print(A)
print(B)

for i in range(N+1):
    for j in range(M+1):
        #print(A[i])
        #print(B[j])
        #print("-------")
        if(A[i] + B[j] <= K):
            if(cnt < i+j):
                cnt = i+j
                #print("-------")
        else:
            break

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')

#Pythonを用いる場合
#numpy 1.18.0, scipy 1.4.1, scikit-learn 0.22, numba 0.47.0, networkx 2.4
 
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,m,k=mii()
arra=limii()
arrb=limii()

cuma=np.cumsum(arra)
cumb=np.cumsum(arrb)
ans=0
cnt=0
tmp=0
placea=0
for i in cuma:
    if i<=k:
        tmp=i
        placea+=1
        ans+=1
        cnt+=1
    else:
        continue
#print(cnt,tmp,ans,placea)
placeb=0
for i in cumb:
    #print(i)
    if i<=k:
        tmp+=i
        cnt+=1
        #print(1111111)
    else:
        while i+cuma[placea-1]>k:
            placea-=1
            cnt-=1
            if placea<1:
                break

    ans=max(ans,cnt)
    if placea<1:
        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()))
a.append(k)
b.append(k)
ans = 0
while a[0] <= k or b[0] <= k:
    if a[0] < b[0]:
        k -= a.pop(0)
    else:
        k -= b.pop(0)
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,M,K=map(int,input().split())
As=list(map(int,input().split()))
As=[a for a in accumulate(As) if a <= K]
Bs=list(map(int,input().split()))
Bs=[b for b in accumulate(Bs) if b <= K]
# print(As)
# print(Bs)

gans = -1
for ai in range(len(As)-1, 0-1, -1):
    ans = -1
    nokori = K-As[ai]
    for bi in range(len(Bs)-1, 0-1, -1):
        if Bs[bi] <= nokori:
            now = ai+1 + bi+1
            if now >= ans:
                ans = now
            else:
                break
            # print("a", ans, ai, bi)
        else:
            continue
    if gans <= ans:
        gans = ans
        continue
    else:
        print(gans)
        exit(0)


if gans == -1:
    print(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()))

A = 0
B = 0

s = 0
for i in range(len(a)):
    if s + a[i] <= k:
        s += a[i]
        A += 1
        #print(s)
    else:
        break
S = k - s
ans = 0
#A -= 1
#print(A)

q = 0
for i in range(A+1):
    if q != 0:
        S += a[A-i]
        A -= 1
    q += 1
    for j in range(B,len(b)):
        if S - b[j] >= 0:
            S -= b[j]
            B += 1
            if A+B >= ans:
                ans = A+B
        else:
            break
            
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()))

ans = 0
pile = 0

aq = deque(a)
bq = deque(b)

while(pile <= k):
    ans += 1
    if len(aq) == 0 and len(bq) == 0:
        # cant pop
        break

    elif len(aq) == 0 and len(bq) != 0:
        #pop form b
        pile += bq.popleft()

    elif len(aq) != 0 and len(bq) == 0:
        #pop from a
        pile += aq.popleft()

    elif aq[0] > bq[0]:
        #pop form b
        pile += bq.popleft()

    elif aq[0] <= bq[0]:
        #pop form a
        pile += aq.popleft()

print(ans-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,M,K = LI()
A = LI()
B = LI()
a = deque(A)
b = deque(B)
ans = 0
while True:
    if len(a) > 0 and len(b) > 0:
        temp = min(a[0],b[0])
        if (temp > K):
            break
        if a[0] > b[0]:
            K = K - b.popleft()
            if K < 0: 
                break
            ans += 1
        elif b[0] > a[0]:
            K = K - a.popleft()
            if K < 0: 
                break
            ans += 1
        elif b[0] == a[0]:
            if len(a) > 1 and len(b) > 1:
                if a[1] > b[1]:
                    K = K - b.popleft()
                    if K < 0: 
                        break
                    ans += 1
                elif b[1] > a[1]:
                    K = K - a.popleft()
                    if K < 0: 
                        break
                    ans += 1           
            elif len(a) == 1 and len(b) > 1:
                if a[0] > b[1]:
                    K = K - b.popleft()
                    if K < 0: 
                        break
                    ans += 1
                elif b[1] > a[0]:
                    K = K - a.popleft()
                    if K < 0: 
                        break
                    ans += 1
            elif len(b) == 1 and len(a) > 1:
                if b[0] > a[1]:
                    K = K - a.popleft()
                    if K < 0: 
                        break
                    ans += 1
                elif a[1] > b[0]:
                    K = K - b.popleft()
                    if K < 0: 
                        break
                    ans += 1  
    elif len(a) == 0 and len(b) > 0:
        K = K - b.popleft()
        if K < 0: 
            break
        ans += 1
    elif len(b) == 0 and len(a) > 0:
        K = K - a.popleft()
        if K < 0: 
                break
        ans += 1
    elif len(a) == 0 and 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()))
a.extend(b)

time = 0
count = 0
for x in a:
  if time > k:
    count -= 1
    break
  time += x
  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()))
AB = A + B
AB.sort()

x = 0
for i in range(N + M):
    if AB[i] <= K:
        x = x + 1
        K = K - AB[i]
        if K <= 0:
            break
        
print(x)

Submission status: Wrong Answer

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

ans = 0
ans_time = 0
adex = 0
bdex = 0


while True:
    if len(a) > 0 and len(b) > 0:
        if a[0] < b[0]:
            ans_time += a.pop(0)
        else:
            ans_time += b.pop(0)
        ans += 1
    elif len(a) > 0 and len(b) <= 0:
        ans_time += a.pop(0)
        ans += 1

    elif len(a) <= 0 and len(b) > 0:
        ans_time += b.pop(0)
        ans += 1
    else:
        break
    #print(ans,ans_time)

    if ans_time > k:
        ans -= 1
        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()))

ans = 0
time = 0


for i in range(len(a)):
    time += a[i]
    if time > k:
        time -=a[i]
        break
    ans += 1
    
tmp = ans
sub = ans

for j in range(len(b)):
    time += b[j]
    if time > k:
        if sub != 0: 
            for i in range(sub):
                time -= a[sub - 1 - i]
                if time <= k:
                    tmp = tmp - i - 1
                    sub = sub - i - 1
                    break
                if i == sub - 1 & time > k:
                    tmp -= 1
        else:
            break
    tmp += 1
    if tmp > ans :
        ans = 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()))
temp=0
an,bn=0,0
ans=0
bf=[]
flag=0
while temp<=k:
    if an==n and bn==m: break
    if an>=n:
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        temp+=a[an]
        if temp>k: break
        an+=1
    else:
        if a[an]<b[bn]:
            if flag:
                b=bf[:]+b[:]
                bn-=len(bf)
                bf=[]
                flag=0
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            if flag:
                a=bf[:]+a[:]
                an-=len(bf)
                bf=[]
                flag=0
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else:
            temp+=a[an]
            if temp>k: break
            bf.append(a[an])
            flag=1
            an+=1
            bn+=1
    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()))

time = 0
i = 0
j = 0
count = 0

while time <= k:
    if i == len(a) and j == len(b):
        time += k
    elif i == len(a):
        time += b[j]
        if time > k:
            break
        j += 1
        count += 1
    elif j == len(b):
        time += a[i]
        if time > k:
            break
        i += 1
        count += 1
    elif a[i] <= b[j]:
        time += a[i]
        if time > k:
            break
        i += 1
        count += 1
    else:
        time += b[j]
        if time > k:
            break
        j += 1
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
import sys

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
i = 0
j = 0
ans = 0
if k >= sum(a) + sum(b):
    ans = n + m
    print(ans)
    sys.exit()
list_ab = []
while c <= (n+m-1):
    if i >= n :
        list_ab.append(b[j])
        j += 1
        c += 1
    elif j >= m :
        list_ab.append(a[i])
        i += 1
        c += 1
    elif a[i] <= b[j]:
        list_ab.append(a[i])
        i += 1
        c += 1
    else:
        list_ab.append(b[j])
        j += 1
        c += 1
book = 0
for i in range(n+m):
    if k >= list_ab[i]:
        k -= list_ab[i]
        book += 1
    else:
        break
print(book)

Submission status: Wrong Answer

Solution code:
from collections import deque
from decimal import *
n,m,k = [Decimal(int(e)) for e in input().split()]
As = deque([Decimal(int(e)) for e in input().split()])
Bs = deque([Decimal(int(e)) for e in input().split()])
result = 0
while True:
    if not As and not Bs:
        break
    if not As and Bs[0]>k:
        break
    if not Bs and As[0]>k:
        break
    if not As and Bs[0]<=k:
        k = k-Bs.popleft()
        result = result + 1
        continue
    if not Bs and As[0]<=k:
        k = k-As.popleft()
        result = result + 1
        continue
    if As[0] < Bs[0]:
        min_reduce = As.popleft()
    else:
        min_reduce = Bs.popleft()

    if min_reduce > k:
        break
    else:
        k = k-min_reduce
        result = result+1
print(result)

Submission status: Wrong Answer

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

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

list = a + b
list = sorted(list)

l = n + m

m = 0
count = 0

for i in range(l):
    if count + list[i] <= k:
        count += list[i]
        m += 1
        print(count)
print(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()))
ans=0
num=0
i=0
j=0
if a[0]>k and b[0]>k:
    ans=0
    print(ans)
    exit()
while num<=k:
    if i<=n-1 and j<=m-1:
        if a[i]>=b[j]:
            if num+b[j]<=k:
                  num+=b[j]
                  j=j+1
                  ans+=1
            else:
                break
        else:
            if num+a[i]<=k:
                num+=a[i]
                i=i+1
                ans+=1
            else:
                break
    elif i>=n and j<=m-1:
        if num+b[j]<=k:
            ans+=1
            num+=b[j]
            j=j+1
    elif i<=n-1 and j>=num:
        if num+a[i]<=k:
            ans+=1
            num+=a[i]
            i=i+1
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from bisect import *

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

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


cum_A = list(accumulate([0] + A))
cum_B = list(accumulate([0] + B))

start = bisect_left(cum_A, K) - 1       # K を超えない最大のcum_Aの要素を導くindex = 和がK以下になるようなAの要素の個数
res = [start]
k = 0

for i in reversed(range(start+1)):
    while cum_A[i] + cum_B[k] <= K:
        if k == M:
            res.append(i + k)
            break
        k += 1
    res.append(i + k-1)


print(max(res))


'''

4 4 11
1 1 10 1
10 1 1 1

'''

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()]
O = 0
ab = a + b
ab.sort()
while k > 0:
    if ab:
        k -= ab[0]
        print(k)
        if k < 0:
            break
        ab.pop(0)
        O +=1
print(O)

Submission status: Wrong Answer

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

N,M,K=(int(x) for x in input().rstrip('\n').split())
As=[int(x) for x in input().rstrip('\n').split()]
Bs=[int(x) for x in input().rstrip('\n').split()]
res = 0
now = 0
for i in range(N+M):
  try:
    a = As[0]
  except:
    a = 1+10**9
  try:
    b = Bs[0]
  except:
    b = 1+10**9
  if a>b:
    now += b
    del Bs[0]
  elif a<b:
    now += a
    del As[0]
  else:
    try:
      a_nex =As[1]
    except:
      a_nex= 1+10**9
    try:
      b_nex = Bs[1]
    except:
      b_nex = 1+ 10**9
    if a_nex>b_nex:
      now += b
      del Bs[0]
    else:
      now += a
      del As[0]
  if now > K:
    break
  res += 1
print(res)