Scanf.scanf "%d" (fun n ->
    let z = 1000000007 in
    let extgcd x y =
        let rec eg r0 r1 a0 a1 b0 b1 =
            if r1 = 0 then a0 else
                let q1 = r0 / r1 in
                eg r1 (r0 mod r1) a1 (a0 - q1 * a1) b1 (b0 - q1 * b1)
        in
        eg x y 1 0 0 1
    in
    let inv x y = (* 1/x in mod y *)
        let c = extgcd x y in
        if c < 0 then c + y else c
    in

    let ( +@) a b = (a + b) mod z in
    let ( -@) a b = (((a - b) mod z) + z) mod z in
    let ( *@) a b = (a * b) mod z in
    let ( /@) a b = a *@ inv b z in
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let k = Array.fold_left (fun acc a -> acc +@ a * a) 0 a in
    let s = Array.fold_left (+@) 0 a in
    Printf.printf "%d\n" @@ (s *@ s -@ k) /@ 2
)


Scanf.scanf "%d" (fun n ->
    let z = 1000000007 in
    let extgcd x y =
        let rec eg r0 r1 a0 a1 b0 b1 =
            if r1 = 0 then a0 else
                let q1 = r0 / r1 in
                eg r1 (r0 mod r1) a1 (a0 - q1 * a1) b1 (b0 - q1 * b1)
        in
        eg x y 1 0 0 1
    in
    let inv x y = (* 1/x in mod y *)
        let c = extgcd x y in
        if c < 0 then c + y else c
    in

    let ( +@) a b = (a + b) mod z in
    let ( -@) a b = (((a - b) mod z) + z) mod z in
    let ( *@) a b = (a * b) mod z in
    let ( /@) a b = a *@ inv b z in
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let k = Array.fold_left (fun acc a -> acc +@ a * a) 0 a in
    let s = Array.fold_left (+) 0 a in
    Printf.printf "%d\n" @@ (s *@ s -@ k) /@ 2
)

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

Constraints

2 \leq N \leq 2\times 10^5

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let arr = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d in
  let ans = ref 0 in
  for i = 0 to n - 2 do
    for j = i + 1 to n - 1 do
      ans := (!ans + (arr.(i) * arr.(j)) mod ((int_of_float(10. ** 9.))+7))
    done;
  done;
  Printf.printf "%d\n" (!ans mod ((int_of_float(10. ** 9.))+7))


let n = read_int ()
let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun i -> i))
let m = Array.fold_left (fun i j -> max i j) 0 a
let check = Array.make (m + 1) false

let d = Array.make (m + 1) 0

let rec pre i =
  if i <= m then (
    if d.(i) = 0 then (
      let rec sieve j =
        let k =  i * j in
        if k <= m then (
          d.(k) <- i;
          sieve (j + 1)
        ) in
      sieve 1;
    );
    pre (i + 1)
  )

let rec solve i =
  if i = n then "pairwise coprime"
  else (
    let rec f a =
      if a > 1 then (
        let p = d.(a) in
        if check.(p) then true
        else (
          check.(p) <- true;
          let rec g a p =
            let r = a mod p in
            if r = 0 then g (a / p) p
            else a in
          f (g a p)
        )
      ) else false in
    if f a.(i) then "setwise coprime"
    else solve (i + 1)
  )

let rec gcd a b =
  let r = a mod b in
  if r = 0 then b
  else gcd b r

let gcd a = Array.fold_left (fun i j -> gcd i j) a.(0) a

let () =
  let g = gcd a in
  let ans =
    if g > 1 then "not coprime"
    else (
      pre 2;
      solve 0
    ) in
  Printf.printf "%s\n" ans

Score : 500 points

Problem Statement

We have N integers. The i-th number is A_i.

