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:
line = input().strip().split()
n = int(line[0])
m = int(line[1])
k = int(line[2])

line = input().strip().split()
a = []
for i in line:
    a.append(int(i))

line = input().strip().split()
b = []
for i in line:
    b.append(int(i))

sum_a = [0]
sum = 0
for i in range(n):
    sum += a[i]
    sum_a.append(sum)

sum_b = [0]
sum = 0
for i in range(m):
    sum += b[i]
    sum_b.append(sum)

sum_a2 = [i for i in sum_a if i <= k]
sum_b2 = [i for i in sum_b if i <= k]

ans = 0
for i in range(len(sum_a2)):
    rem = k - sum_a2[i]
    b_candidate = [i for i in sum_b2 if i <= k]
    b_len = len(b_candidate)
    if ans < i + b_len - 1:
        ans = i + b_len - 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m, k = map(int, input().split())
Alist = deque()
Blist = deque()
Alist.extend(list(map(int, input().split())))
Blist.extend(list(map(int, input().split())))

ABlist = deque()
book = 0
nm = n+m
while (len(ABlist) <= nm):
    if (len(Alist) == 0):
        ABlist.extend(Blist)
        break
    elif (len(Blist) == 0):
        ABlist.extend(Alist)
        break

    if (Alist[0] < Blist[0]):
        ABlist.append(Alist[0])
        Alist.popleft()
    else:
        ABlist.append(Blist[0])
        Blist.popleft()

while (len(ABlist) > 0):
    if (k - ABlist[0] >= 0):
        k -= ABlist[0]
        ABlist.popleft()
        book += 1
    else:
        break
print(book)

Submission status: Wrong Answer

Solution code:

def solve():
    total = 0
    cnt = 0
    i = 0
    j = 0
    while cnt<N+M:
#        print(i,j)
        if i==N and j==M:
            break
        elif j == M:
            if total + A[i] <= K:
                total+=A[i]
                i += 1
            else:
                break
        elif i == N:
            if total + B[j] <= K:
                total+=B[j]
                j += 1
            else:
                break
        elif A[i] <= B[j]:
            if total + A[i] <= K:
                total+=A[i]
                i += 1
            else:
                break
        else:
            if total + B[j] <= K:
                total+=B[j]
                j += 1
            else:
                break
    print(i+j)

if __name__=="__main__":
    N,M,K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AB = A+B
    AB.sort()
#    print(AB)
    solve()

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()))
cumsum_a = [0]*(n+1)
cumsum_b = [0]*(m+1)
ai = 0
bi = 0
for i in range(1, n+1):
    cumsum_a[i] = cumsum_a[i-1] + a[i-1]
    ai = i
    if cumsum_a[i] > k:
        ai = i-1
        break
for i in range(1, m+1):
    cumsum_b[i] = cumsum_b[i - 1] + b[i-1]
    bi = i
    if cumsum_b[i] > k:
        bi = i-1
        break

ans = 0

for i in range(ai, 0, -1):
    for j in range(bi, 0, -1):
        if cumsum_a[i] + cumsum_b[j] <= k:
            ans = max(ans, i + j)
            break

print(ans)

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()))
lA = len(A)
lB = len(B)

i = 0
j = 0
h = 0
dp = [-1] * 410000
dp[0] = 0
while dp[h] <= K:
    if i >= lA and j >= lB:
        print(lA + lB)
        sys.exit(0)
    h += 1
    #print(h)
    if i < lA:
        dp[h] = dp[h-1] + A[i]
        #print(dp[h])
    else:
        dp[h] = 10 ** 18
    if j < lB:
        if dp[h] >= dp[h-1] + B[j]:
            dp[h] = dp[h-1] + B[j]
            #print(dp[h])
            j += 1
        else:
            i += 1
    else:
        i += 1
    #print(i,j)

print(h-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
ans = 0
n, m, k = map(int,input().split())
a = deque(list(map(int,input().split())))
b = deque(list(map(int,input().split())))
result = 0

while True:
    if len(a) == 0 or len(b) == 0:
        a.append(10**10)
        b.append(10**10)
    if ans > k:
        print(result-1)
        break
    if a[0] + b[0] == 2*(10**10):
        print(result)
        break

    if a[0] > b[0]:
        ans += b.popleft()
        result += 1
    else:
        ans += a.popleft()
        result += 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()))
countA=2
countB=2
shoyou=K
tot=0
sumA=[0,A[0]]
sumB=[0,B[0]]
mmm=True

for i in range(len(A)-1):
    sumA.append(A[countA-1]+sumA[countA-1])
    countA+=1
for i in range(len(B)-1):
    sumB.append(B[countB-1]+sumB[countB-1])
    countB+=1
for i in range(1,N+M+1):
    for j in range(0,min(i,len(sumB))):
        if 0<=i-j<=len(sumA)-1 and j<=len(sumB)-1:
            if sumA[i-j]+sumB[j]>K:
                mmm=False
                break
        elif i-j<0 or len(sumA)-1<i-j or j>=len(sumB):
            break
    if mmm==True:
        tot+=1
    elif mmm==False:
        print(tot)
        break
if mmm==True:
    print(tot)

Submission status: Wrong Answer

Solution code:

def main():
    N, M, K = map(int, input().split()) 
    A = list(map(int, input().strip().split()))
    B = list(map(int, input().strip().split()))
    
    x = 0
    y = 0
    time = 0
    books = 0

    while time <= K:
        if x < N :
            if y < M:
                if A[x] <= B[y]:
                    if time + A[x] <= K:
                        time = time + A[x]
                        x = x + 1
                        books = books + 1
                    else:
                        return books
                else:
                    if time + B[y] <= K:
                        time = time + B[y]
                        y = y + 1
                        books = books + 1
                    else:
                        return books
            elif y >= M:
                if time + A[x] <= K:
                    time = time + A[x]
                    x = x + 1
                    books = books + 1
                else:
                    return books
        elif x >= N and y < M:
            if time + B[y] <= K:
                time = time + B[y]
                y = y + 1
                books = books + 1
            else:
                return books
        else:
            return books

def main_2():
    books = main()
    print(books)

main_2()

Submission status: Wrong Answer

Solution code:
import random
N, M, K=[int(i) for i in input().split(" ")]
a=[int(i) for i in input().split(" ")]
b=[int(i) for i in input().split(" ")]

while True:
    if (N>10**8) | (M>10**8) |(K>10**8):
        if random.random()>0.85:
            print(0)
            break
    time=0
    a_idx=0
    b_idx=0
    count=0
    while time<K:
        count+=1
        try:
            if a_idx+1==N:
                time+=b[b_idx]
                b_idx+=1
                if time>K:
                    count-=1
                continue
            elif b_idx+1==M:
                time+=a[b_idx]
                a_idx+=1
                if time>K:
                    count-=1
                continue    

            if a[a_idx]<b[b_idx]:
                time+=a[a_idx]
                a_idx+=1

            elif a[a_idx]>b[b_idx]:
                time+=b[b_idx]
                b_idx+=1
            else:
                try:
                    a_com=a_idx
                    b_com=b_idx
                    while True:
                        if a[a_com]>b[b_com]:
                            time+=a[a_idx]
                            a_idx+=1
                            break
                        elif a[a_com]<b[b_com]:
                            time+=b[b_idx]
                            b_idx+=1
                            break
                        a_com+=1
                        b_com+=1
                except:
                    time+=a[a_idx]
                    a_idx+=1
            if time>K:
                count-=1            
        except:
            break
    print(count)
    break

Submission status: Wrong Answer

Solution code:
from queue import deque

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

A = input().split()
B = input().split()

que = deque([(0, K, 0, 0)])

max_count = 0

while que:
    count, k, a_index, b_index = que.popleft()
    max_count = max(max_count, count)
    if a_index < len(A) and b_index < len(B):
        break
    if a_index < len(A) and 0 <= k-int(A[a_index]):
        que.appendleft((count+1, k-int(A[a_index]), a_index+1, b_index))
    if b_index < len(B) and 0 <= k-int(B[b_index]):
        que.appendleft((count+1, k-int(B[b_index]), a_index, b_index+1))

print(max_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()))
k=0
c=0
if sum(a) + sum(b) <= K:
    print(N+M)
else:
    while k <= K:
        if len(a) > 0 and len(b)>0:
            if (a[0] < b[0]) and ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)

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


        elif (len(a) > 0) and (len(b) ==0):
            if ((k+a[0]) <=K):
                k+=a[0]
                c+=1
                a.pop(0)
            else:
                break
        elif (len(b) > 0) and (len(a) ==0):
            if ((k+b[0]) <=K):
                k+=b[0]
                c+=1
                b.pop(0)
            else:
                break
        else:
            break
    print(c)

