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 copy
N,M,K = map(int,input().split())
A = [0]*N
B = [0]*M
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = 0
b = 0

tmp = 0
ans = 0
# for i in range(200000):
#     if A[a] < B[b]:
#         if tmp <= K:
#             tmp += A[a]
#             ans += 1
#             a += 1
#     elif A[a] > B[b]:
#         if tmp <= K:
#             tmp += B[b]
#             ans += 1
#             b += 1
#     if a > len(A)-1 or b > len(B)-1:
#         break
fa = False
fb = False

while len(A) != 0 or len(B) != 0:
    AA = copy.copy(A)
    BB = copy.copy(B)
    if len(AA) != 0:
        a = AA.pop(0)
        fa = True
    if len(BB) != 0:
        b = BB.pop(0)
        fb = True
    if fa and fb:
        if a<=b:
            tmp += a
            ans += 1
            A = AA
        else:
            tmp += b
            ans += 1
            B = BB
    else:
        if fa:
            tmp += a
            ans += 1
            A = AA
        if fb:
            tmp += b
            ans += 1
            B = BB

    if tmp >= K:
        break
    fa = False
    fb = False
    
    #print(tmp,A,B)
    
if len(A)==0 and len(B)==0:
    print(ans)
else:
    print(ans-1)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, M, K = map(int, input().split())
A = input().split()
B = input().split()
for i in range(len(A)):
  A[i] = int(A[i])
for i in range(len(B)):
  B[i] = int(B[i])
A_cumsum = np.array(A).cumsum()
B_cumsum = np.array(B).cumsum()
max_count = 0
for i in range(N):
  if A_cumsum[i] > K:
    break
  for j in range(M):
    if A_cumsum[i] + B_cumsum[j] <= K and max_count <= A_cumsum[i] + B_cumsum[j]:
      max_count = i + j + 2
    else:
      break
print(max_count)

Submission status: Wrong Answer

Solution code:
a=[input() for i in range(3)]
a1=[int(i) for i in a[0].split()]
a2=[int(i) for i in a[1].split()]
a3=[int(i) for i in a[2].split()]

time=0
count=0

while time<=a1[2]:

	#print(a2,a3)

	if len(a3)==0 and len(a2)==0:
		break

	elif len(a3)==0:
		time+=a2[0]
		count+=1
		del a2[0]

	elif len(a2)==0:
		time+=a3[0]
		count+=1
		del a3[0]

	else:
		if a2[0]<=a3[0]:
			time+=a2[0]
			count+=1
			del a2[0]
		#a2.append(1000000000)

		else:
			time+=a3[0]
			count+=1
			del a3[0]
		#a3.append(1000000000)


if count==0:
	print(0)

elif len(a3)==0 and len(a2)==0:
	print(count)

else:
	print(count-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()))

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
ans += A
#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:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

count = 0
anum = 0
bnum = 0

while(k > 0):
    print(anum)
    print(bnum)
    if anum == n:
        if b[bnum] <= k:
            k -= b[bnum]
            count += 1
            bnum += 1
            print(k)
        else:
            break
    elif bnum == m:
        if a[anum] <= k:
            k -= a[anum]
            count += 1
            anum += 1
            print(k)
        else:
            break
    elif a[anum] < b[bnum]:
        if a[anum] <= k:
            k -= a[anum]
            count += 1
            anum += 1
            print(k)
        else:
            break

    else:
        if b[bnum] <= k:
            k -= b[bnum]
            count += 1
            bnum += 1
            print(k)
        else:
            break

print(count)

Submission status: Wrong Answer

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

A_ = list(itertools.accumulate(A))
B_ = list(itertools.accumulate(B))

ans = 0
l = len(A_)

for idx, i in enumerate(A_):
  if i <= K:
          ans = max(idx+1 + bisect.bisect_right(B_, K-i), ans)
  else:
    ans = max(ans, bisect.bisect_right(B_, 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()))

accum_a = [0] * (n + 1)
for i in range(1, n + 1):
    accum_a[i] = accum_a[i - 1] + a[i - 1]

accum_b = [0] * (m + 1)
for i in range(1, m + 1):
    accum_b[i] = accum_b[i - 1] + b[i - 1]