\{A_i\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N.

\{A_i\} is said to be setwise coprime when \{A_i\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1.

Determine if \{A_i\} is pairwise coprime, setwise coprime, or neither.

Here, GCD(\ldots) denotes greatest common divisor.

Constraints

2 \leq N \leq 10^6

1 \leq A_i\leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

If \{A_i\} is pairwise coprime, print pairwise coprime; if \{A_i\} is setwise coprime, print setwise coprime; if neither, print not coprime.

Sample Input 1

3
3 4 5

Sample Output 1

pairwise coprime

GCD(3,4)=GCD(3,5)=GCD(4,5)=1, so they are pairwise coprime.

Sample Input 2

3
6 10 15

Sample Output 2

setwise coprime

Since GCD(6,10)=2, they are not pairwise coprime. However, since GCD(6,10,15)=1, they are setwise coprime.

Sample Input 3

3
6 10 16

Sample Output 3

not coprime

GCD(6,10,16)=2, so they are neither pairwise coprime nor setwise coprime.


let as_num_list s =
  let splitted = String.split_on_char ' ' s in
  List.map int_of_string splitted

let rec gcd a b =
  if b = 0
  then a
  else gcd b (a mod b)

let main () =
  let n = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in
  let as_str = read_line () in
  let a_list = as_num_list as_str in
  let sum = ref 0 in
  let isPairwise = ref true in
  (
    for i = 0 to n - 2 do
      for j = i + 1 to n - 1 do
        if gcd (List.nth a_list i) (List.nth a_list j) <> 1
        then isPairwise := false
      done
    done;
    if !isPairwise
    then Printf.printf "pairwise coprime"
    else
      if List.fold_left gcd (List.hd (List.sort compare a_list)) a_list = 1
      then Printf.printf "setwise coprime"
      else Printf.printf "not coprime";
    print_newline ()
  )

let _ = main ()


Scanf.scanf "%d" (fun n ->
    let div = Array.make 1000000 0 in
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let rec loop i g =
        if i = n then g else
            let g = Scanf.scanf " %d" (fun a ->
                let g = gcd a g in
                let rec loop r i =
                    if r = 1 then () else
                    if i * i > r then div.(r) <- div.(r) + 1 else
                        if r mod i <> 0 then loop r (i + 1) else
                            let () = div.(i) <- div.(i) + 1 in
                            let rec loop2 r =
                                if r mod i = 0 then loop2 (r / i)
                                    else loop r (i + 1)
                            in
                            loop2 r
                in
                loop a 2;
                g
            )
            in
            loop (i + 1) g
    in
    let g = loop 0 0 in
    print_endline @@
        if g <> 1 then "not coprime" else
            if Array.fold_left max 0 div <= 1 then "pairwise coprime" else "setwise coprime"
)


Scanf.scanf "%d" (fun n ->
    let div = Array.make 1000000 0 in
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in
    let rec loop i g =
        if i = n then g else
            let g = Scanf.scanf " %d" (fun a ->
                let g = gcd a g in
                let rec loop r i =
                    if r = 1 then () else
                    if i * i > r then div.(r) <- div.(r) + 1 else
                        if r mod i <> 0 then loop r (i + 1) else
                            let () = div.(i) <- div.(i) + 1 in
                            let rec loop2 r =
                                if r mod i = 0 then loop2 (r / i)
                                    else loop r (i + 1)
                            in
                            loop2 r
                in
                loop a 2;
                g
            )
            in
            loop (i + 1) g
    in
    let g = loop 0 0 in
    print_endline @@
        if g <> 1 then "not coprime" else
            if Array.fold_left max 0 div = 1 then "pairwise coprime" else "setwise coprime"
)


Scanf.scanf "%d %d %d" (fun hh ww m ->
    let module S = Set.Make (struct type t = int let compare = compare end) in
    let module S2 = Set.Make (struct type t = int * int let compare = compare end) in
    let node_h  = Array.make hh S.empty in
    let node_w  = Array.make ww S.empty in
    let count_h = Array.make hh 0 in
    let count_w = Array.make ww 0 in
    let hw = Array.init m (fun _ -> Scanf.scanf " %d %d" (fun h w -> h - 1, w - 1)) in

    Array.iter (fun (h, w) ->
        node_h.(h)  <- S.add w node_h.(h);
        node_w.(w)  <- S.add h node_w.(w);
        count_h.(h) <- count_h.(h) + 1;
        count_w.(w) <- count_w.(w) + 1;
    ) hw;
    let rec loop_h i set_h =
        if i = hh then set_h else
            loop_h (i + 1) (S2.add (count_h.(i), i) set_h)
    in
    let rec loop_w i set_w =
        if i = ww then set_w else
            loop_w (i + 1) (S2.add (count_w.(i), i) set_w)
    in
    let set_h = loop_h 0 S2.empty in
    let set_w = loop_w 0 S2.empty in

    let ((mch, mih) as mxh) = S2.max_elt set_h in
    let ((mcw, miw) as mxw) = S2.max_elt set_w in

    let rec loop_w w acc =
        if w = ww then acc else
            let cur = if S.mem w node_h.(mih) then mch + count_w.(w) - 1 else mch + count_w.(w) in
            loop_w (w + 1) (max acc cur)
    in
    let acc = loop_w 0 0 in
    let rec loop_h h acc =
        if h = hh then acc else
            let cur = if S.mem h node_w.(miw) then mcw + count_h.(h) - 1 else mcw + count_h.(h) in
            loop_h (h + 1) (max acc cur)
    in
    let acc = loop_h 0 acc in
    Printf.printf "%d\n" acc
)

Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4

Sample Output 3

6


module IntPairSet = Set.Make (struct
  type t = int * int
  let compare = compare
end)

let () = Scanf.scanf "%d %d %d\n" @@ fun h w m ->
  let hs = Array.make h 0 in
  let ws = Array.make w 0 in
  let ms = IntPairSet.of_list @@ List.init m @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun h w -> h - 1, w - 1 in
  IntPairSet.iter (fun (h, w) ->
    hs.(h) <- 1 + hs.(h);
    ws.(w) <- 1 + ws.(w)) ms;
  let his = Array.init h Fun.id in
  let wis = Array.init w Fun.id in
  Array.sort (fun i j -> compare hs.(j) hs.(i)) his;
  Array.sort (fun i j -> compare ws.(j) ws.(i)) wis;
  Printf.printf "%d\n" @@
  max (IntPairSet.fold (fun (h, w) -> max @@ hs.(h) + ws.(w) - 1) ms min_int) @@
  List.fold_left max min_int @@
  List.init h @@ fun i ->
    let rec solve j =
      if w <= j then min_int
      else if not (IntPairSet.mem (his.(i), wis.(j)) ms)
      then hs.(his.(i)) + ws.(wis.(j))
      else solve (j + 1) in
    solve 0


module IntPairSet = Set.Make (struct
  type t = int * int
  let compare = compare
end)

let () = Scanf.scanf "%d %d %d\n" @@ fun h w m ->
  let hs = Array.make h 0 in
  let ws = Array.make w 0 in
  let ms = IntPairSet.of_list @@ List.init m @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun h w -> h - 1, w - 1 in
  IntPairSet.iter (fun (h, w) ->
    hs.(h) <- 1 + hs.(h);
    ws.(w) <- 1 + ws.(w)) ms;
  let his = Array.init h Fun.id in
  let wis = Array.init h Fun.id in
  Array.sort (fun i j -> compare hs.(j) hs.(i)) his;
  Array.sort (fun i j -> compare ws.(j) ws.(i)) wis;
  Printf.printf "%d\n" @@
  max (IntPairSet.fold (fun (h, w) -> max @@ hs.(h) + ws.(w) - 1) ms min_int) @@
  List.fold_left max min_int @@
  List.init h @@ fun i ->
    let rec solve j =
      if w <= j then min_int
      else if not (IntPairSet.mem (his.(i), wis.(j)) ms)
      then hs.(his.(i)) + ws.(wis.(j))
      else solve (j + 1) in
    solve 0


let n = read_int () in
let ans = (1000 - n mod 1000) mod 1000 in
Printf.printf "%d\n" ans

Score : 100 points

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

If we use only 1000-yen bills to pay the price, how much change will we receive?

Assume we use the minimum number of bills required.

Constraints

1 \leq N \leq 10000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the amount of change as an integer.

Sample Input 1

1900

Sample Output 1

100

We will use two 1000-yen bills to pay the price and receive 100 yen in change.

Sample Input 2

3000

Sample Output 2

0

We can pay the exact price.


let n = read_int ()
let () =
  Printf.printf "%d\n" @@ (if n mod 1000 <> 0 then 1000 - (n mod 1000) else 0)


let solve n =
  if n mod 1000 == 0 then 0
  else 1000 - n mod 1000

let () =
  let n = read_int () in
  print_int (solve n);
  print_char '\n'


let rec payment price =
  if price <= 1000 then 1000 - price
  else payment (price - 1000)

let ans = payment (read_int ())
let () = print_endline @@ string_of_int ans


let n = read_int ();;

let amount n = let rec iter result m =
                        if m > result then iter (result+1000) m
                        else -(m - result)
                      in iter 1000 n;;

Printf.printf "%d\n" (amount n);;


let () = Scanf.scanf "%d " (fun n ->
  let rec calc n = if n > 0 then calc (n - 1000) else print_int (-n) in
  calc n)


let () = Scanf.scanf "%d " (fun n ->
  let r = ref n in
  while !r > 0 do
    r := !r - 1000
  done;
  print_int (- !r))


let () = Scanf.scanf "%d" @@ fun n ->
  let need = (n + 999) / 1000 in
  let ans = 1000 * need - n in
  Printf.printf "%d\n" ans


open Printf
open Scanf

let solve n =
  let mod1000 x = x mod 1000 in
  let sub1000 x = 1000 - x in
  mod1000 @@ sub1000 @@ mod1000 n

let () =
  scanf "%d " solve |> printf "%d\n"


let () = Scanf.scanf "%d" @@ fun n ->
  Printf.printf "%d\n" @@
  if n mod 1000 = 0
  then 0
  else 1000 - n mod 1000


open Batteries
open Printf

let ($) g f x = g (f x)

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let c2i = int_of_char
let i2c = char_of_int
let f2i = int_of_float
let i2f = float_of_int

let c2s = String.of_char
let s2cl = String.to_list
let cl2s = String.of_list

let puts s = print_endline s
let putyn b = puts (if b then "Yes" else "No")
let puti i = puts @@ i2s i

let rep lb bnd ub f = match bnd with
  | `To -> for i = lb to ub do f i done
  | `Til -> for i = lb to (ub - 1) do f i done

let rep2 lb1 bnd1 ub1 () lb2 bnd2 ub2 f =
  match (bnd1, bnd2) with
  | (`To, `To) -> for i = lb1 to ub1 do for j = lb2 to ub2 do f i j done done
  | (`To, `Til) -> for i = lb1 to ub1 do for j = lb2 to (ub2 - 1) do f i j done done
  | (`Til, `To) -> for i = lb1 to (ub1 - 1) do for j = lb2 to ub2 do f i j done done
  | (`Til, `Til) -> for i = lb1 to (ub1 - 1) do for j = lb2 to (ub2 - 1) do f i j done done

module JunkList = struct
  include List

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let nempty l = not @@ is_empty l

  let hdop l = if nempty l then Some (hd l) else None

  (* let taker = drop *)
  let takewh = take_while

  (* let dropr = take *)
  let dropwh = drop_while

  let split_by = span

  let chunk = ntake

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  let partition_to_map f l =
    foldr (fun x m -> Map.modify_opt (f x) (function
      | None -> Some [x]
      | Some [] -> Some [x]
      | Some vl -> Some (x::vl)) m) Map.empty l;;

  let scanl f z ls =
    let rec loop acl acc = function
      | [] -> rev acl
      | h::t ->
        let r = f acc h in
        loop (r::acl) r t
    in loop [] z ls

  let scanr f z ls =
    let rec loop acl acc = function
      | [] -> acl
      | h::t ->
        let r = f h acc in
        loop (r::acl) r t
    in loop [] z (rev ls)

  (* let scanli = scan_lefti *)
  (* let scanri = scan_righti *)

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = JunkList

module JunkArray = struct
  include Array

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let is_empty r = len r = 0
  let nempty r = len r > 0

  let take n r = left r n
  let taker n r = right r n

  let takeb x r =
    let l = len r in
    let rec loop i =
      if i >= l then r
      else if r.(i) = x then take i r
      else loop (i + 1)
    in loop 0

  (* let takewh = take_while *)

  let drop n r = tail r n
  let dropr n r = take ((len r) - n) r
  (* let dropwh = drop_while *)

  let index_of x r =
    let l = len r in
    let rec loop i =
      if i >= l then None
      else if r.(i) = x then Some i
      else loop (i + 1)
    in loop 0

  (* let split_by = span *)

  (* let chunk = ntake *)

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  (* let partition_to_map f l = *)
  (*   foldr (fun x m -> Map.modify_opt (f x) (function *)
  (*     | None -> Some [x]  *)
  (*     | Some [] -> Some [x]  *)
  (*     | Some vl -> Some (x::vl)) m) Map.empty l;; *)

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let concat2 = append

  let flatmap (f : 'a -> 'b array) a =
    map f a |> enum |> L.of_enum |> concat

  let zipf = map2
  (* let zipfi = map2i *)

end

module A = JunkArray

module JunkString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

  let maptol f s = L.map f (s2cl s)
  let maptoli f s = L.map f (s2cl s)

  let zipf f a b = L.zipf f (s2cl a) (s2cl b)
  let zipfi f a b = L.zipfi f (s2cl a) (s2cl b)

  let mkstr f sep ls =
    L.foldl (fun acc x -> acc ^ sep ^ (f x)) (f @@ L.hd ls) (List.tl ls)

end

module S = JunkString

let spsp s = S.split_on_char ' ' s

module JunkSortedSet = struct
  include Set

  module Make (Ord : Set.OrderedType) = struct
    include Set.Make(Ord)

    let nmem x st = not @@ mem x st

    let foldl f s z = fold (fun x a -> f a x) z s

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let foldl f s z = fold (fun x a -> f a x) z s

  let size = cardinal
end

module Ss = JunkSortedSet

module JunkSortedMap  = struct
  include Map

  module Make (Ord : Map.OrderedType) = struct
    include Map.Make(Ord)

    let nmem x st = not @@ mem x st

    let get = find
    let get_or x d m = find_default d x m

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let get = find
  let get_or x d m = find_default d x m

  let size = cardinal
end

module Ms = JunkSortedMap

let set_power ls =
  let rec loop ls pls =
    match ls with
    | [] -> pls
    | hd::tl -> loop tl (L.flatmap (fun p -> [p; hd::p]) pls)
  in loop ls [[]]

exception ArithmeticException of string

module JunkIntArithmetic = struct

  let rec power x e =
    if e = 0 then x
    else
      let rec loop r x2 e' =
        if e' = 0 then r
        else if e' land 1 = 0 then loop r (x2 * x2) (e' lsr 1)
        else loop (r * x2) (x2 * x2) (e' lsr 1)
      in loop 1 x e

  let rec powers_le_1000 x =
    if x > 32 then [x]
    else
      L.init 9 (fun e -> power x (e + 1))
      |> L.filter (fun y -> y < 1000)

  let primes_le_1000 = [2;3;5;7;11;13;17;19;23;29;31;37;41;
    43;47;53;59;61;67;71;73;79;83;89;97;101;
    103;107;109;113;127;131;137;139;149;151;157;163;167;
    173;179;181;191;193;197;199;211;223;227;229;233;239;
    241;251;257;263;269;271;277;281;283;293;307;311;313;
    317;331;337;347;349;353;359;367;373;379;383;389;397;
    401;409;419;421;431;433;439;443;449;457;461;463;467;
    479;487;491;499;503;509;521;523;541;547;557;563;569;
    571;577;587;593;599;601;607;613;617;619;631;641;643;
    647;653;659;661;673;677;683;691;701;709;719;727;733;
    739;743;751;757;761;769;773;787;797;809;811;821;823;
    827;829;839;853;857;859;863;877;881;883;887;907;911;
    919;929;937;941;947;953;967;971;977;983;991;997]

    let primes_and_powers_le_1000 =
      [2;3;4;5;7;8;9;11;13;16;17;19;23;25;27;29;31;32;37;41;
      43;47;49;53;59;61;64;67;71;73;79;81;83;89;97;
      101;103;107;109;113;121;125;127;128;131;137;139;149;151;
      157;163;167;169;173;179;181;191;193;197;199;211;223;227;
      229;233;239;241;243;251;256;257;263;269;271;277;281;283;
      289;293;307;311;313;317;331;337;343;347;349;353;359;361;
      367;373;379;383;389;397;401;409;419;421;431;433;439;443;
      449;457;461;463;467;479;487;491;499;503;509;512;521;523;
      529;541;547;557;563;569;571;577;587;593;599;601;607;613;
      617;619;625;631;641;643;647;653;659;661;673;677;683;691;
      701;709;719;727;729;733;739;743;751;757;761;769;773;787;
      797;809;811;821;823;827;829;839;841;853;857;859;863;877;
      881;883;887;907;911;919;929;937;941;947;953;961;967;971;
      977;983;991;997]

  let factorize_with ps n =
    let rec loop acc ps e n' =
      if n' <= 1 then ([], n')
      else
        match ps with
        | [] -> (acc, n')
        | p::t ->
          let r = n' mod p in
          if r = 0 then (
            if n' = p then ((p, e + 1) :: acc, 1)
            else loop acc ps (e + 1) (n' / p)
          ) else
            loop (if e = 0 then acc else (p, e) :: acc) t 0 n'
    in
    let (fs, r) = loop [] ps 0 n in
    (L.rev fs, r)

  let factorize_le_million n =
    factorize_with primes_le_1000 n

  let rec to_bits_le n =
    if n = 0 then []
    else (n mod 2) :: to_bits_le (n lsr 1)

  let rec gcd x y =
    match x mod y with
    | 0 -> y
    | z -> gcd y z

  let ext_euclid a b =
    let rec next (r0, s0, t0) (r1, s1, t1) =
      match r1 with
      | 0 -> (r0, s0, t0)
      | r2 ->
        let q = r0 / r1 in
        next (r1, s1, t1) (r0 - q * r1, s0 - q * s1, t0 - q * t1)
    in
    if a < b then
      let (g, y, x) = next (b, 1, 0) (a, 0, 1) in (g, x, y)
    else
      next (a, 1, 0) (b, 0, 1)

  let modinv x m =
    let (g, y, z) = ext_euclid x m in
    match g with
    | 1 -> y mod m
    | _ -> raise @@ ArithmeticException "Not relatively prime"

end

module N = JunkIntArithmetic

let rds () = read_line ()

let rdi () = s2i @@ rds ()

let rdhs () = spsp @@ rds ()
let rdhi () = rdhs () |> L.map s2i

let rdhf () = rdhs () |> L.map s2f

let rdf2 () = match rdhf () with
| a::b::_ -> (a, b)
| _ -> (0., 0.)

let rdf3 () = match rdhf () with
| a::b::c::_ -> (a, b, c)
| _ -> (0., 0., 0.)

let rdf4 () = match rdhf () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0., 0., 0., 0.)

let rds2 () = match rdhs () with
  | a::b::_ -> (a, b)
  | _ -> ("", "")

let rdi2 () = match rdhi () with
  | a::b::_ -> (a, b)
  | _ -> (0, 0)

let rdi3 () = match rdhi () with
| a::b::c::_ -> (a, b, c)
| _ -> (0, 0, 0)

let rdi4 () = match rdhi () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0, 0, 0, 0)

let rdi5 () = match rdhi () with
| a::b::c::d::e::_ -> (a, b, c, d, e)
| _ -> (0, 0, 0, 0, 0)

let rdv n rf =
  let rec loop n =
    if n <= 0 then []
    else (
      let s = rf () in
      s::(loop (n - 1))
    )
  in loop n

let rdvi n = rdv n rdi
let rdvi2 n = rdv n rdi2
let rdvi3 n = rdv n rdi3
let rdvi4 n = rdv n rdi4

let print_list aa =
  printf "[ "; aa |> L.iter (fun a -> printf "%d " a); puts "]"

let print_array aa =
  printf "[ "; aa |> A.iter (fun a -> printf "%d " a); puts "]"

let _ =
  let n = rdi () in
  let r = n mod 1000 in
  puti @@ if r = 0 then 0 else 1000 - r;
  ()


let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n

let () =
  let t = (n + 999) / 1000 in
  Printf.printf "%d\n" @@ -(n - t * 1000)


Scanf.scanf "%d" (fun n ->
    Printf.printf "%d\n" @@ if n mod 1000 = 0 then 0 else 1000 - (n mod 1000)
)


let a = read_int ()
let _ = (1000 - (a mod 1000)) |> Printf.printf "%d\n"


Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    let last = Array.make 26 (-1) in
    let t = Array.make d 0 in
    for i = 0 to d - 1 do
        let rec loop j mi mx =
            if j = 26 then (last.(mi) <- i; t.(i) <- mi) else
                if s.(i).(j) + c.(j) * (i - last.(j)) > mx then loop (j + 1) j (s.(i).(j) + c.(j) * (i - last.(j)))
                                                           else loop (j + 1) mi mx
        in
        loop 0 0 0
    done;
    let basicsat =
        let rec loop i acc =
            if i = d then acc else loop (i + 1) (acc + s.(i).(t.(i)))
        in
        loop 0 0
    in
    let module S = Set.Make (struct type t = int let compare = compare end) in
    let last = Array.make 26 (S.add d @@ S.singleton (-1)) in
    Array.iteri (fun i t -> last.(t) <- S.add i last.(t)) t;

    let f a = a * (a - 1) / 2 in

    let reducesat =
        let calc set =
            let rec loop prev acc = function
            | [] -> acc
            | hd :: tl -> loop hd (acc + f (hd - prev)) tl
            in
            match S.elements set with
            | hd :: tl -> loop hd 0 tl
            | [] -> 0
        in
        let rec loop i acc =
            if i = 26 then acc else loop (i + 1) (acc + c.(i) * calc last.(i))
        in
        loop 0 0
    in

    let trycalc a b =
        let pre = t.(a) in
        if pre = b then 0 else
        let p1, _, p2 = S.split a last.(pre) in
        let p1m = S.max_elt p1 in
        let p2m = S.min_elt p2 in
        let delta = c.(pre)* f (a - p1m) + f (p2m - a) - f (p2m - p1m) - s.(a).(pre) in

        let p1, _, p2 = S.split a last.(b) in
        let p1m = S.max_elt p1 in
        let p2m = S.min_elt p2 in
        let delta = c.(b) * (- f (a - p1m) - f (p2m - a) + f (p2m - p1m)) + s.(a).(b) in
        delta
    in
    let apply a b =
        let pre = t.(a) in
        last.(pre) <- S.remove a last.(pre);
        last.(b)   <- S.add    a last.(b);
        t.(a) <- b
    in
    for i = 1 to 10 do
        let rec loop_a a acc =
            let rec loop_b b ((ma, mb, md) as acc) =
                if b = 26 then acc else
                    let d = trycalc a b in
                    if d > md then loop_b (b + 1) (a, b, d)
                              else loop_b (b + 1) acc
            in
            if a = d then acc else loop_a (a + 1) (loop_b 0 acc)
        in
        let a, b, d = loop_a 0 (0, 0, min_int) in

        apply a b;
    done;
    Array.iter (fun c -> Printf.printf "%d\n" (c + 1)) t
)

