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.

Solution in Scala (2.13.1) (judge verdict: Runtime Error)

import java.util.Scanner

object Main extends App {

  def readALine(c:Int, ac:List[Int]=Nil):List[Int] =
    if (c > 0) readALine(c-1, sc.nextInt::ac) else ac.reverse
  // def readLines(r:Int, c:Int, ac:List[List[Int]]):List[List[Int]] =
  //   if (r > 0) readLines(r-1, c, readALine(c, Nil)::ac) else ac.reverse
  val sc = new Scanner(System.in)
  val N,M,K = sc.nextInt
  val Ai = readALine(N)
  val Bi = readALine(M)

  def find(array:Array[Int], key:Int):Int = {
    require(!array.isEmpty,"Array must have elements.")
    def iter(l:Int, r:Int):Int = {
      if (l == r) if (array(l) > key) l-1 else l
      val bound = (l+r)>>1
      // println(s"l:$l r:$r bound:$bound key:$key")
      array(bound) match {
        case v if v > key => if (array(bound-1) < key) bound-1 else iter(l-1, bound)
        case v if v < key => if (array(bound+1) > key) bound else iter(bound, r+1)
        case _ => bound
      }
    }

    val maxIdx = array.length-1
    key match {
      case k if k > array(maxIdx) => maxIdx
      case k if k < array(0) => -1
      case _ => iter(0, array.length-1) 
    }
  }

  def solver():Int = {
    def acumulate(list:List[Int], ac:List[Int]):List[Int] =
      if (list.isEmpty) ac
      else if (ac.isEmpty) acumulate(list.tail,list.head::ac)
      else acumulate(list.tail,(ac.head+list.head)::ac)
    val cumAi = acumulate(Ai, Nil).reverse.toArray
    val cumBi = acumulate(Bi, Nil).reverse.toArray
 
    // println(cumAi.mkString(","))
    // println(cumBi.mkString(","))
 
    def iter(i:Int, max:Int):Int = {
      if (i < cumAi.length && cumAi(i) < K) {
        val j = find(cumBi, K - cumAi(i))
        // println(s"i:$i j:$j max:$max")
        if (i+j+2 > max) iter(i+1, i+j+2) else iter(i+1, max)
      } else max
    }
    iter(0, 0)
  }

  println(solver())

}