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:
#!/usr/bin/env python3
N, M, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
i = 0
j = M-1

B_SUM = [0] * M
B_SUM[0] = B[0]

max_book = 0
if B_SUM[0] <= K:
    max_book = 1

for i in range(1, M):
    B_SUM[i] = B[i] + B_SUM[i - 1]
    if B_SUM[i] <= K:
        max_book = i + 1

A_SUM = 0

b_index = M - 1

for i in range(N):
    A_SUM += A[i]
    while (b_index >= 0):
        B_SUM[b_index]
        if A_SUM + B_SUM[b_index] <= K:
            max_book = max(max_book, b_index + 1 + i + 1)
            break
        b_index -= 1
print(max_book)

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()))
count=0
i=0
j=0
while count<=k:
  if a[-1]==10**10 and b[-1]==10**10:
    break
  else:
    if a[i]<b[j] and a[i]<=k-count:
      count+=a[i]
      if i+2>n:
        a.append(10**10)
        i+=1
      else:
        i+=1
    elif b[j]<=a[i] and b[j]<=k-count:
      count+=b[j]
      if j+2>m:
        b.append(10**10)
        j+=1
      else:
        j+=1
    else:
      break
print(i+j)

Submission status: Wrong Answer

Solution code:
import sys

N, M, K = [int(x) for x in sys.stdin.readline().rstrip().split()]
A = [int(x) for x in sys.stdin.readline().rstrip().split()]
B = [int(x) for x in sys.stdin.readline().rstrip().split()]

ai = 0
bi = 0
total = 0

while ai + bi < N + M:
    if bi == len(B):
        if (total + A[ai]) <= K:
            total += A[ai]
            ai += 1
        else:
            break
    elif ai == len(A):
        if (total + B[bi]) <= K:
            total += B[bi]
            bi += 1
        else:
            break
    else:
        if A[ai] <= B[bi]:
            if (total + A[ai]) <= K:
                total += A[ai]
                ai += 1
            else:
                break
        else:
            if (total + B[bi]) <= K:
                total += B[bi]
                bi += 1
            else:
                break
print(ai + bi)

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()))
books = 0
i,j,q = 0,0,0
bsum = 0
newl = [0]*(n+m)
while i<n and j<m:
    if a[i]<b[j]:
        newl[q] = a[i]
        i += 1
        q += 1
    else:
        newl[q] = b[j]
        j += 1
        q += 1
while i<n:
    newl[q] = a[i]
    q += 1
    i += 1
while j<m:
    newl[q] = b[j]
    q += 1
    j += 1
for i in newl:
    bsum += i
    if bsum <= k:
        books += 1
    else:
        break
print(books)

Submission status: Wrong Answer

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

cnt = 0

list = A + B
list = [int(s) for s in list]
list.sort()
ans = 0

for s in range(len(list)):
    if list[s] < int(K)-ans:
        cnt += 1
        ans += list[s]

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()))
tt = 0
a = 0
b = 0
if (K >= (sum(A)+sum(B))):
    print(N+M)
elif (A[a]>K)and(B[b]>K):

    print(0)
else:
    while (tt <= K ):

        if (a < N)and (b < M):
            if A[a]>B[b]:
                tt += B[b]
                b += 1
                if tt > K:
                    b -= 1
                    break

                elif tt == K:
                    break
            elif A[a]<B[b]:
                tt += A[a]
                a += 1
                if tt > K:
                    a -= 1
                    break

                elif tt == K:
                    break
            elif (A[a]==B[b])and(a < N-1)and (b < M-1):
                if A[a+1]>B[b+1]:
                    tt += B[b]
                    b += 1
                    if tt > K:

                        b -= 1
                        break
                    elif tt == K:
                        break
            elif (A[a]==B[b])and(a == N-1)and (b < M-1):
                tt += B[b]
                b += 1
                if tt > K:

                    b -= 1
                    break
                elif tt == K:
                    break
            else:
                tt += A[a]
                b += 1
                if tt > K:

                    a -= 1
                    break
                elif tt == K:
                    break



        elif (a == N)and (b < M):
            tt += B[b]
            b += 1
            if tt > K:
                b -= 1
                break

            elif tt == K:
                break
        elif (a < N)and (b == M):
            tt += A[a]
            a += 1
            if tt > K:
                a -= 1
                break

            elif tt == K:
                break
        else:
            break
    print(a + b)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