Problem Statement

AtCoder currently hosts three types of contests: ABC, ARC, and AGC. As the number of users has grown, in order to meet the needs of more users, AtCoder has decided to increase the number of contests to 26 types, from AAC to AZC. For convenience, we number these 26 types as type 1 through type 26. AtCoder wants to schedule contests for D days so that user satisfaction is as high as possible. For every day, AtCoder will hold exactly one contest, and each contest will end on that day. The satisfaction is calculated as follows.

The satisfaction at the beginning of day 1 is 0. Satisfaction can be negative.

Holding contests increases satisfaction. The amount of increase will vary depending on a variety of factors. Specifically, we know in advance that holding a contest of type i on day d will increase the satisfaction by s_{d,i}.

If a particular type of contest is not held for a while, the satisfaction decreases. Each contest type i has an integer c_i, and at the end of each day d=1,2,...,D, the satisfaction decreases as follows. Let \mathrm{last}(d,i) be the last day before day d (including d) on which a contest of type i was held. If contests of type i have never been held yet, we define \mathrm{last}(d,i)=0. At the end of day d, the satisfaction decreases by \sum _{i=1}^{26}c_i \times (d-\mathrm{last}(d,i)).

Please schedule contests on behalf of AtCoder.
If the satisfaction at the end of day D is S, you will get a score of \max(10^6 + S, 0).
There are 50 test cases, and the score of a submission is the total scores for each test case.
You can make submissions multiple times, and the highest score among your submissions will be your score.

Constraints

D = 365

Each c_i is an integer satisfying 0\leq c_i \leq 100.

Each s_{d,i} is an integer satisfying 0\leq s_{d,i} \leq 20000.

Input

Input is given from Standard Input in the following format:

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}

Output

Let t_d (1\leq t_d \leq 26) be the type of the contest that will be held at day d.
Print D integers t_d to Standard Output in the following format:

t_1
t_2
\vdots
t_D

Any output that does not follow the above format may result in 0 pointsWA for that test case.

Input Generation

Each integer c_i and s_{d,i} is generated independently and uniformly at random from the integers in the range described in the problem statement.

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192

Sample Output 1

1
17
13
14
13

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case. The final satisfaction with this output is 79325, so the score is 1079325.

Input generator, score calculator, and visualizer

Beginner's Guide

If you don't know what to do, proceed to problem B or C.


Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    let last = Array.make 26 (-1) in
    let f a = a * (a - 1) / 2 in
    for i = 0 to d - 1 do
        let rec loop j mi mx =
            if j = 26 then (last.(mi - 1) <- i; Printf.printf "%d\n" mi) else
                let g = s.(i).(j) + c.(j) * (i - last.(j)) in
                if g > mx then loop (j + 1) (j + 1) g else loop (j + 1) mi mx
        in
        loop 0 0 0
    done
)


Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    let last = Array.make 26 (-1) in
    let t = Array.make d 0 in
    for i = 0 to d - 1 do
        let rec loop j mi mx =
            if j = 26 then (last.(mi) <- i; t.(i) <- mi) else
                if s.(i).(j) + c.(j) * (i - last.(j)) > mx then loop (j + 1) j (s.(i).(j) + c.(j) * (i - last.(j)))
                                                           else loop (j + 1) mi mx
        in
        loop 0 0 0
    done;
    let basicsat =
        let rec loop i acc =
            if i = d then acc else loop (i + 1) (acc + s.(i).(t.(i)))
        in
        loop 0 0
    in
    let module S = Set.Make (struct type t = int let compare = compare end) in
    let last = Array.make 26 (S.add d @@ S.singleton (-1)) in
    Array.iteri (fun i t -> last.(t) <- S.add i last.(t)) t;

    let f a = a * (a - 1) / 2 in

    let reducesat =
        let calc set =
            let rec loop prev acc = function
            | [] -> acc
            | hd :: tl -> loop hd (acc + f (hd - prev)) tl
            in
            match S.elements set with
            | hd :: tl -> loop hd 0 tl
            | [] -> 0
        in
        let rec loop i acc =
            if i = 26 then acc else loop (i + 1) (acc + c.(i) * calc last.(i))
        in
        loop 0 0
    in

    let trycalc a b =
        let pre = t.(a) in
        if pre = b then 0 else
        let p1, _, p2 = S.split a last.(pre) in
        let p1m = S.max_elt p1 in
        let p2m = S.min_elt p2 in
        let delta = c.(pre)* f (a - p1m) + f (p2m - a) - f (p2m - p1m) - s.(a).(pre) in

        let p1, _, p2 = S.split a last.(b) in
        let p1m = S.max_elt p1 in
        let p2m = S.min_elt p2 in
        let delta = c.(b) * (- f (a - p1m) - f (p2m - a) + f (p2m - p1m)) + s.(a).(b) in
        delta
    in
    let apply a b =
        let pre = t.(a) in
        last.(pre) <- S.remove a last.(pre);
        last.(b)   <- S.add    a last.(b);
        t.(a) <- b
    in
    for i = 1 to 10 do
        let rec loop_a a acc =
            let rec loop_b b ((ma, mb, md) as acc) =
                if b = 26 then acc else
                    let d = trycalc a b in
                    if d > md then loop_b (b + 1) (a, b, d)
                              else loop_b (b + 1) acc
            in
            if a = d then acc else loop_a (a + 1) (loop_b 0 acc)
        in
        let a, b, d = loop_a 0 (0, 0, min_int) in

        apply a b;
    done;
    Array.iter (Printf.printf "%d\n") t
)


Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    let last = Array.make 26 (-1) in
    let f a = a * (a - 1) / 2 in
    for i = 0 to d - 1 do
        let rec loop j mi mx =
            if j = 26 then (last.(mi - 1) <- i; Printf.printf "%d\n" mi) else
                if s.(i).(j) + c.(j) * (i - last.(j)) > mx then loop (j + 1) (j + 1) s.(i).(j)
                                                           else loop (j + 1) mi mx
        in
        loop 0 0 0
    done
)


Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    for i = 0 to d - 1 do
        let rec loop j mi mx =
            if j = 26 then Printf.printf "%d\n" mi else
                if s.(i).(j) > mx then loop (j + 1) (j + 1) s.(i).(j)
                                  else loop (j + 1) mi mx
        in
        loop 0 0 0
    done
)


let n, m, k = Scanf.scanf "%d %d %d" (fun n m k -> n, m, k)
let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun i-> i))
let b = Array.init m (fun _ -> Scanf.scanf " %d" (fun i-> i))

let rec f i a c n =
  if i < n then (
    c.(i + 1) <- c.(i) + a.(i);
    f (i + 1) a c n
  )

let c = Array.make (n + 1) 0
let d = Array.make (m + 1) 0

let rec g ans i j =
  if i = n + 1 || k < c.(i) then ans
  else
    let rec h i j =
      if k < c.(i) + d.(j) then h i (j - 1)
      else j in
    let j = h i j in
    g (max ans (i + j)) (i + 1) j

let () =
  f 0 a c n;
  f 0 b d m;
  let ans = g 0 0 m in
  Printf.printf "%d\n" ans

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.


let rec upper_bound l r p =
    if r <= 1 + l
    then l
    else let m = (l + r) / 2 in
        if p m
        then upper_bound m r p
        else upper_bound l m p

