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.

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
tim, ans = sum(B), 0
r = M - 1


while r > -1 and tim > K:
    tim -= B[r]
    r -= 1
ans = r + 1


for l in range(N):
    tim += A[l]
    while r > -1 and tim > K:
        tim -= B[r]
        r -= 1
    if tim > K:
        break
    ans = max(ans, l + r + 2)
print(ans)

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

a = [0]+list(map(int, input().split(" ")))
b = [0]+list(map(int, input().split(" ")))

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

j,mx= m,0
for i in range(n+1):
    if a[i]>k:
        break
    while(b[j]>k-a[i]):
        j-=1
    if (i+j>mx):
        mx=i+j
print(mx)

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    
    
    
    
    time = 0

    for bi in b:
        time += bi

    j = m
    ans = 0

    for i in range(n + 1):
        
        while (j > 0 and time > k):
            j -= 1
            time -= b[j]

        
        if time > k:
            break

        
        ans = max(ans, i + j)

        
        
        if (i == n):
            break

        
        
        time += a[i]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    
    
    
    
    
    
    time = 0

    for bi in b:
        time += bi

    j = m
    ans = 0

    for i in range(n + 1):
        
        while (j > 0 and time > k):
            j -= 1
            time -= b[j]

        
        if time > k:
            break

        
        ans = max(ans, i + j)

        
        
        if (i == n):
            break

        
        
        time += a[i]

    print(ans)


if __name__ == '__main__':
    main()

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

ruiseki_A = [0]
ruiseki_B = [0]

for i in range(len(A)):
    ruiseki_A.append(ruiseki_A[i]+A[i])
for i in range(len(B)):
    ruiseki_B.append(ruiseki_B[i]+B[i])


ans = 0
for i in range(len(ruiseki_A)):
    tmp = K - ruiseki_A[i]
    if tmp < 0:
        continue
    r = len(ruiseki_B)
    l = 0
    while r-l > 1:
        mid = (r + l) // 2
        if ruiseki_B[mid] <= tmp:
            l = mid
        else:
            r = mid
    ans = max(ans, i+l)

print(ans)

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

a = [0 for i in range(n+1)]
b = [0 for j in range(m+1)]
for i in range(n):
    a[i+1] = a[i]+A[i]
for j in range(m):
    b[j+1] = b[j]+B[j]

j = m
res = 0
for i in range(n+1):
    if a[i] > k:
        break
    while k - a[i] < b[j]:
        j -= 1
    res = max(res, i+j)

print(res)

Solution (Python):
import numpy as np
n,m,k = map(int,input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
a_csum = np.cumsum(a_ls)
b_csum = np.cumsum(b_ls)
def isOK(index, key, l):
    if l[index] > key:
        return True
    else:
        return False
def binary_search(l, key):
    '''
    keyより大きい値を持つ最小のインデックスを返す
    '''
    left = -1
    right = len(l)

    while (right - left) > 1:
        mid = left + (right - left) // 2
        
            
                
            
        if isOK(mid, key, l):
            right = mid
        else:
            left = mid
    
    
    return right
ans = 0
for i in range(n):
    rest = k - a_csum[i]
    if rest < 0:
        break
    ind = binary_search(b_csum, rest)
    canread_b = max(0,ind)
    canread = i+1 + canread_b
    ans = max(canread, ans)
rest = k
ind = binary_search(b_csum, rest)
canread_b = max(0,ind)
ans = max(canread_b, ans)

print(ans)

Solution (Python):
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    n, m, k = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))

    a, b = [0], [0]
    for i in range(n):
        a.append(a[i] + A[i])
    for i in range(m):
        b.append(b[i] + B[i])

    ans = 0
    j = m

    for i in range(n + 1):
        if a[i] > k:
            break
        while b[j] > k - a[i]:
            j -= 1
        ans = max(ans, i + j)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
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]
for i in range(n):
    ai.append(ai[i] + a[i])
for i in range(m):
    bi.append(bi[i] + b[i])

ans, j = 0, m
for i in range(n + 1):
    if ai[i] > k:
        break
    while ai[i] + bi[j] > k:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ruia=[0]
ruib=[0]
mx=0
for (i,j) in zip(a,range(n)):
  ruia.append(ruia[j]+i)

