100 atcoder#ABC172C. [ABC172C] Tsundoku

[ABC172C] Tsundoku

Score : 300300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of NN books on it, and Desk B similarly has MM books on it.

It takes us AiA_i minutes to read the ii-th book from the top on Desk A (1iN)(1 \leq i \leq N), and BiB_i minutes to read the ii-th book from the top on Desk B (1iM)(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 KK minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

  • 1N,M2000001 \leq N, M \leq 200000
  • 1K1091 \leq K \leq 10^9
  • 1Ai,Bi1091 \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:

NN MM KK

A1A_1 A2A_2 \ldots ANA_N

B1B_1 B2B_2 \ldots BMB_M

Output

Print an integer representing the maximum number of books that can be read.

3 4 240
60 90 120
80 150 80 150
3

In this case, it takes us 6060, 9090, 120120 minutes to read the 11-st, 22-nd, 33-rd books from the top on Desk A, and 8080, 150150, 8080, 150150 minutes to read the 11-st, 22-nd, 33-rd, 44-th books from the top on Desk B, respectively.

We can read three books in 230230 minutes, as shown below, and this is the maximum number of books we can read within 240240 minutes.

  • Read the topmost book on Desk A in 6060 minutes, and remove that book from the desk.
  • Read the topmost book on Desk B in 8080 minutes, and remove that book from the desk.
  • Read the topmost book on Desk A in 9090 minutes, and remove that book from the desk.
3 4 730
60 90 120
80 150 80 150
7
5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000
0

Watch out for integer overflows.