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: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

    ans = 0
    for i in range(n+1):
        if at[i] > k:
            break
        for j in reversed(range(m+1)):
            if k >= at[i] + bt[j]:
                break
        ans = max(ans, i+j)

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
import copy
# import numpy as np
from collections import deque

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

cnt = 0


a.insert(0, 0)
b.insert(0, 0)


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

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


for i in range(n+1):
    for j in range(m+1):
        if a[i] + b[j] <= k:
            pass
        else:
            if a[i] + b[j-1] <= k:
                cnt = max(cnt, i + j - 1)
            break
    else:
        if a[i] + b[j] <= k:
            cnt = max(cnt, i + j)
print(cnt)

Submission status: Time Limit Exceeded

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 = [0]
sumlist_b = []

for i in range(n):
    x += a[i]
    if x > k:
        break
    sumlist_a.append(x)
a_available = len(sumlist_a)

# for j in range(m):
#     y += b[j]
#     sumlist_b.append(y)
#     if y > k:
#         break
ans = 0
#ansがaから全部本をとってこれる最大個数
for i in range(len(sumlist_a)):
    time = sumlist_a[i]
    current_ans = i
    for j in range(m):
        time += b[j]
        if time > k:
            break
        else:
            current_ans += 1
    ans = max(ans,current_ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from collections import defaultdict, Counter, deque
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    
    n,m,k = map(int, input().split())
    n_list = list(map(int, input().split()))
    m_list = list(map(int, input().split()))

    a_list = [0]
    b_list = [0]

    ans = 0
    num = 0

    for i in range(n):
        a_list.append(a_list[i] + n_list[i])

    for i in range(m):
        b_list.append(b_list[i] + m_list[i])


    for i in range(n+1):
        if k < a_list[i]:
            break
        else:
            for j in range(m+1):
                num = a_list[i] + b_list[j]
                if k < num:
                    break
                else:
                    if ans < i+j:
                        ans = i+j
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from collections import defaultdict, Counter, deque
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    
    n,m,k = map(int, input().split())
    n_list = list(map(int, input().split()))
    m_list = list(map(int, input().split()))

    a_list = [0]
    b_list = [0]

    ans = 0
    num = 0

    for i in range(n):
        a_list.append(a_list[i] + n_list[i])

    for i in range(m):
        b_list.append(b_list[i] + m_list[i])


    for i in range(n+1):
        if k < a_list[i]:
            continue
        else:
            for j in range(m+1):
                num = a_list[i] + b_list[j]
                if k < num:
                    break
                else:
                    if ans < i+j:
                        ans = i+j
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
import copy
# import numpy as np
from collections import deque

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

cnt = 0

a_sum = [0]
b_sum = [0]

a_sum.extend(a)
b_sum.extend(b)


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

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


for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            pass
        else:
            if a_sum[i] + b_sum[j-1] <= k:
                cnt = max(cnt, i + j - 1)
            break
    else:
        if a_sum[i] + b_sum[j] <= k:
            cnt = max(cnt, i + j)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
from itertools import accumulate #list(accumulate(A))

N, M, lim = mi()
A = li()
B = li()

A = [0] + list(accumulate(A))
ans = 0

for i in range(N+1):
    right = 0
    cnt = 0
    wa = 0
    left = 0

    while right < M and wa + B[right] <= lim - A[i]:
        wa += B[right]
        right += 1

    cnt = right - left

    if cnt > 0 or A[i] <= lim:
        ans = max(ans, cnt+i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#n = int(input())
n,m,k = [int(z) for z in input().split()]
a = [int(z) for z in input().split()]
b = [int(z) for z in input().split()]
time = 0
x=[0]
y=[0]
#maX = -100000
num = 0
for i in range(n):
      x.append(x[i]+a[i])
for j in range(m):
      y.append(y[j]+b[j])

for i in range(n+1):
      if (x[i]>k):
            break
      for j in range(m+1):
            if (y[j]>k):
                  break
            z = x[i]+y[j]
            if (z<=k):
                  num = max(num,i+j)
                  
print(num)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
import copy
# import numpy as np
from collections import deque

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

cnt = 0

a_sum = [0]
b_sum = [0]

a_sum.extend(a)
b_sum.extend(b)


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

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


for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            cnt = max(cnt, i + j)
        else:
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#n = int(input())
n,m,k = [int(z) for z in input().split()]
a = [int(z) for z in input().split()]
b = [int(z) for z in input().split()]
time = 0
x=[0]
y=[0]
#maX = -100000
num = 0
for i in range(n):
      x.append(x[i]+a[i])
for j in range(m):
      y.append(y[j]+b[j])

for i in range(n+1):
      for j in range(m+1):
            z = x[i]+y[j]
            if (z<=k):
                  num = max(num,i+j)
                  
print(num)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m, k = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]

A = np.cumsum([0] + A)
B = np.cumsum([0] + B)
n_read = 0
for i, a in enumerate(A):
    if a > k:
        break
    for j, b in enumerate(B):
        if a + b > k:
            break
        if n_read < i + j:
            n_read = i + j
print(n_read)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().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):
  j=m
  if A[i]>k:
    break
  nokori=k-A[i]

  while(nokori<B[j] and 0<=j):
    j-=1

    
  ans=max(ans,i+j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m, k = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]

