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:
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        #print(left,middle,right,t,ss)
        if ss[middle]==t:
            #print(middle, ss)
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            #print(na,nb,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab


def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main2(n,m,k,a,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()))

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

for i in range(n+1):
    if A[i] > k:
        break
    for j in range(m, -1, -1):
        time = A[i]+B[j]
        count = i+j
        if time <= k and count > ans:
            ans = count
            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()))

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

for i in range(n+1):
    for j in range(m, -1, -1):
        time = A[i]+B[j]
        count = i+j
        if time <= k and count > ans:
            ans = count
            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()))

a=[0]
b=[0]

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


print(ans)

Submission status: Time Limit Exceeded

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

Atime=[0]
Btime=[0]
j=1
for i in Al:
    tmp = i + Atime[j-1]
    if tmp <= K: 
        Atime.append(i + Atime[j-1] )
        j +=1
    else:
        break

j=1
for i in Bl:
    tmp = i + Btime[j-1]
    if tmp <= K:
        Btime.append(i + Btime[j-1])
        j +=1
    else:
        break

res=0
for i in reversed(range(len(Atime))):

    for j in reversed(range(len(Btime))):
        if Atime[i]+Btime[j]>K:
            pass
        else:
            res=max(res,i+j)
            break

print(res)

Submission status: Time Limit Exceeded

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

Atime=[0]
Btime=[0]
j=1
for i in Al:
    Atime.append(i + Atime[j-1] )
    j +=1

j=1
for i in Bl:
    Btime.append(i + Btime[j-1])
    j +=1

res=0
for i in reversed(range(len(Atime))):
    for j in reversed(range(len(Btime))):
        if Atime[i]+Btime[j]>K:
            pass
        else:
            res=max(res,i+j)
            break

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest
import os
import bisect

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


def cumsum(target: []):
    result = []
    for cnt, a in enumerate(target):
        result.append(a) if cnt == 0 else result.append(result[cnt - 1] + a)
    return result


# 実装を行う関数
def resolve(test_def_name=""):
    n, m, k = map(int, input().split())
    a_s = list(map(int, input().split()))
    b_s = list(map(int, input().split()))

    sum_a_s = [0] + cumsum(a_s)
    sum_b_s = [0] + cumsum(b_s)

    # k=100として AをX冊読んだ場合の残時間のリストを作る。
    # つまり・・0=100 1=80 2=40 という感じ。
    remain_time_s = []
    for sum_a in sum_a_s:
        if sum_a > k:
            break
        remain_time_s.append(k - sum_a)

    ans = 0
    b_limit = len(sum_b_s)
    # 最適解を求める。
    for cnt_a, remain_time in enumerate(remain_time_s):
        # Bは逆から見ていく。
        for cnt_b, b in reversed(list(enumerate(sum_b_s[0:b_limit]))):
            if b > remain_time:
                b_limit -= 1
                continue
            ans = max(ans, cnt_a + cnt_b)
            break

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 4 240
60 90 120
80 150 80 150"""
        output = """3"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3 4 730
60 90 120
80 150 80 150"""
        output = """7"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def test_origin_1(self):
        test_input = """2 3 100
90 10
10 10 10"""
        output = """3"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest
import os
import bisect

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


def cumsum(target: []):
    result = []
    for cnt, a in enumerate(target):
        result.append(a) if cnt == 0 else result.append(result[cnt - 1] + a)
    return result


# 実装を行う関数
def resolve(test_def_name=""):
    n, m, k = map(int, input().split())
    a_s = list(map(int, input().split()))
    b_s = list(map(int, input().split()))

    sum_a_s = [0] + cumsum(a_s)
    sum_b_s = [0] + cumsum(b_s)

    # k=100として AをX冊読んだ場合の残時間のリストを作る。
    # つまり・・0=100 1=80 2=40 という感じ。
    remain_time_s = []
    for sum_a in sum_a_s:
        if sum_a > k:
            break
        remain_time_s.append(k - sum_a)

    ans = 0
    b_limit = len(sum_b_s)
    # 最適解を求める。
    for cnt_a, remain_time in enumerate(remain_time_s):
        # Bは逆から見ていく。
        for cnt_b, b in reversed(list(enumerate(sum_b_s[0:b_limit]))):
            if b > remain_time:
                b_limit -= 1
                continue
            ans = max(ans, cnt_a + cnt_b)

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 4 240
60 90 120
80 150 80 150"""
        output = """3"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3 4 730
60 90 120
80 150 80 150"""
        output = """7"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def test_origin_1(self):
        test_input = """2 3 100