for (i,j) in zip(b,range(m)):
  ruib.append(ruib[j]+i)
ans=0
for i in range(n+1):
  if ruia[i] > k:
    break
  while ruib[m] > k - ruia[i]:
    m -= 1
  ans = max(ans, i + m)
print(ans)

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

a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

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

sum_A = [0 for _ in range(n+1)] 
for i in range(1, n+1):
    sum_A[i] = sum_A[i-1] + A[i-1]
sum_B = [0 for _ in range(m+1)] 
for j in range(1, m+1):
    sum_B[j] = sum_B[j-1] + B[j-1]

ans = 0
i = 0
j = m
while i <= n and sum_A[i] <= k:
    while sum_A[i] + sum_B[j] > k:
        j -= 1
    ans = max(ans, i + j)
    i += 1
print(ans)

Solution (Python):

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = [0]
B = [0]

for i in range(n):
    A.append(A[i] + a[i])
for i in range(m):
    B.append(B[i] + b[i])
ans = 0
j = m

for i in range(n + 1):
    if A[i] > k:
        break
    
    while A[i] + B[j] > k:
        j -= 1
    ans = max(ans, i + j)
print(ans)

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

a, b = [0], [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans, i + j)
print(ans)

Solution (Python):
import bisect
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
time = 0
cnt = 0
C = []
SA =[0]
SB =[0]
for a in A:
    SA.append(SA[-1] + a)
for b in B:
    SB.append(SB[-1] + b)
for x in range(N+1):
    L = K - SA[x]
    if L < 0:
        break
    y_max = bisect.bisect_right(SB, L) - 1

    C.append(y_max + x)
print(max(C))

Solution (Python):
n, m, k =map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ta=sum(a)
a.append(0)
tb=0
ans=0
j=0
for i in range(n+1):
    ta -= a[n-i]
    if ta>k:
        continue
    while tb + ta<=k:

        if j ==m:
            ans=max(ans,n-i+j)
            break
        ans=max(ans,n-i+j)
        tb += b[j]
        j +=1

print(ans)

Solution (Python):
import bisect


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

max_books = 0

sum_a = []
sum_b = []

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

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

sum_a.insert(0, 0)
sum_b.insert(0, 0)

for i in range(n+1):
    r = k - sum_a[i]
    if r == 0:
        books = i
    elif r < 0:
        books = 0
    else:
        book_b = bisect.bisect_right(sum_b, r)
        books = i + book_b - 1
    if books >= max_books:
        max_books = books

print(max_books)

Solution (Python):
import bisect
from itertools import accumulate

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

A_cumsum = list(accumulate(A))
B_cumsum = list(accumulate(B))

max_books = 0
for a, A_sum in enumerate([0] + A_cumsum):
    if A_sum > K:
        break
    else:
        b = bisect.bisect(B_cumsum, K - A_sum)
        max_books = max(a + b, max_books)

print(max_books)

Solution (Python):
import sys

N, M, K = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    B.append(n)

ans = 0
for i in range(N + 1):
    if A[i] > K:
        break
    for j in range(M, -1, -1):
        if A[i] + B[j] <= K:
            ans = max(ans, i + j)
            M = j
            break

print(ans)

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

sumA = [0]
sumB = [0]
tmp = 0
answer = 0
st = 0

for i in range(N):
    tmp = tmp + A[i]
    sumA.append(tmp)

tmp = 0
for i in range(M):
    tmp = tmp + B[i]
    sumB.append(tmp)

for i in range(N, -1, -1):
    booktmp = 0

    for j in range(st, M + 1):

        if sumA[i] + sumB[j] <= K:
            booktmp = i + j
        else:
            st = j
            break
    answer = max(answer, booktmp)
    if j == M:
        break


print(answer)

Solution (Python):

