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:
def resolve():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    count = 0
    while len(A) != 0 and len(B) != 0:
        if A[0] <= B[0]:
            ans += A[0]
            A = A[1:]
        else:
            ans += B[0]
            B = B[1:]
        if ans >= K:
            print(count)
            exit()
        count += 1
    if len(A) != 0 and len(B) == 0:
        for i in range(len(A)):
            ans += A[0]
            A = A[1:]
        if ans >= K:
            print(count)
            exit()
        count += 1
    elif len(A) == 0 and len(B) != 0:
        for i in range(len(B)):
            ans += B[0]
            B = B[1:]
        if ans >= K:
            print(count)
            exit()
        count += 1
    print(count)
resolve()

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
        ans -= 1
        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:
N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

time = 0
count = 0

if((A_list[0]>K)and(B_list[0]>K)):
  print("0")
  exit()
while(time<K):
  if((len(A_list)==0)and(len(B_list)==0)):
    #print("1")
    break
  elif(len(A_list)==0):
    #print("2")
    time += B_list[0]
    del B_list[:1]
  elif(len(B_list)==0):
    #print("3")
    time += A_list[0]
    del A_list[:1]
  elif(A_list[0]==B_list[0]):
    if(len(A_list)==1):
      #print("4")
      time += B_list[0]
      del B_list[:1]
    elif(len(B_list)==1):
      #print("5")
      time += A_list[0]
      del A_list[:1]
    elif(A_list[1]<B_list[1]):
      #print("6")
      time += A_list[0]
      del A_list[:1]
    else:
      #print("7")
      time += B_list[0]
      del B_list[:1]
  elif(A_list[0]>B_list[0]):
    #print("8")
    time += B_list[0]
    del B_list[:1]
  else:
    #print("9")
    time += A_list[0]
    del A_list[:1]
  #print(A_list,B_list)
  #print(time)
  if(time<=K):
    count += 1
    
    
print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    a, b = deque(A), deque(B)
    ans = 0
    while 1:
        if a[0] < b[0]:
            if K < a[0]:
                break
            else:
                K -= a.popleft()
                a.append(float('inf'))
                ans += 1
        else:
            if K < b[0]:
                break
            else:
                K -= b.popleft()
                b.append(float('inf'))
                ans += 1
    print(ans)


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()))
cnt_a=0
cnt_b=0
ans=0
num_a=0
num_b=0

for i in range(n):
    num_a+=a[i]
    if i==n-1:
        num_a=n
        break
    if num_a>k:
        num_a=i
        break
for i in range(m):
    num_b+=b[i]
    if i ==m-1:
        num_b=m
        break
    if num_b>k:
        num_b=i
        break

num=max(num_a,num_b)
for i in range(200000):
    if cnt_a<n and cnt_b<m:
        if a[cnt_a]<=b[cnt_b]:
            k-=a[cnt_a]
            if k<0:
                break
            ans+=1
            cnt_a+=1
        else:
            k-=b[cnt_b]
            if k<0:
                break
            ans+=1
            cnt_b+=1
    elif cnt_a<n and cnt_b>=m:
        k-=a[cnt_a]
        if k<0:
            break
        ans+=1
        cnt_a+=1
    elif cnt_a>=n and cnt_b<m:
        k-=b[cnt_b]
        if k<0:
            break
        ans+=1
        cnt_b+=1
    else:
        break
print(max(num,ans))

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 and len(bq)==0):
    break
  else:
    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:
a = input().split(" ")
N = int(a[0])
M = int(a[1])
K = int(a[2])
Ai = input().split(" ")
Bi = input().split(" ")
Ai = [int(s) for s in Ai]
Bi = [int(s) for s in Bi]
time_ = int(K)
time = 0
acount = 0
bcount = 0

for i in range(10000000000000000):
    try:
        time += Ai[i]
        if time > time_:
            time -= Ai[i]
            break
        acount += 1
    except IndexError:
        break
atime = time
time = 0
for i in range(10000000000000000):
    try:
        time += Bi[i]
        if time > time_:
            time -= Bi[i]
            break
        bcount += 1
    except IndexError:
        break