let () = Scanf.scanf "%d %d %d\n" @@ fun n m k ->
    let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
    let bs = Array.init m @@ fun _ -> Scanf.scanf "%d " @@ fun b -> b in
    let accum values value = match values with
        [] -> [value]
        | values -> (value + List.hd values) :: values in
    let inc = Array.of_list @@ List.rev @@ Array.fold_left accum [0] as_ in
    let inc' =  Array.of_list @@ List.rev @@ Array.fold_left accum [0] bs in
    Printf.printf "%d\n" @@
    Array.fold_left max min_int @@
    Array.init (n + 1) @@ fun i ->
        if k < inc.(i) then 0
        else i + upper_bound 0 (m + 1) (fun j -> inc.(i) + inc'.(j) <= k)


open Batteries
open Printf

let ($) g f x = g (f x)

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let c2i = int_of_char
let i2c = char_of_int
let f2i = int_of_float
let i2f = float_of_int

let c2s = String.of_char
let s2cl = String.to_list
let cl2s = String.of_list

let puts s = print_endline s
let putyn b = puts (if b then "Yes" else "No")
let puti i = puts @@ i2s i

let rep lb bnd ub f = match bnd with
  | `To -> for i = lb to ub do f i done
  | `Til -> for i = lb to (ub - 1) do f i done

let rep2 lb1 bnd1 ub1 () lb2 bnd2 ub2 f =
  match (bnd1, bnd2) with
  | (`To, `To) -> for i = lb1 to ub1 do for j = lb2 to ub2 do f i j done done
  | (`To, `Til) -> for i = lb1 to ub1 do for j = lb2 to (ub2 - 1) do f i j done done
  | (`Til, `To) -> for i = lb1 to (ub1 - 1) do for j = lb2 to ub2 do f i j done done
  | (`Til, `Til) -> for i = lb1 to (ub1 - 1) do for j = lb2 to (ub2 - 1) do f i j done done

module JunkList = struct
  include List

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let nempty l = not @@ is_empty l

  let hdop l = if nempty l then Some (hd l) else None

  (* let taker = drop *)
  let takewh = take_while

  (* let dropr = take *)
  let dropwh = drop_while

  let split_by = span

  let chunk = ntake

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  let partition_to_map f l =
    foldr (fun x m -> Map.modify_opt (f x) (function
      | None -> Some [x]
      | Some [] -> Some [x]
      | Some vl -> Some (x::vl)) m) Map.empty l;;

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = JunkList

module JunkArray = struct
  include Array

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let is_empty r = len r = 0
  let nempty r = len r > 0

  let take n r = left r n
  let taker n r = right r n

  let takeb x r =
    let l = len r in
    let rec loop i =
      if i >= l then r
      else if r.(i) = x then take i r
      else loop (i + 1)
    in loop 0

  (* let takewh = take_while *)

  let drop n r = tail r n
  let dropr n r = take ((len r) - n) r
  (* let dropwh = drop_while *)

  let index_of x r =
    let l = len r in
    let rec loop i =
      if i >= l then None
      else if r.(i) = x then Some i
      else loop (i + 1)
    in loop 0

  (* let split_by = span *)

  (* let chunk = ntake *)

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  (* let partition_to_map f l = *)
  (*   foldr (fun x m -> Map.modify_opt (f x) (function *)
  (*     | None -> Some [x]  *)
  (*     | Some [] -> Some [x]  *)
  (*     | Some vl -> Some (x::vl)) m) Map.empty l;; *)

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let concat2 = append

  let flatmap (f : 'a -> 'b array) a =
    map f a |> enum |> L.of_enum |> concat

  let zipf = map2
  (* let zipfi = map2i *)

end

module A = JunkArray

module JunkString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

  let maptol f s = L.map f (s2cl s)
  let maptoli f s = L.map f (s2cl s)

  let zipf f a b = L.zipf f (s2cl a) (s2cl b)
  let zipfi f a b = L.zipfi f (s2cl a) (s2cl b)

  let mkstr f sep ls =
    L.foldl (fun acc x -> acc ^ sep ^ (f x)) (f @@ L.hd ls) (List.tl ls)

end

module S = JunkString

let spsp s = S.split_on_char ' ' s

module JunkSortedSet = struct
  include Set

  module Make (Ord : Set.OrderedType) = struct
    include Set.Make(Ord)

    let nmem x st = not @@ mem x st

    let foldl f s z = fold (fun x a -> f a x) z s

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let foldl f s z = fold (fun x a -> f a x) z s

  let size = cardinal
end

module Ss = JunkSortedSet

module JunkSortedMap  = struct
  include Map

  module Make (Ord : Map.OrderedType) = struct
    include Map.Make(Ord)

    let nmem x st = not @@ mem x st

    let get = find
    let get_or x d m = find_default d x m

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let get = find
  let get_or x d m = find_default d x m

  let size = cardinal
end

module Ms = JunkSortedMap

let set_power ls =
  let rec loop ls pls =
    match ls with
    | [] -> pls
    | hd::tl -> loop tl (L.flatmap (fun p -> [p; hd::p]) pls)
  in loop ls [[]]

exception ArithmeticException of string

module JunkIntArithmetic = struct

  let rec power x e =
    if e = 0 then x
    else
      let rec loop r x2 e' =
        if e' = 0 then r
        else if e' land 1 = 0 then loop r (x2 * x2) (e' lsr 1)
        else loop (r * x2) (x2 * x2) (e' lsr 1)
      in loop 1 x e

  let rec powers_le_1000 x =
    if x > 32 then [x]
    else
      L.init 9 (fun e -> power x (e + 1))
      |> L.filter (fun y -> y < 1000)

  let primes_le_1000 = [2;3;5;7;11;13;17;19;23;29;31;37;41;
    43;47;53;59;61;67;71;73;79;83;89;97;101;
    103;107;109;113;127;131;137;139;149;151;157;163;167;
    173;179;181;191;193;197;199;211;223;227;229;233;239;
    241;251;257;263;269;271;277;281;283;293;307;311;313;
    317;331;337;347;349;353;359;367;373;379;383;389;397;
    401;409;419;421;431;433;439;443;449;457;461;463;467;
    479;487;491;499;503;509;521;523;541;547;557;563;569;
    571;577;587;593;599;601;607;613;617;619;631;641;643;
    647;653;659;661;673;677;683;691;701;709;719;727;733;
    739;743;751;757;761;769;773;787;797;809;811;821;823;
    827;829;839;853;857;859;863;877;881;883;887;907;911;
    919;929;937;941;947;953;967;971;977;983;991;997]

    let primes_and_powers_le_1000 =
      [2;3;4;5;7;8;9;11;13;16;17;19;23;25;27;29;31;32;37;41;
      43;47;49;53;59;61;64;67;71;73;79;81;83;89;97;
      101;103;107;109;113;121;125;127;128;131;137;139;149;151;
      157;163;167;169;173;179;181;191;193;197;199;211;223;227;
      229;233;239;241;243;251;256;257;263;269;271;277;281;283;
      289;293;307;311;313;317;331;337;343;347;349;353;359;361;
      367;373;379;383;389;397;401;409;419;421;431;433;439;443;
      449;457;461;463;467;479;487;491;499;503;509;512;521;523;
      529;541;547;557;563;569;571;577;587;593;599;601;607;613;
      617;619;625;631;641;643;647;653;659;661;673;677;683;691;
      701;709;719;727;729;733;739;743;751;757;761;769;773;787;
      797;809;811;821;823;827;829;839;841;853;857;859;863;877;
      881;883;887;907;911;919;929;937;941;947;953;961;967;971;
      977;983;991;997]

  let factorize_with ps n =
    let rec loop acc ps e n' =
      if n' <= 1 then ([], n')
      else
        match ps with
        | [] -> (acc, n')
        | p::t ->
          let r = n' mod p in
          if r = 0 then (
            if n' = p then ((p, e + 1) :: acc, 1)
            else loop acc ps (e + 1) (n' / p)
          ) else
            loop (if e = 0 then acc else (p, e) :: acc) t 0 n'
    in
    let (fs, r) = loop [] ps 0 n in
    (L.rev fs, r)

  let factorize_le_million n =
    factorize_with primes_le_1000 n

  let rec to_bits_le n =
    if n = 0 then []
    else (n mod 2) :: to_bits_le (n lsr 1)

  let rec gcd x y =
    match x mod y with
    | 0 -> y
    | z -> gcd y z

  let ext_euclid a b =
    let rec next (r0, s0, t0) (r1, s1, t1) =
      match r1 with
      | 0 -> (r0, s0, t0)
      | r2 ->
        let q = r0 / r1 in
        next (r1, s1, t1) (r0 - q * r1, s0 - q * s1, t0 - q * t1)
    in
    if a < b then
      let (g, y, x) = next (b, 1, 0) (a, 0, 1) in (g, x, y)
    else
      next (a, 1, 0) (b, 0, 1)

  let modinv x m =
    let (g, y, z) = ext_euclid x m in
    match g with
    | 1 -> y mod m
    | _ -> raise @@ ArithmeticException "Not relatively prime"

end

module N = JunkIntArithmetic

module JunkMatrix = struct

  type 'a t = { w : int; h : int; e : 'a array array }

  let make w h e0 =
    { w = w; h = h; e = A.make_matrix w h e0 }

  let from_columns w h e0 elems =
    let matr = make w h e0 in
    elems |> L.iteri (fun j col ->
      col |> L.iteri (fun i x -> matr.e.(i).(j) <- x));
    matr

  let blit (mats, rs, cs, w, h) (matd, rd, cd) =
    rep2 0 `Til w () 0 `Til h (fun i j ->
        matd.e.(rd + i).(cd + j) <- mats.e.(rs + i).(cs + j))

  let expand (w1, h1) (w2, h2) e0 mat =
    let matr = make w2 h2 e0 in
    blit (mat, 0, 0, w1, h1) (matr, 0, 0);
    matr

end

module M = JunkMatrix

module type FieldOps = sig
  type elt
  val zero : elt
  val one : elt
  val neg : elt -> elt
  val rcp : elt -> elt
  val add : elt -> elt -> elt
  val sub : elt -> elt -> elt
  val mul : elt -> elt -> elt
  val div : elt -> elt -> elt
end

module JunkLinearAlgebra (Fop : FieldOps) = struct

  type t = Fop.elt M.t

  let zero w h = M.make w h Fop.zero

  let prod (mat1 : t) (mat2 : t) =
    let matr = zero mat1.w mat2.h in
    rep2 0 `Til mat1.w () 0 `Til mat2.h (fun i j ->
        matr.e.(i).(j) <-
          mat1.e.(i) |> A.foldli (fun
            acc k x -> Fop.add acc (Fop.mul x mat2.e.(k).(i)))
            Fop.zero);
    matr

  exception NotZero of int
  let is_zero (mat : t) =
    try (
      rep 0 `Til mat.w (fun i ->
        if mat.e.(i) |> A.for_all (fun x -> x <> Fop.zero)
          then raise (NotZero 1)
      );
      true
    ) with _ -> false

  let col_addmul c1 (c2, a) (mat : t) =
    rep 0 `Til mat.h (fun i ->
      mat.e.(i).(c1) <- Fop.add mat.e.(i).(c1) (Fop.mul a mat.e.(i).(c2)))

  let col_inner_prod c1 c2 (mat : t) =
    let sum = ref Fop.zero in
    rep 0 `Til mat.h (fun i ->
      sum := Fop.add !sum @@ Fop.mul mat.e.(i).(c1) mat.e.(i).(c2));
    !sum

  (* let orth_extend k0 k1 mat = *)
  (*   let matx = extend (k0, h) ((k0 + k1), h) 0 mat in *)
  (*   rep k0 `Til (k0 + k1) (fun t -> *)
  (*     rep 0 `Til t (fun s ->  *)
  (*       let ip = col_inner_prod s t h matx in *)
  (*       col_addmul t (s, ip) h matx)); *)
  (*   submat (k0, 0) (k0 + k1 - 1, h - 1) matx *)

end

module type OrderedData = sig
  type t
  val compare : t -> t -> int
  val default : t
end

module JunkGraph = struct

  type 'a v_t = int * 'a
  type 'a t = { vs : 'a v_t array; nb : int Ss.t array }

  let empty nv vd =
    let vs0 = Array.make nv (0, vd) in
    for i = 0 to (nv - 1) do vs0.(i) <- (i, vd) done;
    {
      vs = vs0;
      nb = A.make nv Ss.empty
    }

  let add_edge (e : int * int) vd g =
    let (v1, v2) = e in
    g.vs.(v1 - 1) <- (v1, vd);
    g.vs.(v2 - 1) <- (v2, vd);
    g.nb.(v1 - 1) <- g.nb.(v1 - 1) |> Ss.add v2;
    g

  let add_uedge e vd g =
    add_edge e vd (add_edge (swap e) vd g)

  let from_edges nv vd es =
    es |> L.foldl (fun g e -> add_edge e vd g) (empty nv vd)

  let from_uedges nv vd es =
    es |> L.foldl (fun g e -> add_uedge e vd g) (empty nv vd)

end

module G = JunkGraph

module JunkGraphAlgorithms (Vdt : OrderedData) = struct

  type t = Vdt.t G.t

  module V = struct
    type t = int * Vdt.t
    let compare (vi1, vd1) (vi2, vd2) =
      let c1 = Vdt.compare vd1 vd2 in
      if c1 = 0 then compare vi1 vi2
      else c1

    let default i = (i, Vdt.default)
    let index (v : t) = fst v
  end

  module Vset = Ss.Make(V)

  let print_vertices label vs =
    printf "%s: " label;
    vs |> Vset.iter (fun (vi, _) -> printf "%d " vi);
    puts ""

  let nbiter (upd : V.t -> V.t -> t -> t * bool)
    (ui : int) (fr : Vset.t) (cls : int Ss.t) (g : t) =
    let nb = g.nb.(ui - 1) in
    let result = nb |> Ss.foldl (fun (ga, nxfr, nxcls) vi ->
    (* puts "nxcls: "; *)
      (* Ss.iter (fun x -> printf "%d " x) nxcls; puts ""; *)
      if ui = vi || Ss.mem vi nxcls then
        (ga, nxfr, nxcls)
      else
        let ue = g.vs.(ui - 1) and ve = g.vs.(vi - 1) in
        let (gu, close) = upd ue ve ga in
        (gu,
          nxfr |> Vset.add ve,
          if close then nxcls |> Ss.add vi else nxcls)
    ) (g, fr, cls)
    in (* puts "";  *)result

  let traverse (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (ve, fr') = Vset.pop_min fr in
      let (vi, _) = ve in
      let (gu, nxfr, nxcls) = nbiter upd vi fr' cls g' in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let frontier (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (gu, nxfr, nxcls) =
        fr |> Vset.foldl (fun (ga, nxfra, nxclsa) ve ->
          let (vi, _) = ve in
          nbiter upd vi nxfra nxclsa ga
        ) (g', Vset.empty, cls)
      in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let bfs (upd : V.t -> V.t -> t -> t) =
    frontier (fun fr cls g' -> (upd fr cls g', true))

end

let rds () = read_line ()

let rdi () = s2i @@ rds ()

let rdhs () = spsp @@ rds ()
let rdhi () = rdhs () |> L.map s2i

let rdhf () = rdhs () |> L.map s2f

let rdf2 () = match rdhf () with
| a::b::_ -> (a, b)
| _ -> (0., 0.)

let rdf3 () = match rdhf () with
| a::b::c::_ -> (a, b, c)
| _ -> (0., 0., 0.)

let rdf4 () = match rdhf () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0., 0., 0., 0.)

let rds2 () = match rdhs () with
  | a::b::_ -> (a, b)
  | _ -> ("", "")

let rdi2 () = match rdhi () with
  | a::b::_ -> (a, b)
  | _ -> (0, 0)

let rdi3 () = match rdhi () with
| a::b::c::_ -> (a, b, c)
| _ -> (0, 0, 0)

let rdi4 () = match rdhi () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0, 0, 0, 0)

let rdi5 () = match rdhi () with
| a::b::c::d::e::_ -> (a, b, c, d, e)
| _ -> (0, 0, 0, 0, 0)

let rdv n rf =
  let rec loop n =
    if n <= 0 then []
    else (
      let s = rf () in
      s::(loop (n - 1))
    )
  in loop n

let rdvi n = rdv n rdi
let rdvi2 n = rdv n rdi2
let rdvi3 n = rdv n rdi3
let rdvi4 n = rdv n rdi4

module Galg = JunkGraphAlgorithms (
  struct
    type t = int
    let compare = compare
    let default = 0
  end)

let print_list aa =
  printf "[ "; aa |> L.iter (fun a -> printf "%d " a); puts "]"

let print_array aa =
  printf "[ "; aa |> A.iter (fun a -> printf "%d " a); puts "]"

let rec bsearch_max_int f lb ub =
  if lb >= ub then (if f lb then lb else (lb - 1))
  else (
    let m = (lb + ub) / 2 in
    if f m then bsearch_max_int f (m + 1) ub
    else bsearch_max_int f lb m
  )

let partial_sum n ls =
  let rec loop s k = function
    | [] -> s
    | h::t ->
      if k >= n then s
      else loop (s + h) (k + 1) t
  in
  loop 0 0 ls

let scanl f z ls =
  let rec loop acl acc = function
    | [] -> L.rev acl
    | h::t ->
      let r = f acc h in
      loop (r::acl) r t
  in loop [] z ls

let _ =
  let (n, m, k) = rdi3 () in
  let aas = rdhi () in
  let bas = rdhi () in
  let aas_sums = A.of_list @@ 0::(scanl (+) 0 aas) in
  let bas_sums = A.of_list @@ 0::(scanl (+) 0 bas) in
  (* print_array aas_sums; *)
  (* print_array bas_sums; *)

  let can_read nbk =
    let rec test na =
      let nb = nbk - na in
      (* printf "%d %d\n" na nb; *)
      if na < 0 || na > n || nb < 0 || nb > m then false
      else (
        (* print_list abk; print_list bbk; puts "----"; *)
        if aas_sums.(na) + bas_sums.(nb) <= k then true
        else test (na + 1)
      )
    in

    (* puti nbk; *)
    test (max 0 (nbk - m))
  in

  puti @@ bsearch_max_int can_read 0 (n + m)


open Batteries
open Printf

let ($) g f x = g (f x)

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let c2i = int_of_char
let i2c = char_of_int
let f2i = int_of_float
let i2f = float_of_int

let c2s = String.of_char
let s2cl = String.to_list
let cl2s = String.of_list

let puts s = print_endline s
let putyn b = puts (if b then "Yes" else "No")
let puti i = puts @@ i2s i

let rep lb bnd ub f = match bnd with
  | `To -> for i = lb to ub do f i done
  | `Til -> for i = lb to (ub - 1) do f i done

let rep2 lb1 bnd1 ub1 () lb2 bnd2 ub2 f =
  match (bnd1, bnd2) with
  | (`To, `To) -> for i = lb1 to ub1 do for j = lb2 to ub2 do f i j done done
  | (`To, `Til) -> for i = lb1 to ub1 do for j = lb2 to (ub2 - 1) do f i j done done
  | (`Til, `To) -> for i = lb1 to (ub1 - 1) do for j = lb2 to ub2 do f i j done done
  | (`Til, `Til) -> for i = lb1 to (ub1 - 1) do for j = lb2 to (ub2 - 1) do f i j done done

module JunkList = struct
  include List

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let nempty l = not @@ is_empty l

  let hdop l = if nempty l then Some (hd l) else None

  (* let taker = drop *)
  let takewh = take_while

  (* let dropr = take *)
  let dropwh = drop_while

  let split_by = span

  let chunk = ntake

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  let partition_to_map f l =
    foldr (fun x m -> Map.modify_opt (f x) (function
      | None -> Some [x]
      | Some [] -> Some [x]
      | Some vl -> Some (x::vl)) m) Map.empty l;;

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = JunkList

module JunkArray = struct
  include Array

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let is_empty r = len r = 0
  let nempty r = len r > 0

  let take n r = left r n
  let taker n r = right r n

  let takeb x r =
    let l = len r in
    let rec loop i =
      if i >= l then r
      else if r.(i) = x then take i r
      else loop (i + 1)
    in loop 0

  (* let takewh = take_while *)

  let drop n r = tail r n
  let dropr n r = take ((len r) - n) r
  (* let dropwh = drop_while *)

  let index_of x r =
    let l = len r in
    let rec loop i =
      if i >= l then None
      else if r.(i) = x then Some i
      else loop (i + 1)
    in loop 0

  (* let split_by = span *)

  (* let chunk = ntake *)

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  (* let partition_to_map f l = *)
  (*   foldr (fun x m -> Map.modify_opt (f x) (function *)
  (*     | None -> Some [x]  *)
  (*     | Some [] -> Some [x]  *)
  (*     | Some vl -> Some (x::vl)) m) Map.empty l;; *)

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let concat2 = append

  let flatmap (f : 'a -> 'b array) a =
    map f a |> enum |> L.of_enum |> concat

  let zipf = map2
  (* let zipfi = map2i *)

end

module A = JunkArray

module JunkString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

  let maptol f s = L.map f (s2cl s)
  let maptoli f s = L.map f (s2cl s)

  let zipf f a b = L.zipf f (s2cl a) (s2cl b)
  let zipfi f a b = L.zipfi f (s2cl a) (s2cl b)

  let mkstr f sep ls =
    L.foldl (fun acc x -> acc ^ sep ^ (f x)) (f @@ L.hd ls) (List.tl ls)

end

module S = JunkString

let spsp s = S.split_on_char ' ' s

module JunkSortedSet = struct
  include Set

  module Make (Ord : Set.OrderedType) = struct
    include Set.Make(Ord)

    let nmem x st = not @@ mem x st

    let foldl f s z = fold (fun x a -> f a x) z s

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let foldl f s z = fold (fun x a -> f a x) z s

  let size = cardinal
end

module Ss = JunkSortedSet

module JunkSortedMap  = struct
  include Map

  module Make (Ord : Map.OrderedType) = struct
    include Map.Make(Ord)

    let nmem x st = not @@ mem x st

    let get = find
    let get_or x d m = find_default d x m

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let get = find
  let get_or x d m = find_default d x m

  let size = cardinal
end

module Ms = JunkSortedMap

let set_power ls =
  let rec loop ls pls =
    match ls with
    | [] -> pls
    | hd::tl -> loop tl (L.flatmap (fun p -> [p; hd::p]) pls)
  in loop ls [[]]

exception ArithmeticException of string

module JunkIntArithmetic = struct

  let rec power x e =
    if e = 0 then x
    else
      let rec loop r x2 e' =
        if e' = 0 then r
        else if e' land 1 = 0 then loop r (x2 * x2) (e' lsr 1)
        else loop (r * x2) (x2 * x2) (e' lsr 1)
      in loop 1 x e

  let rec powers_le_1000 x =
    if x > 32 then [x]
    else
      L.init 9 (fun e -> power x (e + 1))
      |> L.filter (fun y -> y < 1000)

  let primes_le_1000 = [2;3;5;7;11;13;17;19;23;29;31;37;41;
    43;47;53;59;61;67;71;73;79;83;89;97;101;
    103;107;109;113;127;131;137;139;149;151;157;163;167;
    173;179;181;191;193;197;199;211;223;227;229;233;239;
    241;251;257;263;269;271;277;281;283;293;307;311;313;
    317;331;337;347;349;353;359;367;373;379;383;389;397;
    401;409;419;421;431;433;439;443;449;457;461;463;467;
    479;487;491;499;503;509;521;523;541;547;557;563;569;
    571;577;587;593;599;601;607;613;617;619;631;641;643;
    647;653;659;661;673;677;683;691;701;709;719;727;733;
    739;743;751;757;761;769;773;787;797;809;811;821;823;
    827;829;839;853;857;859;863;877;881;883;887;907;911;
    919;929;937;941;947;953;967;971;977;983;991;997]

    let primes_and_powers_le_1000 =
      [2;3;4;5;7;8;9;11;13;16;17;19;23;25;27;29;31;32;37;41;
      43;47;49;53;59;61;64;67;71;73;79;81;83;89;97;
      101;103;107;109;113;121;125;127;128;131;137;139;149;151;
      157;163;167;169;173;179;181;191;193;197;199;211;223;227;
      229;233;239;241;243;251;256;257;263;269;271;277;281;283;
      289;293;307;311;313;317;331;337;343;347;349;353;359;361;
      367;373;379;383;389;397;401;409;419;421;431;433;439;443;
      449;457;461;463;467;479;487;491;499;503;509;512;521;523;
      529;541;547;557;563;569;571;577;587;593;599;601;607;613;
      617;619;625;631;641;643;647;653;659;661;673;677;683;691;
      701;709;719;727;729;733;739;743;751;757;761;769;773;787;
      797;809;811;821;823;827;829;839;841;853;857;859;863;877;
      881;883;887;907;911;919;929;937;941;947;953;961;967;971;
      977;983;991;997]

  let factorize_with ps n =
    let rec loop acc ps e n' =
      if n' <= 1 then ([], n')
      else
        match ps with
        | [] -> (acc, n')
        | p::t ->
          let r = n' mod p in
          if r = 0 then (
            if n' = p then ((p, e + 1) :: acc, 1)
            else loop acc ps (e + 1) (n' / p)
          ) else
            loop (if e = 0 then acc else (p, e) :: acc) t 0 n'
    in
    let (fs, r) = loop [] ps 0 n in
    (L.rev fs, r)

  let factorize_le_million n =
    factorize_with primes_le_1000 n

  let rec to_bits_le n =
    if n = 0 then []
    else (n mod 2) :: to_bits_le (n lsr 1)

  let rec gcd x y =
    match x mod y with
    | 0 -> y
    | z -> gcd y z

  let ext_euclid a b =
    let rec next (r0, s0, t0) (r1, s1, t1) =
      match r1 with
      | 0 -> (r0, s0, t0)
      | r2 ->
        let q = r0 / r1 in
        next (r1, s1, t1) (r0 - q * r1, s0 - q * s1, t0 - q * t1)
    in
    if a < b then
      let (g, y, x) = next (b, 1, 0) (a, 0, 1) in (g, x, y)
    else
      next (a, 1, 0) (b, 0, 1)

  let modinv x m =
    let (g, y, z) = ext_euclid x m in
    match g with
    | 1 -> y mod m
    | _ -> raise @@ ArithmeticException "Not relatively prime"

end

module N = JunkIntArithmetic

module JunkMatrix = struct

  type 'a t = { w : int; h : int; e : 'a array array }

  let make w h e0 =
    { w = w; h = h; e = A.make_matrix w h e0 }

  let from_columns w h e0 elems =
    let matr = make w h e0 in
    elems |> L.iteri (fun j col ->
      col |> L.iteri (fun i x -> matr.e.(i).(j) <- x));
    matr

  let blit (mats, rs, cs, w, h) (matd, rd, cd) =
    rep2 0 `Til w () 0 `Til h (fun i j ->
        matd.e.(rd + i).(cd + j) <- mats.e.(rs + i).(cs + j))

  let expand (w1, h1) (w2, h2) e0 mat =
    let matr = make w2 h2 e0 in
    blit (mat, 0, 0, w1, h1) (matr, 0, 0);
    matr

end

module M = JunkMatrix

module type FieldOps = sig
  type elt
  val zero : elt
  val one : elt
  val neg : elt -> elt
  val rcp : elt -> elt
  val add : elt -> elt -> elt
  val sub : elt -> elt -> elt
  val mul : elt -> elt -> elt
  val div : elt -> elt -> elt
end

module JunkLinearAlgebra (Fop : FieldOps) = struct

  type t = Fop.elt M.t

  let zero w h = M.make w h Fop.zero

  let prod (mat1 : t) (mat2 : t) =
    let matr = zero mat1.w mat2.h in
    rep2 0 `Til mat1.w () 0 `Til mat2.h (fun i j ->
        matr.e.(i).(j) <-
          mat1.e.(i) |> A.foldli (fun
            acc k x -> Fop.add acc (Fop.mul x mat2.e.(k).(i)))
            Fop.zero);
    matr

  exception NotZero of int
  let is_zero (mat : t) =
    try (
      rep 0 `Til mat.w (fun i ->
        if mat.e.(i) |> A.for_all (fun x -> x <> Fop.zero)
          then raise (NotZero 1)
      );
      true
    ) with _ -> false

  let col_addmul c1 (c2, a) (mat : t) =
    rep 0 `Til mat.h (fun i ->
      mat.e.(i).(c1) <- Fop.add mat.e.(i).(c1) (Fop.mul a mat.e.(i).(c2)))

  let col_inner_prod c1 c2 (mat : t) =
    let sum = ref Fop.zero in
    rep 0 `Til mat.h (fun i ->
      sum := Fop.add !sum @@ Fop.mul mat.e.(i).(c1) mat.e.(i).(c2));
    !sum

  (* let orth_extend k0 k1 mat = *)
  (*   let matx = extend (k0, h) ((k0 + k1), h) 0 mat in *)
  (*   rep k0 `Til (k0 + k1) (fun t -> *)
  (*     rep 0 `Til t (fun s ->  *)
  (*       let ip = col_inner_prod s t h matx in *)
  (*       col_addmul t (s, ip) h matx)); *)
  (*   submat (k0, 0) (k0 + k1 - 1, h - 1) matx *)

end

module type OrderedData = sig
  type t
  val compare : t -> t -> int
  val default : t
end

module JunkGraph = struct

  type 'a v_t = int * 'a
  type 'a t = { vs : 'a v_t array; nb : int Ss.t array }

  let empty nv vd =
    let vs0 = Array.make nv (0, vd) in
    for i = 0 to (nv - 1) do vs0.(i) <- (i, vd) done;
    {
      vs = vs0;
      nb = A.make nv Ss.empty
    }

  let add_edge (e : int * int) vd g =
    let (v1, v2) = e in
    g.vs.(v1 - 1) <- (v1, vd);
    g.vs.(v2 - 1) <- (v2, vd);
    g.nb.(v1 - 1) <- g.nb.(v1 - 1) |> Ss.add v2;
    g

  let add_uedge e vd g =
    add_edge e vd (add_edge (swap e) vd g)

  let from_edges nv vd es =
    es |> L.foldl (fun g e -> add_edge e vd g) (empty nv vd)

  let from_uedges nv vd es =
    es |> L.foldl (fun g e -> add_uedge e vd g) (empty nv vd)