A = np.cumsum([0] + A)
B = np.cumsum([0] + B)
n_read = 0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        if a + b <= k and n_read < i + j:
            n_read = i + j
print(n_read)

Submission status: Time Limit Exceeded

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

A_sums = [0] * (N + 1)
B_sums = [0] * (M + 1)
for i in range(N):
  A_sums[i + 1] = A_sums[i] + A[i]
for i in range(M):
  B_sums[i + 1] = B_sums[i] + B[i]

ans = 0
for i in range(N + 1):
  for j in range(M + 1):
    if A_sums[i] + B_sums[j] > K:
      break
    ans = max(ans, i + j)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

asum = [0]
bsum = [0]
tmp = 0
for i in range(n):
    tmp += a[i]
    asum.append(tmp)
tmp = 0
for i in range(m):
    tmp += b[i]
    bsum.append(tmp)


ans = 0
ms = copy.deepcopy(m)
for na in range(n+1):
    res = k - asum[na]
    if res < 0:
        break
    flg = True
    nb = copy.deepcopy(m)
    while flg:
        if bsum[nb] > res:
            nb -= 1
            continue
        ans = max(ans, na+nb)
        ms = copy.deepcopy(nb)
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

asum = [0]
bsum = [0]
tmp = 0
for i in range(n):
    tmp += a[i]
    asum.append(tmp)
tmp = 0
for i in range(m):
    tmp += b[i]
    bsum.append(tmp)


ans = 0
for na in range(n+1):
    res = k - asum[na]
    if res < 0:
        break
    flg = True
    nb = copy.deepcopy(m)
    while flg:
        if bsum[nb] > res:
            nb -= 1
            continue
        ans = max(ans, na+nb)
        break
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for a_cnt in range(n + 1):
    if a[a_cnt] > k:
        continue
    b_cnt = 0
    while b_cnt < m and a[a_cnt] + b[b_cnt + 1] <= k:
        b_cnt += 1

    ans = max(ans, a_cnt + b_cnt)

print(ans)

Submission status: Time Limit Exceeded

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

read = 0

c = k
for i in range(0, n):
    d = c
    for j in range(0, m):
        d = d-b[j]
        if d<0:
            break
        read = read if read > i+j+1 else i+j+1
    c = c-a[i]
    if c<0:
        break
    read = read if read > i+1 else i+1

read += 0 if d-a[-1]<0 else 1
print(read)

Submission status: Time Limit Exceeded

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

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

#print(sumA)
    
#sumA = list(itertools.accumulate(A))
#sumB = list(itertools.accumulate(B))
ans=0
j=M
for i in range(N+1):
    if sumA[i]>K:
        break
    #j-=1
    while K<sumA[i]+sumB[j] and j>=0:
        j-=1
    if i==0:
        best=j
    ans=max(ans,i+j)
    j=best
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
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] + a[i]

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

print(ans)

Submission status: Time Limit Exceeded

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

asum = [0]
bsum = [0]
tmp = 0
for i in range(n):
    tmp += a[i]
    asum.append(tmp)
tmp = 0
for i in range(m):
    tmp += b[i]
    bsum.append(tmp)


ans = 0
for na in range(n+1):
    for nb in range(m+1):
        time = asum[na] + bsum[nb]
        if time > k:
            break
        ans = max(ans, na+nb)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = 0