Submission status: Wrong Answer

Solution code:
l = input().split()
N, M, K = int(l[0]), int(l[1]), int(l[2])
A = input().split()
B = input().split()

cnt = 0
t = 0
i = j = 0
while t < K:
    if i < N:
        a = int(A[i])
    if i >= N:
        a = 0
    if j < M:
        b = int(B[j])
    if j >= M:
        b = 0
    subt = t
    if (a >= b and b != 0) or (b != 0 and a == 0):
        subt += b
        j += 1
    if (a < b and a != 0) or (a != 0 and b == 0):
        subt += a
        i += 1
    if subt > K or cnt >= N+M:
        break
    if subt <= K:
        t = subt
        cnt += 1

print(cnt)

Submission status: Wrong Answer

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

time=0
ans=0
count=0
for i in range(N):
  if(time+A[i]<=K):
    time+=A[i]
    count+=1
  else:
    break
ans=count
btime=0
bcount=0
for i in range(M):
  if(btime+B[i]<=K):
    btime+=B[i]
    bcount+=1
  else:
    break
a=0
b=0
time+=btime
if(count+bcount==0):
  print(0)
  exit()
if(count==N and bcount==M):
  print(N+M)
  exit()
for i in range(count+bcount):
  if(b<bcount and a<count):
    if(A[count-1-a]<=B[bcount-1-b]):
      time-=B[bcount-1-b]
      b+=1
    else:
      time-=A[count-1-a]
      a+=1
  elif(b>=bcount):
    time-=A[count-1-a]
    a+=1
  elif(a>=count):
    time-=B[bcount-1-b]
    b+=1
  if(time<=K):
    print(count+bcount-a-b)
    exit()

Submission status: Wrong Answer

Solution code:
import numpy as np
n, m, k = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))

i, j = 0, 0
readed = 0
count = 0
tmp1 = []
tmp2 = []
for i in range(n, 0, -1):
    for j in range(m, 0, -1):
        tmp1.append(sum(a[:i])+sum(b[:j]))
        tmp2.append(i+j)
tmp1 = np.array(tmp1)
tmp = sorted(list(np.where(tmp1 > k)[0]),reverse=True)
if len(tmp) != 0:
    for i in tmp:
        tmp2.pop(i)
if len(tmp2) == 0:
    print(0)
else:
    print(max(tmp2))

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()))
subseqA = [0] # i th element is sum of first i books
subseqB = [0]
for elem in a:
    subseqA.append(subseqA[-1]+elem)
for elem in b:
    subseqB.append(subseqB[-1]+elem)
maxBooks = 0
curABooks = 0
curBBooks = 0
for i in range(N+1):
    if K < subseqA[i]:
        curABooks = i - 1
        maxBooks = i - 1
        break
for i in range(curABooks,-1,-1):
    for j in range(curBBooks + 1,M+1):
        if subseqA[i] + subseqB[j] > K:
            curBBooks = j - 1
            break
    if i + curBBooks > maxBooks:
        maxBooks = i + curBBooks
print(maxBooks)

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
for i in range(n):
  cnt+=a[i]
  if cnt>k:
    cnt-=a[i]
    break
  else:
    ans+=1
j=i
kari=ans
a=a[::-1]
for i in range(m):
  cnt+=b[i]
  kari+=1
  if j<n:
    while cnt>k:
      cnt-=a[j]
      j+=1
      if j==n:        
        break
      kari-=1
  if cnt<=k:
    ans=max(ans,kari)
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()))
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
else:
    d = 0
    A = deque(A)
    B = deque(B)
    while sum(A) + sum(B) > K:
        if len(A) == 0:
            B.pop()
            d += 1
        elif len(B) == 0:
            A.pop()
            d += 1
        else:
            if A[-1] <= B[-1]:
                B.pop()
                d += 1
            else:
                A.pop()
                d += 1
    print(N + M - d +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()))
res=k
a_read=0
b_read=0
u=0
while res>=0:
  if (a_read==n and b_read==m):
    u=1
    break
  elif (a_read==n):
    res-=b[b_read]
    b_read+=1
  elif (b_read==m):
    res-=a[a_read]
    a_read+=1
  elif (a[a_read]<=b[b_read]):
    res-=a[a_read]
    a_read+=1
  else:
    res-=b[b_read]
    b_read+=1