max_cnt = 0
for a_cnt in range(n + 1):
    if accum_a[a_cnt] > k:
        break

    if b[0] > k - accum_a[a_cnt]:
        continue

    b_cnt = bisect.bisect_right(accum_b, k - accum_a[a_cnt]) - 1

    sum_cnt = a_cnt + b_cnt
    if sum_cnt > max_cnt:
        max_cnt = sum_cnt

print(max_cnt)

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, len(A)):
  A[i] += A[i - 1]
for i in range(1, len(B)):
  B[i] += B[i - 1]
#print(A, B)
res = 0
for i in range(len(A)):
  idx = -(i + 1)
  temp = k - A[idx]
  bcan = bisect.bisect_right(B, temp)
  if temp >= 0:
    res = max(res, (len(A) - i + bcan))
print(res)

Submission status: Wrong Answer

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

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

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

re = i = 0

A.extend(B)
A.sort()

while(m >= 0 and i < a+b):
    m -= A[i]
    if(m >= 0):
        re += 1
        i+=1
print(re)

Submission status: Wrong Answer

Solution code:
info = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

N = list()
M = list()
N.append(A[0])
for i in range(1, info[0]):
  N.append(N[i-1]+A[i])
M.append(B[0])
for i in range(1, info[1]):
  M.append(M[i-1]+B[i])

ans = 0
for i in range(info[0]-1, -1, -1):
  if N[i] > info[2]:
    continue
  for j in range(info[1]-1, -1, -1):
    if N[i]+M[j] <= info[2]:
      ans = max(ans, i+j+2)
      break
    
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_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

ans = 0
for i in range(len(a)):
  time = a_sum[i]
  tmp = i + 1 + bisect.bisect(b_sum, k - time)
  ans = max(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()))
countNum = 0
countNumTemp = countNum
countTime = 0
countTimeTemp = countTime
loopNum = max(N,M)
while countTimeTemp <= K:
    countTime = countTimeTemp
    countNum = countNumTemp
    try:
        if A[0] <= B[0]:
            countTimeTemp += A.pop(0)
            countNumTemp += 1
        else:
            countTimeTemp += B.pop(0)
            countNumTemp += 1
    except:
        if len(A) != 0:
            countTimeTemp += A.pop(0)
            countNumTemp += 1
        elif len(B) != 0:
            countTimeTemp += B.pop(0)
            countNumTemp += 1
        else:
            break
print(countNum)

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

ai = 0
bi = 0
time = 0
book = 0
while len(A) > 0 and len(B) > 0:
 if (A[ai] > B[bi]) and (time + B[ai] <= K):
   time += B[ai]
   B.pop(0)
   book += 1
 elif (A[ai] <= B[bi]) and (time + A[ai] <= K):
   time += A[bi]
   A.pop(0)
   book += 1
 else:
   break

tmp = []
if (len(A) > 0):
 tmp = A
else:
 tmp = B

ti = 0
while len(tmp)>0:
  if time + tmp[ti] <= K:
    time += tmp[ti]
    tmp.pop(0)
    book += 1
  else:
    break

print (book)

Submission status: Wrong Answer

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

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ia, ib = 0, 0
t = 0
cnt = 0

while True:
    if a and b:
        if a[0] >= b[0]:
            t += b[0]
            b.pop()
        elif a[0] < b[0]:
            t += a[0]
            a.pop()
    
    elif a and not b:
        t += a[0]
        a.pop()
    elif b and not a:
        t += b[0]
        b.pop()
    
    cnt += 1

    if cnt == n+m:
        break
    if t > k:
        cnt -= 1
        break

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

tmp = []
s = 0
for i in a:
    if s + i <= k:
        tmp.append(i)
        s += i
    else:
        break
a = tmp
tmp = []
s = 0
for i in b:
    if s + i <= k:
        tmp.append(i)
        s += i
    else:
        break
b = tmp
line = b[::-1] + a
print(line)
ans = 0
right = 0
sum_ = 0
for left in range(len(line)):
    while right < len(line) and sum_ + line[right] <= k:
        sum_ += line[right]
        right += 1
    l = right -left 
    if l > ans:
        ans = l

    if right == left: right += 1
    else: sum_ -= line[left]

print(ans)

Submission status: Wrong Answer

Solution code:
row1 = input()
row2 = input()
row3 = input()

n, m, k = [int(s) for s in row1.split(' ')]
a_list = [int(t) for t in row2.split(' ')]
b_list = [int(t) for t in row3.split(' ')]

max_len = 0

