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 Go (1.14.1) (judge verdict: Time Limit Exceeded)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() (ret string) {

	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			return ""
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

type Utls struct {
}

type uString string

var utls = &Utls{}

func (u *Utls) readLineU() uString {
	return uString(readLine())
}

func (u *Utls) atoi(str string) int {
	r, _ := strconv.Atoi(str)
	return r
}
func (u *Utls) itoa(n int) string {
	r := strconv.Itoa(n)
	return r
}
func (u uString) atoi() int {
	r, err := strconv.Atoi(string(u))
	if err != nil {
		return -1
	}
	return r
}
func (u uString) atoiArr() []int {
	ret := make([]int, 0)

	l := strings.Split(string(u), " ")

	for i := 0; i < len(l); i++ {
		s := (uString(l[i]))

		ret = append(ret, s.atoi())

	}
	return ret
}
func (u uString) String() string {
	return string(u)
}

var memo map[string]int = make(map[string]int)
var cmp map[string]int= make(map[string]int)

var (
	N,M,K int
	A []int
	B []int
)
type node struct {

	idx1,idx2 int
	cnt int
	

}
func newNode(i1,i2 int) *node{
	ret:= &node{

	}
	ret.idx1 = i1 
	ret.idx2 = i2
	return ret
}
func (n*node)newNode(i1,i2 int) (*node, bool){
	ret:= &node{

	}
	if  i1 >= N || i2>= M{
		cmp[n.String()] = n.idx1+n.idx2 + 2
	
		return nil,false
	}
	ret.idx1 = i1 
	ret.idx2 = i2
	if n.idx1 != i1{
		ret.cnt += n.cnt+ A[i1]
	} else {
		ret.cnt += n.cnt+ B[i2]
	}
	if  ret.cnt > K{

		cmp[n.String()] = n.idx1+n.idx2 + 2
		
		return nil,false
	}
	if ret.cnt == K{
		cmp[ret.String()] = ret.idx1+ret.idx2 +2
	
		return nil,false
	}
	
	
	_,ok:= memo[ret.String()]
	if ok {
		return nil,false
	}
	r2 := newNode(i1+1,i2)
	r3:= newNode(i1,i2+1)
	_,ok2:= memo[r2.String()]
	_,ok3:= memo[r3.String()]
	
		if  ok2 && ok3 {
			return nil,false
		}


	memo[ret.String()] = ret.cnt
	return ret,true
}
func (n*node) String()string {
	ret:= utls.itoa(n.idx1) +" "+ utls.itoa(n.idx2)
	
	return ret

}
func (n*node) next ()[]*node {
	ret:= make([]*node ,0)
	n1,b:=	n.newNode(n.idx1+1, n.idx2)
	if b {
		ret=append(ret,n1)
	}
	n2,b:=	n.newNode(n.idx1, n.idx2+1)
	if b {
		ret=append(ret,n2)
	}
	return ret

}



func main() {
	 l1 := utls.readLineU().atoiArr()
	 A = utls.readLineU().atoiArr()
	 B = utls.readLineU().atoiArr()
  N= l1[0]
  M= l1[1]
	 K= l1[2]
	 nodes:= make([]*node ,0)

	 nodes= append(nodes, newNode(-1,-1))

	 for len(nodes)>0 {
		 n := nodes[0]
		
		nodes= nodes[1:]
	//	for _,n := range ns{
			 nn :=n.next()
			 if len(nn) > 0{
				 nodes= append(nodes, nn...)
				// fmt.Println(len(nn))
			 }
			
			 sort.Slice(nodes ,  func (i,j int) bool{

				return  nodes[i].cnt < nodes[j].cnt 
			 } )
			
		
	//	}
		 
	 }
	 
//	 r :=  (a)+ (a*a) + (a*a*a)

ret:= 0
for  _,v:= range cmp {
	//fmt.Println ( v)
	if ret< v{
		
ret= v
	}
}
	 fmt.Println ( ret)
}