sum_a = [0]
for i in range(n):
  if t + a[i] <= k:
    t += a[i]
    sum_a.append(t)
  else:
    break
sum_b = [0]
t = 0
for i in range(m):
  if t + b[i] <= k:
    t += b[i]
    sum_b.append(t)
  else:
    break

ans = []
for i in range(len(sum_a)):
  for j in range(len(sum_b)):
    if sum_a[i] + sum_b[len(sum_b) - j - 1] <= k:
      ans.append(i + len(sum_b) - j - 1)
      break
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

def aaa(al,bl):
    p=0
    for i in range(len(al)):
        for l in range(len(bl)):
            if al[-i-1]+bl[-l-1]<=k:
                q=len(al)+len(bl)-i-l-2
                if p<q:
                    p=q
                break
        if al[-i-1]+bl[-1]<k:
            break
    return p

x=input()
x0=x.split()
n=int(x0[0])
m=int(x0[1])
k=int(x0[2])
a=input()
b=input()
a0=[int(i) for i in a.split()]
b0=[int(i) for i in b.split()]
a=0
b=0
al=list([0])
bl=list([0])
for i in range(n):
    a=a+a0[i]
    if a>k:
        break
    al.append(a)
for i in range(m):
    b=b+b0[i]
    if b>k:
        break
    bl.append(b)

z=aaa(al,bl)
print(z)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().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=0

