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:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys


def cumulative_sum(ls):
    res = []
    tmp = 0
    for el in ls:
        tmp += el
        res.append(tmp)
    return res


d1 = stdin.readline().rstrip().split()
N = int(d1[0])
M = int(d1[1])
K = int(d1[2])
A = [int(s) for s in stdin.readline().rstrip().split()]
B = [int(s) for s in stdin.readline().rstrip().split()]

num = 0
M = 0
pos = 0
for el in A:
    num += el
    if num > K:
        break
    M += 1
res = 0
sums = cumulative_sum(A)
for m in range(M+1):
    summ = 0
    if m != 0:
        summ = sums[m-1]
    count = 0
    for el in B:
        summ += el
        if summ > K:
            break
        count += 1
    if m+count >= res:
        res = m+count
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate

input = lambda: sys.stdin.readline().rstrip()
n, m, k = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + a
b = [0] + b
# 累積和を利用
a_accum = list(accumulate(a))
b_accum = list(accumulate(b))

ans = 0
for i in range(len(a_accum)):
    for j in range(len(b_accum)):
        if (a_accum[i] + b_accum[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()))

i = 1
time = 0
while time < k:
  combination = [[j,i-j] for j in range(i+1) if j<n+1 and i-j<m+1]

  minimum = sum(a[0:combination[0][0]]) + sum(b[0:combination[0][1]])
  for c in combination[1:]:
    s = sum(a[0:c[0]]) + sum(b[0:c[1]])
    if s < minimum:
      minimum = s
  time = minimum

  if time > k:
    break
  i += 1
  
print(i-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)

def main():
    N,M,K = [int(i) for i in input().strip().split()]
    A = [int(i) for i in input().strip().split()]
    B = [int(i) for i in input().strip().split()]

    pre_A = [0] * (N + 1)
    pre_B = [0] * (M + 1)


    for i in range(1,N+1):
        pre_A[i] = pre_A[i-1] + A[i-1]
    for i in range(1,M+1):
        pre_B[i]= pre_B[i-1] + B[i-1]

    ans = 0

    for i in range(N+1):
        for j in range(M+1):
            if pre_A[i] + pre_B[j] <= K:
                ans = max(ans,i+j)
            else:
                break

    return ans

if __name__ == "__main__":
    print(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()))
X = N + M
sum_n = 0
sum_m = 0
ans = 0
for i in range(1,X+1):
    for j in range(i+1):
        n = j
        m = i - j
        if(n <= N and m <= M):
            for k in range(n):
                sum_n += A[k]
            for l in range(m):
                sum_m += B[l]
            if((sum_m + sum_n) <= K):
                ans=i
                sum_m = 0
                sum_n = 0
                break
            sum_m = 0
            sum_n = 0
print(ans)

Submission status: Time Limit Exceeded

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

time_A = list(map(int, input().split()))
time_B = list(map(int, input().split()))
max_books = 0
allot_to_A = 0
allot_to_B = K

while True:
    num_books = 0
    change_point_A = 10000000000
    change_point_B = 10000000000
    time_bankA = allot_to_A
    time_bankB = allot_to_B

    for i in range(len(time_A)):
        if time_A[i] <= time_bankA:
            time_bankA -= time_A[i]
            num_books += 1
        else:
            change_point_A = time_A[i] - time_bankA  # Aで読める本が１冊増える時間
            break

    for j in range(len(time_B)):
        if time_B[j] <= time_bankB:
            time_bankB -= time_B[j]
            num_books += 1
        else:
            change_point_B = time_bankB + 1  # Bで読める本が１冊へる時間
            break

    max_books = max(max_books, num_books)
    change_point = min(change_point_A, change_point_B)
    allot_to_A += change_point
    allot_to_B -= change_point
    if allot_to_A < 0 or allot_to_B < 0:
        break

print(max_books)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = []
timeA = 0
a = 0
timeB = 0
b = 0  
for i in range(n+1):
    for j in range(m+1):
        if i != 0:
            timeA = sum(A[:i])
            a = i
            if j != 0:  
                timeB = sum(B[:j])            
                b = j
            else:
                timeB = 0 
                b = 0  
        else:
            timeA = 0
            a = 0
         
        if timeA + timeB <= k:
            ans.append(a+b)