for a_max in range(n, 0, -1):
  time = 0
  book_list = []
  i = 0
  while i < a_max:
    if time + a_list[i] > k:
      break
    time += a_list[i]
    book_list.append(a_list[i])
    i += 1
  j = 0
  while j < m:
    if time + b_list[j] > k:
      break
    time += b_list[j]
    book_list.append(b_list[j])
    j += 1
  if len(book_list) > max_len:
    print(book_list)
    max_len = len(book_list)
print(max_len)

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_ac=[]
b_ac=[]

asum=0
bsum=0

for i in range(n):
    asum+=a[i]
    if asum>k:
        break
    a_ac.append(asum)

for i in range(m):
    bsum+=b[i]
    if bsum>k:
        break
    b_ac.append(bsum)
#print(a_ac,b_ac)
n=len(a_ac)
m=len(b_ac)

mx=0

if n ==0:
    print(m)
else:
    for i in reversed(range(n)):
        temp=a_ac[i]
        lim=k-temp
        numb=0
        for j in range(m):
            if b_ac[j]>lim:
                numb=j
                break
            if j == m-1:
                numb=m
        mx=max(mx,numb+i+1)
        #print(mx,numb)

    print(mx)

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_max = 0
sum = 0
for i in range(n):
  sum += A[i]
  if sum > k:
    sum -= A[i]
    break
  a_max += 1
  
ans = a_max
kotae =ans
j = 0

print(a_max, 0)
for i in range(m):
  sum += B[i]
  ans += 1
  while sum > k and j < a_max:
    sum -= A[a_max-1-j]
    ans -= 1
    j += 1
  if sum > k:
    print(kotae)
    exit()
  kotae = max(kotae, ans)
  if a_max-j == 0:
    break
  
print(kotae)

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
j=0
t=0
A.append(1000000001)
B.append(1000000001)
while t < K:
    if A[i]>=B[j] and t+B[j]<=K:
        t+=B[j]
        j+=1
    elif A[i]<B[j] and t+A[i]<=K:
        t+=A[i]
        i+=1
    else:
      	break
print(i+j)

Submission status: Wrong Answer

Solution code:
i1 = input().split(" ")
n = int(i1[0])
m = int(i1[1])
k = int(i1[2])
i2 = input().split(" ")  # desk A
i3 = input().split(" ")  # desk B

t = 0
at =0
bt =0

def isWithinBudget(val, k):
    if k - val < 0:
        return False
    else:
        return True

for i in range(len(i2 + i3)):
    if bt  >= len(i3)and at  >= len(i2):
        break
    elif bt  >= len(i3):
        # pick form desk A
        val = int(i2[at])
        at += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    elif at  >= len(i2):
        val = int(i3[bt])
        bt += 1
        if isWithinBudget(val, k):
            k -= val
            t += 1
        else:
            break
    else:
        if int(i3[bt]) >= int(i2[at]):
            # pick form desk A
            val = int(i2[at])
            at += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break
        else:
            val = int(i3[bt])
            bt += 1
            if isWithinBudget(val, k):
                k -= val
                t += 1
            else:
                break

    if k < 0:
        break
print(t)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
aq = deque()
bq = deque()
res=0

for i in range(0,N):
  aq.append(alist[i])

for j in range(0,M):
  bq.append(blist[j])

while(0<K):
  if(len(aq)!=0):
    a=aq.popleft()
  else:
    a=10**10
  if(len(bq)!=0):
    b=bq.popleft()
  else:
    b=10**10
  if(a<b):
    res+=1
    K-=a
    if(K<0):
      res-=1
      break
    bq.appendleft(b)
  elif(b<a):
    res+=1
    K-=b
    if(K<0):
      res-=1
      break
    aq.appendleft(a)
  elif(a==b):
    res+=1  
    K-=a
    if(K<0):
      res-=1
      break
    res+=1
    K-=b
    if(K<0):
      res-=1
      break
print(res)

Submission status: Wrong Answer

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

A_np = np.array(A)
B_np = np.array(B)

C_np = np.append(A_np,B_np)
C_np = np.sort(C_np)

x = 0
y = 0
for i in range(len(C_np)):
    if K >= x + C_np[i]:
        x += C_np[i]
        y += 1
    else:
        break