def main():
        
    N_satu, M_satu, K_fun = map(int, input().split())

    A_fun = list(map(int, input().split()))
    B_fun = list(map(int, input().split()))

    SumOf_A = [0]
    SumOf_B = [0]

    for i in range(N_satu):
        next_val = SumOf_A[i] + A_fun[i]
        SumOf_A.append(next_val)

    for i in range(M_satu):
        next_val = SumOf_B[i] + B_fun[i]
        SumOf_B.append(next_val)

    index_b = M_satu
    max_cnt = 0

    for i in range(N_satu + 1):
        if (SumOf_A[i] > K_fun):
            break
        
        while (SumOf_A[i] + SumOf_B[index_b] > K_fun):
            index_b -= 1
        
        max_cnt = max(max_cnt, i + index_b)
        

    print(max_cnt)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()


N, M, K = inpl()
A = inpl()
B = inpl()
ib = M
a = 0
b = sum(B)
ans = 0
for i in range(N+1):
    if i != 0:
        a += A[i-1]
    if a > K:
        break
    while a+b > K:
        if ib > 0:
            ib -= 1
            b -= B[ib]
        else:
            break
    ans = max(ans, i+ib)
print(ans)


ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Solution (Python):
from bisect import bisect

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

def make_cum(ary):
    cum = [0]
    for v in ary:
        cum.append(cum[-1] + v)
    return cum


def solve():
    ans = 0
    cuma = make_cum(A)
    cumb = make_cum(B)
    for ia in range(N+1):
        if cuma[ia] > K:
            break
        ib = bisect(cumb, K-cuma[ia]) - 1
        ans = max(ans, ia + ib)
    return ans
        

if __name__ == "__main__":
    print(solve())

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

A, B = [0], [0]
for i in range(n):
    A.append(a[i]+A[i])
for i in range(m):
    B.append(b[i]+B[i])

ans, j = 0, m
for i in range(n+1):
    if A[i] > k:
        break
    while B[j] > k - A[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

Solution (Python):
import numpy as np
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
a = np.cumsum(a)
b = np.cumsum(b)

ans = 0
for a_num, time in enumerate(a):
    if time > k:
        continue
    b_num = bisect_right(b, k - time)
    num = a_num + b_num - 1
    if num > ans:
        ans = num
print(ans)

Solution (Python):



N, M, K = map(int, input().split())
result = 0 
a_time = list(map(int, input().split()))
b_time = list(map(int, input().split()))

sum = 0

a_num = 0
for i in range(N):
    sum += a_time[i]
    a_num += 1
    if sum > K:
        sum -= a_time[i]
        a_num -= 1
        break
i = a_num
j = 0
while i >= 0:
    while sum < K and j < M:
        sum += b_time[j]
        j += 1
    if sum > K:
        j -= 1
        sum -= b_time[j]
    if result < i + j:
        result = i + j
    i -= 1
    sum -= a_time[i]

print(result)

Solution (Python):
from itertools import accumulate

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0] + list(accumulate(a))
sb = [0] + list(accumulate(b))

ans = 0
p = m
for i in range(n + 1):
    if sa[i] > k:
        break
    for j in range(p, -1, -1):
        if sa[i] + sb[j] <= k:
            p = j
            ans = max(ans, i + j)
            break
print(ans)

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

a, b = [0], [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans, i + j)
print(ans)

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

total = 0
count = 0
flag = n-1
for i in range(n):
    total += a[i]
    if total > k:
        total -= a[i]
        flag = i-1
        break
    count += 1
ans = count
for i in range(m):
    if b[i] > k:
        break
    total += b[i]
    count += 1
    while total > k:
        if flag == -1:
            break
        else:
            total -= a[flag]
            flag -= 1
            count -= 1
    if total > k: 
        break
    ans = max(ans, count)

print(ans)

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

total = 0
count = 0
flag = n-1
for i in range(n):
    total += a[i]
    if total > k:
        total -= a[i]
        flag = i-1
        break
    count += 1
ans = count
for i in range(m):
    if b[i] > k:
        break
    total += b[i]
    count += 1
    while total > k:
        if flag == -1:
            break
        else:
            total -= a[flag]
            flag -= 1
            count -= 1
    if total > k: 
        break
    ans = max(ans, count)

total = 0
count = 0
flag = m-1
for i in range(m):
    total += b[i]
    if total > k:
        total -= b[i]
        flag = i-1
        break
    count += 1
for i in range(n):
    if a[i] > k:
        break
    total += a[i]
    count += 1
    while total > k:
        if flag == -1:
            break
        else:
            total -= b[flag]
            flag -= 1
            count -= 1
    if total > k: 
        break
    ans = max(ans, count)