90 10
10 10 10"""
        output = """3"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Time Limit Exceeded

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

    sum_A = [A[0]]
    sum_B = [B[0]]

    for i in range(n - 1):
        sum_A.append(sum_A[i] + A[i + 1])

    for i in range(m - 1):
        sum_B.append(sum_B[i] + B[i + 1])

    R = []

    b = m - 1
    flag = False
    while b > -1:
        if sum_B[b] <= k:
            R.append(b + 1)
            flag = True
            break
        else:
            b -= 1

    if not flag:
        R.append(0)

    for i in range(n):
        a = sum_A[i]

        b = m - 1
        flag = False
        while b > -1:
            if sum_B[b] <= k - a:
                R.append(i + 1 + b + 1)
                flag = True
                break
            else:
                b -= 1

        if not flag:
            if 0 <= k - a:
                R.append(i + 1)
            else:
                R.append(0)

    print(max(R))
    # while k > 0 and (a < n or b < m):
    #     if a == n:
    #         time = B[b]
    #     elif b == m:
    #         time = A[a]
    #     else:
    #         time = min(A[a], B[b])

    #     if k - time < 0:
    #         break

    #     r += 1
    #     k -= time
    #     print(k)

    #     if a == n:
    #         b += 1
    #     elif b == m:
    #         a += 1
    #     else:
    #         if A[a] >= B[b]:
    #             b += 1
    #         else:
    #             a += 1

    # print(r)


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

acA = [0]
for i in range(n):
  acA.append(acA[i] + A[i])

acB = [0]
for i in range(m):
  acB.append(acB[i] + B[i])

ans = 0
for a in range(n+1):
  for b in range(m+1):
    if acA[a] + acB[b] <= k:
      ans = max(ans, a + b)
    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()))
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

for i in range(n+1):
    if a[i] > k:
        break
    for j in range(m+1):
        if a[i] + b[j] <= k:
            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]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])
ans = 0

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

Submission status: Time Limit Exceeded

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

    AS = [0]
    for i in range(n):
        AS.append(AS[i] + A[i])

    BS = [0]
    for i in range(m):
        BS.append(BS[i] + B[i])

    ans = 0
    for ai in range(n, -1, -1):
        if AS[ai] > k:
            continue

        bi = 0
        while AS[ai] + BS[bi] <= k:
            bi += 1
            if bi == m + 1:
                break

        ans = max(ai + bi - 1, ans)

    print(ans)


if __name__ == '__main__':
    main()

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_cumsum = np.array(A).cumsum()
B_cumsum = np.array(B).cumsum()
max_count_A = 0
max_count_B = 0
max_count_AB = 0
max_j = 0

for i in range(N):
  if A_cumsum[i] <= K and max_count_A <= i+1:
    max_count_A = i + 1
  else:
    break
for i in range(M):
  if B_cumsum[i] <= K and max_count_B <= i+1:
    max_count_B = i + 1
  else:
    break
    
for i in range(N):
  if A_cumsum[i] + B_cumsum[max_j] > K:
    for j in range(max_j)[::-1]:
      if A_cumsum[i] + B_cumsum[j] <= K:
        if max_count_AB <= i + j + 2:
          max_count_AB = i + j + 2
          max_j = j
        else:
          break
  else:
    for j in range(max_j, M):
      if A_cumsum[i] + B_cumsum[j] <= K and max_count_AB <= i + j + 2:
        max_count_AB = i + j + 2
        max_j = j
      else:
        break
    
print(max(max_count_A, max_count_B, max_count_AB))

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_cumsum = np.array(A).cumsum()
B_cumsum = np.array(B).cumsum()
max_count_A = 0
max_count_B = 0
max_count_AB = 0
max_j = 0

for i in range(N):
  if A_cumsum[i] <= K and max_count_A <= i+1:
    max_count_A = i + 1
  else:
    break
for i in range(M):
  if B_cumsum[i] <= K and max_count_B <= i+1:
    max_count_B = i + 1
  else:
    break
    
for i in range(N):
  if A_cumsum[i] + B_cumsum[max_j] >= K:
    for j in range(max_j + 1):
      if A_cumsum[i] + B_cumsum[j] <= K:
        if max_count_AB <= i + j + 2:
          max_count_AB = i + j + 2
          max_j = j
      else:
        break
  else:
    for j in range(max_j, M):
      if A_cumsum[i] + B_cumsum[j] <= K and max_count_AB <= i + j + 2:
        max_count_AB = i + j + 2
        max_j = j
      else:
        break
    