print(x)

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 = 0
a = a[::-1]
b = b[::-1]
a_i = n-1
b_i = m-1
t = 0
while t+min(a[a_i], b[b_i]) < k:
    if a_i < 0 and b_i >= 0:
        if t+b[b_i] <= k:
            c += 1
            t += b[b_i]
            b_i -= 1
    elif b_i < 0 and a_i >= 0:
        if t+a[a_i] <= k:
            c += 1
            t += a[a_i]
            a_i -= 1
    elif a_i < 0 and b_i < 0:
        break
    elif t < k:
        if a[a_i] < b[b_i] and t+a[a_i] <= k:
            c += 1
            t += a[a_i]
            a_i -= 1
        elif a[a_i] > b[b_i] and t+b[b_i] <= k:
            c += 1
            t += b[b_i]
            b_i -= 1
    else:
        break

print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque()
li = list(map(int, input().split()))
for a in li: A.append(a)
B = deque()
li = list(map(int, input().split()))
for b in li: B.append(b)

ans = 0
flag = 0
while K >= 0:
    if A[0] < B[0]:
        t = A.popleft()
    else:
        t = B.popleft()
    K -= t
    ans += 1
    if len(A)*len(B) == 0:
        flag = 1
        break

if flag == 1:
    if len(B) == 0:
        while K >= 0 and len(A) > 0:
            t = A.popleft()
            K -= t
            ans += 1
    elif len(A) == 0:
        while K >= 0 and len(B) > 0:
            t = B.popleft()
            K -= t
            ans += 1

if K < 0: 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()))
a1 = 0
b1 = 0
c = []
         
while((len(a) != a1 or len(b) != b1)):
  if(len(a) == a1):
    c.append(b[b1]);b1+=1;continue
  if(len(b) == b1):
    c.append(a[a1]);a1+=1;continue
  
  if(a[a1] > b[b1]):
    c.append(b[b1]);b1+=1;continue
  else:
    c.append(a[a1]);a1+=1;continue

tmp = 0
ans = 0
for i in c:
  tmp += i
  if(tmp <= k):ans+=1
  else:print(ans);exit()
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()))
total = 0
ac = 0
bc = 0
alen = len(a)
blen= len(b)
allbook = alen + blen
count = 0
while True:
  if k < total:
    break
  if bc < blen:
    if ac < alen and(a[ac] < b[bc]):
      total += a[ac]
      ac += 1
      count += 1
      print("a", ac)
      continue
    else:
      total += b[bc]
      bc += 1
      count += 1
      print("b", bc)
      continue
  if ac < alen:
    if bc < blen and (a[ac] >= b[bc]):
      total += b[bc]
      bc += 1
      count += 1
      continue
    else:
      total += a[ac]
      ac += 1
      count += 1
      continue
  break
print(count)

Submission status: Wrong Answer

Solution code:
import random
n, m, k = map(int, input().split())
# n, m, k = random.randint(1, 200000), random.randint(1, 200000), random.randint(1, 10**9)
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
# a_list = [random.randint(1, 10**3) for i in range(n)]
# b_list = [random.randint(1, 10**3) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [a_list[0]]
b_sum = [b_list[0]]
max_a = 0
max_b = 0
for i in range(1, max(n, m)):
    if i < n:
        a_sum.append(a_sum[i-1] + a_list[i])
        if a_sum[i] < k:
            max_a = i+1
    if i < m:
        b_sum.append(b_sum[i-1] + b_list[i])

max_num = 0
max_a += 1
while max_a > 0:
    max_a -= 1
    while max_b < m and b_sum[max_b] <= k-a_sum[max_a-1]:
        max_b += 1
    if max_num < max_a + max_b:
        max_num = max_a + max_b

print(max_num)
# 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()))
ans = 0
cnt = 0

while K > 0:
	if A != [] and B != []:
		ele = min(A[0],B[0])
		if ele == A[0]:
			A.pop(0)
		else:
			B.pop(0)
		K -= ele
	elif A != []:
		ele = A.pop(0)
		K -= ele
	elif B != []:
		ele = B.pop(0)
		K -= ele
	cnt += 1

if K < 0:
  cnt -= 1

print(cnt)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

acc1 = list(accumulate(A))
acc2 = list(accumulate(B))

def is_ok(a, b):
    if acc1[a] + acc2[b] <= K:  return 1
    return 0

def binary_search(i, left, right):
    while (abs(right - left) > 1):
        mid = (left + right) // 2
        if is_ok(i, mid):  right = mid
        else:  left = mid
    return right

