Problem: 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.

Submission attempts (in order):
n, m, k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
ans = [0]
time_sum = 0
i, j = 0, 0

flag_a = True
flag_b = True
flag = False
for i in range(0, len(aa)+1):
    for j in range(0, len(bb)+1):
        asum, bsum = sum(aa[:i]), sum(bb[:j])
        if k < asum or k < bsum:
            flag = True
            break
        else:
            time_total = asum + bsum
            if time_total > k:
                break
            else:
                ans.append(i+j)
    if flag:
        break

print(max(ans))




def main3():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    counter = 0
    ans = []

    if bb[0] >= aa[0]:
        if k < aa[0]:
            return counter
    if bb[0] <= aa[0]:
        if k < bb[0]:
            return counter

    if (sum(aa)+sum(bb)) <= k:
        return n+m

    for i in range(n+1):
        counter = i
        time_sum = sum(aa[:i])
        if time_sum > k:
            break
        elif time_sum == k:
            ans.append(counter)
        else:
            for j in range(1, m):
                time_sum += bb[j]
                if time_sum < k:
                    counter += 1
                elif time_sum == k:
                    counter += 1
                    ans.append(counter)
                else:
                    ans.append(counter)
                    break

    if len(ans) == 1:
        return ans[0]
    else:
        return max(ans)


if __name__ == '__main__':
    ans = main3()
    print(ans)




def main():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    for i in range(n):
        for j in range(1, m):
            time_sum = aa[:i] + bb[:j]
            if time_sum <= k:
                ans.append(i+j)

    if(len(ans) == 1):
        return ans[0]
    else:
        return max(ans)


if __name__ == '__main__':
    ans = main()
    print(ans)


n, m, k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
asum = [0]
bsum = [0]

for i in range(len(aa)):
    asum.append(asum[i]+aa[i])
for i in range(len(bb)):
    bsum.append(bsum[i]+bb[i])

j = len(bsum)-1
ans = 0
for i in range(len(asum)):
    while j >= 0:
        if k >= asum[i] + bsum[j]:
            ans = max(ans, i+j)
            break
        else:
            j -= 1


print(ans)


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted