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 numpy as np

n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
wk=0
aa=[]
bb=[]
for i in a:
    wk+=i
    aa.append(wk)
wk=0
for i in b:
    wk+=i
    bb.append(wk)
ak=np.array(aa)
bk=np.array(bb)

ass = len([1 for i in ak if i<=k]) 
bs = len([1 for i in bk if i<=k]) 

an=0
for i in range(ass-1,-1,-1):
    wk = len([1 for j in bk if j<=(k-ak[i])]) 
    an=max(an,(i+1+wk))
an=max(an,ass)
an=max(an,bs)

print(an)

Submission status: Time Limit Exceeded

Solution code:
# aで読む冊数を固定して、bで何冊読めるか考える
# 累積和にしておく、読めるbの冊数は2分探索で求める
# 計算時間はO(N*logM)


# 2分探索、残りの時間を超えないbの冊数を探索
def binary_search(l, item):
    low = 0
    high = len(l) - 1

    while low <= high:
        mid = (low + high) // 2
        guess = l[mid]
        if guess == item:
            return mid
        elif guess > item:
            high = mid - 1 
        else:
            # elseは guess < item の場合なので、l[mid] == l[-1] は　bが全部読めることを示している
            # l[mid+1] > item ならば l[mid] < item（時間） < l[mid+1] となっている、mid冊は読めるけど、mid+1冊を読む時間はないことを示している
            if l[mid] == l[-1] or l[mid+1] > item:
                return mid
            else:
                low = mid + 1


import itertools

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とbの累積和を計算
# accumulateオブジェクトをリストに変換
a_cumsum = list(itertools.accumulate(a))
b_cumsum = list(itertools.accumulate(b))



# 読める本の冊数を記録しておくリスト
books = []

# aの冊数を0冊~最大までそれぞれ固定して、bの冊数を求める
for minute in a_cumsum:
    minutes = 0
    numbers = 0
    
    minutes += minute
    
    if minutes <= k:

        # aの冊数を計算
        numbers += a_cumsum.index(minute)

        # 残り時間
        rest_minutes = k - minutes
        
        # bの冊数を計算
        b_books = binary_search(b_cumsum, rest_minutes)
        numbers += int(b_books)
    
    books.append(numbers)

print(max(books))

Submission status: Time Limit Exceeded

Solution code:
anum,bnum,acc = map(int,input().split())
alist = list(map(int,input().split()))
blist = list(map(int,input().split()))


ans = 0;a = 0;b = 0
while sum(alist[:a+1]) <= acc:
    if a == anum:
        break
    a += 1

while sum(alist[:a]+blist[:b+1]) <= acc:
    if b == bnum:
        break
    b += 1

if a == 0 and b == 0:
    pass
else:
    ans = a+b
    for i in range(1,a):
        while sum(alist[:a-i]+blist[:b+1]) <= acc:
            if b == bnum:
                break
            b += 1
            if a + b > ans:
                ans = 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()))
 
sa = [0]
sb = [0]
 

for i in range(n):
    sa.append(sa[i]+a[i])
 
for i in range(m):
    sb.append(sb[i]+b[i])
 
ans = 0
 
for i in range(n+1):
    if sa[i] > k:
        break
    mb = 0
    lb = m+1
    for j in range(100):
        mid = (mb+lb)//2
        if sb[mid] + sa[i] <= k:
            mb = mid
        else:
            lb = mid
    ans = max(ans, i + mb)
 
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()))

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

p = n
for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break

_max = 0
keep = 0
for i in range(p, -1, -1):
    for j in range(keep, m + 1):
        if C[i] + D[j] > k:
            keep = j - 1
            break
        elif _max < i + j:
            _max = i + j
print(_max)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N, M, K = map(int, input().split())
A, B = [], []
A = list(map(int, input().split()))
B = list(map(int, input().split()))
max_books = 0
order_list = set(list(itertools.permutations(['A']*N + ['B']*M)))
for order in order_list:
  ia, ib = 0, 0
  time = 0
  books = 0
  for which in order:
    books += 1
    if which == 'A':
      time += A[ia]
      ia += 1
    else:
      time += B[ib]
      ib += 1
    if time == K:
      max_books = max(max_books, books)
      break
    if time > K:
      max_books = max(max_books, books-1)
      break
print(max_books)

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

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break
    p = n

_max = 0
for i in range(p, -1, -1):
    for j in range(m + 1):
        if C[i] + D[j] > k:
            break
        elif _max < i + j:
            _max = i + j
print(_max)

Submission status: Time Limit Exceeded