btime = time
allc = bcount
if bcount > acount:
    hic = bcount
else:
    hic = acount
# AiからBiに移す
for i in range(acount):
    ci = i
    try:
        while time + Ai[ci] <= time_:
            time += Ai[ci]
            allc += 1
            ci += 1
        if hic < allc:
            hic = allc
    except IndexError:
        continue
    try:
        time -= Bi[bcount-1]
        bcount -= 1
        allc -= 1
    except IndexError:
        pass
    try:
        while time + Ai[ci] <= time_:
            time += Ai[ci]
            allc += 1
            ci += 1
        if hic < allc:
            hic = allc
    except IndexError:
        break

print(hic)

Submission status: Wrong Answer

Solution code:
def main ():
    X = input()
    X = X.split(' ')
    N = int(X[0])
    M = int(X[1])
    K = int(X[2])
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
  
    total = 0
    posA = 0
    posB = 0
    time = 0
    z = 0
    while z == 0:
        if posA < len(A) and A[posA] < B[posB]:
            if time + A[posA] <= K:
                time += A[posA]
                if posA != len(A) - 1:
                    posA += 1
                total += 1
            else:
                print(total)
                return
        elif  posB < len(B) and A[posA] > B[posB]:
            if time + B[posB] <= K:
                time += B[posB]
                if posB != len(B) - 1:
                    posB += 1
                total += 1
            else:
                print(total)
                return
        else:
            print(0)
            return
main ()

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
ali = [int(i) for i in input().split()]
bli = [int(i) for i in input().split()]
ti = 0
count = 0

while ti <= k:
  if ali == [] and bli ==[]:
    break
  if ali == [] and bli != []:
    if ti + bli[0] > k:
      break
    else:
      ti += bli[0]
      del bli[0]
      count += 1
  elif bli == [] and ali != []:
    if ti + ali[0] > k:
      break
    else:
      ti += ali[0]
      del ali[0]
      count += 1
  else:
    if ti + min(ali[0], bli[0]) > k:
      break
    elif ali[0] >= bli[0] and ti + bli[0] <= k:
      ti += bli[0]
      del bli[0]
      count += 1
    elif ali[0] < bli[0] and ti + ali[0] <= k:
      ti += ali[0]
      del ali[0]
      count += 1
    
print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m,k = map(int,input().split())
a = deque(map(int,input().split()))
b = deque(map(int,input().split()))
sorts = []
while a or b:
    if not a:
       sorts.append(b.popleft())
    elif not b:
        sorts.append(a.popleft())
    else:
        if a[0] > b[0]:
            sorts.append(b.popleft())
        else:
            sorts.append(a.popleft())

ans = 0
count = 0
for i in range(len(sorts)):
    ans += sorts[i]
    count += 1
    if ans > k:
        print(count - 1)
        exit()
print(count)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline().rstrip('\n')
    

def calc(N, M, K, A, B):
    result = 0
    na = 0
    if A[0] <= K:
        na = 1
        for i in range(1, len(A)):
            A[i] += A[i - 1]
            if A[i] > K:
                break
            na += 1
    nb = 0
    if B[0] <= K:
        nb = 1
        for i in range(1, len(B)):
            B[i] += B[i - 1]
            if B[i] > K:
                break
            nb += 1
    for i in range(na - 1, -1, -1):
        for j in range(nb - 1, -1, -1):
            if A[i] + B[j] <= K:
                break
        result = max(i + j + 2, result)
    return result