List1 = list(map(int, input().split()))
List2 = list(map(int, input().split()))
List1.reverse()
List2.reverse()
count = 0
sums = 0
while sums <= K:
    if len(List1) == 0:
        if len(List2) != 0:
            if (sums + List2[-1]) <= K:
                sums += List2[-1]
                List2.pop()
                count += 1

            else: break
        else: break
            
    elif len(List2) == 0:
        if len(List1) != 0:
            if sums + List1[-1] <= K:
                sums += List1[-1]
                List1.pop()
                count += 1

            else: break

        else: break
            
    elif List1[-1] <= List2[-1]:
        if sums + List1[-1] <= K:
            count += 1
            sums += List1[-1]
            List1.pop()

        else: break
        
        
    elif List1[-1] >= List2[-1]:
        if (sums + List2[-1]) <= K:
            count += 1
            sums += List2[-1]
            List2.pop()
        else: break
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()))

count = 0
time = 0
for i in range(N + M):
  if len(A) == 0:
    book = B.popleft()
  elif len(B) == 0:
    book = A.popleft()
  else:
    if A[0] < B[0]:
      book = A.popleft()
    else:
      book = B.popleft()
  
  time += book
  if time > K:
    break
  else:
    count += 1

print(count)

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()))
 
l = []
for i in range(min(n, m)):
  l.append(min(a[i], b[i]))
  l.append(max(a[i], b[i]))

for i in range(min(n, m), max(n, m)):
  if n > m:
    l.append(a[i])
  else:
    l.append(b[i])

total = 0
cnt = 0
for i in range(n+m):
  if total+l[i] > k:
    break
  total += l[i]
  cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
sm=0
c=0
if sum(al)+sum(bl)<=k:
    print(n+m)
else:
    f=0
    i=0
    j=0
    while i<n and j<m:
        if sm+al[i]>k:
            f=1
            print(c)
            break
        else:
            sm+=al[i]
            c+=1
            i+=1
            if sm+bl[j]>k:
                f=1
                print(c)
                break
            else:
                c+=1
                sm+=bl[j]
                j+=1
    if f!=1:
        while i<n:
            if sm+al[i]>k:
                f=1
                print(c)
                break
            else:
                sm+=al[i]
                c+=1
                i+=1
    if f!=1:
        while j<m:
            if sm+bl[j]>k:
                print(c)
                break
            else:
                sm+=bl[j]
                c+=1
                i+=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()))

i = 0
for s in range(n+m):
    if i>k:
        if a[0]>=b[0]:
            i = i + a[0]
            del a[0]
        else:
            i = i + b[0]
            del a[0]
    elif i==k:
        print(s+1)
    else:
        print(s)

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

Submission status: Wrong Answer

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

N,M,K=(int(x) for x in input().rstrip('\n').split())
As=[int(x) for x in input().rstrip('\n').split()]
Bs=[int(x) for x in input().rstrip('\n').split()]
res = 0
now = 0
now_a = 0
now_b = 0
for i in range(N+M):
  if now_a==len(As):
    c=1
    if now_b==len(Bs):
      break
    else:
      c=0
  elif now_b==len(Bs):
    c=1
  elif As[now_a]!=Bs[now_b]:
    if As[now_a]<Bs[now_b]:
      c=1
    else:
      c=0
  else:
    nex_a=now_a+1
    nex_b=now_b+1
    if all(x==y for x,y in zip(As[now_a:],Bs[now_b:])):
      c=1
    else:
      while 5572>573:
        if nex_a==len(As):
          c=0
          break
        elif nex_b==len(Bs):
          c=1
          break
        else:
          if As[nex_a]<Bs[nex_b]:
            c=1
            break
          elif As[nex_a]>Bs[nex_b]:
            c=0
            break
          else:
            nex_a+=1
            nex_b+=1
  if c == 1:
    now += As[now_a]
    now_a += 1
  else:
    now += Bs[now_b]
    now_b += 1
  if now>K:
    break
  res += 1
print(res)

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()))
c = 0
 
while k >= 0:
  if len(a) == 0 and len(b) == 0:
    break;
  elif len(a) == 0:
    p = b.pop(0)
  elif len(b) == 0:
    p = a.pop(0)    
  else:
    p = a.pop(0) if a[0] > b[0] else b.pop(0)

  k -= p
  if  k >= 0:
    c += 1
  
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()))

Sa = [0]*N
Sb = [0]*M
Sa[0] = A[0]
Sb[0] = B[0]
a = 0
b = 0
ans = 0
if Sa[0] > K:
    a = 0
else:
    for i in range(1, N):
        Sa[i] = Sa[i-1] + A[i]
        if Sa[i] > K:
            a = i
            break
    else:
        a = N

if Sb[0] > K:
    b = 0