Solution code:
info = input().split(' ')
n = int(info[0])
m = int(info[1])
K = int(info[2])
a = [int(val) for val in input().split(' ')]
b = [int(val) for val in input().split(' ')]
max_val = sum(a) + sum(b)

def remove_book():
    global n, m, a, b
    only_a = a[0] + a[1] if n > 1 else max_val
    only_b = b[0] + b[1] if m > 1 else max_val
    both_ab = a[0] + b[0] if n > 0 and m > 0 else (a[0] if n > 0 else b[0])
    min_val = only_a
    min_index = 0
    if min_val > only_b:
        min_val = only_b
        min_index = 1
    if min_val > both_ab:
        min_index = 2
    ret = a[0] if n > 0 else b[0]
    if min_index == 0:
        a.remove(a[0])
        n -= 1
    elif min_index == 1:
        ret = b[0]
        b.remove(b[0])
        m -= 1
    else:
        if n == 0:
            ret = b[0]
            b.remove(b[0])
            m -= 1
            return ret
        if m == 0:
            ret = a[0]
            a.remove(a[0])
            n -= 1
            return ret
        if a[0] < b[0]:
            ret = a[0]
            a.remove(a[0])
            n -= 1
        else:
            ret = b[0]
            b.remove(b[0])
            m -= 1
    return ret


count = 0
sum_val = 0
while n > 0 or m > 0:
    sum_val += remove_book()
    if sum_val > K:
        break
    else:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = input().split()
B = input().split()
for n in range(N):
    A[n] = int(A[n])
for m in range(M):
    B[m] = int(B[m])
Ka = 0
ca = 0
for a in range (N):
    if Ka + A[a] <= K:
        Ka += A[a]
        ca += 1
    else:
        break
Kb = 0
cb= 0
for b in range (M):
    if Kb + B[b] <= K:
        Kb += B[b]
        cb += 1
    else:
        break
list_c = [ca, cb]
for i in range (1, ca+1):
    for j in range (1, cb+1):
        c = i + j
        s = 0
        for a in range (i):
            s += A[a]
        for b in range (j):
            s += B[b]
        if s <= K:
            list_c.append(c)
list_c.sort()
print(list_c[len(list_c) - 1])

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = input().split()
B = input().split()
for n in range(N):
    A[n] = int(A[n])
for m in range(M):
    B[m] = int(B[m])
Ka = 0
ca = 0
for a in range (N):
    if Ka + A[a] <= K:
        Ka += A[a]
        ca += 1
    else:
        break
Kb = 0
cb= 0
for b in range (M):
    if Kb + B[b] <= K:
        Kb += B[b]
        cb += 1
    else:
        break
list_c = [ca, cb]
for i in range (1, ca+1):
    for j in range (1, cb+1):
        c = i + j
        s = 0
        for a in range (i):
            s += A[a]
        for b in range (j):
            s += B[b]
        if s <= K:
            list_c.append(c)
list_c.sort()
print(list_c[len(list_c) - 1])

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]*(N+1)
b = [0]*(M+1)

#累積和の計算
for i in range(1, N+1):
    a[i] = a[i-1] + A[i-1]
for j in range(1, M+1):
    b[j] = b[j-1] + B[j-1]

ans = 0
j = M
for i in range(N+1):
    left = 0
    right = j
    if K - a[i] < 0:
        break
    while left < right:
        mid = (left + right)//2
        if b[mid] < K - a[i]:
            left = mid + 1
        elif b[mid] > K - a[i]:
            right = mid - 1
    if b[left] <= K - a[i]:
        j = left
    else:
        j = left - 1
    ans = max(ans, i + j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input = stdin.readline
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]*(N+1)
b = [0]*(M+1)

#累積和の計算
for i in range(1, N+1):
    a[i] = a[i-1] + A[i-1]
for j in range(1, M+1):
    b[j] = b[j-1] + B[j-1]

ans = 0
for i in range(N+1):
    left = 0
    right = M
    if K - a[i] < 0:
        break
    while left < right:
        mid = (left + right)//2
        if b[mid] < K - a[i]:
            left = mid + 1
        elif b[mid] > K - a[i]:
            right = mid - 1
    if b[left] <= K - a[i]:
        j = left
    else:
        j = left - 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]*(N+1)
b = [0]*(M+1)

#累積和の計算
for i in range(1, N+1):
    a[i] = a[i-1] + A[i-1]
for j in range(1, M+1):
    b[j] = b[j-1] + B[j-1]