print(max(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]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
    
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)

maxval=len(Asum)-1

tmp=0
cnt=0
for i in range(len(Asum)):
  for j in range(len(Bsum)):
    cnt+=1
    j=tmp+j
    if j>=len(Bsum)-1:j=len(Bsum)-1
    if Asum[-i-1]+Bsum[j]>K:
      maxval=max(maxval,len(Asum)-1-i+j-1)
      tmp=j
      break
    elif j==len(Bsum)-1:
      maxval=max(maxval,len(Asum)-1-i+j)
      break
print(maxval)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from sys import exit
N, M, K = map(int, input().split())
A = [0] + list(accumulate(list(map(int, input().split()))))
B = [0] + list(accumulate(list(map(int, input().split()))))
n = -1
m = -1
for a in A:
  n += 1
  if a >= K:
    break
for b in B:
  m += 1
  if b >= K:
    break
ans = 0
if n >=m:
  for i in range(n + 1):
    for j in range(m, -1, -1):
      if A[i] + B[j] <= K:
        if ans <= i + j:
          ans = max(ans, i + j)
          m = j
          break
else:
  for i in range(m + 1):
    for j in range(n, -1, -1):
      if B[i] + A[j] <= K:
        if ans <= i + j:
          ans = max(ans, i + j)
          n = j
          break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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()))
A.insert(0, 0)
B.insert(0, 0)
A_sum = [np.sum(A[0:e+1]) for e in range(len(A)) ]
B_sum = [np.sum(B[0:e+1]) for e in range(len(B)) ]
num=0
for i in range(len(A)):
    for l in range(len(B)):
        if num<i+l:
            Time=A_sum[i]+B_sum[l]
            if Time<=K:
                num=i+l
print(num)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split(' '))
list_a = list(map(int, input().split(' ')))
list_b = list(map(int, input().split(' ')))
 
a_sum=[list_a[0]]
for i in range(1, n):
  sum=list_a[i]+a_sum[i-1]
  a_sum.append(sum)
  
b_sum=[list_b[0]]
for i in range(1, m):
  sum=list_b[i]+b_sum[i-1]
  b_sum.append(sum)
  
ans=[]
if (a_sum[0]>k) & (b_sum[0]>k):
  print(0)
else:
  for i in range(n):
    if a_sum[i]<=k:
      ans.append(i+1)
    for j in range(m):
      if b_sum[j]<=k:
        ans.append(j)
      if a_sum[i]+b_sum[j] <= k:
        key=i+j+2
        if key not in ans:
          ans.append(key)
  print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input().split()
N = int(s[0])
M = int(s[1])
K = int(s[2])

A = []
s2 = input().split()
for i in range(N):
  A.append(int(s2[i]))
  
B = []
s3 = input().split()
for i in range(M):
  B.append(int(s3[i]))

ssum = 0
result = 0
i = 0
while ssum < K and i <= N:
  subsum = sum(A[:i])
  ssum = subsum
  j = 0
  while j <= M:
    if subsum + sum(B[:j]) > K:
      break
    else:
      j += 1
  if (i + j - 1) > result:
    result = i + j - 1
  i += 1

print(result)

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()))
memo = [[None] * (M + 1) for _ in range(N + 1)]
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
r = 0
for i, a in enumerate(A):
    for j, b in enumerate(B):
        k = K - a - b
        if k < 0:
            break
        r = max(r, i + j)
print(r)

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()))
c = []
a.append(300000)
b.append(300000)
e = 0
f = 0
for i in range(len(a)):
    b.append(a[i])
while sum(c) < k:
    if a[e] < b[f] < 250000:
        c.append(a[e])
        e += 1
    elif b[f] < a[e] < 250000:
        c.append(b[f])
        f += 1
if sum(c) == k:
    print(len(c))
else:
    print(len(c)-1)

Submission status: Time Limit Exceeded

Solution code:
n, m, k =map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i,j= 0,0
cnt = 0
dp = [[0 for i in range(n+1)] for j in range(m+1)]
ans = 0
for i in range(n):
    dp[0][i+1] = dp[0][i]+a[i]
    if ans<i+1 and k>=dp[0][i+1]:
        ans = i+1