#if u==1:
print(n+m)
#else:
  #print(a_read+b_read-1)

Submission status: Wrong Answer

Solution code:
m,n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = a + b
d = sorted(c)
count = 0
x = 0
for i in range(len(c)):
  if k >= count:
    count = count + c[i]
    x = x+1
  else:
    pass
print(x)

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=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ans=0
if a[0]>k and b[0]>k:
  print(0)
  exit()
for x in range(n):
  for y in range(m):
    if sum(a[0:x])+sum(b[0:y])<=k:
      ans=max(ans, x+y+2)
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()))

suma = [ -1 for i in range(n) ]
sumb = [ -1 for i in range(m) ]
suma[0] = a[0]
sumb[0] = b[0]
nn = n
mm = m
for i in range(1,n):
  suma[i] = suma[i - 1] + a[i]
  if suma[i] > k:
    nn = i
    break
for i in range(1,m):
  sumb[i] = sumb[i - 1] + b[i]
  if sumb[i] > k:
    mm = i
    break

c = 0
t = 0
cc = 0
for i in range(nn):
  if suma[i] <= k:
    t = suma[i]
    c = i + 1
    cc = max(cc,c)
  else:
    t = 0
    c = 0
  for j in range(mm):
    tt = t + sumb[j]
    if tt <= k:
      cc = max(cc,c + j + 1)
    else:
      break

print(cc)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
sum = 0
count = 0
a_sum = 0
res = []
b_list = []
for bt in b:
    sum += int(bt)
    if sum <= k:
        count += 1
        b_list.append(sum)
    else:
        break
#print(b_list)
res.append(count)
count = 0
ia = 1
for at in a:
    a_sum += int(at)
    if k < a_sum:
        print(max(res))
        sys.exit()
    count = ia
    sum = a_sum
    for bsum in b_list:
        sum = a_sum + bsum
        if k < sum:
            print(max(res))
            sys.exit()
        else:
            count += 1
    ia += 1
    res.append(count)
print(max(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()))
time = 0
count = 0
i = -1
A = 0
while i != n-1:
    i += 1
    A += a[i]
    if A > k:
        break
    j = -1
    B = 0
    while j != m-1:
        j += 1
        B += b[j]
        if A + B > k:
            break
    temp = (i+1) + (j+1) - 1
    if temp > count:
        count = temp
print(count)

Submission status: Wrong Answer

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

from collections import deque

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

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

accum_a = [0]
accum_b = [0]


for a_i in range(0,len(an)):


    sumCandidate = accum_a[-1] + an[a_i]
    if sumCandidate > k:
        break
    accum_a.append(sumCandidate)

for b_i in range(0,len(bn)):
    i_ = accum_b[-1] + bn[b_i]
    if i_ > k:
        break
    accum_b.append(i_)


def Find(a, b, k1):
    if a[-1] + b[-1] <= k1:
        return len(a) - 1 + len(b) - 1

    if len(a) > len(b):
        rest = k - a[-1]
        for i in range(len(b)):
            if rest <= b[-i]:
                return len(a) - 1 + len(b) - 1 - i

    if len(b) > len(a):
        rest = k - b[-1]
        for i in range(len(a)):
            if rest <= a[-i]:
                return len(b) - 1 + len(a) - 1 - i

    if a[-1] > b[-1]:
        rest = k - b[-1]
        return Find(a, b[0:len(b)-1], rest)+1

    rest = k1 - a[-1]
    return Find(a[0:len(a)-1],b,rest)+1

print(Find(accum_a, accum_b, k))

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
t_sum = 0
cnt = 0
flag = True

for _ in range(N+M):
    
  if a == N:
    for _ in range(M+1):
      if b == M:
        flag = False
        break
      t_sum += B[b]
      cnt += 1
      b += 1
      if t_sum > K:
        cnt -= 1
        flag = False
        break
  if b == M:
    for _ in range(N+1):
      if a == N:
        flag = False
        break
      t_sum += A[a]
      cnt += 1
      a += 1
      if t_sum > K:
        cnt -= 1
        flag = False
        break
  if not flag:
    break
    
  if A[a] <= B[b]:
    t_sum += A[a]
    cnt += 1
    a += 1
  elif A[a] > B[b]:
    t_sum += B[b]
    cnt += 1
    b += 1
  
  if t_sum > K:
    cnt -= 1
    break

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = deque([int(i) for i in readline().split()])
    B = deque([int(i) for i in readline().split()])
    cnt = 0
    while (len(A) > 0) or (len(B) > 0): 
        if K <= 0:
            break
        if (len(A) > 0) and (len(B)) > 0:
            a = A.popleft()
            b = B.popleft()
            if a <= b:
                K -= a
                B.appendleft(b)
            else:
                K -= b
                A.appendleft(a)
        elif (len(A) > 0) and (len(B) == 0):
            a = A.popleft()
            K -= a
        elif (len(A) == 0) and (len(B) > 0):
            b = B.popleft()
            K -= b
        if K >= 0:
            cnt += 1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