ans = 0
for i in range(N+1):
    left = 0
    right = M
    if K - a[i] < 0:
        break
    while left < right:
        mid = (left + right)//2
        if b[mid] < K - a[i]:
            left = mid + 1
        elif b[mid] > K - a[i]:
            right = mid - 1
    if b[left] <= K - a[i]:
        j = left
    else:
        j = left - 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_sum, b_sum = [0], [0]
for i in range(n):
  a_sum.append(a_sum[i]+a[i])
for i in range(m):
  b_sum.append(b_sum[i]+b[i])
ans, j = 0, m
for i in range(n+1):
  if a_sum[i] > k:
    break
  while b_sum[j] > k - a_sum[i]:
    j =- 1
  ans = max(ans, i+j)
print(ans)

Submission status: Time Limit Exceeded

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

na = 0
nb = 0
t = 0
b = 0
hq = []
heapq.heappush(hq,(t, na, nb))

while hq:
  t, na, nb = heapq.heappop(hq)
  if (na < N) and (t + A[na] <= K):
    heapq.heappush(hq,(t + A[na], na + 1, nb))
    b = max(b,na + nb + 1)
  if (nb < M) and (t + B[nb] <= K):
    heapq.heappush(hq,(t + B[nb], na, nb + 1))
    b = max(b,na + nb + 1)
#
print(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()))

a = [0]
b = [0]
T = 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(N+1):
  for j in range(M+1):
    T = a[i] + b[j]
    if T <= K:
      ans = max(ans, (i + j))
    else:
      pass


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_sum = [0]
for i in range(n):
  a_sum.append(a[i] + a_sum[i])
b_sum = [0]
for i in range(m):
  b_sum.append(b[i] + b_sum[i])

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

a_sum = [0]
for i in range(n):
  a_sum.append(a[i] + a_sum[i])
b_sum = [0]
for i in range(m):
  b_sum.append(b[i] + b_sum[i])

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

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

    al = [0]
    bl = [0]

    for i in range(N) :
        al.append(al[i] + A_list[i])
    for i in range(M) :
        bl.append(bl[i] + B_list[i])
    #print("al = %s" % al)
    #print("bl = %s" % bl)
    book = 0

    for i in range(N+1) :
        re = M
        if al[i] > K :
            break
        for j in range(M+1) :
            if bl[re] <= K - al[i] :
                break
            else :
                re -= 1

        book = max(book, i + re)

    print(book)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N , M, K = map(int, input().split())
    A_list = [A for A in input().split()]
    B_list = [B for B in input().split()]
    A_list = list(map(int, A_list))
    B_list = list(map(int, B_list))

    al = [0]
    bl = [0]

    for i in range(N) :
        al.append(al[i] + A_list[i])
    for i in range(M) :
        bl.append(bl[i] + B_list[i])
    #print("al = %s" % al)
    #print("bl = %s" % bl)
    re = M
    book = 0

    for i in range(N+1) :
        re = M
        if al[i] > K :
            break
        for j in range(M+1) :
            if bl[re] <= K - al[i] :
                break
            else :
                re -= 1

        book = max(book, i + re)

    print(book)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N , M, K = map(int, input().split())
    A_list = [A for A in input().split()]
    B_list = [B for B in input().split()]
    A_list = list(map(int, A_list))
    B_list = list(map(int, B_list))

    al = [0]
    bl = [0]

    for i in range(N) :
        al.append(al[i] + A_list[i])
    for i in range(M) :
        bl.append(bl[i] + B_list[i])
    #print("al = %s" % al)
    #print("bl = %s" % bl)
    re = M
    ans = 0
    book = []

    for i in range(N+1) :
        re = M
        if al[i] > K :
            continue
        for j in range(M+1) :
            ans = al[i] + bl[re]
            #print(ans)
            re -= 1
            if ans <= K :
                book.append(i + re + 1)
                break

    print(max(book))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def calc_acc(n,k,l):
    acc_l = [0]
    #acc_l[0] = l[0] 
    for i in range(n):
        #acc_l[i] = acc_l[i-1] + l[i]
        acc_l.append(acc_l[i] + l[i])
        #if acc_l[i] > k:
        #     break
    #print(acc_l)
    return acc_l

def read_book(a,b,k):
    #print(a)
    #print(b)
    book = 0
    for i,va in enumerate(a):
        if va > k:
            break
        for j,vb in enumerate(b):
            if va + vb > k:
                #print("break")
                break
            else:
                #print(i,j,book,va,vb,va+vb,k)
                book = max([i + j,book])
    return book

def tsundoku(n,m,k,a,b):
    acc_a = calc_acc(n,k,a)
    acc_b = calc_acc(m,k,b)
    book_a = read_book(acc_a,acc_b,k)
    #book_b = read_book(acc_b,acc_a,k)

    print(book_a)
    