#print(dp[0])
for j in range(m):
    dp[j+1][0] = dp[j][0]+b[j]
    if ans<j+1 and dp[j+1][0]<=k:
        ans = j+1
    #print(dp[j+1][0])
#print(dp)
for i in range(1,m+1):
    for j in range(1,n+1):
        dp[i][j] = dp[0][j] + dp[i][0]
        if dp[i][j]<=k:
            ans = max(ans,i+j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate

input = lambda: sys.stdin.readline().rstrip()
n, m, k = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + a
b = [0] + b
# 累積和を利用
a_accum = list(accumulate(a))
b_accum = list(accumulate(b))

ans = 0
for i in range(len(a_accum)):
    for j in range(len(b_accum)):
        if (a_accum[i] + b_accum[j] <= k):
            ans = max(ans, i + j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import *
from bisect import bisect_right
n,m,time = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

A = [0]+list(accumulate(a))
B = [0]+list(accumulate(b))

ans = 0
for i in range(bisect_right(A, time)-1,-1,-1):
    for j in range(bisect_right(B, time)-1,-1,-1):
        if A[i] + B[j] <= time:
            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()))
ans = 0
rest = k
sm = 0
end = -1
for j, b in enumerate(B):
    sm += b
    if sm < k:
        end = j
    else:
        sm -= b
        break
ans = end+1
for i, a in enumerate(A):
    sm += a
    if sm <= k:
        ans = max(end+1+i+1, ans)
        continue
    else:
        for j, b in enumerate(B[:end+1][::-1]):
            sm -= b
            if sm <= k:
                end -= j+1
                ans = max(end+1+i+1, ans)
                break
print(ans)

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()))
 
T_A = [0, A[0]]
T_B = [0, B[0]]
for i in range(1, N):
  T_A.append(T_A[i] + A[i])
for j in range(1, M):
  T_B.append(T_B[j] + B[j])
      
book = 0
for i in range(len(T_A)):
  for j in range(len(T_B)):
    if K < T_A[i] + T_B[j]:
      break
    else:
      if i + j  > book:
        book = i + j
      else:
        pass
    
print(book)

Submission status: Time Limit Exceeded

Solution code:

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

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M+1)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i+1] = bp + b
        bp += b

    for bi,b in enumerate(B_cum):
        if b > K:
            break

    max_cnt = bi-1 
    bp = bi

    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        for bi in list(range(bp+1))[::-1]:
            b = B_cum[bi]
            if b+a <= K:
                break
        bp = bi
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline

if __name__ == '__main__':
  n,m,k=map(int,input().strip("\n").split())
  A=list(map(int,input().strip("\n").split()))
  B=list(map(int,input().strip("\n").split()))
#  A.append(10**9+1)
#  B.append(10**9+1)

  def dfs(a,b,k,s):
    if k<0:
      return s-1
    if k==0:
      return s
    if a<n:
      ansA=dfs(a+1,b,k-A[a],s+1)
    else:
      ansA=s

    if b<m:
      ansB=dfs(a,b+1,k-B[b],s+1)
    else:
      ansB=s
    return max(ansA,ansB)

  ans=max(dfs(1,0,k-A[0],1),dfs(0,1,k-B[0],1))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque
from itertools import accumulate
def main():
    n, m, k = map(int, input().split())
    A = [0] + list(map(int, input().split()))
    B = [0] + list(map(int, input().split()))
    X = list(accumulate(A))
    Y = list(accumulate(B))

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



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

a_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

iMax = bisect.bisect(a_sum, k)
jMax = bisect.bisect(b_sum, k)

if iMax == 0 and jMax == 0:
  print(0)
  exit()

ans = 0
for i in range(iMax):
  tmp = i + 1 + bisect.bisect(b_sum[:jMax], k - a_sum[i]) 
  ans = max(ans, tmp)

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

AL = [0]
for i in range(N):
    AL.append(AL[-1] + A[i])

BL = [0]
for i in range(M):
    BL.append(BL[-1] + B[i])

ans = 0
for i in range(N+1):
    for j in range(M+1):
        if AL[i] + BL[j] <= K:
            ans = max(ans, i+j)
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys

d1 = stdin.readline().rstrip().split()
N = int(d1[0])
M = int(d1[1])
K = int(d1[2])
A = [int(s) for s in stdin.readline().rstrip().split()]
B = [int(s) for s in stdin.readline().rstrip().split()]