print(max(max_count_A, max_count_B, max_count_AB))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ma = 0
    a2 = [0]
    b2 = [0]
    for i in range(len(a)):
        a2.append(a[i]+a2[i])
    for i in range(len(b)):
        b2.append(b[i]+b2[i])
    jj = len(b2)-1
    for i in range(len(a2)):
        if(a2[i]>k):
            break
        j = jj
        while((a2[i]+b2[j])>k):
            j -= 1
        ma = max(ma,i+j)

    print(ma)
            
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ma = 0
    a2 = [0]
    b2 = [0]
    for i in range(len(a)):
        a2.append(a[i]+a2[i])
    for i in range(len(b)):
        b2.append(b[i]+b2[i])
    jj = len(b2)-1
    for i in range(len(a2)):
        cnt = i
        if(a2[i]>k):
            break
        j = jj
        while((a2[i]+b2[j])>k):
            j -= 1
        ma = max(ma,i+j)


    print(ma)
            
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ma = 0
    a2 = [0]
    b2 = [0]
    for i in range(len(a)):
        a2.append(a[i]+a2[i])
    for i in range(len(b)):
        b2.append(b[i]+b2[i])

    for i in range(len(a2)):
        cnt = i
        if(a2[i]>k):
            break
        j = len(b2) -1
        while((a2[i]+b2[j])>k):
            j -= 1
        ma = max(ma,i+j)

    print(ma)
            
main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

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

Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))

iA = bisect.bisect_right(Ac,K)

A_ok = [0]+Ac[:iA]

ans = 0
for i in range(len(A_ok)):
    K2 = K - A_ok[i]
    iB = bisect.bisect_right(Bc,K2)
    Bc = Bc[:iB]
    ans = max(ans,i+iB)

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()))
c = [0]*(N+1)
d = [0]*(N+1)
a_sum = 0
a_num = 0
i = 0
while a_sum + a[i] <= K:
	a_sum += a[i]
	i += 1
	if i >= N:
		break
c[i] = a_sum
a_num = i

for i in range(a_num+1):
	#print(a_sum)
	d[a_num-i] = a_num-i
	#print(c)
	j = 0
	c[a_num-i] = a_sum
	while c[a_num-i] +b[j] <=K:
		c[a_num-i] += b[j]
		j += 1
		if j >= M:
			break
		
	#print(c)
	d[a_num-i] += j
	a_sum -= a[a_num - i-1]
	#print(c)

print(max(d))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ma = 0
    a2 = [0]
    b2 = [0]
    for i in range(len(a)):
        a2.append(a[i]+a2[i])
    for i in range(len(b)):
        b2.append(b[i]+b2[i])

    for i in range(len(a2)):
        #print(i)
        sum = a2[i]
        cnt = i
        if(a2[i]>k):
            break
        for j in range(len(b2)-1,0,-1):
            #print("i:{},j:{},b2[j]:{}".format(i,j,b2[j]))
            if(a2[i]+b2[j]<=k):
                cnt += j
                break
        ma = max(ma,cnt)
        #print("ma:",ma)

    print(ma)
            
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ma = 0
    a2 = [0]
    for i in range(len(a)):
        a2.append(a[i]+a2[i])
        
    for i in range(len(a2)):
        sum = a2[i]
        cnt = i
        if(sum>k):
            break
        for j in range(len(b)):
            sum += b[j]
            if(sum>k):
                break
            else:
                cnt += 1
        ma = max(ma,cnt)

    print(ma)
            
main()

Submission status: Time Limit Exceeded

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

    for i in range(len(a)+1):
        sum = 0
        cnt = i
        for j in range(i):
            sum += a[j]
        if(sum>k):
            break
        for j in range(len(b)):
            sum += b[j]
            if(sum>k):
                break
            else:
                cnt += 1
        ma = max(ma,cnt)
        
    print(ma)
            
main()

Submission status: Time Limit Exceeded

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

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
from numpy import cumsum
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A = cumsum([0] + A)
B = cumsum([0] + B)
n_read = 0
b_tail = B[-1]
for i, a in enumerate(A):
    if a > k:
        break
    j = m
    if b_tail + a > k:
        for j, b in enumerate(B):
            if a + b > k:
                j -= 1
                break
    n_read = max(n_read, i + j)
print(n_read)

Submission status: Time Limit Exceeded

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

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

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

print(count)

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

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

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

print(count)

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_sum=[0,A[0]]
B_sum=[0,B[0]]
for i in range(1,N):
    A_sum.append(A_sum[i]+A[i])
for i in range(1,M):
    B_sum.append(B_sum[i]+B[i])

b_read=0
ans=0
for i in range(N,-1,-1):
    for j in range(b_read,M+1):
        if A_sum[i]+B_sum[j]<=K:
            ans=max(ans,i+j)
            b_read=j