end

module G = JunkGraph

module JunkGraphAlgorithms (Vdt : OrderedData) = struct

  type t = Vdt.t G.t

  module V = struct
    type t = int * Vdt.t
    let compare (vi1, vd1) (vi2, vd2) =
      let c1 = Vdt.compare vd1 vd2 in
      if c1 = 0 then compare vi1 vi2
      else c1

    let default i = (i, Vdt.default)
    let index (v : t) = fst v
  end

  module Vset = Ss.Make(V)

  let print_vertices label vs =
    printf "%s: " label;
    vs |> Vset.iter (fun (vi, _) -> printf "%d " vi);
    puts ""

  let nbiter (upd : V.t -> V.t -> t -> t * bool)
    (ui : int) (fr : Vset.t) (cls : int Ss.t) (g : t) =
    let nb = g.nb.(ui - 1) in
    let result = nb |> Ss.foldl (fun (ga, nxfr, nxcls) vi ->
    (* puts "nxcls: "; *)
      (* Ss.iter (fun x -> printf "%d " x) nxcls; puts ""; *)
      if ui = vi || Ss.mem vi nxcls then
        (ga, nxfr, nxcls)
      else
        let ue = g.vs.(ui - 1) and ve = g.vs.(vi - 1) in
        let (gu, close) = upd ue ve ga in
        (gu,
          nxfr |> Vset.add ve,
          if close then nxcls |> Ss.add vi else nxcls)
    ) (g, fr, cls)
    in (* puts "";  *)result

  let traverse (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (ve, fr') = Vset.pop_min fr in
      let (vi, _) = ve in
      let (gu, nxfr, nxcls) = nbiter upd vi fr' cls g' in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let frontier (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (gu, nxfr, nxcls) =
        fr |> Vset.foldl (fun (ga, nxfra, nxclsa) ve ->
          let (vi, _) = ve in
          nbiter upd vi nxfra nxclsa ga
        ) (g', Vset.empty, cls)
      in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let bfs (upd : V.t -> V.t -> t -> t) =
    frontier (fun fr cls g' -> (upd fr cls g', true))

end

let rds () = read_line ()

let rdi () = s2i @@ rds ()

let rdhs () = spsp @@ rds ()
let rdhi () = rdhs () |> L.map s2i

let rdhf () = rdhs () |> L.map s2f

let rdf2 () = match rdhf () with
| a::b::_ -> (a, b)
| _ -> (0., 0.)

let rdf3 () = match rdhf () with
| a::b::c::_ -> (a, b, c)
| _ -> (0., 0., 0.)

let rdf4 () = match rdhf () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0., 0., 0., 0.)

let rds2 () = match rdhs () with
  | a::b::_ -> (a, b)
  | _ -> ("", "")

let rdi2 () = match rdhi () with
  | a::b::_ -> (a, b)
  | _ -> (0, 0)

let rdi3 () = match rdhi () with
| a::b::c::_ -> (a, b, c)
| _ -> (0, 0, 0)

let rdi4 () = match rdhi () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0, 0, 0, 0)

let rdi5 () = match rdhi () with
| a::b::c::d::e::_ -> (a, b, c, d, e)
| _ -> (0, 0, 0, 0, 0)

let rdv n rf =
  let rec loop n =
    if n <= 0 then []
    else (
      let s = rf () in
      s::(loop (n - 1))
    )
  in loop n

let rdvi n = rdv n rdi
let rdvi2 n = rdv n rdi2
let rdvi3 n = rdv n rdi3
let rdvi4 n = rdv n rdi4

module Galg = JunkGraphAlgorithms (
  struct
    type t = int
    let compare = compare
    let default = 0
  end)

let print_list aa =
  printf "[ "; aa |> L.iter (fun a -> printf "%d " a); puts "]"

let print_array aa =
  printf "[ "; aa |> A.iter (fun a -> printf "%d " a); puts "]"

let rec bsearch_max_int f lb ub =
  if lb >= ub then (if f lb then lb else (lb - 1))
  else (
    let m = (lb + ub) / 2 in
    if f m then bsearch_max_int f (m + 1) ub
    else bsearch_max_int f lb m
  )

let partial_sum n ls =
  let rec loop s k = function
    | [] -> s
    | h::t ->
      if k >= n then s
      else loop (s + h) (k + 1) t
  in
  loop 0 0 ls

let _ =
  let (n, m, k) = rdi3 () in
  let aas = rdhi () in
  let bas = rdhi () in

  let can_read nbk =
    let rec test na =
      let nb = nbk - na in
      (* printf "%d %d\n" na nb; *)
      if na > n || nb > m then false
      else (
        (* print_list abk; print_list bbk; puts "----"; *)
        if (partial_sum na aas) + (partial_sum nb bas) <= k then true
        else test (na + 1)
      )
    in

    (* puti nbk; *)
    test (max 0 (nbk - m))
  in

  puti @@ bsearch_max_int can_read 0 (n + m)


open Batteries
open Printf

let ($) g f x = g (f x)

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let c2i = int_of_char
let i2c = char_of_int
let f2i = int_of_float
let i2f = float_of_int

let c2s = String.of_char
let s2cl = String.to_list
let cl2s = String.of_list

let puts s = print_endline s
let putyn b = puts (if b then "Yes" else "No")
let puti i = puts @@ i2s i

let rep lb bnd ub f = match bnd with
  | `To -> for i = lb to ub do f i done
  | `Til -> for i = lb to (ub - 1) do f i done

let rep2 lb1 bnd1 ub1 () lb2 bnd2 ub2 f =
  match (bnd1, bnd2) with
  | (`To, `To) -> for i = lb1 to ub1 do for j = lb2 to ub2 do f i j done done
  | (`To, `Til) -> for i = lb1 to ub1 do for j = lb2 to (ub2 - 1) do f i j done done
  | (`Til, `To) -> for i = lb1 to (ub1 - 1) do for j = lb2 to ub2 do f i j done done
  | (`Til, `Til) -> for i = lb1 to (ub1 - 1) do for j = lb2 to (ub2 - 1) do f i j done done

module JunkList = struct
  include List

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let nempty l = not @@ is_empty l

  let hdop l = if nempty l then Some (hd l) else None

  (* let taker = drop *)
  let takewh = take_while

  (* let dropr = take *)
  let dropwh = drop_while

  let split_by = span

  let chunk = ntake

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  let partition_to_map f l =
    foldr (fun x m -> Map.modify_opt (f x) (function
      | None -> Some [x]
      | Some [] -> Some [x]
      | Some vl -> Some (x::vl)) m) Map.empty l;;

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = JunkList

module JunkArray = struct
  include Array

  let nmem x l = not @@ mem x l

  let len = length
  let size = length
  let is_empty r = len r = 0
  let nempty r = len r > 0

  let take n r = left r n
  let taker n r = right r n

  let takeb x r =
    let l = len r in
    let rec loop i =
      if i >= l then r
      else if r.(i) = x then take i r
      else loop (i + 1)
    in loop 0

  (* let takewh = take_while *)

  let drop n r = tail r n
  let dropr n r = take ((len r) - n) r
  (* let dropwh = drop_while *)

  let index_of x r =
    let l = len r in
    let rec loop i =
      if i >= l then None
      else if r.(i) = x then Some i
      else loop (i + 1)
    in loop 0

  (* let split_by = span *)

  (* let chunk = ntake *)

  let foldl = fold_left
  let foldli = fold_lefti
  let foldr f z l = fold_right f l z
  let foldri f z l = fold_righti f l z

  (* let partition_to_map f l = *)
  (*   foldr (fun x m -> Map.modify_opt (f x) (function *)
  (*     | None -> Some [x]  *)
  (*     | Some [] -> Some [x]  *)
  (*     | Some vl -> Some (x::vl)) m) Map.empty l;; *)

  (* let scanl = scan_left *)
  (* let scanli = scan_lefti *)
  (* let scanr = scan_right *)
  (* let scanri = scan_righti *)

  let concat2 = append

  let flatmap (f : 'a -> 'b array) a =
    map f a |> enum |> L.of_enum |> concat

  let zipf = map2
  (* let zipfi = map2i *)

end

module A = JunkArray

module JunkString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

  let maptol f s = L.map f (s2cl s)
  let maptoli f s = L.map f (s2cl s)

  let zipf f a b = L.zipf f (s2cl a) (s2cl b)
  let zipfi f a b = L.zipfi f (s2cl a) (s2cl b)

  let mkstr f sep ls =
    L.foldl (fun acc x -> acc ^ sep ^ (f x)) (f @@ L.hd ls) (List.tl ls)

end

module S = JunkString

let spsp s = S.split_on_char ' ' s

module JunkSortedSet = struct
  include Set

  module Make (Ord : Set.OrderedType) = struct
    include Set.Make(Ord)

    let nmem x st = not @@ mem x st

    let foldl f s z = fold (fun x a -> f a x) z s

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let foldl f s z = fold (fun x a -> f a x) z s

  let size = cardinal
end

module Ss = JunkSortedSet

module JunkSortedMap  = struct
  include Map

  module Make (Ord : Map.OrderedType) = struct
    include Map.Make(Ord)

    let nmem x st = not @@ mem x st

    let get = find
    let get_or x d m = find_default d x m

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

  let get = find
  let get_or x d m = find_default d x m

  let size = cardinal
end

module Ms = JunkSortedMap

let set_power ls =
  let rec loop ls pls =
    match ls with
    | [] -> pls
    | hd::tl -> loop tl (L.flatmap (fun p -> [p; hd::p]) pls)
  in loop ls [[]]

exception ArithmeticException of string

module JunkIntArithmetic = struct

  let rec power x e =
    if e = 0 then x
    else
      let rec loop r x2 e' =
        if e' = 0 then r
        else if e' land 1 = 0 then loop r (x2 * x2) (e' lsr 1)
        else loop (r * x2) (x2 * x2) (e' lsr 1)
      in loop 1 x e

  let rec powers_le_1000 x =
    if x > 32 then [x]
    else
      L.init 9 (fun e -> power x (e + 1))
      |> L.filter (fun y -> y < 1000)

  let primes_le_1000 = [2;3;5;7;11;13;17;19;23;29;31;37;41;
    43;47;53;59;61;67;71;73;79;83;89;97;101;
    103;107;109;113;127;131;137;139;149;151;157;163;167;
    173;179;181;191;193;197;199;211;223;227;229;233;239;
    241;251;257;263;269;271;277;281;283;293;307;311;313;
    317;331;337;347;349;353;359;367;373;379;383;389;397;
    401;409;419;421;431;433;439;443;449;457;461;463;467;
    479;487;491;499;503;509;521;523;541;547;557;563;569;
    571;577;587;593;599;601;607;613;617;619;631;641;643;
    647;653;659;661;673;677;683;691;701;709;719;727;733;
    739;743;751;757;761;769;773;787;797;809;811;821;823;
    827;829;839;853;857;859;863;877;881;883;887;907;911;
    919;929;937;941;947;953;967;971;977;983;991;997]

    let primes_and_powers_le_1000 =
      [2;3;4;5;7;8;9;11;13;16;17;19;23;25;27;29;31;32;37;41;
      43;47;49;53;59;61;64;67;71;73;79;81;83;89;97;
      101;103;107;109;113;121;125;127;128;131;137;139;149;151;
      157;163;167;169;173;179;181;191;193;197;199;211;223;227;
      229;233;239;241;243;251;256;257;263;269;271;277;281;283;
      289;293;307;311;313;317;331;337;343;347;349;353;359;361;
      367;373;379;383;389;397;401;409;419;421;431;433;439;443;
      449;457;461;463;467;479;487;491;499;503;509;512;521;523;
      529;541;547;557;563;569;571;577;587;593;599;601;607;613;
      617;619;625;631;641;643;647;653;659;661;673;677;683;691;
      701;709;719;727;729;733;739;743;751;757;761;769;773;787;
      797;809;811;821;823;827;829;839;841;853;857;859;863;877;
      881;883;887;907;911;919;929;937;941;947;953;961;967;971;
      977;983;991;997]

  let factorize_with ps n =
    let rec loop acc ps e n' =
      if n' <= 1 then ([], n')
      else
        match ps with
        | [] -> (acc, n')
        | p::t ->
          let r = n' mod p in
          if r = 0 then (
            if n' = p then ((p, e + 1) :: acc, 1)
            else loop acc ps (e + 1) (n' / p)
          ) else
            loop (if e = 0 then acc else (p, e) :: acc) t 0 n'
    in
    let (fs, r) = loop [] ps 0 n in
    (L.rev fs, r)

  let factorize_le_million n =
    factorize_with primes_le_1000 n

  let rec to_bits_le n =
    if n = 0 then []
    else (n mod 2) :: to_bits_le (n lsr 1)

  let rec gcd x y =
    match x mod y with
    | 0 -> y
    | z -> gcd y z

  let ext_euclid a b =
    let rec next (r0, s0, t0) (r1, s1, t1) =
      match r1 with
      | 0 -> (r0, s0, t0)
      | r2 ->
        let q = r0 / r1 in
        next (r1, s1, t1) (r0 - q * r1, s0 - q * s1, t0 - q * t1)
    in
    if a < b then
      let (g, y, x) = next (b, 1, 0) (a, 0, 1) in (g, x, y)
    else
      next (a, 1, 0) (b, 0, 1)

  let modinv x m =
    let (g, y, z) = ext_euclid x m in
    match g with
    | 1 -> y mod m
    | _ -> raise @@ ArithmeticException "Not relatively prime"

end

module N = JunkIntArithmetic

module JunkMatrix = struct

  type 'a t = { w : int; h : int; e : 'a array array }

  let make w h e0 =
    { w = w; h = h; e = A.make_matrix w h e0 }

  let from_columns w h e0 elems =
    let matr = make w h e0 in
    elems |> L.iteri (fun j col ->
      col |> L.iteri (fun i x -> matr.e.(i).(j) <- x));
    matr

  let blit (mats, rs, cs, w, h) (matd, rd, cd) =
    rep2 0 `Til w () 0 `Til h (fun i j ->
        matd.e.(rd + i).(cd + j) <- mats.e.(rs + i).(cs + j))

  let expand (w1, h1) (w2, h2) e0 mat =
    let matr = make w2 h2 e0 in
    blit (mat, 0, 0, w1, h1) (matr, 0, 0);
    matr

end

module M = JunkMatrix

module type FieldOps = sig
  type elt
  val zero : elt
  val one : elt
  val neg : elt -> elt
  val rcp : elt -> elt
  val add : elt -> elt -> elt
  val sub : elt -> elt -> elt
  val mul : elt -> elt -> elt
  val div : elt -> elt -> elt
end

module JunkLinearAlgebra (Fop : FieldOps) = struct

  type t = Fop.elt M.t

  let zero w h = M.make w h Fop.zero

  let prod (mat1 : t) (mat2 : t) =
    let matr = zero mat1.w mat2.h in
    rep2 0 `Til mat1.w () 0 `Til mat2.h (fun i j ->
        matr.e.(i).(j) <-
          mat1.e.(i) |> A.foldli (fun
            acc k x -> Fop.add acc (Fop.mul x mat2.e.(k).(i)))
            Fop.zero);
    matr

  exception NotZero of int
  let is_zero (mat : t) =
    try (
      rep 0 `Til mat.w (fun i ->
        if mat.e.(i) |> A.for_all (fun x -> x <> Fop.zero)
          then raise (NotZero 1)
      );
      true
    ) with _ -> false

  let col_addmul c1 (c2, a) (mat : t) =
    rep 0 `Til mat.h (fun i ->
      mat.e.(i).(c1) <- Fop.add mat.e.(i).(c1) (Fop.mul a mat.e.(i).(c2)))

  let col_inner_prod c1 c2 (mat : t) =
    let sum = ref Fop.zero in
    rep 0 `Til mat.h (fun i ->
      sum := Fop.add !sum @@ Fop.mul mat.e.(i).(c1) mat.e.(i).(c2));
    !sum

  (* let orth_extend k0 k1 mat = *)
  (*   let matx = extend (k0, h) ((k0 + k1), h) 0 mat in *)
  (*   rep k0 `Til (k0 + k1) (fun t -> *)
  (*     rep 0 `Til t (fun s ->  *)
  (*       let ip = col_inner_prod s t h matx in *)
  (*       col_addmul t (s, ip) h matx)); *)
  (*   submat (k0, 0) (k0 + k1 - 1, h - 1) matx *)

end

module type OrderedData = sig
  type t
  val compare : t -> t -> int
  val default : t
end

module JunkGraph = struct

  type 'a v_t = int * 'a
  type 'a t = { vs : 'a v_t array; nb : int Ss.t array }

  let empty nv vd =
    let vs0 = Array.make nv (0, vd) in
    for i = 0 to (nv - 1) do vs0.(i) <- (i, vd) done;
    {
      vs = vs0;
      nb = A.make nv Ss.empty
    }

  let add_edge (e : int * int) vd g =
    let (v1, v2) = e in
    g.vs.(v1 - 1) <- (v1, vd);
    g.vs.(v2 - 1) <- (v2, vd);
    g.nb.(v1 - 1) <- g.nb.(v1 - 1) |> Ss.add v2;
    g

  let add_uedge e vd g =
    add_edge e vd (add_edge (swap e) vd g)

  let from_edges nv vd es =
    es |> L.foldl (fun g e -> add_edge e vd g) (empty nv vd)

  let from_uedges nv vd es =
    es |> L.foldl (fun g e -> add_uedge e vd g) (empty nv vd)

end

module G = JunkGraph

module JunkGraphAlgorithms (Vdt : OrderedData) = struct

  type t = Vdt.t G.t

  module V = struct
    type t = int * Vdt.t
    let compare (vi1, vd1) (vi2, vd2) =
      let c1 = Vdt.compare vd1 vd2 in
      if c1 = 0 then compare vi1 vi2
      else c1

    let default i = (i, Vdt.default)
    let index (v : t) = fst v
  end

  module Vset = Ss.Make(V)

  let print_vertices label vs =
    printf "%s: " label;
    vs |> Vset.iter (fun (vi, _) -> printf "%d " vi);
    puts ""

  let nbiter (upd : V.t -> V.t -> t -> t * bool)
    (ui : int) (fr : Vset.t) (cls : int Ss.t) (g : t) =
    let nb = g.nb.(ui - 1) in
    let result = nb |> Ss.foldl (fun (ga, nxfr, nxcls) vi ->
    (* puts "nxcls: "; *)
      (* Ss.iter (fun x -> printf "%d " x) nxcls; puts ""; *)
      if ui = vi || Ss.mem vi nxcls then
        (ga, nxfr, nxcls)
      else
        let ue = g.vs.(ui - 1) and ve = g.vs.(vi - 1) in
        let (gu, close) = upd ue ve ga in
        (gu,
          nxfr |> Vset.add ve,
          if close then nxcls |> Ss.add vi else nxcls)
    ) (g, fr, cls)
    in (* puts "";  *)result

  let traverse (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (ve, fr') = Vset.pop_min fr in
      let (vi, _) = ve in
      let (gu, nxfr, nxcls) = nbiter upd vi fr' cls g' in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let frontier (upd : V.t -> V.t -> t -> t * bool)
    (fr0 : Vset.t) (cls0 : int Ss.t) (g : t) =

    let rec progress fr cls g' =
      let (gu, nxfr, nxcls) =
        fr |> Vset.foldl (fun (ga, nxfra, nxclsa) ve ->
          let (vi, _) = ve in
          nbiter upd vi nxfra nxclsa ga
        ) (g', Vset.empty, cls)
      in
      if Vset.is_empty nxfr then (gu, nxcls)
      else progress nxfr nxcls gu
    in
    progress fr0 cls0 g

  let bfs (upd : V.t -> V.t -> t -> t) =
    frontier (fun fr cls g' -> (upd fr cls g', true))

end

let rds () = read_line ()

let rdi () = s2i @@ rds ()

let rdhs () = spsp @@ rds ()
let rdhi () = rdhs () |> L.map s2i

let rdhf () = rdhs () |> L.map s2f

let rdf2 () = match rdhf () with
| a::b::_ -> (a, b)
| _ -> (0., 0.)

let rdf3 () = match rdhf () with
| a::b::c::_ -> (a, b, c)
| _ -> (0., 0., 0.)

let rdf4 () = match rdhf () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0., 0., 0., 0.)

let rds2 () = match rdhs () with
  | a::b::_ -> (a, b)
  | _ -> ("", "")

let rdi2 () = match rdhi () with
  | a::b::_ -> (a, b)
  | _ -> (0, 0)

let rdi3 () = match rdhi () with
| a::b::c::_ -> (a, b, c)
| _ -> (0, 0, 0)

let rdi4 () = match rdhi () with
| a::b::c::d::_ -> (a, b, c, d)
| _ -> (0, 0, 0, 0)

let rdi5 () = match rdhi () with
| a::b::c::d::e::_ -> (a, b, c, d, e)
| _ -> (0, 0, 0, 0, 0)

let rdv n rf =
  let rec loop n =
    if n <= 0 then []
    else (
      let s = rf () in
      s::(loop (n - 1))
    )
  in loop n

let rdvi n = rdv n rdi
let rdvi2 n = rdv n rdi2
let rdvi3 n = rdv n rdi3
let rdvi4 n = rdv n rdi4

module Galg = JunkGraphAlgorithms (
  struct
    type t = int
    let compare = compare
    let default = 0
  end)

let print_list aa =
  printf "[ "; aa |> L.iter (fun a -> printf "%d " a); puts "]"

let print_array aa =
  printf "[ "; aa |> A.iter (fun a -> printf "%d " a); puts "]"

let rec bsearch_max_int f lb ub =
  if ub = lb then lb
  else (
    let m = (lb + ub) / 2 in
    if f m then bsearch_max_int f m ub
    else bsearch_max_int f lb m
  )

let _ =
  let (n, m, k) = rdi3 () in
  let aas = rdhi () in
  let bas = rdhi () in

  let rec read nbk =
    let rec loop na =
      let nb = nbk - na in
      if na > n || nb > m then false
      else (
        printf "%d %d\n" na nb;
        let abk = aas |> L.take na in
        let bbk = bas |> L.take nb in
        (* print_list abk; print_list bbk; puts "----"; *)
        if L.sum abk + L.sum bbk <= k then true
        else loop (na + 1)
      )
    in

    if nbk > n + m then nbk - 1
    else if loop (max 0 (nbk - n)) then read (nbk + 1)
    else nbk - 1
  in

  puti @@ read 1


let () =
  Scanf.scanf " %d %d %d" @@ fun n m k ->
  let arr1 = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let arr2 = Array.init m (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let lst1 = Array.to_list arr1 in
  let lst2 = Array.to_list arr2 in
  (* 時間 t で、机B で読める本の冊数を返す *)
  let can_read t lst = List.fold_left
    (fun (t', n') b ->
      if b <= t' then (t' - b, n' + 1) else (t', n'))
    (t, 0) lst in
  let now = ref 0 in
  (* A で n 冊 読む時、A と B 合計で何冊読めるか計算し、最大値を返す *)
  let h lst = List.fold_left
    (fun (c', t') a -> (now := !now + 1;
      let nokori = t' - a in
      let (a', b') = can_read nokori lst2 in
      if (c' < (!now + b')) then (!now + b', nokori)
      else (c', nokori)
    )) (0, k) lst in
  (fun (a, b) -> Printf.printf "%d" a) (h lst1)


let () =
  Scanf.scanf " %d %d %d" @@ fun n m k ->
  let arr1 = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let arr2 = Array.init m (fun _ -> Scanf.scanf " %d" (fun x -> x)) in
  let lst1 = Array.to_list arr1 in
  let lst2 = Array.to_list arr2 in
  let time = ref k in
  let rec g lst n = match lst with
   | [] -> n
   | first :: rest -> if first <= !time then (time := !time - first; g rest (n + 1))
                      else n in
  let rec f l1 l2 n = match (l1, l2) with
  | ([], []) -> n
  | ([], _) -> g l2 n
  | (_, []) -> g l1 n
  | (first1 :: rest1, first2 :: rest2) ->
    if first1 <= !time || first2 <= !time then
    (if first1 < first2 then (time := !time - first1; f rest1 l2 (n + 1))
     else (time := !time - first2; f l1 rest2 (n + 1)))
    else n in
  Printf.printf "%d" (f lst1 lst2 0)


let () = Scanf.scanf "%d %d %d\n" @@ fun n m k ->
  let acum = Array.make (n+1) 0 in
  let bcum = Array.make (m+1) 0 in
  let ok = ref 0 in
  let ng = ref (n+m+1) in
  let check x =
    let ids = List.init (n+1) Fun.id in
    let predicate i = x-m <= i && i <= x && acum.(i) + bcum.(x-i) <= k in
    List.exists predicate ids in
  for i = 1 to n do
    if i < n
    then Scanf.scanf " %d" @@ fun a -> acum.(i) <- acum.(i-1) + a
    else Scanf.scanf " %d\n" @@ fun a -> acum.(i) <- acum.(i-1) + a
  done;
  for i = 1 to m do
    if i < m
    then Scanf.scanf " %d" @@ fun b -> bcum.(i) <- bcum.(i-1) + b
    else Scanf.scanf " %d\n" @@ fun b -> bcum.(i) <- bcum.(i-1) + b
  done;
  while !ng - !ok > 1 do
    let mid = (!ok + !ng) / 2 in
    if check mid
    then ok := mid
    else ng := mid
  done;
  Printf.printf "%d\n" !ok


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern

let (n, m, k) = Scanf.sscanf (read_line ()) "%d %d %d" @@ fun n m k -> (n, m, k)
let a = read_line () |> split_string ~pattern:" " |> List.map int_of_string |> Array.of_list
let b = read_line () |> split_string ~pattern:" " |> List.map int_of_string |> Array.of_list

let sa = Array.make (n + 1) 0
let sb = Array.make (m + 1) 0

let rec solve i = function
  | 0 -> (i, 0)
  | j -> if sa.(i) + sb.(j) > k then solve i (j - 1) else (i + j, j)

let () =
  for i = 1 to n do sa.(i) <- sa.(i - 1) + a.(i - 1) done;
  for i = 1 to m do sb.(i) <- sb.(i - 1) + b.(i - 1) done;
  Printf.printf "%d\n" @@ fst
    @@ Array.fold_left (fun (m, j) i ->
      if sa.(i) > k then (m, j)
      else let (nm, nj) = solve i j in (max m nm, nj)
    ) (0, m)
    @@ Array.init (n + 1) @@ fun i -> i


let rec upper_bound l r p =
  if r <= 1 + l
  then l
  else let m = (l + r) / 2 in
       if p m
       then upper_bound m r p
       else upper_bound l m p

let () = Scanf.scanf "%d %d %d\n" @@ fun n m k ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let bs = Array.init m @@ fun _ -> Scanf.scanf "%d " @@ fun b -> b in
  let inc = Array.make (n + 1) 0 in
  let inc' = Array.make (m + 1) 0 in
  for i = 0 to n - 1 do
    inc.(i + 1) <- inc.(i) + as_.(i)
  done;
  for i = 0 to m - 1 do
    inc'.(i + 1) <- inc'.(i) + bs.(i)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left max min_int @@
  Array.init (n + 1) @@ fun i ->
    if k < inc.(i)
    then 0
    else i + upper_bound 0 (m + 1) (fun j -> inc.(i) + inc'.(j) <= k)


open Batteries
let n,m,k = Scanf.sscanf (read_line()) "%d %d %d" (fun n m k -> n,m,k)
let a = read_line () |> String.split_on_char ' ' |> List.map int_of_string |> Array.of_list
let b = read_line () |> String.split_on_char ' ' |> List.map int_of_string |> Array.of_list

let rec main_loop a_where b_where time_count book_count =
  if a_where >= n && b_where >= m then book_count else
  if a_where >= n then (
    if time_count + b.(b_where) > k then book_count else
    main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1)
  ) else
  if b_where >= m then (
    if time_count + a.(a_where) > k then book_count else
    main_loop (a_where+1) (b_where) (time_count + a.(a_where)) (book_count+1)
  ) else
  (
    if time_count + a.(a_where) > k then (
      if time_count + b.(b_where) > k then book_count else
      main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1)
    ) else
      if a.(a_where) < b.(b_where) then (
        main_loop (a_where+1) (b_where) (time_count + a.(a_where)) (book_count+1)
      ) else (
        main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1)
      )
    )
let _ = Printf.printf "%d\n" @@ main_loop 0 0 0 0


open Batteries

let n,m,k = Scanf.sscanf (read_line()) "%d %d %d" (fun n m k -> n,m,k)
let a = read_line () |> String.split_on_char ' ' |> List.map int_of_string |> Array.of_list
let b = read_line () |> String.split_on_char ' ' |> List.map int_of_string |> Array.of_list
let all = (Array.length a) + (Array.length b)
let all_count = (Array.fold_left (fun k l -> k+l) 0 a) + (Array.fold_left (fun k l -> k+l) 0 b)

let rec main_loop a_where b_where time_count book_count =
  if k >= all_count then all else
  if a_where >= n && b_where >= m then book_count else
  if a_where >= n then (
    if time_count + b.(b_where) > k then book_count else
    main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1)
  ) else
  if b_where >= m then (
    if time_count + a.(a_where) > k then book_count else
    main_loop (a_where+1) (b_where) (time_count + a.(a_where)) (book_count+1)
  ) else
  (
    if time_count + a.(a_where) > k then (
      if time_count + b.(b_where) > k then book_count else
      main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1)
    ) else
    max (main_loop (a_where+1) (b_where) (time_count + a.(a_where)) (book_count+1)) (main_loop (a_where) (b_where+1) (time_count + b.(b_where)) (book_count+1))
  )
let _ = Printf.printf "%d\n" @@ main_loop 0 0 0 0


let () = Scanf.scanf "%d %d %d\n" @@ fun n m k ->
  let as_ = Array.init (n + 1) @@ fun i ->
    if i = n
    then max_int
    else Scanf.scanf "%d " @@ fun a -> a in
  let bs = Array.init (m + 1) @@ fun i ->
    if i = m
    then max_int
    else Scanf.scanf "%d " @@ fun b -> b in
  let rec solve acc k i j =
    if k < as_.(i) && k < bs.(j)
    then acc
    else if as_.(i) <= bs.(j)
    then solve (1 + acc) (k - as_.(i)) (i + 1) j
    else solve (1 + acc) (k - bs.(j)) i (j + 1) in
  Printf.printf "%d\n" @@ solve 0 k 0 0


open Printf
open Scanf

let id x = x

let n, m, k = scanf "%d %d %d\n" (fun n m k -> n, m, k)
let aIn = Array.init n (fun i -> scanf "%d " id)
let bIn = Array.init m (fun i -> scanf "%d " id)

exception Break

let main () =
    let aa = Array.make (n + 1) 0 in
    let sum = ref 0 in
    for i = 1 to n do
        sum := !sum + aIn.(i - 1);
        aa.(i) <- !sum
    done;
    let bb = Array.make (m + 1) 0 in
    sum := 0;
        for i = 1 to m do
        sum := !sum + bIn.(i - 1);
        bb.(i) <- !sum
    done;
    let ai = ref 0 in
    let bi = ref 0 in
    try while true do
        let curA = aa.(!ai) in
        let curB = bb.(!bi) in
        if !ai < n && !bi < m then begin
            let nextA = aa.(!ai + 1) in
            let nextB = bb.(!bi + 1) in
            if nextA + curB <= k && curA + nextB <= k then begin
                if nextA < nextB then begin
                    incr ai
                end else begin
                    incr bi
                end
            end else if nextA + curB <= k then begin
                incr ai
            end else if curA + nextB <= k then begin
                incr bi
            end else begin
                raise Break
            end
        end else if !ai < n then begin
            let nextA = aa.(!ai + 1) in
            if nextA + curB <= k then incr ai
            else raise Break
        end else if !bi < m then begin
            let nextB = bb.(!bi + 1) in
            if curA + nextB <= k then incr bi
            else raise Break
        end else begin
            raise Break
        end
    done with Break -> ();
    Printf.printf "%d\n" (!ai + !bi)

let () = main ()


open Batteries
module EnumL = Enum.Labels
module ListL = List.Labels
module ArrayL = Array.Labels

let dbg0 x = Printf.eprintf "[debug]%s\n" @@ dump x
let dbg1 x = dbg0 x; x

let id = identity

let (++) n m = if n <= m then  List.range n `To m else []
let (++-) n m = if n >= m then List.range n `Downto m else []
let (++^) n m = if n < m then List.range n `To (pred m) else []

module ModOp = struct
  let prime = 1_000_000_007
  let (-) a b = (a + prime - b) mod prime
  let (+) a b = (a + b) mod prime
  let ( * ) a b = (a * b) mod prime

  let rec pow x n =
    if n = 0 then 1
    else if n mod 2 = 0 then pow (x*x) (n/2)
    else x * pow (x*x) (n/2)
end

let scan fmt = Scanf.sscanf (read_line ()) fmt

let scan_lines n fmt f =
  if n = 0 then []
  else List.map (fun _ -> scan fmt f) (1 ++ n)

let scan_list ?sep cnv =
  let line = read_line () in
  (match sep with
   | None -> List.map String.of_char @@ String.to_list line
   | Some sep -> String.split_on_char sep line)
  |> List.map cnv

let scan_array ?sep cnv = Array.of_list @@ scan_list ?sep cnv

let scan_matrix n m e ?sep conv =
  let arr = Array.make_matrix n m e in
  ListL.iter (0 ++^ n)
    ~f:(fun i -> arr.(i) <- Array.of_list @@ scan_list ?sep conv);
  arr

let atoi c = Char.code c - Char.code '0'
let itoa n = Char.chr (n + Char.code '0')

let rec powerset = function
  | [] -> [[]]
  | hd::tl ->
    let pws =  powerset tl in
    pws @ ListL.map pws ~f:(fun pw -> hd::pw)

let permutations l =
  let rec interleave x = function
    | [] -> [[x]]
    | (hd::tl) as l ->
      (x::l) :: (interleave x tl |> ListL.map ~f:(fun l -> hd::l)) in
  let rec aux = function
    | [] -> [[]]
    | a::rest ->
      aux rest |> ListL.map ~f:(interleave a) |> List.concat in
  aux l

let intersection l =
  EnumL.filter ~f:(fun x -> EnumL.exists ~f:((=) x) @@ Enum.clone l)

let zip l m =
  let n = min (List.length l) (List.length m) in
  List.combine (List.take n l) (List.take n m)

let lower_bound n f =
  let rec aux l u =
    if u - l > 1 then
      let m = (l + u) / 2 in
      if f m then aux l m
      else aux m u
    else u in aux (-1) n

let between n x m = n <= x && x < m

let (n,m,k) = scan "%d %d %d" Tuple3.make
let a = scan_list ~sep:' ' Int.of_string
let b = scan_list ~sep:' ' Int.of_string

let () =
  let aa = Array.make (succ n) 0 in
  let ab = Array.make (succ m) 0 in
  ListL.iteri a ~f:(fun i v -> aa.(i+1) <- aa.(i) + v);
  ListL.iteri b ~f:(fun i v -> ab.(i+1) <- ab.(i) + v);
  let rec aux f left right =
    if right - left > 1 then
      let mid = (left + right)/2 in
      if f mid then aux f mid right
      else aux f left mid
    else left
  in
  let f i = ListL.exists (0++i)
      ~f:(fun j ->
          let (x, y) = j, i - j in
          if 0 <= x && x <= n && 0 <= y && y <= m then aa.(x) + ab.(y) <= k
          else false)
  in
  Printf.printf "%d\n" @@ aux f 0 (n+m+1)