else:
    for i in range(1, M):
        Sb[i] = Sb[i-1] + B[i]
        if Sb[i] > K:
            b = i
            break
    else:
        b = M

for i in range(a):
    temp = K - Sa[i]
    if temp < Sb[0]:
        ans = max(ans, i+1)
        continue
    elif temp >= Sb[b-1]:
        ans = max(ans, i+1+b)
        continue
    ok = 0
    ng = b
    t = (ok+ng)//2
    while ok+1 < ng:
        t = (ok+ng)//2
        if temp > Sb[t]:
            ok = t
        else:
            ng = t
    ans = max(ans, i+1+ok+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()))
ni = 0
mi = 0
now = 0
cnt= 0
while True:
    if ni < n and mi < m:
        if a[ni] <= b[mi] and now + a[ni] <= k:
            now += a[ni]
            ni += 1
            cnt += 1
        elif a[ni] >= b[mi] and now + b[mi] <= k:
            now += b[mi]
            mi += 1
            cnt += 1
        else:
            break
    elif ni < n:
        if now + a[ni] <= k:
            now += a[ni]
            ni += 1
            cnt += 1
        else:
            break
    elif mi < m:
        if now + b[mi] <= k:
            now += b[mi]
            mi += 1
            cnt += 1
        else:
            break
    else:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

    Aq = deque()
    Bq = deque()

    for i in range(N):
        Aq.append(A[i])

    for i in range(M):
        Bq.append(B[i])

    times = 0
    read_books = 0

    while times <= K:
        # print("K {} times {} read {} Aq {} bq {}".format(K, times, read_books, Aq, Bq))

        if not Aq and not Bq:
            break

        if not Aq:
            b = Bq.popleft()
            if b+times > K:
                break
            else:
                times += b
                read_books += 1
                continue

        if not Bq:
            a = Aq.popleft()
            if a + times > K:
                break
            else:
                times += a
                read_books += 1
                continue


        A_head = Aq.popleft()
        B_head = Bq.popleft()

        if A_head+times > K and B_head+times > K:
            break

        if A_head < B_head:
            times += A_head
            read_books += 1
            Bq.appendleft(B_head)

        else:
            times += B_head
            read_books += 1
            Aq.appendleft(A_head)

    print(read_books)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n,m,k = MAP()

a = LIST()

b = LIST()

prefA = [0]*n
prefA[0] = a[0]

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


pref = [0]*m

pref[0] = b[0]

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

# print(prefA)
# print(pref)

ans = 0

for i in range(n):
    left = k-prefA[i]

    lo = 0
    hi = m-1

    temp = (i+1)

    if(left>0):
        while(lo<=hi):
            # print(lo, hi, ans)
            mid = (lo+hi)//2

            if(pref[mid]>left):
                hi = mid - 1
            else:
                lo = mid + 1
                ans = max(ans, temp + lo)
        ans = max(ans, temp + lo)

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()))
sum_a=sum(a)
sum_b=sum(b)

cumsum=0
cnt=0
i=0
j=0
while True:
    if i<=n-1:
        a_i=a[i]
    else:
        a_i=10**9+1
    
    if j<=m-1:
        b_j=b[j]
    else:
        b_j=10**9 + 1

    if a_i < b_j:
        cumsum+=a_i
        sum_a-=a_i
        i+=1
    elif a_i > b_j:
        cumsum+=b_j
        sum_b-=b_j
        j+=1
    elif a_i==b_j:
        if sum_a<=sum_b:
            cumsum+=a_i
            i+=1
        else:
            cumsum+=b_j
            j+=1
    
    if cumsum<=k:
        cnt+=1
    else:
        break

    if i>n-1 and j>m-1:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
n, m, k=map(int, input().split())
a=list(map(int, input().split()))+[10**9]*(10**6)
b=list(map(int, input().split()))+[10**9]*(10**6)

ans=0

cnt1=0
cnt2=0
cost1=0
cost2=0

for i in range(n):
    cost1+=a[i]
    if cost1<=k:
        cnt1+=1
    else:
        cost1-=a[i]
        break

for j in range(m):
    cost2+=b[j]
    if cost2<=k:
        cnt2+=1
    else:
        cost2-=b[j]
        break

if cnt1>cnt2:
    ans+=cnt1
    k-=cost1
    for i in range(m):
        k-=b[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)

elif cnt2>cnt1:
    ans+=cnt2
    k-=cost2
    for i in range(n):
        k-=a[i]
        if k>=0:
            ans+=1
        else:
            break
    
    print(ans)