ans = 0
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  
      if acc1[i] <= K:  ans = max(1, ans)
    else:  ans = max(ans,  i + ret + 2)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
it = 200000
k = 0
count = 0
select = list()
for i in range(it):
  if len(A) == 0 and len(B) == 0:
    break
  elif len(A) == 0 and len(B) != 0:
    # Bの本のみ
    select = B
  elif len(B) == 0 and len(A) != 0:
    # Aの本のみ
    select = A
  elif A[0] <= B[0]:
    # BよりもAの方が早く読める(同じも含む)
    select = A
  elif A[0] > B[0]:
    select = B
  k += select[0]
  if k <= K:
    count += 1
  else:
    break
  select.pop(0)
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()))
total=0
i=0
j=0
while total<K:
    if i<N and j<M:
        if a[i]>=b[j]:
            if total+b[j]<=K:
                total+=b[j]
                j+=1
            else:
                break
        else:
            if total+a[i]<=K:
                total+=a[i]
                i+=1
            else:
                break
    elif N<i:
        if total+b[j]<=K:
            total+=b[j]
            j+=1
        else:
            break
    elif M<j:
        if total+a[i]<=K:
            total+=a[i]
            i+=1
        else:
            break
print(i+j)
print(total)

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]*N
b=[0]*M
a[0]=A[0]
b[0]=B[0]
for i in range(N-1):
    a[i+1]=a[i]+A[i]
for i in range(M-1):
    b[i+1]=b[i]+B[i]
diff=2*10**9
count=0
for i in range(N):
    j=bisect.bisect_right(b,K-a[i])-1
    if 0<=K-(a[i]+b[j])<diff:
        diff=K-(a[i]+b[j])
        count=(i+1)+(j+1)
print(count)

Submission status: Wrong Answer

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

idxA = idxB = 0
readTime :int = 0
readBooks :int = 0

while readTime < k:
    """
    if idxA < n and arA[idxA] <= arB[idxB]:
        readTime += arA[idxA]
        idxA += 1
    else:
        readTime += arB[idxB]
        idxB += 1
    """
    if idxA >= n:
        readTime += arB[idxB]
        idxB += 1
    elif idxB >= m:
        readTime += arA[idxA]
        idxA += 1
    elif arA[idxA] <= arB[idxB]:
        readTime += arA[idxA]
        idxA += 1
    else:
        readTime += arB[idxB]
        idxB += 1

    if readTime > k:
        break
    
    readBooks += 1

    if idxA >= n and idxB >= m:
        break

""" sortしたら上から順ではなくなるので NG
arAll = arA + arB
arAll.sort()

for idx in range(n+m):
    readTime += arAll[idx]
    if readTime > k:
        break
    readBooks += 1
"""

print(readBooks)

Submission status: Wrong Answer

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

d1 = stdin.readline().rstrip().split()
N = int(d1[0])
M = int(d1[1])
K = int(d1[2])
A = [int(s) for s in stdin.readline().rstrip().split()]
B = [int(s) for s in stdin.readline().rstrip().split()]

count = 0
minute = 0
posA = 0
posB = 0
while(True):
    if A[-1] == 0 and B[-1] == 0:
        break
    if A[-1] > 0 and B[-1] > 0 and A[posA] == B[posB]:
        lim = 0
        if len(A)-posA <= len(B)-posB:
            lim = len(A)-posA
        else:
            lim = len(B)-posB
        for i in range(1, lim):
            if A[posA+i] < B[posB+i]:
                minute += A[posA]
                A[posA] = 0
                posA += 1
                break
            if A[posA+i] > B[posB+i]:
                minute += B[posB]
                B[posB] = 0
                posB += 1
                break
        else:
            minute += A[posA]
            A[posA] = 0
            posA += 1
        if minute > K:
            break
        count += 1
        continue
    if A[-1] > 0 and (B[-1] == 0 or A[posA] < B[posB]):
        minute += A[posA]
        A[posA] = 0
        posA += 1
        if minute > K:
            break
        count += 1
        continue
    if B[-1] > 0 and(A[-1] == 0 or A[posA] > B[posB]):
        minute += B[posB]
        B[posB] = 0
        posB += 1
        if minute > K:
            break
        count += 1
        continue
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()))

from itertools import accumulate
aa=list(accumulate(a))
bb=list(accumulate(b))

from bisect import bisect_left,bisect