if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    tsundoku(n,m,k,a,b)

Submission status: Time Limit Exceeded

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

def calc_acc(n,k,l):
    acc_l = [0]
    #acc_l[0] = l[0] 
    for i in range(n):
        #acc_l[i] = acc_l[i-1] + l[i]
        acc_l.append(acc_l[i] + l[i])
        #if acc_l[i] > k:
        #     break
    #print(acc_l)
    return acc_l

def read_book(a,b,k):
    #print(a)
    #print(b)
    book = 0
    for i,va in enumerate(a):
        for j,vb in enumerate(b):
            if va + vb > k:
                #print("break")
                break
            else:
                #print(i,j,book,va,vb,va+vb,k)
                book = max([i + j,book])
    return book

def tsundoku(n,m,k,a,b):
    acc_a = calc_acc(n,k,a)
    acc_b = calc_acc(m,k,b)
    book_a = read_book(acc_a,acc_b,k)
    #book_b = read_book(acc_b,acc_a,k)

    print(book_a)
    
if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    tsundoku(n,m,k,a,b)

Submission status: Time Limit Exceeded

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

def calc_acc(n,k,l):
    acc_l = [0]
    #acc_l[0] = l[0] 
    for i in range(n):
        #acc_l[i] = acc_l[i-1] + l[i]
        acc_l.append(acc_l[i] + l[i])
        #if acc_l[i] > k:
        #     break
    #print(acc_l)
    return acc_l

def read_book(a,b,k):
    #print(a)
    #print(b)
    book = 0
    for i,va in enumerate(a):
        for j,vb in enumerate(b):
            if va + vb > k:
                #print("break")
                break
            else:
                #print(i,j,book,va,vb,va+vb,k)
                book = max([i + j,book])
    return book

def tsundoku(n,m,k,a,b):
    acc_a = calc_acc(n,k,a)
    acc_b = calc_acc(m,k,b)
    book_a = read_book(acc_a,acc_b,k)
    book_b = read_book(acc_b,acc_a,k)

    print(max([book_a,book_b]))
    
if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

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

a = [0]
b = [0]
T = 0
ans = []

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

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

for i in range(N+1):
  for j in range(M+1):
    T = a[i] + b[j]
    if T <= K:
      ans.append(i + j)

    else:
      pass


print(max(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int, input().split())
A = [0] + list(accumulate([int(i) for i in input().split()]))
B = [0] + list(accumulate([int(i) for i in input().split()]))
ans = 0
for n in range(N+1):
    a = A[n]
    for m in reversed(range(M+1)):
        if a + B[m] <= K:
            ans = max(ans, n+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()))

a = [0] * (n + 1)
b = [0] * (m + 1)

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

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

ans = 0

for i in range(n + 1):
    if a[i] > k:
        break
    for j in range(m, -1, -1):
        if b[j] <= k - a[i]:
            break
    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()))
A.insert(0,0)
B=list(map(int,input().split()))
B.insert(0,0)
best=0
for a in range(len(A)):
    atime=0
    btime=0
    for i in range(a+1):
        atime=atime+A[i]
        if atime>K:
          break
    else:
        for b in range(len(B)):
            btime=btime+B[b]
            if atime+btime>K:
                book=a+b-1
                if book>best:
                    best=book
                break
        else:
            book=a+b
            if book>best:
                best=book
print(best)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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):
    j = m
    if A[i] > k:
        break
    while A[i]  > k- B[j]:
        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]

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


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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


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

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


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n,m,k = map(int, stdin.readline().rstrip().split())
a = list(map(int, stdin.readline().rstrip().split()))
b = list(map(int, stdin.readline().rstrip().split()))

A = [0]
B = [0]

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


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n,m,k = map(int, stdin.readline().rstrip().split())
a = list(map(int, stdin.readline().rstrip().split()))
b = list(map(int, stdin.readline().rstrip().split()))

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

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

res = ans(A, B)
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=[sum(A[:i+1]) for i in range(n)]
b=[sum(B[:j+1]) for j in range(m)]
i=n-1
j=m-1
x=0
while i>=0:
  if a[i]>k:
    i-=1
    continue
  while j>=0:
    if b[j]<=k-a[i]:
      break
    j-=1
  x=max(x,i+j+2)
  i-=1
print(x)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

"""
dp
time / books_no?
10 0 0
20 0 0
30 0 0
40 0 0
50 0 0
60 1 0
70 1 0
80 0 1




"""