print(ans)

Solution (Python):
from sys import stdin
import sys

n,m,k = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]

cc = []
count = 0
sumA = 0

for i in range(n):
    sumA_ = sumA + A[i]
    if sumA_ > k: break

    sumA = sumA_
    count = count + 1

maxA = count

sumB = 0
j = 0

for i in range(maxA+1):
    if i != 0:
        count = count - 1
        sumA = sumA - A[maxA-i]

    while True:
        sumB_ = sumB + B[j]

        if sumA + sumB_ > k:
            cc.append(count)
            break

        sumB = sumB_
        count = count + 1
        j = j + 1

        if j == m:
            cc.append(count)
            break

    if j == m:
        break


if cc == []:
    print (0)
else:
    print (max(cc))

Solution (Python):
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b = [0], [0]

for i in range(n):
    a.append(A[i]+a[i])
for i in range(m):
    b.append(B[i]+b[i])

ans, j = 0, m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
a.insert(0,0)
b.insert(0,0)
b_num = sum(b)
b_best = M
j = M
a_num = 0
maxmize = 0
num = 0
for i in range(N+1):
  a_num +=a[i]
  flag = 1
  while flag == 1:
    num = b_num+a_num
    if num<=K:
      maxmize = max(M+i,maxmize)
      break
    else:
      if  not b:
        break
      else:
        j = b.pop()
        M-=1
        b_num = b_num-j
print(maxmize)

Solution (Python):
import itertools
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]+list(map(int,input().split()))
a=list(itertools.accumulate(a))
b=list(itertools.accumulate(b))
ans=0
for i in range(m+1):
  x=k-b[i]
  y=bisect.bisect_right(a,x)
  z=i+y
  if x<0:
      z=0
  ans=max(ans,z)
print(ans)

Solution (Python):
def search(a, s):
    min_i = 0
    max_i = len(a)-1
    i = int(len(a)/2)

    if a[min_i] < s and s < a[max_i]:
        while True:
            if a[i] <= s and s < a[i+1]:
                break
            elif s < a[i]:
                max_i = i
                i = min_i + int((max_i - min_i)/2)
            else:
                min_i = i
                i = min_i + int((max_i - min_i)/2)

        return i
    elif s <= a[min_i]:
        return min_i
    else:
        return max_i

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

A_SUM = [0]
B_SUM = [0]

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

for j in range(M):
    B_SUM.append(B[j] + B_SUM[j])

count = 0
for i in range(N+1):
    if K >= A_SUM[i]:
        a = K - A_SUM[i]
        j = search(B_SUM, a)
        if count < i + j:
            count = i + j

print(count)

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


a = [0]
b = [0]

for i in range(N):
    a.append(a[i] + A[i])
for j in range(M):
    b.append(b[j] + B[j])

ans = 0
j = M

for i in range(N+1):
    if a[i] > K:
        break
    while a[i] + b[j] > K:
        j -= 1
    ans = max(ans, i+j)
    
            
print(ans)

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

a, b = [0], [0]
for i in range(n):
    a += [a[i]+A[i]]
for i in range(m):
    b += [b[i]+B[i]]

ans, i = 0, m
for j in range(n+1):
    if a[j] > k:
        break
    while b[i] > k-a[j]:
        i -= 1
    ans = max(ans, i+j)
print(ans)

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

import numpy as np
A0 = [0] + A
B0 = [0] + B
AA = np.cumsum(A0)
BB = np.cumsum(B0)

ans = 0
jmax = M
for i in range(0, N+1):
    a = AA[i]
    if a > K:
        break
    for j in range(jmax, -1, -1):
        b = BB[j]

        if a + b <= K:
            ans = max(ans, i+j)

            break
    jmax = j
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

A = [0]
B = [0]

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

for i in range(M):
    B.append(B[i]+b[i])
ans = 0

p = M
for i in range(0,N+1):
    if A[i] > K:
        break
    while B[p] > K - A[i]:
        p -= 1
    ans = max(ans,i+p)

print(ans)

Solution (Python):
n,m,k=map(int,input().split())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))