A = [int(p) for p in input().split()]
B = [int(p) for p in input().split()]
Af = False
Bf = False
count = 0

for _ in range(n+m):
  if k<=0:
    break
  if Af and Bf:
    break
  if A[0] > k and B[0] > k:
    break
  if Af:
    k -= B.pop(0)
    count += 1
    if len(B)==0:
      B.append((10**9)+1)
      Bf = True
    continue
  if Bf:
    k -= A.pop(0)
    count += 1
    if len(A)==0:
      A.append((10**9)+1)
      Af = True
    continue
  if A[0] < B[0]:
    k -= A.pop(0)
    count += 1
    if len(A)==0:
      A.append((10**9)+1)
      Af = True
      continue
  else:
    k -= B.pop(0)
    count += 1
    if len(B)==0:
      B.append((10**9)+1)
      Bf = True
print(count)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

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

sA=[A[0]]
for i in range(1,n):
    sA.append(sA[-1]+A[i])
sB=[B[0]]
for i in range(1,m):
    sB.append(sB[-1]+B[i])

ans=0
for i in range(n):
    temp=k-sA[i]
    if temp<0:
        break
    add=bisect_right(sB,temp)
    ans=max(ans,i+1+add)

print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
d1=input()
d2=input()
a=a.split(' ')
d1=d1.split(' ')
d2=d2.split(' ')
a=[int(i) for i in a]
d1=[int(i) for i in d1]
d2=[int(i) for i in d2]
c=0
t=0
while t<a[2] :
    if len(d1)>0 and len(d2)>0:
        if d1[0]<d2[0]:
            t+=d1[0]
            if t<a[2]:
                c+=1
                d1.remove(d1[0])
        else:
            t+=d2[0]
            if t<a[2]:
                c+=1
                d2.remove(d2[0])
    elif len(d1)>0:
        t+=d1[0]
        if t<a[2]:
            c+=1
            d1.remove(d1[0])
    elif len(d2)>0:
        t+=d2[0]
        if t<a[2]:
            c+=1
            d2.remove(d2[0])
    else:
        c+=0
print(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()))
a = 0
b = 0
ans = 0
sumA = 0
sumB = 0
for i in range(len(A)):
  if sumA + A[i] <= k:
    a += 1
  else:
    break

for i in range(len(B)):
  if sumB +B[i] <= k:
    b += 1
  else:
    break
#print(a)
#print(b)

su = sum(A[0:a]) + sum(B[0:b])
if su <= k:
  print(a+b)
else:
  for i in range(min(a,b)):
    ans += 1
    if A[a-1] >= B[b-1]:
      if su - A[a-1] <= k:
        break
      else:
        a -=1
    if A[a-1] < B[b-1]:
      if su - B[b-1] <= k:
        break
      else:
        b -=1   
  print(a+b)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m, k = map(int, input().split())
Alist = deque()
Blist = deque()
Alist.extend(list(map(int, input().split())))
Blist.extend(list(map(int, input().split())))

ABlist = deque()
book = 0
nm = n+m
while (len(ABlist) <= nm):
    if (len(Alist) == 0):
        ABlist.extend(Blist)
        break
    elif (len(Blist) == 0):
        ABlist.extend(Alist)
        break

    if (Alist[0] < Blist[0]):
        ABlist.append(Alist[0])
        Alist.popleft()
    else:
        ABlist.append(Blist[0])
        Blist.popleft()


while (len(ABlist) > 0):
    if (k - ABlist[0] > 0):
        k -= ABlist[0]
        ABlist.popleft()
        book += 1
    else:
        break
print(book)

Submission status: Wrong Answer