num = 0
M = 0
pos = 0
for el in A:
    num += el
    if num > K:
        break
    M += 1
res = 0
for m in range(M+1):
    summ = 0
    for el in range(m):
        summ += A[el]
    count = 0
    for el in B:
        summ += el
        if summ > K:
            break
        count += 1
    if m+count >= res:
        res = m+count
print(res)

Submission status: Time Limit Exceeded

Solution code:
anum, bnum, time= map(int, input().split(" "))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
tempa = [0]
tempb = [0]
total = 0
for i in range(anum):
    tempa.append(tempa[-1] + a[i])
    if total > time:
        break
for i in range(bnum):
    tempb.append(tempb[-1] + b[i])
    if total > time:
        break
maxi = 0
for i in range(anum + 1):
    for j in range(bnum + 1):
        if tempa[i] + tempb[j] <= time:
            if i + j > maxi:
                maxi = i + j
        else:
            break
print(maxi)

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

Sa = [0]*N
Sb = [0]*M
Sa[0] = A[0]
Sb[0] = B[0]
a = 0
b = 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

ans = max(a, b)
for i in range(a):
    temp = K - Sa[i]
    for j in range(b):
        if Sb[j] > temp:
            ans = max(ans, i+1+j)
            break
    else:
        ans = max(ans, i+1+b)
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()))

time = 0
cnt = 0
ans = 0
for i in range(n+1):
    for j in range(m+1):
        time = sum(A[:i])+sum(B[:j])
        cnt = i + j
        if time <= k and ans < cnt:
            ans = 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()))
AS=[0]
BS=[0]
r=0
for i in A:
  AS.append(i+AS[-1])
for i in B:
  BS.append(i+BS[-1])
for i in range(n+1):
  for j in range(m+1):
    if AS[i]+BS[j]<=k:
      if i+j>r:
        r=i+j
print(r)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
data = stdin.readlines()

n = int(data[0].split()[0])
m = int(data[0].split()[1])

k = int(data[0].split()[2])

a = [int(s) for s in data[1].split()]
b = [int(s) for s in data[2].split()]

c = [0]
time = 0
for i in range(1,n+m+1):
    for j in range(0,i+1):
        if j <= n and 0 <= i-j <= m:
            if sum(a[0:j])+sum(b[0:i-j]) <= k:
                c.append(i)
print(max(c))

Submission status: Time Limit Exceeded

Solution code:

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

# cumsum
A_cum = [0] * (N+1)
B_cum = [0] * (M+1)
ap = 0
for i, a in enumerate(A_list):
    A_cum[i+1] = ap + a
    ap += a
bp = 0
for i, b in enumerate(B_list):
    B_cum[i+1] = bp + b
    bp += b

for bi,b in enumerate(B_cum):
    if b > K:
        break

max_cnt = bi-1 
bp = bi

for i in range(N+1):
    a = A_cum[i]
    if a > K:
        break
    for bi in list(range(bp+1))[::-1]:
        b = B_cum[bi]
        if b+a <= K:
            break
    bp = bi
    max_cnt = max(max_cnt, i+bi)

print(max_cnt)

Submission status: Time Limit Exceeded

Solution code:

import bisect

def appendd(N, A, sumlis, lis, K):
    for a in range(N):
        if A[a]+sumlis > K:
            if lis[-1] > A[a]:
                sumlis += A[a] - lis[-1]
                del lis[-1]
                bisect.insort_left(lis, A[a] )
        else :
            bisect.insort_left(lis, A[a] )            
            sumlis += A[a]
    return sumlis, lis


def func():
    N,M,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    sumlis = 0
    lis =[0]
    
    sumlis, lis = appendd(N, A, sumlis,lis,K)
    _,     lis = appendd(M, B, sumlis,lis,K)
    print(len(lis)-1)
if __name__=='__main__':
    func()

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()))
ans=0
i=0
while i<=n:
    if sum(a[:i])>k:
        break
    else:
        time=sum(a[:i])
        if time+sum(b)<=k:
            max_b=m
        else:
            for j in range(m):
                if time+b[j]<=k:
                    time+=b[j]
                else:
                    max_b=j
                    break
        ans_i=max_b+i
        ans=max(ans_i,ans)
        i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