N, M, K = LI()
A = LI()
B = LI()

"""
片方に注目して探索すればよい　

単純にfor,forだと計算量がまずいが，前回の値の部分から探索すればいいのでそこまで
計算量は増えない

"""

a = [0]
b = [0]

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

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

# print(a)
ans = 0
best = mod
for i in range(N + 1):
    if a[i] > K:
        break
    for j in range(min(best + 1, M + 1)):
        if a[i] + b[j] > K:
            break
        ans = max(ans, i + j)
        best = 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()))

calc_A = [0]
calc_B = [0]

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

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

ans = 0

for i in range(N+1):
    j = M
    if calc_A[i] > K:
        break
    while calc_B[j] > K - calc_A[i]:
        if j > 0:
            j -= 1
        else:
            break
    if ans < i + j:
        ans = i + j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
# import bisect

sysin=stdin.readline

N,M,K=[int(x) for x in sysin().rstrip().split()]
a=[int(x) for x in sysin().rstrip().split()]
b=[int(x) for x in sysin().rstrip().split()]

# print(N+M,K)
# print(a)
# print(b)
# N,M=3,4
# K=240
# a=[60, 90, 120]
# b=[80, 150, 80, 150]

ans=0

accum_a=[0]
for i in a:
    _a=accum_a[-1]+i
    if _a>K:
        break
    else:
        accum_a.append(_a)
accum_a.append(K)

accum_b=[0]
for i in b:
    accum_b.append(accum_b[-1]+i)

ans=0

mountA=len(accum_a)-1

for j in range (mountA):
    Kb=K-accum_a[j]
    _accum_b=accum_b
    _accum_b.append(Kb)
    _accum_b.sort()
    bookb=_accum_b.index(Kb)-1
    if _accum_b.count(Kb)==2:
        bookb+=1
    if j+bookb>ans:
        ans=j+bookb

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import numpy as np
def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())), dtype=np.int64)
    B = np.array([0] + list(map(int, input().split())), dtype=np.int64)

    As = np.cumsum(A)
    Bs = np.cumsum(B)

    count = 0
    Amax = bisect_left(As, K+1)
    Bmax = bisect_left(Bs, K+1)

    for i in range(Amax):
        j = M
        while As[i] + Bs[j] > K:
            j -= 1
        count = max(count, (i+j))

    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, heappop, heappush
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    start = (0, (-1,-1))
    hq = [start]
    heapify(hq)
    count = -2
    footprint = set()
    while True:
        if len(hq) == 0:
            break
        cost, (i, j) = heappop(hq)
        if cost > K:
            break
        if (i, j) in footprint:
            continue
        footprint.add((i,j))
        count = max(count, i+j)
        #print(f"cost={cost} i={i} j={j}")

        if i+1 < len(A):
            heappush(hq, (cost + A[i+1], (i+1, j)))
        if j+1 < len(B):
            heappush(hq, (cost + B[j+1], (i, j+1)))

    print(count+2)



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()))
T = 0
c = 0

for i in range(n+1):
    if sum(a[:i]) > k:
        break
    for j in range(m+1):
        t = sum(a[:i]) + sum(b[:j])
        if k < t:
            break
        elif T <= t <= k and c < i+j:
            T = t
            c = i+j
        
print(c)

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
c = 0

for i in range(n+1):
    for j in range(m+1):
        t = sum(a[:i]) + sum(b[:j])
        if T <= t <= k and c < i+j:
            T = t
            c = i+j
        elif k < t:
            break
        
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
# N, M, K = map(int, "3 4 1000000000".split())
# A = deque(map(int, "600000000 90 120".split()))
# B = deque(map(int, "800000000 150 80 150".split()))

result = 0

while (A != deque() and K >= A[0]) or (B != deque() and K >= B[0]):
    a, b = 0, 0
    na, nb = 0, 0
    for i in A:
        if a+i <= K:
            a += i
            na += 1
        else:
            break
    for i in B:
        if b+i <= K:
            b += i
            nb += 1
        else:
            break


    if na > nb:
        result += 1
        na -= 1
        K -= A.popleft()
    elif na < nb:
        result += 1
        nb -= 1
        K -= B.popleft()
    elif na == nb:
        result += 1
        if A[0] <= B[0]:
            na -= 1
            K -= A.popleft()
        else:
            nb -= 1
            K -= B.popleft()
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()))
T = 0
c = 0

for i in range(n+1):
    for j in range(m+1):
        t = sum(a[:i]) + sum(b[:j])
        if T <= t <= k and c < i+j:
            T = t
            c = i+j

print(c)