else:
    loop=max(n, m)
    i=0
    j=0
    T=0
    ans=0
    
    for t in range(2*loop):
        if a[i]>=b[j]:
            T+=b[j]
            if T<=k:
                ans+=1
                j+=1
            else:
                print(ans)
                exit()
        elif b[j]>a[i]:
            T+=a[i]
            if T<=k:
                ans+=1
                i+=1
            else:
                print(ans)
                exit()

Submission status: Wrong Answer

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

if (N>10*5) | (M>10**5) |(K>10*5):
    print(0)
else:
    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)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

time_count = 0
count = 0
while True:
    if A:
        a = A.popleft()
    else:
        a = float('inf')
    
    if B:
        b = B.popleft()
    else:
        b = float('inf')

    if a <= b:
        time_count += a
        count += 1
        B.appendleft(b)
        if time_count > K:
            time_count -= a
            count -= 1
            break
    else:
        time_count += b
        count += 1
        A.appendleft(a)
        if time_count > K:
            time_count -= b
            count -= 1
            break 
    
print(count)

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()))
K = K/1000
A_list = [A/1000 for A in A_list]
B_list = [B/1000 for B in B_list]
A_i = 0
B_i = 0
read_time = 0
books = 0
while True:
    if len(B_list) <= B_i:
        if len(A_list) <= A_i:
            break
        read_time += A_list[A_i]
        if read_time > K:
            break
        books += 1
        A_i += 1
    elif len(A_list) <= A_i:
        read_time += B_list[B_i]
        if read_time > K:
            break
        books += 1
        B_i += 1
    elif A_list[A_i] < B_list[B_i]:
        read_time += A_list[A_i]
        if read_time > K:
            break
        books += 1
        del A_list[A_i]
    elif A_list[A_i] >= B_list[B_i]:
        read_time += B_list[B_i]
        if read_time > K:
            break
        books += 1
        B_i += 1

print(books)

Submission status: Wrong Answer

Solution code:
# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    P = X[X <= k]
    Q = Y[Y <= k]
    n = len(P)
    m = len(Q)
    # print(P,Q)

    cnt = 0
    # Aをi個、Bをj個選ぶ
    for i in range(n):
        for j in range(m)[::-1]:
            print(i,j,X[i],Y[j])
            if X[i] + Y[j] <= k:
                cnt = max(cnt, i + j)
                break
    print(cnt)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))
book = n + m
nokori = k
Ans = book
an.append(1000000000)
bn.append(1000000000)
for i in range(book):
    if an[0] <= bn[0]:
        nokori = nokori - an[0]
        del an[0]
    elif an[0] > bn[0]:
        nokori = nokori - bn[0]
        del bn[0]
    if nokori < 0:
        Ans = i
        break
print(Ans)

Submission status: Wrong Answer

Solution code:
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
t = 0
d = 0
for i in range(a[0]):
  for j in range(a[1]):
    if b[0] > a[2] and c[0] > a[2]:
      d = 1
      break
    if a[2] >= sum(b)+sum(c):
      d = len(b)+len(c)+1
      break
    if c[j] <= b[i]:
      t += c[j]
      d += 1
    else:
      t += b[i]
      d += 1
      break
    if t >= a[2]:
      break
  if t >= a[2]:
    break
print(d-1)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = [i for i in map(int, input().split())]
B = [i for i in map(int, input().split())]

time = 0
books = 0
for i in range(N+M):
    if not A:
        if not B:
            break
        if (time + B[0]) > K:
            break
        else:
            time += B.pop(0)
            books += 1

    elif not B:
        if (time + A[0]) > K:
            break
        else:
            time += A.pop(0)
            books += 1
    elif (time + min(A[0], B[0]))> K:
        break
    else:
        if A[0] < B[0]:
            time += A.pop(0)
            books += 1
        else:
            time += B.pop(0)
            books += 1

print(books)

Submission status: Wrong Answer

Solution code:
N = input().split(" ")
A= input().split(" ")
B= input().split(" ")
A.extend(B)
A_int = [int(n) for n in A]
A_int.sort(reverse=False)
cont = 0

if sum(A_int) == N[2]:
  print(len(A_int))
else:
  for i in range(len(A_int)):
    N[2] = int(N[2]) - A_int[i]
    if N[2] > -1:
      cont = cont + 1
    else:
      break
  print(cont)

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)
time0 = 0
acount = 0
bcount = 0

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