for i in range(n+1):
  j=0
  if A[i]>k:
    break
  nokori=k-A[i]

  while(B[j]<=nokori and j<m):
    j+=1

  if nokori<B[j]:
    j-=1
    
  ans=max(ans,i+j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M, K, *AB = map(int, open(0).read().split())
a = []
b = []

for x in range(len(AB)):
    if x < N:
        a.append(AB[x])
    else:
        b.append(AB[x])

sum = 0
a_max = len(a) - 1
for ind, x in enumerate(a):
    if sum + x > K:
        a_max = ind - 1
        break
    sum += x

sum = 0
b_max = len(b) -1
for ind, x in enumerate(b):
    if sum + x > K:
        b_max = ind - 1
        break
    sum += x

def solve(max_count, K, a, b, a_max, b_max):
    ind = 0
    while ind <= a_max:
        sum = 0
        count = 0
        for i, x in enumerate(a):
            if i > ind:
                break
            sum += x
            count += 1
        
        for i, x in enumerate(b):
            if sum + x > K:
                break
            sum += x
            count += 1

        max_count = max(max_count, count)
        ind += 1

    print(max_count)

max_count = max(a_max, b_max) + 1

if a_max < 0 and b_max < 0:
    print(0)
elif a_max < 0 or b_max < 0:
    print(max_count)
else:
    solve(max_count, K, a, b, a_max, b_max)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

a = [0]
b = [0]
for i in range(len(A)):
    a.append(a[i] + A[i])

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


# print(a)
# print(b)


ans = 0

for i_n in range(n + 1):
    if a[i_n] > k:
        break

    for j_n in range(m + 1):
        if a[i_n] + b[j_n] <= k:
            # print(i_n, j_n, a[i_n] + b[j_n])
            ans = max(ans, i_n + j_n)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
x=input()
x0=x.split()
n=int(x0[0])
m=int(x0[1])
k=int(x0[2])
a=input()
b=input()
a0=[int(i) for i in a.split()]
b0=[int(i) for i in b.split()]
a=0
b=0
al=list([0])
bl=list([0])
for i in range(n):
    a=a+a0[i]
    if a>k:
        break
    al.append(a)
for i in range(m):
    b=b+b0[i]
    if b>k:
        break
    bl.append(b)
p=0
for i in range(len(al)):
    for l in range(len(bl)):
        if al[-i-1]+bl[-l-1]<=k:
            q=len(al)+len(bl)-i-l-2
            if p<q:
                p=q
            break
    if al[-i-1]+bl[-1]<k:
        break
print(p)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

nmax=0

Asum=list()
Asum.append(0)
Bsum=list()
Bsum.append(0)

imax_a = N
for i in range(N):
    s = Asum[i]+A[i]
    Asum.append(s)
    if s > K:
        imax_a = i+1
        break

imax_b = M
for i in range(M):
    s = Bsum[i]+B[i]
    Bsum.append(s)
    if s > K:
        imax_b = i+1
        break
        
for i in range(imax_a+1):
    can_update = 1
    for j in range(imax_b, -1, -1):
        time = Asum[i]+Bsum[j]
        # print("i=",i,"j=",j,"time=",time)
        if time <= K:
            if can_update == 1:
                imax_b = j
                can_update = 0

            if nmax < i+j:
                nmax = i+j
                # print("i=",i," j=",j," Asum",Asum[i]," Bsum", Bsum[j], " time=",time, " nmax=", nmax)
                break

print(nmax)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from sys import exit

N, M, K = [int(x) for x in input().split()]

a_book = [0]
for index, i in enumerate(input().split(), 1):
    if a_book[index-1] + int(i) <= K:
        a_book.append(a_book[index-1] + int(i))
    else:
        N = index - 1
        break

b_book = [0]
for index, i in enumerate(input().split(), 1):
    if b_book[index-1] + int(i) <= K:
        b_book.append(b_book[index-1] + int(i))
    else:
        M = index - 1
        break

search = deque()
check = {}

max_books = 0

for a_pos in range(N, -1, -1):
    a = a_book[a_pos]
    b_pos = 0
    for x in range(b_pos, M+1):
        if a + b_book[x] > K:
            break

        if max_books < a_pos + x:
            max_books = a_pos + x

print(max_books)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
# import copy
# import numpy as np
from collections import deque

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

cnt = 0

a_sum = deque([0])
b_sum = deque([0])
atmp = 0
btmp = 0

for i in a:
    atmp += i
    a_sum.append(atmp)

for i in b:
    btmp += i
    b_sum.append(btmp)


for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            cnt = max(cnt, i + j)
        else:
            break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
# import copy
# import numpy as np
from collections import deque

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

cnt = 0

a_sum = deque([0])
b_sum = deque([0])
atmp = 0
btmp = 0

for i in a:
    atmp += i
    a_sum.append(atmp)

for i in b:
    btmp += i
    b_sum.append(btmp)


for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            cnt = max(cnt, i + j)

print(cnt)

Submission status: Time Limit Exceeded

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
for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

for k in range(N+1):
    if AA[k] <= K:
        for j in range(M-1, -1, -1):
            if  AA[k] + BB[j+1]> K >= AA[k] + BB[j]:
                ans = max(k+j, ans)
                break
            else:
                continue
        if K >= AA[k] + BB[M]:
            ans = max(k+M, ans)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = 0
sum_a = [0]
for i in range(n):
  if t + a[i] <= k:
    t += a[i]
    sum_a.append(t)
  else:
    break
sum_b = [0]
t = 0
for i in range(m):
  if t + b[i] <= k:
    t += b[i]
    sum_b.append(t)
  else:
    break
ans = []
for i in range(len(sum_a)):
  for j in range(len(sum_b)):
    if sum_a[i] + sum_b[j] <= k:
      ans.append(i + j)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from sys import exit
# import copy
# import numpy as np
# from collections import deque

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

cnt = 0

a_sum = [0]
b_sum = [0]
atmp = 0
btmp = 0

for i in a:
    atmp += i
    a_sum.append(atmp)

for i in b:
    btmp += i
    b_sum.append(btmp)


for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            cnt = max(cnt, i + j)

print(cnt)

Submission status: Time Limit Exceeded

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
for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

for k in range(N+1):
    if AA[k] <= K:
        for j in range(M):
            if  AA[k] + BB[j+1]> K >= AA[k] + BB[j]:
                ans = max(k+j, ans)
                break
            else:
                continue
        if K >= AA[k] + BB[M]:
            ans = max(k+M, ans)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

nmax=0

Asum=list()
Asum.append(0)
Bsum=list()
Bsum.append(0)

imax_a = N
for i in range(N):
    s = Asum[i]+A[i]
    Asum.append(s)
    if s > K:
        imax_a = i+1

imax_b = M
for i in range(M):
    s = Bsum[i]+B[i]
    Bsum.append(s)
    if s > K:
        imax_b = i+1
        
for i in range(N+1):
    can_update = 1
    for j in range(imax_b, -1, -1):
        time = Asum[i]+Bsum[j]
        # print("i=",i,"j=",j,"time=",time)
        if time <= K:
            if can_update == 1:
                imax_b = j
                can_update = 0

            if nmax < i+j:
                nmax = i+j
                # print("i=",i," j=",j," Asum",Asum[i]," Bsum", Bsum[j], " time=",time, " nmax=", nmax)
                break

print(nmax)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def solve(K,a,b):
    ans = 0
    time = 0
    cum_a = [0]*(len(a)+1)
    for i in range(len(a)):
        cum_a[i+1] = cum_a[i] + a[i]

    cum_b = [0]*(len(b)+1)
    for i in range(len(b)):
        cum_b[i+1] = cum_b[i] + b[i]
    
    for i in range(len(cum_a)):
        if cum_a[i] > K:
            break
        for j in range(len(cum_b)):
            time = cum_a[i] + cum_b[j]
            if time <= K:
                ans = max(ans,i+j)
            else:
                break
    return ans

def main():
    N,M,K = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    print(solve(K,a,b))
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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]
for i in range(len(a)):
    A.append(A[i]+a[i])