(N, M, K) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
print(calc(N,M,K,A,B))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from bisect import bisect
from collections import deque
def main():
    n, m, k = map(int, input().split())
    a = deque(map(int, input().split()))
    b = deque(map(int, input().split()))
    aa = tuple(accumulate(a))
    ba = tuple(accumulate(b))
    aa1 = bisect(aa,k)
    ba1 = bisect(ba,k)
    if aa1 > ba1:
        r = aa1
        time = aa[r-1]
        while b:
            time += b.popleft()
            if time <= k:
                r += 1
    elif aa1 < ba1:
        r = ba1
        time = ba[r - 1]
        while a:
            time += a.popleft()
            if time <= k:
                r += 1
    else:
        if aa[aa1-1] < ba[ba1-1]:
            r = aa1
            time = aa[r-1]
            while b:
                time += b.popleft()
                if time <= k:
                    r += 1
        else:
            r = ba1
            time = ba[r - 1]
            while a:
                time += a.popleft()
                if time <= k:
                    r += 1
    print(r)


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()))
result = 0
count = 0
if sum(a) + sum(b) <= k:
  print(len(a)+len(b))
else:
  while True:
    if not a and not b:
      if result >= k:
        print(count-1)
      else:
        print(count)
      break
    if result >= k:
      print(count-1)
      break
    if not a:
      result += b.pop(0)
      count += 1
    elif not b:
      result += a.pop(0)
      count += 1
    elif a[0] <= b[0]:
      result += a.pop(0)
      count += 1
    else:
      result += b.pop(0)
      count += 1

Submission status: Wrong Answer

Solution code:
from collections import deque


N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
INF = 10**10
ans = 0
now = 0

while now <= K:
    if A:
        a = A[0]
    else:
        a = INF
    if B:
        b = B[0]
    else:
        b = INF
    now += min(a, b)
    ans += 1
    if now >= INF:
        break
    i = 1
    while a == b:
        try:
            a = A[i]
            b = B[i]
        except IndexError:
            break
        i += 1
    
    if a >= b:
        B.popleft()
    else:
        A.popleft()

print(ans-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()))
x = 0
y = 0
cnt = 0
cnt2 = 0
sumlist_a = []
sumlist_b = []
while x < k :
    x += a[cnt]
    if x > k:
        break
    cnt += 1
    sumlist_a.append(x)
    if cnt == n:
        break
ans = len(sumlist_a)
sumlist_a.reverse()

#ansがaから全部本をとってこれる最大個数
p=0
for i in sumlist_a:
    while y < k - i:
        y += b[cnt2]
        if y > k - i:
            break
        cnt2 += 1
        if cnt2 == m:
            break
    ans = max(ans, len(sumlist_a)+cnt2-p)
    p += 1
    cnt2 = 0
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
total=0
flag=True
while flag==True:
    if len(A)==0:
        A.append(10**10)
    elif len(B)==0:
        B.append(10**10)
    elif A[0]<=B[0]:
        a=A.pop(0)
        if total+a<=K:
            ans+=1
            total+=a
        else:
            flag=False
    else:
        b=B.pop(0)
        if total+b<=K:
            ans+=1
            total+=b
        else:
            flag=False

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import collections

def input():
    return sys.stdin.readline()

def greedy_reading(n, m, k, a_list, b_list):

    max_books = len(a_list) + len(b_list)

    k_remain = k
    count = 0
    for i in range(max_books):
        if len(a_list) > 0 and len(b_list) > 0:
            if a_list[0] <= b_list[0]:
                if k_remain < a_list[0]:
                    break
                k_remain -= a_list.popleft()
                count += 1
            else:
                if k_remain < b_list[0]:
                    break
                k_remain -= b_list.popleft()
                count += 1
        elif len(a_list) > 0:
            if k_remain < a_list[0]:
                break
            k_remain -= a_list.popleft()
            count += 1
        elif len(b_list) > 0:
            if k_remain < b_list[0]:
                break
            k_remain -= b_list.popleft()
            count += 1


    return count