NMK = list(map(int, input().split()))

Asum = []
Bsum = []

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

sum = 0
for i in range(NMK[0]):
    sum += A[i]
    Asum.append(sum)

sum = 0
for i in range(NMK[1]):
    sum += B[i]
    Bsum.append(sum)

cutA = 0
max = NMK[2]
indexA = 0
while(True):
    cutA = Asum[indexA]
    if (max < cutA):
        break
    indexA += 1
    if (indexA == NMK[0]):
        break
if (indexA == 0):
    maxA = 0
else:
    maxA = Asum[indexA - 1]

cutB = 0
indexB = 0
while(True):
    cutB = Bsum[indexB]
    if (max < cutB):
        break
    indexB += 1
    if (indexB == NMK[1]):
        break
if (indexB == 0):
    maxB = 0
else:
    maxB = Bsum[indexB - 1]


def returnAnswer(max, min, Marray, marray):
    answer = 0
    remain = 0
    maxAnswer = 0
    for i in range(max):
        answer = max - i
        index = 0
        remain = NMK[2] - Marray[max - i - 1]
        while (True):
            if (remain >= marray[index]):
                answer += 1
                index += 1
                if (index == min):
                    break
            else:
                break

        if (maxAnswer < answer):
            maxAnswer = answer
    return maxAnswer

if (indexA > indexB):
    if (indexA == 0):
        print(0)
        exit()
    print(returnAnswer(indexA, indexB, Asum, Bsum))
else:
    if (indexB == 0):
        print(0)
        exit()
    print(returnAnswer(indexB, indexA, Bsum, Asum))

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from sys import exit
N, M, K = map(int, input().split())
A = [0] + list(accumulate(list(map(int, input().split()))))
B = [0] + list(accumulate(list(map(int, input().split()))))
n = -1
m = -1
for a in A:
  n += 1
  if a >= K:
    break
for b in B:
  m += 1
  if b >= K:
    break
ans = 0
for i in range(n + 1):
  for j in range(m, -1, -1):
    if A[i] + B[j] <= K:
      if ans <= i + j:
        ans = max(ans, i + j)
        m = j
        break
print(ans)

Submission status: Time Limit Exceeded

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

left=0
right=N+M
while left<=right:
    m=(left+right)//2
    flag=False
    for i in range(m+1):
        if i>N or m-i>M:
            continue
        if K>=sum(A[0:i])+sum(B[0:m-i]):
            flag=True
            break
    if flag:
        left=m+1
    else:
        right=m-1

if flag:
    ans=m
else:
    ans=m-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 = [np.sum(A[0:e+1]) for e in range(len(A)) ]
B_sum = [np.sum(B[0:e+1]) for e in range(len(B)) ]
num=0
for i in range(len(A)):
    for l in range(len(B)):
        if num<i+l:
            Time=A_sum[i]+B_sum[l]
            if Time<=K:
                num=i+l
print(num)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

total = 0

stack = [(0, 0, 0, 0, 0)]
res = 0
flg = 0
while stack:
    av, ai, bv, bi, ans = stack.pop()
    if av + bv > K:
        res = max(res, ans-1)
        flg = 1
        continue
    if ai <= len(A) - 1:
        stack.append((av+A[ai], ai+1, bv, bi, ans+1))
        # print((av+A[ai], ai+1, bv, bi, ans+1))
    if bi <= len(B) - 1:
        stack.append((av, ai, bv+B[bi], bi+1, ans+1))
        # print((av+A[ai], ai+1, bv, bi, ans+1))

if flg:
    print(res)
else:
    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()))
def main(n,m,k,a,b):
    result=0
    count= 0
    List2 = []
    result2=0
    count2 = 0
    List3 = []

    if a > b:
        List = a
        L = b
    else:
        List = b
        L = a
    for e in List:
        result += e
        if result > k:
            result = result - e
            break
        count+=1
        List2.append(e)
    for e in L:
        result2 += e
        if result2 > k:
            result2 = result2 - e
            break
        count2+=1
        List3.append(e)

    if k < a[0] and k < b[0]:
        if count > count2:
            return count
        return count2

    if result > result2:
        List = List2
        L = List3
    else:
        List = List3
        L = List2

    if count2 > count:
        result = result2
        count = count2   

    t = 0
    for i in range(len(List)):
        t += List[i]
        t2 = 0
        flag = 0
        for j in range(len(L)):
            t2 += L[j]
            if t+t2 > k:
                flag += 1
            if t+t2 >= result and t+t2 <= k and count < i+j+2:
                result = t+t2
                count = i+j+2
            if flag != 0:
                break

    return count