for i in range(len(b)):
    B.append(B[i]+b[i])

cnt = 0
for i, a_ in enumerate(A):
    m = k-a_
    j = len(B)-1
    t = 0
    if m < 0:
        break
    while m < B[j]:
        j-=1
    t = i+j
    cnt = max(cnt, t)
print(cnt)

Submission status: Time Limit Exceeded

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

suba=[0]
subb=[0]

for i in range(n):
    suba.append(a[i]+suba[i])
for j in range(m):
    subb.append(b[j]+subb[j])

ans=0

for i in range(n+1):
    j=m
    while True:
        #print(i,j)
        if k<suba[i]:
            i=0
            j=0
            break

        if subb[j]<=k-suba[i]:
            break
        j-=1
    ans=max(ans,i+j)
print(ans)

Submission status: Time Limit Exceeded

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
for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

for k in range(N+1):
    if AA[k] <= K:
        for j in range(M+1):
            if  K >= AA[k] + BB[j]:
                ans = max(k+j, ans)
                continue
            else:
                break
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools as it
input = sys.stdin.readline

N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

AS = list(it.accumulate(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max = 0
for i in range(N+1):
    a = AS[i]
    if a > K:
        continue
    j = M
    while j < M + 1:
        b = BS[j]
        if a + b <= K:
            if max < i + j:
                max = i + j
            break
        j -= 1

print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
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]+ a[i]

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

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

Submission status: Time Limit Exceeded

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


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()
sumlist_a.append(0)

#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)-1+cnt2-p)
    p += 1
    cnt2 = 0
    y = 0
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a = [0] + [sum(a[:i + 1]) for i in range(n)]
b = [0] + [sum(b[:i + 1]) for i in range(m)]
ans = 0
t = 0
for i in range(n + 1):
    for j in range(m + 1):
        if a[i] + b[j] <= k:
            ans = max(ans, i + j)
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=map(int,input().split())
ab=[int(i) for i in input().split()] + [int(i) for i in input().split()]
ans = 0
for i in range(len(ab)):
    x = min(ab)
    k -= x
    ab.remove(x)
    if k < 0:
        break
    ans += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
A,B, K=map(int,input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
res = 0
a=0
b=0
inter = 0
n = max(A, B)
for i in range(n):
  if len(List_A)==0 and len(List_B)==0:
    break
  elif len(List_A)==0 and len(List_B)!=0:
    inter+=List_B.pop(0)
    if K < inter:
      break
    res +=1
  elif len(List_A) !=0 and len(List_B)==0:
    inter+=List_A.pop(0)
    if K < inter:
      break
    res +=1
  else:
    a = List_A.pop(0)
    b = List_B.pop(0)
    inter = inter + a+b
    if K < inter: 
      inter = inter -a
      if K < inter:
        inter = inter +a-b
        if K < inter:
          break
        else:
          res +=1
      else:
        res +=1
    else:
      res +=2
print(res)

Submission status: Time Limit Exceeded

Solution code:
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)
a_sum = 0

max_cnt = 0
for i in range(n + 1):
    a_sum += A[i]
    if a_sum > k:
        break

    b_sum = 0
    for j in range(m + 1):
        b_sum += B[j]
        if a_sum + b_sum > k:
            break

        if max_cnt < i + j:
            max_cnt = i + j

print(max_cnt)