kmax=0
for i in range(n):
    if k<aa[i]:
        break
    kk=k-aa[i]
    k2=min(bisect(bb,kk),m)
#    print(i,k2)
    kmax=max(kmax,k2+i+1)

print(kmax)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

        sum_A = np.cumsum(A)
        sum_B = np.cumsum(B)

        max_ij = 0
        for i in range(len(sum_A)):
                if sum_A[i]<=K:
                        for j in range(len(sum_B)):
                                if sum_B[j]<=K:
                                        temp = sum_A[i]+sum_B[j]
                                        if temp<=K:
                                                max_ij = max(max_ij,i+j+2)
        ans = max_ij

        return int(ans)

if __name__=="__main__":
        ans = main()
        print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int,input().split())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
AL.extend(BL)
AAL = sorted(AL)

if sum(AAL) <= K:
    print(len(AAL))
    exit()

cnt = 0
ans = 0
for i in range(N+M):
    cnt += AAL[i]
    ans += 1
    if cnt >= K:
        print(ans-1)
        exit()

Submission status: Wrong Answer

Solution code:
def calc(A, B, K):
  t = 0
  ai = 0
  bi = 0
  
  if K < A[ai] and K < B[bi]:
    return 0
  
  al = len(A)
  bl = len(B)
  
  while True:
    if al > ai and bl > bi:
      if A[ai] <= B[bi]:
        t += A[ai]
        #print(t)
        if t >= K:
          break;
        ai += 1
        #print(ai)
      else:
        t += B[bi]
        #print(t)
        if t >= K:
          break;
        bi += 1
        #print(bi)
        
    elif al > ai:
      if A[ai] <= B[bi-1]:
        t += A[ai]
        #print(t)
        if t >= K:
          break;
        ai += 1
        #print(ai)
      else:
        break;
        
    elif bl > bi:
      if B[bi] <= A[ai-1]:
        t += B[bi]
        #print(t)
        if t >= K:
          break;
        bi += 1
        #print(bi)
      else:
        break;
    else:
      break;
      
  return ai + bi

N, M, K = map(int, input().split())
a = input().split()
A = [int(b.strip()) for b in a]
b = input().split()
B = [int(a.strip()) for a in b]

#print(N)
#print(M)
#print(K)
#print(A)
#print(B)


print(calc(A, B , K))

Submission status: Wrong Answer

Solution code:
from collections import deque
def resolve():
    N, M, K = list(map(int, input().split()))
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))

    read = deque()
    w = 0

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

    while w <= K:        
        if len(A) == 0 and len(B) == 0:
            break

        if len(A) == 0:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            B.pop()
            continue

        if len(B) == 0:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            A.pop()
            continue

        if A[0] < B[0] or len(B) == 0:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            A.pop()
        else:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            B.pop()

    print(len(read))


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()))
temp=0
an,bn=0,0
ans=0
bf=[]
flag=0
while temp<=k:
    if an==n and bn==m: break
    if an>=n:
        if flag:
            b=bf[:]+b[:]
            bn-=len(bf)
            bf=[]
            flag=0
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        if flag:
            a=bf[:]+a[:]
            an-=len(bf)
            bf=[]
            flag=0
        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()))

ans = 0
bb = 0
for i in range(len(a)-1):
    s = sum(a[0:len(a)-i])
    if s <= k:
        if ans < len(a)-i:
            ans = len(a)-i
        s = s + sum(b[0:bb])
        bbb = bb
        for j in range(bbb, len(b)):
            s = s + b[j]
            bb = j+1
            if s <= k:
                if ans < len(a)-i+j+1:
                    ans = len(a)-i+j+1
            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()))
aa = [0] 
bb = [0]
for i in range(m):
    bb.append(bb[-1]+B[i])
for i in range(n):
    aa.append(aa[-1]+A[i])
aa.append(aa[-1]+1)
bb.append(bb[-1]+1)

#print(aa)
val =0
a = 0
ans = 0

for i in range(n+1):
    temp = k-aa[i]
    if temp<=0:
        break
    s = 0
    l = m+1
    mid = l//2
    while l-s>1:
        if temp>bb[mid]:
            s = mid
            mid = (s+l)//2
        elif temp==bb[mid]:
            s = mid
            break
        else:
            l = mid
            mid = (s+l)//2
   # print(s)
    #print(ans, s+i)
    ans = max(ans, s+i)
print(ans)