print(hic)

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()))
totalbook = 0
totaltime = 0
for a in range(200000):
    if A == []:
        tt = B.pop(0)
    elif B == []:
        tt = A.pop(0)
    else:
        if A[0] <= B[0]:
            tt = A.pop(0)
        else:
            tt = B.pop(0)
    if totaltime + tt <= K:
        totaltime += tt
        totalbook += 1
    else:
        break
    if A == [] and B == []:
        break
print(totalbook)

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_idx = N - 1
b_idx = M - 1
cnt = N + M
summation = sum(A_list) + sum(B_list)
if summation <= K:
  print(N + M)
  exit()
else:
  while True:
    # print(cnt, summation)
    if a_idx >= 0 and b_idx >= 0:
      if A_list[a_idx] >= B_list[b_idx]:
        summation -= A_list[a_idx]
        cnt -= 1
        a_idx -= 1
      else:
        summation -= B_list[b_idx]
        cnt -= 1
        b_idx -= 1
    elif a_idx < 0 and b_idx >= 0:
      summation -= B_list[b_idx]
      cnt -= 1
      b_idx -= 1
    elif a_idx >= 0 and b_idx < 0:
      summation -= A_list[a_idx]
      cnt -= 1
      a_idx -= 1
    else:
      break

    if a_idx == -1 and b_idx == -1:
      break
    else:
      if summation <= 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()))

S = [0]

Sum = 0


for i in range(N):
  K = K - A[i]
  if K >= 0:
    Sum += 1
  else:
    K = K + A[i]
    break
S.append(Sum)

for j in range(M):
  K = K - B[j]
  if K >= 0:
    Sum += 1
  else:
    if i == 0:
      break
    for k in range(i):
      K = K + A[i-k-1]
      Sum = Sum - 1
      if K >= 0:
        i = i-k-1
        break
    if K >= 0:
      Sum += 1
    else:
      Sum -= 1
  S.append(Sum)
print(max(S))

Submission status: Wrong Answer

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

cnt = 0

list = A + B
list = [int(s) for s in list]
list.sort()
ans = 0

for s in range(len(list)):
    if list[s] <= int(K)-ans:
        cnt += 1
        ans += list[s]

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline

n, m, k = map(int, readline().split())
a = deque(map(int, readline().split()))
b = deque(map(int, readline().split()))
for i in range(n+m):
    if not a and not b:
        print(i)
        sys.exit()
    elif not a:
        x = b.popleft()
        if k < x:
            print(i)
            sys.exit()
        else:
            k -= x
    elif not b:
        x = a.popleft()
        if k < x:
            print(i)
            sys.exit()
        else:
            k -= x
    elif a[0] < b[0]:
        x = a.popleft()
        if k < x:
            print(i)
            sys.exit()
        else:
            k -= x
    else:
        x = b.popleft()
        if k < x:
            print(i)
            sys.exit()
        else:
            k -= x
print(n+m)

Submission status: Wrong Answer

Solution code:

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


A_cs = [0]*(len(A)+1)
for i in range(1,len(A)+1):
    A_cs[i] = A_cs[i-1]+ A[i-1]


B_cs = [K]*(len(B)+1)
total = 0
for i in range(1,len(B)+1):
    B_cs[i] = B_cs[i-1]- B[i-1]

    for j in range(len(A_cs)):
        if B_cs[i] -A_cs[j]<0:
            j = j-1
            break
    if total < i + j:
        if j == -1:
            i = 0
            j = 0
        total = i + j

Submission status: Wrong Answer

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

for i in range(n):
    sum += a[i]
for i in range(m):
    sum += b[i]

if sum<=k:
    print(n+m)
    sys.exit()


sum = 0
cnt =0
while sum < k and  (len(a) != 0 or len(b) != 0 ):
    if len(a) != 0 and len(b) != 0:
        if a[0]<b[0]:
            sum += a[0]
            del a[0]
        else:
            sum += b[0]
            del b[0]
    elif len(a) == 0:
        sum += b[0]
        del b[0]
    else :
        sum += a[0]
        del a[0]
    if sum<=k :
        cnt+=1

print(cnt)

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

pre_ans = 0
ans = 0
c = 0
temp = 0
while True:
  if pre_ans == ans:
    temp = 100000000000
  else:
    temp = 0
  if sum(A[-4:]) >= sum(B[-4:])+temp:
    if ans+B[-1] > X:
      pre_ans = ans
      if temp == 100000000000:
        break
      continue
    ans += B.pop()
    c += 1
    if len(B) == 1:
      B[0] *=10
  else:
    if ans+A[-1] > X:
      pre_ans = ans
      if temp == 100000000000:
        break
      continue
    ans += A.pop()
    c += 1
    if len(A) == 1:
      A[0] *=10
print(c)