def resolve():
    n, m, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    a_list = collections.deque(a_list)
    b_list = collections.deque(b_list)
    ans = greedy_reading(n, m, k, a_list, b_list)

    a_sum = 0
    b_sum = 0
    a_num = 0
    b_num = 0
    a_remain = 0
    b_remain = 0
    # Aから
    for i in range(len(a_list)):
        a_sum += a_list[i]
        if a_sum > k:
            a_sum -= a_list[i]
            a_num = i
            break

    if a_num == 0:
        a_num = len(a_list)
        a_remain = k - a_sum

    # B
    for i in range(len(b_list)):
        b_sum += b_list[i]
        if b_sum > k:
            b_sum -= b_list[i]
            b_num = i
            break

    if b_num == 0:
        b_num = len(b_list)
        b_remain = k - b_sum

    # Aオーバーしない
    a_b_sum = a_sum
    a_b_num = a_num
    if a_remain == 0:
        for i in range(len(b_list)):
            a_b_sum += b_list[i]
            if a_b_sum > b_remain:
                a_b_sum -= b_list[i]
                a_b_num = i
                break
        if ans < a_b_num:
            print(a_b_num)
            exit()

    # bオーバーしない
    b_a_sum = b_sum
    b_a_num = b_num
    if b_remain == 0:
        for i in range(len(a_list)):
            b_a_sum += a_list[i]
            if b_a_num > a_remain:
                b_a_sum -= a_list[i]
                b_a_num = i
                break
        if ans < b_a_num:
            print(b_a_num)
            exit()

    print(ans)











if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

max_A_num = 0
for A_num in range(N+1):
    A_time = sum(A[:A_num])
    if A_time <= K:
        max_A_num = A_num
    else:
        break
max_book_count = 0
max_B_num = 0
for A_num_2 in range(A_num,-1,-1):
    A_time_2 = sum(A[:A_num_2])
    B_time = sum(B[:max_B_num])
    for B_num in range(max_B_num, M+1):
        if B_num > 0:
            B_time = B_time + B[B_num-1]
        if A_time_2 + B_time <= K:
            max_B_num = B_num
            if max_book_count < A_num_2 + B_num:
                max_book_count = A_num_2 + B_num
        else:
            break
print(max_book_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()))

i = 0
j = 0
sum = 0
ans = 0

while True:
    if i < n and j < m:
        if A[i] < B[j]:
            if sum + A[i] <= k:
                sum += A[i]
                i += 1
                ans += 1
            else:
                break
        else:
            if sum + B[j] <= k:
                sum += B[j]
                j += 1
                ans += 1
            else:
                break
    elif i == n and j < m:
        if sum + B[j] <= k:
            sum += B[j]
            j += 1
            ans += 1
        else:
            break
    elif i < n and j == m:
        if sum + A[i] <= k:
            sum += A[i]
            i += 1
            ans += 1
        else:
            break
    else:
        break