print(main(n,m,k,a,b))

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

AL = [0]
for i in range(N):
    AL.append(AL[-1] + A[i])

BL = [0]
for i in range(M):
    BL.append(BL[-1] + B[i])

ans = 0
for i in range(N+1):
    for j in range(M+1):
        if AL[i] + BL[j] <= K:
            ans = max(ans, i+j)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def ABC172_C():
    import sys
    sys.setrecursionlimit(4100000)

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

    if k < a[0] or k < b[0]:
        print(0)
        return

    minamo = []
    def saiki(i,j,sum):
        if sum > k:
            minamo.append(i+j)
            return

        if sum == k:
            minamo.append(i+j+1)
            return

        if i+1 <= n:
            saiki(i+1,j,sum+a[i])
        if j+1 <= m:
            saiki(i,j+1,sum+b[j])

    saiki(0,0,0)
    print(max(minamo)-1)

ABC172_C()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

a_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

iMax = bisect.bisect(a_sum, k)
jMax = bisect.bisect(b_sum, k)

if iMax == 0 and jMax == 0:
  print(0)
  exit()

ans = 0
for i in range(iMax):
  tmp = i + 1 + bisect.bisect(b_sum, k - a_sum[i]) 
  ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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

def solve(K,a,b):
    cum_a = [sum(a[:i]) for i in range(len(a)+1)]
    cum_b = [sum(b[:i]) for i in range(len(b)+1)]
    ans = 0
    for i in range(len(cum_a)):
        for j in range(len(cum_b)):
            if  cum_b[j] > K:
                break
            time = int(cum_a[i]) + int(cum_b[j])
            if time <= K:
                ans = max(ans,i+j)
        if cum_a[i] > K:
            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()))

sumlistA = [0]
sumlistB = [0]
tmpA = 0
tmpB = 0
for i in range(N):
    tmpA += A[i]
    sumlistA.append(tmpA)
for i in range(M):
    tmpB += B[i]
    sumlistB.append(tmpB)
i = 0
while sumlistA[i] <= K: 
        if i == N:
            a = N
            break
        a = i
        i += 1
tmp = 0
#Aは0からa冊
for i in range(a+1):
    k = K-sumlistA[i]
    j = 0
    while sumlistB[j] <= k:
        if j == M:
            b = M
            break
        b = j
        j += 1
    if tmp < i+b:
        tmp = i+b

print(tmp)

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()))
num = min(N,M)
C= [None]*(num*2)
C[::2] = A[:num]
C[1::2] = B[:num]
C.extend(A[num:])
C.extend(B[num:])
if N>M:
  C.extend(A[N-M:N])
elif M>N:
  C.extend(B[M-N:M])
  
if min(C[0],C[1])>K:
  print('0')
  exit()
k=0
ans=0
for i in range(len(C)):
  key=min(C[0],C[1])
  k+=key
  ans+=1
  C.remove(key)
  if k>=K:
    break
  if len(C)==1:
    k+=C[0]
    ans+=1
    break
if k==K:
  print(ans)
else:
  print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
def Tsun(A,B,N,M,K):
  sumA = [0]*N
  sumB = [0]*M
  SAC = 0
  SBC = 0
  for i in range(N):
    if sum(A[:i+1]) <= K:
      sumA[i] = sum(A[:i+1])
      SAC += 1
    else:
      break
  for i in range(M):
    if sum(B[:i+1]) <= K:
      sumB[i] = sum(B[:i+1])
      SBC += 1
    else:
      break
  X = SAC
  for i in range(SAC):
    for j in range(SBC):
      if X <= i+j+2 and sumA[i] + sumB[j] <= K:
        X = i+j+2
  return X
  


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

if sum(A)+sum(B) <= K:
  print(N+M)
  exit()
else:
  print(Tsun(A,B,N,M,K))