print(ans)

Submission status: Time Limit Exceeded

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

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

count = 0
for i in range(n+1):
  res = k-a[i]
  if res<0:
    break
  for t in reversed(range(m+1)):
    if b[t] <= res:
      break
  if count<(i+t):
    count = i+t

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np 
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# -------------------
n, m, k = map(int, readline().split())
#print(n)
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
a = [0] + a
b = [0] + b
acc = list(itertools.accumulate(a))
bcc = list(itertools.accumulate(b))

#acc = np.array(acc)
#print(acc)
#bcc = np.array(bcc)

ans = 0

for i, val in enumerate(acc):
  if (val > k):
    break
  else:
    rest = k - val
    #print(rest)
    for j, val2 in enumerate(bcc):
      if (val2 <= rest):
        ans = max(ans, j + i)
      else:
        break

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):
    if a > k:
        break
    if B[-1] + a > k:
        for j, b in enumerate(B[::-1]):
            if a + b <= k:
                n_read = max(n_read,  i + len(B) - j - 1)
                break
    else:
        n_read = max(n_read, i + len(B) - 1)
print(n_read)

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 = [a for a in np.cumsum([0] + A) if a <= k]
B = [b for b in np.cumsum([0] + B) if b <= k]
n_read = 0
for i, a in enumerate(A):
    if B[-1] + a > k:
        for j, b in enumerate(B[::-1]):
            if a + b <= k:
                n_read = max(n_read,  i + len(B) - j - 1)
                break
    else:
        n_read = max(n_read, i + len(B) - 1)
print(n_read)

Submission status: Time Limit Exceeded

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

a_apnd = a.append
b_apnd = b.append
for i in range(n):
  a_apnd(a[i]+A[i])
for i in range(m):
  b_apnd(b[i]+B[i])
  
b_apnd((10**9)+1)
count = 0
for i in range(n+1):
  res = k-a[i]
  if res<0:
    break
  for t in range(m+2):
    if b[t] > res:
      t -= 1
      break
  if count<(i+t):
    count = i+t

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np 
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
acc = list(itertools.accumulate(a))
bcc = list(itertools.accumulate(b))

acc = np.array(acc)
#print(acc)
bcc = np.array(bcc)

ans = 0

for i, val in enumerate(acc):
  if (val > k):
    break
  else:
    rest = k - val
    #print(rest)
    for j, val2 in enumerate(bcc):
      if (val2 <= rest):
        ans = max(ans, j + i)
      else:
        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()))
ans = 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 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] <= K:
    #     b_cnt += 1
    b_cnt = M
    while A[a_cnt] + B[b_cnt] > K:
        b_cnt -= 1

    ans = max(ans, a_cnt + b_cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline


def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + [int(i) for i in input().split()])
    B = np.array([0] + [int(i) for i in input().split()])

    A.cumsum(out=A)
    B.cumsum(out=B)
    ans = 0

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

    A_max_i = i
    B_min_i = 0
    for A_i in range(A_max_i, -1, -1):
        for B_i in range(B_min_i, M + 1):
            if A[A_i] + B[B_i] > K:
                break
        else:
            B_i += 1
        ans = max(ans, A_i + B_i - 1)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
a=input().split()
A=[int(c) for c in a]
b=input().split()
B=[int(c) for c in b]
answers=[0]
As=[0]
Bs=[0]
wholeA=0
wholeB=0
for a in range(N):
    wholeA+=A[a]
    As.append(wholeA)
for b in range(M):
    wholeB+=B[b]
    Bs.append(wholeB)

for a in range(N+1):
    for b in range(M+1):
        whole = As[a]
        whole+=Bs[b]
        if whole>K:
            break
        else:
            answers.append(a+b)

print(max(answers))

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()))
ans=0
a=np.cumsum(A)
b=np.cumsum(B)
a=np.insert(a,0,0)
b=np.insert(b,0,0)

for i in range(n+1):
    if a[i] > k:
        break
    t=k-a[i]
    st=m
    for j in reversed(range(st+1)):
        if b[j] <= t:
            st=j
            ans=max(i+j,ans)
            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 = [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(len(sumlist_b)):
        if time + sumlist_b[j] > k:
            current_ans += j
            break
        if j == len(sumlist_b)-1:
            current_ans += j+1
    ans = max(ans, current_ans)
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 = [a for a in np.cumsum([0] + A) if a <= k]
B = [b for b in np.cumsum([0] + B) if b <= k]
n_read = max([i + j for i, a in enumerate(A) for j, b in enumerate(B) if a + b <= k])
print(n_read)