Solution code:
def main():
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    a = [0] * (len(A) + 1)

    for i in range(0, len(A)+1):
        if i == 0:
            a[i] = 0
        else:
            a[i] = a[i-1] + A[i-1]
        if a[i] > K:
            break
    l = 0
    ans = i
    if (i < 0):
        ans = 0
        k = K
        while l < len(B):
            if k > B[l]:
                k = k - B[l]
                l = l + 1
            else:
                break
        ans = max(ans,  l)
    else:
        i = i - 1
        k = K - a[i]
        for j in range(i, 0, -1):
            while l < len(B):
                if k > B[l]:
                    k = k - B[l]
                    l = l + 1
                else:
                    break
            ans = max(ans, j + l)
            k = k - A[j-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

A_sum = 0
A_max = 0
B_sum = 0
B_max = 0
best_num = 0
A_max = N
flag = True # Aだけ読んで時間が尽きるかどうか

# Aを最大まで読んだ時の冊数と時間
for i in range(N):
    A_sum += A[i]
    if A_sum > K:
        A_sum -= A[i]
        A_max = i
        flag = False
        break
A = A[0:A_max]
now_num = A_max # 今の読んだ数
mixsum = A_sum # A,Bを合わせた時間
b_num = 0 # bを読んだ数
if flag:
    now_num += M
    b_num = M
    for i in range(M):
        mixsum += B[i]
        if mixsum > K:
            mixsum -= B[i]
            now_num = A_max + i
            b_num = i  #i冊読んだので次取るならi番目から
            break
A.reverse()
best_num = now_num
for i in range(A_max):
    mixsum -= A[i]
    now_num -= 1
    for j in range(b_num, M):
        mixsum += B[j]
        now_num += 1
        if mixsum > K:
            mixsum -= B[j]
            now_num -= 1
            break
    if now_num > best_num:
        best_num = now_num
print(best_num)

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]:
        kmax=max(kmax,i)
        break
    kk=k-aa[i]
#    k2=min(bisect(bb,kk),m-1)
    k2=bisect(bb,kk)
    if k2<m-1 and bb[k2+1]==kk:
        k2+=1
#    print(i,k2)
    kmax=max(kmax,k2+i+1)
kmax=min(kmax,m+n)

print(kmax)

Submission status: Wrong Answer

Solution code:
# 初期入力
from collections import deque
import sys
input = sys.stdin.readline
N,M,K = (int(i) for i in input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dq_a =deque(A)
dq_b =deque(B)

#上から読む＝配列の前から読む
read_time =0
ans =0
a=dq_a.popleft()
b=dq_b.popleft()
for i in range(N+M):
    if a <=b :
        if read_time +a <=K:
            read_time +=a
            ans +=1
            if len(dq_a) >0:
                a =dq_a.popleft()
            else:
                a=10**9 +1
            continue
        else:
            break
    if a >=b:
        if read_time +b <=K:
            read_time +=b
            ans +=1
            if len(dq_b) >0:
                b =dq_b.popleft()
            else:
                b=10**9 +1
        else:
            break
      
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
A_Read = 0
B_Read = 0
A_Finish = 0
B_Finish = 0
ans = 0
while K >= 0:
    if A_Finish == 1 and B_Finish == 1:
        break
    elif A_Finish == 1:
        K -= B_list[B_Read]
        # print(B_list[B_Read])
        B_Read += 1
        if B_Read == M:
            B_Finish = 1
            break
    elif B_Finish == 1:
        K -= A_list[A_Read]
        # print(A_list[A_Read])
        A_Read += 1
        if A_Read == N:
            A_Finish = 1
            break
    else:
        if A_list[A_Read] <= B_list[B_Read]:
            K -= A_list[A_Read]
            # print(A_list[A_Read])
            A_Read += 1
            if A_Read == N:
                A_Finish = 1
        else:
            K -= B_list[B_Read]
            # print(B_list[B_Read])
            B_Read += 1
            if B_Read == M:
                B_Finish = 1
    # print(K)
    if K < 0:
        break
    elif K == 0:
        ans += 1
        break
    # print(K)
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
elif (min(A) > K) or (min(B) > K):
    print(0)
else:
    d = 0
    A = deque(A)
    B = deque(B)
    while sum(A) + sum(B) > K:
        if len(A) == 0:
            B.pop()
            d += 1
        elif len(B) == 0:
            A.pop()
            d += 1
        else:
            if A[-1] <= B[-1]:
                B.pop()
                d += 1
            else:
                A.pop()
                d += 1
    print(N + M - d +1)