"""
while sum <= k:    
    if i < n and j < m:
        if A[i] < B[j]:
            if sum + A[i] <= k:
                sum += A[i]
                i += 1
                ans += 1
                print('i:', i)
        else:
            if sum + B[j] <= k:
                sum += B[j]
                j += 1
                ans += 1
                print('j:', j)
        #if flag == True and flag1 == True:
         #   break
    elif i == n:
        if sum + B[j] <= k:
            sum += B[j]
            j += 1
            ans += 1
        else:
            break
    elif j == m:
        if sum + A[i] <= k:
            sum += A[i]
            i += 1
            ans += 1
        else:
            break
    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()))

sumA = [A[0]]
sumB = [B[0]]
maxA = 0
maxB = 0

ans = 0

for i in range(N-1):
    if sumA[-1] + A[i+1] <= K:
        sumA.append(sumA[-1] + A[i+1])
        maxA = i+1
    else:
        break

for i in range(M-1):
    if sumB[-1] + B[i+1] <= K:
        sumB.append(sumB[-1] + B[i+1])
        maxB = i+1
    else:
        break

ans = max(maxA + 1, maxB + 1)

for i in range(len(sumB)):
    if sumA[maxA] + sumB[i] <= K:
        ans = max(ans, maxA+1 + i + 1)
    else:
        break

for i in range(len(sumA)):
    if sumB[maxB] + sumA[i] <= K:
        ans = max(ans, maxB + 1 + i + 1)
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import bisect


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


def main():
    N, M, K = LI()
    AArray = LI()
    BArray = LI()

    aSum = [AArray[0]]
    bSum = [BArray[0]]

    cnt = 0

    for i in range(1, N):
        aSum.append(aSum[i-1]+AArray[i])
    for i in range(1, M):
        bSum.append(bSum[i-1]+BArray[i])

    for i in range(N):
        a = aSum[i]
        if a > K:
            if i == 0:
                b = bisect.bisect_right(bSum, K)
                cnt = max(cnt, b)
            break
        if a == K:
            cnt = max(cnt, i+1)
            break
        sa = K-a
        b = bisect.bisect_right(bSum, sa)
        cnt = max(cnt, i+1+b)

    # while K > 0 and (len(AArray) > 0 or len(BArray) > 0):
    #     if len(AArray) > 0 and len(BArray) > 0:
    #         if AArray[0] >= BArray[0]:
    #             b = BArray.pop(0)
    #             if K-b >= 0:
    #                 K -= b
    #                 cnt += 1
    #             else:
    #                 break
    #         elif AArray[0] < BArray[0]:
    #             a = AArray.pop(0)
    #             if K-a >= 0:
    #                 K -= a
    #                 cnt += 1
    #             else:
    #                 break
    #     elif len(AArray) == 0:
    #         b = BArray.pop(0)
    #         if K-b >= 0:
    #             K -= b
    #             cnt += 1
    #         else:
    #             break
    #     else:
    #         a = AArray.pop(0)
    #         if K-a >= 0:
    #             K -= a
    #             cnt += 1
    #         else:
    #             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()))
a = 0
b = 0
ans = 0
t = sum(A)
temp = 0
if t <= K:
    ans = N
j = 0
for i in range(0,N+1):
    if i != 0:
        t -= A[-i]
    ans2 = 0
    while t < K and j < len(B):
        if t + B[j] <= K:
            ans2 += 1
            t += B[j]
            j += 1
        else:
            break
    if t <= K:
        ans = max(ans,len(A)-i+ans2)
print(ans)

Submission status: Wrong Answer

Solution code:


def main2():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    counter = 0
    ans = []

    if bb[0] >= aa[0]:
        if k < aa[0]:
            return counter
    if bb[0] <= aa[0]:
        if k < bb[0]:
            return counter

    for i in range(n+1):
        counter = i
        time_sum = sum(aa[:i])
        for j in range(m):
            time_sum += bb[j]
            if time_sum > k and j != 0:
                ans.append(counter)
                break
            if time_sum > k and j == 0:
                break
            elif time_sum == k:
                counter += 1
                ans.append(counter)
                break
            else:
                counter += 1

    ans.sort(reverse=True)
    return ans[0]


if __name__ == '__main__':
    ans = main2()
    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
ai=0
bi=0
loopcnt=len(a)+len(b)
for i in range(loopcnt):
  ans+=1
  if len(a)-ai==0:
    k-=b[bi]
    bi+=1
  elif len(b)-bi==0:
    k-=a[ai]
    ai+=1
  elif a[ai]>b[bi]:
    k-=b[bi]
    bi+=1
  else:
    k-=a[ai]
    ai+=1
  if k<0:
    ans-=1
    break
print(ans)

Submission status: Wrong Answer

Solution code:
N, M ,K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
la = len(A)
lb = len(B)
num = 0

a = 0
b = 0

while 1:
  if b == lb and a == la:
    break
  if b == lb:
    if A[a] <= K:
      num += 1
      K -= A[a]
      a += 1
      continue
    else:
      break
  if a == la:
    if B[b] <= K:
      num += 1
      K -= B[b]
      b += 1
      continue
    else:
      break
    
      
  if A[a] < B[b] and A[a] <= K:
    num += 1
    K -= A[a]
    a += 1
  elif A[a] > B[b] and B[b] <= K:
    num += 1
    K -= B[b]
    b += 1
    
print(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()))

sum = 0
ans = 0

while sum < k:
    if len(a) == 0 and len(b) == 0:
        break
    elif len(a) == 0 :
        sum = sum + b.pop(0)
        ans += 1
    elif len(b) == 0:
        sum = sum + a.pop(0)
        ans += 1
    elif a[0] <= b[0]:
        sum = sum + a.pop(0)
        ans += 1
    elif b[0] < a[0] :
        sum = sum + b.pop(0)
        ans += 1
if sum > k:
    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()))

total_max = 0
A_count = 0
B_count = 0

counter = 0

for i in range (0, N):
	if A[i] < K and counter ==0:
		A_count+=1
		K-=A[i]
	else:
		counter = 1
		break

counter = 0

for i in range (0, M):
	if B[i] < K and counter ==0:
		B_count+=1
		K-=B[i]
	else:
		counter = 1
		break

totalmax = A_count+B_count

for i in range (0, A_count):
	D = K + A[A_count-1-i]
	E = D
	zencount = A_count+B_count-(i+1)
	for j in range (0, M):
		if B[j] < E:
			E-=B[j]
			zencount+=1
		else:
			break
	if zencount >= totalmax:
		totalmax = zencount

print(totalmax)

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 len(A) > 0 or len(B) > 0:
    if len(A) > 0 and len(B) > 0:
        K -= B.pop(0) if A[0] > B[0] else A.pop(0)
    else:
        if len(A) > 0:
            K -= A.pop(0)
        elif len(B) > 0:
            K -= B.pop(0)

    if K < 0: break
    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# import heapq


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):

    sumA = [0]
    sum = 0
    for a in A:
        sum += a
        sumA.append(sum)
    sumB = [0]
    sum = 0
    for b in B:
        sum += b
        sumB.append(sum)
    
    
    # print(sumA)
    # print(sumB)
    # True - ng - ok - False
    def is_ok(mid: int, i):
        # 判定する条件
        if mid < 0:
            return True
        elif mid >= M + 1:
            return False
        # print(i, mid, sumA[i] + sumB[mid])
        return sumA[i] + sumB[mid] <= K

    def binSearch(ok: int, ng: int, i):
        # print(ok, ng)
        while abs(ok - ng) > 1:
            mid = (ok + ng) // 2
            if is_ok(mid, i):
                ok = mid
            else:
                ng = mid
            # print(ok, ng)
        return ok

    ans = -1
    for i in range(N + 1):
        j = binSearch(-1, M + 1, i)
        # print("a", i, j)
        ans = max(ans, i + j)
        if j <= 0:
            break

    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

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

co = -1
nowk = 0
nowa = 0
nowb = 0

while nowk <= k:
    if nowa == len(a) and nowb == len(b):
        co += 1
        break
    elif nowa == len(a):
        nowk += b[nowb]
        nowb +=1
    elif nowb == len(b):
        nowk += a[nowa]
        nowa +=1
    elif a[nowa] > b[nowb]:
        nowk += b[nowb]
        nowb +=1
    else:
        nowk += a[nowa]
        nowa +=1
    co += 1

print(co)

Submission status: Wrong Answer

Solution code:
N, M, X = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(10000000000)
B.append(10000000000)
A.reverse()
B.reverse()


ans = 0
c = 0
while True:
  if sum(A[-2:]) >= sum(B[-2:]):
    b = B.pop()
    if ans+b > X:
      break
    ans += b
    c += 1
    if len(B) == 1:
      B[0] *=10
  else:
    a = A.pop()
    if ans+a > X:
      break
    ans += a
    c += 1
    if len(A) == 1:
      A[0] *=10
print(c)

Submission status: Wrong Answer

Solution code:
import sys


def main():
    input = sys.stdin.buffer.readline
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    num_a = 0
    while num_a <= n:
        # Aの上からi冊の和
        s = sum(a[:num_a])
        if s > k:
            break
        # Bから読める本の数
        for j in range(len(b)):
            if s + b[j] <= k:
                s += b[j]
            else:
                num_b = j
                break
        else:
            num_b = len(b)
        if num_a + num_b >= ans:
            ans = num_a + num_b
        else:
            break
        num_a += 1

    print(ans)


if __name__ == '__main__':
    main()