a=[0]
b=[0]

for i in range(n):a.append(a[i]+aa[i])
for i in range(m):b.append(b[i]+bb[i])

ans=0
j=m
for i in range(n+1):
    if(a[i]>k):break
    while b[j]>k-a[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

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

books = 0

A,B = [0],[0]
for i in range(n):
    A.append(A[i]+a[i])
for j in range(m):
    B.append(B[j]+b[j])

l = m

for i in range(n+1):
    if A[i]>k:
        break
    while B[l]>k-A[i]:
        l -= 1
    books = max(books,i+l)

print(books)

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

totalA = [0]
totalB = [0]
sumA = 0
for i in range(N):
    sumA += A[i]
    totalA.append(sumA)
sumB = 0
for i in range(M):
    sumB += B[i]
    totalB.append(sumB)

iB = M
countMax = 0
for iA in range(N+1):
  if totalA[iA]>K:
    break
  else:
    while totalA[iA] + totalB[iB] > K:
      iB -= 1
  countMax = max(countMax,iA+iB)

print(countMax)

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


a_num, b_num, a_time, b_time = 0, 0, 0, 0

for n,b in enumerate(B):
  b_time += b
  b_num += 1
  if b_time > K:
    b_time -= b
    b_num -= 1
    break
    
ans = b_num

for n,a in enumerate(A):
  a_time += a
  a_num += 1
  time = a_time + b_time
  while time > K and b_num > 0:
    b_time -= B[b_num - 1]
    b_num -= 1
    time = a_time + b_time
  if time > K:
    break
  ans = max(ans, a_num + b_num)

print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
answer = 0
b_ind = 0

for x in range(N-1):
    A[x+1] += A[x]
    if A[x+1] > K:
        A = A[:x+1]
        break

for y in range(M-1):
    B[y+1] += B[y]
    if B[y+1] > K:
        B = B[:y+1]
        break

A = [0] + A
B = [0] + B

na = len(A)
nb = len(B) 

if A[-1] + B[-1] <= K:
    answer = len(A) + len(B) - 2
    print(answer)

else:
    for i in range(na):
        for j in range(nb-b_ind-1, -1, -1):
            if A[i] + B[j] <= K:
                if answer < i+j:
                    answer = i+j
                b_ind = nb-j-1
                break
    print(answer)

Solution (Python):

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

a=[0]
b=[0]

for i in range(n):
    a.append(a[i]+A[i])
for i in range(m):
    b.append(b[i]+B[i])

ANS=[0]
idx=m

for i in range(n+1):
    x=k-a[i]
    if x<0:
        break
    while b[idx]>x:
        idx-=1
    ANS.append(i+idx)
print(max(ANS))

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
asum = [0]*(n+1)
for i in range(n):
    asum[i+1] = asum[i] + a[i]
for i in range(1,m):
    b[i] += b[i-1]
ans = 0
from bisect import bisect_right
for i in range(n+1):
    tim = k - asum[i]
    if tim < 0:break
    tmp = bisect_right(b,tim)
    ans = max(ans,i+tmp)
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
na = len(A)
nb = len(B)
total_A = [0]
total_B = [0]
for i in range(1,na+1):
  ta = total_A[i-1] + A[i-1]
  total_A.append(ta)
for i in range(1,nb+1):
  tb = total_B[i-1] + B[i-1]
  total_B.append(tb)
ans = 0
b_start = 0
a = 0
total = 0
nna = na + 1
nnb = nb + 1
for i in range(nna):
  a = total_A[i]
  for j in range(b_start,nnb):
    b = total_B[nnb-1-j]
    if a+b <= K:
      b_start = j
      total = i + (nnb-1-j)
      ans = max(ans,total)
      break
print(ans)

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

a = [0]
b = [0]

for i in range(n):
    a.append(a[i] + A[i])

for i in range(m):
    b.append(b[i] + B[i])

ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k-a[i]:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Solution (Python):
def main():
    import bisect
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a_sum,b_sum = [0], [0]
    for i in range(n):
        a_sum.append(a_sum[i]+a[i])
    for i in range(m):
        b_sum.append(b_sum[i]+b[i])
    ans = 0
    for i in range(n+1):
        cst = a_sum[i]
        if cst > k:
            break
        cst = k-cst
        j = bisect.bisect_right(b_sum,cst)
        if i+j-1>ans:
            ans = i+j-1
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
def get_sum_arr(arr):
    sum_arr = [0]
    N = len(arr)
    for i in range(1,N+1):
        sum_arr.append(sum_arr[i-1] + arr[i-1])
    return sum_arr

def get_max_b(left_time,j,B_sum_arr):
    
    while B_sum_arr[j] > left_time and j > 0:
        j -= 1
    return j
def get_max_book_num(A,B,K):
    A_sum_arr,B_sum_arr = get_sum_arr(A),get_sum_arr(B)
    count,i,j = 0,0,len(B_sum_arr)-1
    for i in range(0,len(A_sum_arr)):
        if K < A_sum_arr[i]:
            break
        else:
            left_time = K - A_sum_arr[i]
            j = get_max_b(left_time,j,B_sum_arr)
        count = max(count,(i+j))
    return count
if __name__ == "__main__":
    N,M,K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    print(get_max_book_num(A,B,K))

Solution (Python):
from itertools import accumulate
from bisect import bisect_right, bisect_left

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

a_books = list(map(int, input().split()))
b_books = list(map(int, input().split()))
a_books = list(accumulate(a_books))
b_books = list(accumulate(b_books))
a_books.insert(0, 0)
b_books.insert(0, 0)
a_index = bisect_right(a_books, K)





max_nums = 0
for i in range(a_index):
    a_cost = a_books[i]
    remain_cost = K - a_cost
    if remain_cost < 0:
        continue
    j = bisect_right(b_books, remain_cost)
    
    max_nums = max(max_nums, i + j - 1)
print(max_nums)

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

a = [0]
b = [0]
total = 0
for Ai in A:
    total += Ai
    a.append(total)
total = 0
for Bi in B:
    total += Bi
    b.append(total)

j = len(b) - 1
ans = 0
for i in range(len(a)):
    if a[i] > k:
        continue
    while a[i] + b[j] > k:
        j -= 1
        if j < 0:
            break
    ans = max(ans, i + j)

print(ans)

Solution (Python):
import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = A[0]
b = B[0]
a_lis = [0,a]
b_lis = [b]
for i in range(1,N):
    a += A[i]
    a_lis.append(a)
for j in range(1,M):
    b += B[j]
    b_lis.append(b)
ans = 0
for i in range(N+1):
    if a_lis[i] <= K:
        j = bisect.bisect_right(b_lis,K-a_lis[i])
        tmp_ans = i + j 
        if ans < tmp_ans:
            ans = tmp_ans
    else:
        break
print(ans)

Solution (Python):
import bisect

n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Acum = [A[0]] + [0]*(n-1)
Bcum = [B[0]] + [0]*(m-1)
for i in range(1,n):
    Acum[i] = Acum[i-1] + A[i]
for i in range(1,m):
    Bcum[i] = Bcum[i-1] + B[i]
maxn = 0
Bcum = [0] + Bcum
for i in range(m+1):
    if Bcum[i] <= k:
        maxn = max(maxn,i + bisect.bisect_right(Acum, k-Bcum[i]))
print(maxn)

Solution (Python):
number=list(map(int,input().split()))
N,M,K=number[0],number[1],number[2]
A=list(map(int,input().split()))
B=list(map(int,input().split()))
answer=0
j=M
a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])

for i in range(N+1):
    if a[i]>K:
        break
    while b[j]>K-a[i]:
        j-=1
    answer=max(answer,i+j)

print(answer)

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

a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])
ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

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

from itertools import accumulate
CA = [0]+A
CB = [0]+B
CA = list(accumulate(CA))
CB = list(accumulate(CB))


import bisect
ans = 0
for i in range(n+1):
    t = k-CA[i]
    if t < 0:
        continue
    j = bisect.bisect_right(CB, t)
    
    ans = max(i+j-1, ans)
print(ans)

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

a, b = [0], [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])

ans, j = 0, m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

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

a, b = [0], [0]
for i in range(N):
    a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])

ans, j = 0, M
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K-a[i]:
        j -= 1
    
    ans = max(ans, i+j)
    
print(ans)