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 = List.range n `To m
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 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 = scan "%d" id
let s = scan "%s" id

let () =
  let s = String.to_list s |> Array.of_list in
  let acc = Array.make n (0,0,0) in
  ArrayL.iteri s ~f:(fun i c ->
      let (r,g,b) = if i = 0 then (0,0,0) else acc.(pred i) in
      acc.(i) <- match c with
        | 'R' -> (succ r, g, b)
        | 'G' -> (r, succ g, b)
        | 'B' -> (r, g, succ b)
        | _ -> (r,g,b));
  ListL.map (1++^n) ~f:(fun j ->
      ListL.map (0++^j) ~f:(fun i ->
          let k = j + (j-i) in
          let same = if k < n && s.(i) <> s.(j) && s.(j) <> s.(k) && s.(k) <> s.(i) then -1 else 0 in
          let (r,g,b) = acc.(n-1) in
          let (r0,g0,b0) = acc.(j) in
          same + (match s.(i), s.(j) with
              | 'R', 'G' -> b-b0
              | 'R' ,'B' -> g-g0
              | 'G', 'R' -> b-b0
              | 'G', 'B' -> r-r0
              | 'B', 'R' -> g-g0
              | 'B', 'G' -> r-r0
              | _ -> 0)
        ) |> List.sum
    ) |> List.sum
  |> Printf.printf "%d\n"

Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


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 = List.range n `To m
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 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 = scan "%d" id
let s = scan "%s" id

let () =
  let s = String.to_list s |> Array.of_list in
  let acc = Array.make n (0,0,0) in
  ArrayL.iteri s ~f:(fun i c ->
      let (r,g,b) = if i = 0 then (0,0,0) else acc.(pred i) in
      acc.(i) <- match c with
        | 'R' -> (succ r, g, b)
        | 'G' -> (r, succ g, b)
        | 'B' -> (r, g, succ b)
        | _ -> (r,g,b));
  ListL.map (1++^n) ~f:(fun j ->
      ListL.map (0++^j) ~f:(fun i ->
          let k = i + (j-i) in
          if k >= n then 0
          else
            let same = if s.(i) <> s.(j) && s.(j) <> s.(k) && s.(k) <> s.(i) then -1 else 0 in
            let (r,g,b) = acc.(n-1) in
            let (r0,g0,b0) = acc.(j) in
            same + (match s.(i), s.(j) with
                | 'R', 'G' -> b-b0
                | 'R' ,'B' -> g-g0
                | 'G', 'R' -> b-b0
                | 'G', 'B' -> r-r0
                | 'B', 'R' -> g-g0
                | 'B', 'G' -> r-r0
                | _ -> 0)
        ) |> List.sum
    ) |> List.sum
  |> pred
  |> Printf.printf "%d\n"

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

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 split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let s = read_line () |> split_string

let cnt = List.fold_left (fun acc n ->
  match acc with
    | [] -> [(n, 1)]
    | (v, cnt) as t :: xs -> if v = n then (v, cnt + 1) :: xs else (n, 1) :: t :: xs
) [] @@ List.sort compare s

let () =
  if List.length cnt < 3 then
    print_endline "0"
  else begin
    let s = Array.of_list s in
    let ans = ref @@ List.fold_left ( * ) 1 @@ List.map snd cnt in
    for i = 0 to n - 2 do
      for j = i + 1 to n - 1 do
        let k = j + (j - i) in
        if k < n && s.(i) <> s.(j) && s.(j) <> s.(k) && s.(i) <> s.(k) then ans := !ans - 1
      done
    done;
    Printf.printf "%d\n" !ans
  end


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let s = read_line () |> split_string

let cnt = List.fold_left (fun acc n ->
  match acc with
    | [] -> [(n, 1)]
    | (v, cnt) as t :: xs -> if v = n then (v, cnt + 1) :: xs else (n, 1) :: t :: xs
) [] @@ List.sort compare s

let () =
  let s = Array.of_list s in
  let ans = ref @@ List.fold_left ( * ) 1 @@ List.map snd cnt in
  for i = 0 to n - 2 do
    for j = i + 1 to n - 1 do
      let k = j + (j - i) in
      if k < n && s.(i) <> s.(j) && s.(j) <> s.(k) && s.(i) <> s.(k) then ans := !ans - 1
    done
  done;
  Printf.printf "%d\n" !ans


let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  let r, g, b =
    List.fold_left (fun (r, g, b) -> function
      | 'R' -> r + 1, g, b
      | 'G' -> r, g + 1, b
      | 'B' -> r, g, b + 1) (0, 0, 0) @@
    List.init (String.length s) @@ String.get s in
  let ignore =
    List.fold_left (List.fold_left ( + )) 0 @@
    List.init (String.length s / 2) @@ fun w ->
      List.init (String.length s - 2 * (w + 1)) @@ fun i ->
        if s.[i] = s.[i + w + 1] || s.[i + w + 1] = s.[i + 2 * (w + 1)] || s.[i] = s.[i + 2 * (w + 1)]
        then 0
        else 1 in
  Printf.printf "%d\n" @@ r * g * b - ignore


open Batteries
(* charのカウント *)
let count_char target str =
  let rec count lst =
    match lst with
    | [] -> 0
    | first :: rest -> if first = target then 1 + count rest else count rest
  in let explode s = List.init (String.length s) (String.get s)
  in count @@ explode str
(* 文字列をcharのlistにする *)
let explode s = List.init (String.length s) (String.get s)
let n = read_int ()
let s = read_line ()
let s2 = s |> explode |> Array.of_list

let r = count_char 'R' s
let g = count_char 'G' s
let b = count_char 'B' s
let rgb = r * g * b
let rec loop i ans2 =
  let rec loop2 j ans =
    if i >= n - 1 then ans else
    let k = 2 * j - i in
    if k >= n then ans else
    if (s2.(i) <> s2.(j)) && (s2.(j) <> s2.(k)) && (s2.(i) <> s2.(k)) then loop2 (j+1) (ans+1)
    else loop2 (j+1) ans
  in if i >= n-2 then ans2 else
  loop (i+1) (ans2 + loop2 (i+1) 0)

let _ = Printf.printf "%d\n" (rgb-(loop 0 0))


open Batteries
(* 文字列をcharのlistにする *)
let explode s = List.init (String.length s) (String.get s)
let n = read_int ()
let s = Array.init n (fun _ -> Scanf.scanf "%c" @@ fun b -> b)
let rec loop1 i =
  let rec loop2 i2 =
    let rec loop3 i3 ans =
      if i3 >= n then ans else
      (* ここに処理を書く *)
        (
        if (s.(i2) != s.(i3)) && (s.(i) != s.(i3)) then (
          if (i3 - i2) != (i2 - i) then loop3 (i3+1) (ans+1) else loop3 (i3+1) ans
        ) else (
        loop3 (i3+1) ans))
    in if i2 >= n-1 then 0 else (
      if s.(i) = s.(i2) then loop2 (i2+1) else
      loop3 (i2+1) 0 + loop2 (i2+1)
    )
  in if i >= n-2 then 0 else (
    loop2 (i+1) + loop1 (i+1))

let _ = Printf.printf "%d\n" @@ loop1 0


open Batteries
(* 文字列をcharのlistにする *)
let explode s = List.init (String.length s) (String.get s)
let n = read_int ()
let s = read_line () |> explode |> Array.of_list
let rec loop1 i =
  let rec loop2 i2 =
    let rec loop3 i3 ans =
      if i3 >= n then ans else
      (* ここに処理を書く *)
        (
        if (s.(i2) != s.(i3)) && (s.(i) != s.(i3)) then (
          if (i3 - i2) != (i2 - i) then loop3 (i3+1) (ans+1) else loop3 (i3+1) ans
        ) else (
        loop3 (i3+1) ans))
    in if i2 >= n-1 then 0 else (
      if s.(i) = s.(i2) then loop2 (i2+1) else
      loop3 (i2+1) 0 + loop2 (i2+1)
    )
  in if i >= n-2 then 0 else (
    loop2 (i+1) + loop1 (i+1))

let _ = Printf.printf "%d\n" @@ loop1 0


open Batteries
(* 文字列をcharのlistにする *)
let explode s = List.init (String.length s) (String.get s)
let n = read_int ()
let s = read_line () |> explode |> Array.of_list
let rec loop1 i =
  let rec loop2 i2 =
    let rec loop3 i3 ans =
      if i3 >= n then ans else
      (* ここに処理を書く *)
        (
        if s.(i) != s.(i2) && s.(i2) != s.(i3) && s.(i) != s.(i3) then (
          if (i2 - i) != (i3 - i2) then loop3 (i3+1) (ans+1) else loop3 (i3+1) ans
        ) else (
        loop3 (i3+1) ans))
    in if i2 >= n-1 then 0 else (
      loop3 (i2+1) 0 + loop2 (i2+1)
    )
  in if i >= n-2 then 0 else (
    loop2 (i+1) + loop1 (i+1))

let _ = Printf.printf "%d\n" @@ loop1 0


let a,b,c = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> (a, b, c))

let ans = Printf.sprintf "%d %d %d" c a b
let () = print_endline ans

Score : 100 points

Problem Statement

We have three boxes A, B, and C, each of which contains an integer.

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.

Then, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


let a,b,c = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> (a, b, c))
let ans = Printf.sprintf "%d %d %d" c a b


let a, b, c = Scanf.sscanf (read_line ()) "%d %d %d" @@ fun a b c -> (a, b, c)

let () = Printf.printf "%d %d %d\n" c a b


(* unihernandez22
 * https://atcoder.jp/contests/abc161/tasks/abc161_a
 * implementation
 * *)

Printf.printf "%s\n" @@
Scanf.scanf "%s %s %s\n" @@ fun x y z -> z ^ " " ^ x ^ " " ^ y;


Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c ->
	print_int (c);
    print_string " ";
	print_int (a);
    print_string " ";
    print_int (b);
);;


open Printf
open Scanf

let solve a b c = printf "%s %s %s\n" c a b

let () =
  scanf "%s %s %s" solve


let () = Scanf.scanf "%d %d %d" @@ fun x y z ->
  Printf.printf "%d %d %d\n" z x y


open Batteries
let x, y, z = Scanf.sscanf (read_line()) "%d %d %d" (fun x_ y_ z_ ->
  x_, y_, z_
)
let _ = Printf.printf "%d %d %d\n" z x y


let () =
  let x,y,z = Scanf.scanf "%d %d %d\n" (fun a b c -> a,b,c) in
  Printf.printf "%d %d %d\n" z x y


Scanf.scanf "%d %d %d" (fun x y z -> Printf.printf "%d %d %d\n" z x y)


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n, m = Scanf.sscanf (read_line ()) "%d %d" @@ fun n m -> (n, m)
let a = read_line () |> split_string ~pattern:" " |> List.map int_of_string

let sum = List.fold_left (+) 0 a
let b = float sum /. float (4 * m) |> ceil |> int_of_float

let () =
  print_endline @@ if List.filter (fun i -> i >= b) a |> List.length >= m then "Yes" else "No"

Score : 200 points

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes.

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n, m = Scanf.sscanf (read_line ()) "%d %d" @@ fun n m -> (n, m)
let a = read_line () |> split_string ~pattern:" " |> List.map int_of_string

let sum = List.fold_left (+) 0 a
let b =  sum / (4 * m)

let () =
  print_endline @@ if List.filter (fun i -> i >= b) a |> List.length < m then "No" else "Yes"


let cmp a b = 0-(a-b);;

Printf.printf "%s\n" @@
Scanf.scanf "%d %d\n" @@ fun n m ->
    let a = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun i -> i in
    Array.sort (cmp) a;
    let sum = Array.fold_right ( + ) a 0 in
    let min = (sum+4*m-1)/(4*m) in
    let ans = ref "Yes" in
    for i = 0 to m-1 do
        if a.(i) < min then
            ans := "No";
    done;
    !ans


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 = List.range n `To m
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 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) = scan "%d %d" Tuple2.make
let ls = scan_list ~sep:' ' Int.of_string

let () =
  let total = List.sum ls in
  let ms = List.sort Int.compare ls |> List.drop (n - m) in
  if ListL.exists ms ~f:((>=) ((pred total)/(4*m))) then
    Printf.printf "No\n"
  else Printf.printf "Yes\n"


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 = List.range n `To m
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 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) = scan "%d %d" Tuple2.make
let ls = scan_list ~sep:' ' Int.of_string

let () =
  let total = List.sum ls in
  let ms = List.sort Int.compare ls |> List.drop (n - m) in
  if ListL.exists ms ~f:((>=) (total-1/(4*m))) then
    Printf.printf "No\n"
  else Printf.printf "Yes\n"


open Array
let n, m = Scanf.scanf "%d %f\n" @@ fun a b -> a, b
let a = init n @@ fun _ -> Scanf.scanf "%f " @@ fun d -> d
let m4 = (fold_left (+.) 0. a) /. (4.*.m)
let ans =
  if (List.length (List.filter (fun x -> x >= m4) (to_list a))) >= int_of_float m
  then "Yes" else "No"
let () = print_endline ans


open Array
let n, m = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let a = init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d
let m4 = (fold_left (+) 0 a) / (4 * m)
let ans =
  if List.length (List.filter (fun x -> x >= m4) (to_list a)) >= m
  then "Yes" else "No"
let () = print_endline ans


open Printf
open Scanf

module L =
  struct
    include List

    let init n f =
      let rec loop x =
        if x = n then []
        else let y = f x in y :: loop (x + 1)
      in loop 0
  end

let id x = x

let solve n m =
  let l = L.init n @@ fun _ -> scanf "%d " id in
  let w = L.fold_left (+) 0 l in
  if m <= L.length (L.filter (fun x -> 4 * m * x >= w) l) then "Yes" else "No"

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


let () =
  let n, m = Scanf.scanf "%d %d\n" (fun n m -> (n, m)) in
  let arr = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
  let sum = float_of_int @@ Array.fold_left ( + ) 0 @@ Array.copy arr in
  let fltr = ceil (sum /. (float_of_int m *. 4.)) and count = ref 0 in
  Array.iter (fun i -> if i >= int_of_float fltr then count := !count + 1) arr ;
  if !count >= m then print_endline "Yes" else print_endline "No"


let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let somme = Array.fold_left ( + ) 0 as_ in
  print_endline @@
  if m <= Array.fold_right (fun a -> ( + ) @@ if a * 4 * m < somme then 0 else 1) as_ 0
  then "Yes"
  else "No"


Scanf.scanf "%d %d" (fun n m ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    Array.sort (fun a b -> compare b a) a;
    let sum = Array.fold_left (+) 0 a in
    print_endline @@ if a.(m - 1) * 4 * m < sum then "No" else "Yes"
)


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 (x,y, a,b,c)  = scan "%d %d %d %d %d" Tuple5.make
let ps = scan_list ~sep:' ' Int.of_string
let qs = scan_list ~sep:' ' Int.of_string
let rs = scan_list ~sep:' ' Int.of_string

let () =
  let qx = ListL.fold_left (List.take x (List.sort (fun x y -> Int.neg @@ Int.compare x y) ps))
      ~init:Heap.empty ~f:(fun h v -> Heap.add v h) in
  let qy = ListL.fold_left  (List.take y (List.sort (fun x y -> Int.neg @@ Int.compare x y) qs))
      ~init:Heap.empty ~f:(fun h v -> Heap.add v h) in
  let (qx, qy) = ListL.fold_left rs ~init:(qx,qy) ~f:(fun (qx,qy) v ->
      let x = Heap.find_min qx in
      let y = Heap.find_min qy in
      if min x y < v then
        if x < y then (Heap.del_min qx |>  Heap.add v, qy)
        else (qx, Heap.del_min qy |> Heap.add v)
      else (qx, qy)
    ) in
  Printf.printf "%d\n"  @@ (Heap.to_list qx |> List.sum)  + (Heap.to_list qy |> List.sum)

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74


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 (x,y, a,b,c)  = scan "%d %d %d %d %d" Tuple5.make
let ps = scan_list ~sep:' ' Int.of_string
let qs = scan_list ~sep:' ' Int.of_string
let rs = scan_list ~sep:' ' Int.of_string

type t =
  | X
  | Y
  | Z

let () =
  let h =
    ListL.fold_left ps ~init:Heap.empty ~f:(fun h v -> Heap.add (-v,X) h)
    |> (fun h -> ListL.fold_left qs ~init:h ~f:(fun h v -> Heap.add (-v,Y) h))
    |>(fun h -> ListL.fold_left rs ~init:h ~f:(fun h v -> Heap.add (-v,Z) h)) in
  let rec aux h i j k =
    if i + j + k = x+y then 0
    else
      let (v, t) = Heap.find_min h in
      let h = Heap.del_min h in
      match t with
      | X ->
        if i = x then
          if k > 0 then
            -v + aux h i (succ j) (pred k)
          else aux h k j k
        else  -v + aux h (succ i) j k
      | Y ->
        if j = y then aux h i j k
        else -v + aux h i (succ j) k
      | Z ->  -v + aux h (succ i) j (succ k)
  in
  Printf.printf "%d\n" @@ aux h 0 0 0


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 (x,y, a,b,c)  = scan "%d %d %d %d %d" Tuple5.make
let ps = scan_list ~sep:' ' Int.of_string
let qs = scan_list ~sep:' ' Int.of_string
let rs = scan_list ~sep:' ' Int.of_string

type t =
  | X
  | Y
  | Z

let () =
  let h =
    ListL.fold_left ps ~init:Heap.empty ~f:(fun h v -> Heap.add (-v,X) h)
    |> (fun h -> ListL.fold_left qs ~init:h ~f:(fun h v -> Heap.add (-v,Y) h))
    |>(fun h -> ListL.fold_left rs ~init:h ~f:(fun h v -> Heap.add (-v,Z) h)) in
  let rec aux h i j k =
    if i + j = x+y then 0
    else
      let (v, t) = Heap.find_min h in
      let h = Heap.del_min h in
      match t with
      | X ->
        if i = x then
          if k > 0 then
            -v + aux h i (succ j) (pred k)
          else aux h k j k
        else  -v + aux h (succ i) j k
      | Y ->
        if j = y then aux h i j k
        else -v + aux h i (succ j) k
      | Z ->  -v + aux h (succ i) j (succ k)
  in
  Printf.printf "%d\n" @@ aux h 0 0 0


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 (x,y, a,b,c)  = scan "%d %d %d %d %d" Tuple5.make
let ps = scan_list ~sep:' ' Int.of_string
let qs = scan_list ~sep:' ' Int.of_string
let rs = scan_list ~sep:' ' Int.of_string

type t =
  | X
  | Y
  | Z

let () =
  let h =
    ListL.fold_left ps ~init:Heap.empty ~f:(fun h v -> Heap.add (-v,X) h)
    |> (fun h -> ListL.fold_left qs ~init:h ~f:(fun h v -> Heap.add (-v,Y) h))
    |>(fun h -> ListL.fold_left rs ~init:h ~f:(fun h v -> Heap.add (-v,Z) h)) in
  let rec aux h i j k =
    if i + j = x+y then 0
    else
      let (v, t) = Heap.find_min h in
      let h = Heap.del_min h in
      match t with
      | X ->
        if i = x then
          if k > 0 then
            -v + aux h (succ i) (succ j) (pred k)
          else aux h k j k
        else  -v + aux h (succ i) j k
      | Y ->
        if j = y then aux h i j k
        else -v + aux h i (succ j) k
      | Z ->  -v + aux h (succ i) j (succ k)
  in
  Printf.printf "%d\n" @@ aux h 0 0 0


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

let (x, y, a, b, c) = Scanf.sscanf (read_line ()) "%d %d %d %d %d" @@ fun x y a b c -> (x, y, a, b, c)
let p = Array.of_list @@ List.map int_of_string @@ split_string @@ read_line ()
let q = Array.of_list @@ List.map int_of_string @@ split_string @@ read_line ()
let r = List.map int_of_string @@ split_string @@ read_line ()

let t_len = x + y

let rec loop arr i acc =
  if List.length acc = 0 || i = t_len then
    Printf.printf "%d\n" @@ Array.fold_left (+) 0 arr
  else
    match acc with
      | [] -> loop arr t_len []
      | x :: xs ->
          let next = ref i in
          while !next < t_len && x < arr.(!next) do next := !next + 1 done;
          if !next < t_len then arr.(!next) <- x;
          loop arr (!next + 1) xs

let rev a b = b - a

let () =
  Array.sort rev p;
  Array.sort rev q;
  let t = Array.append (Array.sub p 0 x) (Array.sub q 0 y) in
  Array.sort rev t;
  loop t 0 r


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

let (x, y, a, b, c) = Scanf.sscanf (read_line ()) "%d %d %d %d %d" @@ fun x y a b c -> (x, y, a, b, c)
let p = Array.of_list @@ List.map int_of_string @@ split_string @@ read_line ()
let q = Array.of_list @@ List.map int_of_string @@ split_string @@ read_line ()
let r = List.map int_of_string @@ split_string @@ read_line ()

let rec loop arr i acc =
  if List.length acc = 0 || (x + y) = i then
    Printf.printf "%d\n" @@ Array.fold_left (+) 0 arr
  else
    match acc with
      | [] -> loop arr c []
      | x :: xs ->
          let next = ref i in
          while !next < (x + y) && x < arr.(!next) do next := !next + 1 done;
          if !next < (x + y) then arr.(!next) <- x;
          loop arr (!next + 1) xs

let rev a b = b - a

let () =
  Array.sort rev p;
  Array.sort rev q;
  let t = Array.append (Array.sub p 0 x) (Array.sub q 0 y) in
  Array.sort rev t;
  Array.sort rev q;
  loop t 0 r


let read_int_array n = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))

let x, y, a, b, z = Scanf.scanf "%d %d %d %d %d" (fun x y a b z -> x, y, a, b, z)

let p = read_int_array a
let q = read_int_array b
let r = read_int_array z

let cmp x y = y - x;;

Array.sort cmp p;
Array.sort cmp q;
Array.sort cmp r;

let rec f i (a, b, c) (ai, bi, ci) ret =
  if i = x + y then ret
  else if b <= a && c <= a then
    f (i+1) ((if ai = x-1 then -1 else p.(ai+1)), b, c) (ai+1, bi, ci) (ret + a)
  else if a <= b && c <= b then
    f (i+1) (a, (if bi = y-1 then -1 else q.(bi+1)), c) (ai, bi+1, ci) (ret + b)
  else
    f (i+1) (a, b, (if ci = z-1 then -1 else r.(ci+1))) (ai, bi, ci+1) (ret + c)
in print_int (f 0 (p.(0), q.(0), r.(0)) (0, 0, 0) 0)


let read_int_array n = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))

let x, y, a, b, c = Scanf.scanf "%d %d %d %d %d" (fun x y a b c -> x, y, a, b, c)

let p = read_int_array a
let q = read_int_array b
let r = read_int_array c

let cmp x y = y - x;;

Array.sort cmp p;
Array.sort cmp q;
Array.sort cmp r;

let rec f i (a, b, c) (ai, bi, ci) ret =
  if i = x + y then ret
  else if b <= a && c <= a then
    f (i+1) ((if ai = x-1 then -1 else p.(ai+1)), b, c) (ai+1, bi, ci) (ret + a)
  else if a <= b && c <= b then
    f (i+1) (a, (if bi = y-1 then -1 else q.(bi+1)), c) (ai, bi+1, ci) (ret + b)
  else
    f (i+1) (a, b, (if ci = c-1 then -1 else q.(ci+1))) (ai, b, ci+1) (ret + c)
in
print_int (f 0 (p.(0), q.(0), r.(0)) (0, 0, 0) 0)


let read_int_array n = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))

let x, y, a, b, c = Scanf.scanf "%d %d %d %d %d" (fun x y a b c -> x, y, a, b, c)

let p = read_int_array a
let q = read_int_array b
let r = read_int_array c

let cmp x y = y - x;;

Array.sort cmp p;
Array.sort cmp q;
Array.sort cmp r;

let rec f i (a, b, c) (ai, bi, ci) ret =
  if i = x + y then ret
  else if b <= a && c <= a then
    f (i+1) ((if ai = x-1 then -1 else p.(ai+1)), b, c) (ai+1, bi, ci) (ret + a)
  else if a <= b && c <= b then
    f (i+1) (a, (if bi = y-1 then -1 else q.(bi+1)), c) (ai, bi+1, ci) (ret + b)
  else
    f (i+1) (a, b, (if ci = c then -1 else q.(ci+1))) (ai, b, ci+1) (ret + c)
in
print_int (f 0 (p.(0), q.(0), r.(0)) (0, 0, 0) 0)


let () = Scanf.scanf "%d %d %d %d %d\n" @@ fun x y a b c ->
  let ps = Array.init a @@ fun _ -> Scanf.scanf "%d " @@ fun p -> p in
  let qs = Array.init b @@ fun _ -> Scanf.scanf "%d " @@ fun q -> q in
  let rs = Array.init c @@ fun _ -> Scanf.scanf "%d " @@ fun r -> r in
  Array.sort (fun p p' -> compare p' p) ps;
  Array.sort (fun q q' -> compare q' q) qs;
  Array.sort (fun r r' -> compare r' r) rs;
  Printf.printf "%d\n" @@
  Array.fold_left ( + ) 0 @@
  Array.sub
    ( Array.of_list @@
      List.merge (fun m n -> compare n m) (Array.to_list (Array.sub ps 0 x)) @@
      List.merge (fun m n -> compare n m) (Array.to_list (Array.sub qs 0 y)) @@
      Array.to_list rs ) 0 (x + y)


let (a, b, c) = Scanf.sscanf (read_line ()) "%d %d %d" @@ fun a b c -> (a, b, c)

let () = print_endline @@ if c - a - b > 0 && (4 * a * b) < (c - a - b) * (c - a - b) then "Yes" else "No"

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.


let (a, b, c) = Scanf.sscanf (read_line ()) "%d %d %d" @@ fun a b c -> (a, b, c)

let () = print_endline @@ if (4 * a * b) < (c - a - b) * (c - a - b) then "Yes" else "No"


let _ = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c ->
  let d = c - a - b in if d > 0 && 4 * a * b < d * d then "Yes" else "No"
) |> print_endline


let _ = Scanf.sscanf (read_line ()) "%f %f %f" (fun a b c ->
  if c -. a -. b < 0. then "No" else
  if (sqrt a) +. (sqrt b) < sqrt c then "Yes" else "No"
) |> print_endline


let () =
  Scanf.scanf "%d %d %d" (fun a b c ->
    let d = c - a - b in
    if d > 0 && 4 * a * b < d * d then "Yes" else "No")
  |> print_endline


let () =
  Scanf.scanf "%d %d %d" (fun a b c ->
    if 4 * a * b < (c - a - b) * (c - a - b) then "Yes" else "No")
  |> print_endline


let () =
  Scanf.scanf "%f %f %f" (fun a b c ->
    if a ** 0.5 +. b ** 0.5 < c ** 0.5 then "Yes" else "No")
  |> print_endline


let _ = Scanf.sscanf (read_line ()) "%f %f %f" (fun a b c ->
  if a +. b >= c then "No" else
  if ((sqrt a) +. (sqrt b)) *. (sqrt a) +. (sqrt b) < c then "Yes" else "No"
) |> print_endline


let _ = Scanf.sscanf (read_line ()) "%f %f %f" (fun a b c ->
  if (sqrt a) +. (sqrt b) < sqrt c then "Yes" else "No"
) |> print_endline


let _ = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c ->
  if a * a < c && b * b < c then "Yes" else "No"
) |> print_endline


let () = Scanf.scanf "%d %d %d" @@ fun a b c ->
  let d = c - a - b in
  print_endline @@
  if 0 <= d && 4 * a * b < d * d then "Yes" else "No"


let (n, m) = Scanf.sscanf (read_line ()) "%d %d" @@ fun n m -> (n, m)
let sc = Array.init m @@ fun _ -> Scanf.sscanf (read_line ()) "%d %d" @@ fun s c -> (s - 1, c)

let ans = Array.make n (-1)

let () =
  Array.iter (fun (s, c) ->
    if ans.(s) = -1 || ans.(s) = c then ans.(s) <- c
    else begin
      print_endline "-1";
      exit 0
    end
  ) sc;
  if n = 1 then
    Printf.printf "%d\n" @@ if ans.(0) = -1 then 0 else ans.(0)
  else begin
    if ans.(0) = 0 then begin
      print_endline "-1";
      exit 0
    end;
    for i = 0 to n - 1 do
      if ans.(i) = -1 then ans.(i) <- if i = 0 then 1 else 0
    done;
    Array.iter print_int ans;
    print_newline ()
  end

Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  if n = 0 then []
  else List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> Scanf.sscanf s "%d %d" Tuple.Tuple2.make)

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      r && match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; true
      | Some n -> n = c
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 when n <> 1 -> "-1"
            | _ -> if arr.(0) = None && n > 1 then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  if n = 0 then []
  else List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> Scanf.sscanf s "%d %d" Tuple.Tuple2.make)

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 -> "-1"
            | _ -> if arr.(0) = None && n > 1 then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  if n = 0 then []
  else List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> Scanf.sscanf s "%d %d" Tuple.Tuple2.make)

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            if n = 1 && m = 0 then "0"
            else
              match arr.(0) with
              | Some 0 -> "-1"
              | _ -> if arr.(0) = None then arr.(0) <- Some 1;
                Array.map (function | None -> "0" | Some c -> String.of_int c) arr
                |> Array.to_list
                |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  if n = 0 then []
  else List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> Scanf.sscanf s "%d %d" Tuple.Tuple2.make)

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 -> "-1"
            | _ -> if arr.(0) = None then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> Scanf.sscanf s "%d %d" Tuple.Tuple2.make)

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 -> "-1"
            | _ -> if arr.(0) = None then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> String.split_on_char ' ' s
                               |> List.map Int.of_string
                               |> (fun l -> (List.at l 0, List.at l 1)))

let () =
  printf "-1"
(* let arr = Array.make n None in
 * List.fold_left (fun r (s, c) ->
 *     match arr.(s-1) with
 *     | None -> arr.(s-1) <- Some c; r && true
 *     | Some n -> r && if n = c then true else false
 *   ) true l
 * |> (fun ans -> printf "%s\n"
 *        (if ans then
 *           match arr.(0) with
 *           | Some 0 -> "-1"
 *           | _ -> if arr.(0) = None then arr.(0) <- Some 1;
 *             Array.map (function | None -> "0" | Some c -> String.of_int c) arr
 *             |> Array.to_list
 *             |> String.concat ""
 *         else "-1")) *)


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> String.split_on_char ' ' s
                               |> List.map Int.of_string
                               |> (fun l -> (List.at l 0, List.at l 1)))

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None -> arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 -> "-1"
            | _ -> if arr.(0) = None then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> String.split_on_char ' ' s |> List.map Int.of_string |> (fun l -> (List.hd l, List.last l)))

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None ->
        arr.(s-1) <- Some c; r && true
      | Some n -> r && if n = c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            match arr.(0) with
            | Some 0 ->  "-1"
            | _ -> if arr.(0) = None then arr.(0) <- Some 1;
              Array.map (function | None -> "0" | Some c -> String.of_int c) arr
              |> Array.to_list
              |> String.concat ""
          else "-1"))


open Batteries
open BatPrintf

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

let scan_list cnv =
  read_line ()
  |> String.split_on_char ' '
  |> List.map cnv

let scan_listn n cnv =
  List.range 1 `To n |> List.map (fun _ -> (cnv % read_line) ())

let bsearch_ge arr a =
  let l = Array.length arr in
  match Array.bsearch Int.ord arr a with
  | `All_lower -> l
  | `All_bigger -> 0
  | `Just_after n -> l - n -1
  | `At n -> l - n
  | `Empty -> 0

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let square x = x * x

let dbg n = Printf.eprintf "%s\n" @@ dump n; n

let (n, m) = scan "%d %d" Tuple.Tuple2.make
let l = scan_listn m (fun s -> String.split_on_char ' ' s |> List.map Int.of_string |> (fun l -> (List.hd l, List.last l)))

let () =
  let arr = Array.make n None in
  List.fold_left (fun r (s, c) ->
      match arr.(s-1) with
      | None ->
        arr.(s-1) <- Some c;
        r && true
      | Some n ->
        r && if n == c then true else false
    ) true l
  |> (fun ans -> printf "%s\n"
         (if ans then
            if arr.(0) = Some 0 then "-1"
            else
              begin
                if arr.(0) = None then arr.(0) <- Some 1;
                Array.map (function | None -> "0" | Some c -> String.of_int c) arr
                |> Array.to_list
                |>  String.concat ""
              end
          else "-1"))


let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let ds = Array.make n None in
  try
    for i = 0 to m - 1 do
      Scanf.scanf "%d %d\n" @@ fun s c ->
        match ds.(s - 1) with
        | None ->
            if n <> 1 && s = 1 && c = 0 then raise Not_found;
            ds.(s - 1) <- Some c
        | Some c' -> if c <> c' then raise Not_found
    done;
    Array.iteri (fun i -> function
      | None -> print_string @@ if i = 0 && n <> 1 then "1" else "0"
      | Some d -> print_int d) ds;
    print_newline ()
  with Not_found -> print_endline "-1"


let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let ds = Array.make n None in
  try
    for i = 0 to m - 1 do
      Scanf.scanf "%d %d\n" @@ fun s c ->
        match ds.(s - 1) with
        | None -> ds.(s - 1) <- Some c
        | Some c' -> if c <> c' then raise Not_found
    done;
    Array.iteri (fun i -> function
      | None ->
          print_string @@ if i = 0 then "1" else "0"
      | Some d ->
          if i = 0 && d = 0 then raise Not_found;
          print_int d) ds;
    print_newline ()
  with Not_found -> print_endline "-1"


Scanf.scanf "%d %d" (fun n m ->
    let state = Array.make n 10 in
    let sc = Array.init m (fun _ -> Scanf.scanf " %d %d" (fun s c -> s, c)) in
    let rec loop i =
        if i < 0 then (
            match n with
            | 1 -> if state.(0) = 10 then 0 else
                   if state.(0) = -1 then -1 else state.(0)
            | 2 -> if state.(0) = -1 || state.(1) = -1 || state.(0) = 0 then (-1) else (
                if state.(0) = 10 then state.(0) <- 1;
                if state.(1) = 10 then state.(1) <- 0;
                state.(0) * 10 + state.(1)
            )
            | 3 -> if state.(0) = -1 || state.(1) = -1 || state.(2) = -1 || state.(0) = 0 then (-1) else (
                if state.(0) = 10 then state.(0) <- 1;
                if state.(1) = 10 then state.(1) <- 0;
                if state.(2) = 10 then state.(2) <- 0;
                state.(0) * 100 + state.(1) * 10 + state.(2)
            )
            | _ -> failwith "??"
        ) else (
            let s, c = sc.(i) in
            let s = s - 1 in
            let c = if state.(s) = 10 || state.(s) = c then c else (-1) in
            let () = state.(s) <- c in
            loop (i - 1)
        )
    in
    loop (m - 1) |> Printf.printf "%d\n"
)


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 rec lower_bound l r p =
  if r <= 1 + l
  then r
  else let m = (l + r) / 2 in
       if p m
       then lower_bound l m p
       else lower_bound m r p

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Array.sort compare as_;
  Printf.printf "%d\n" @@ succ @@ upper_bound (-1100000000000000000) 1100000000000000000 @@ fun p ->
    let somme = Array.fold_left ( + ) 0 @@ Array.init n @@ fun i ->
      if 0 <= as_.(i) then
        upper_bound 0 (i + 1) @@ function
          | 0 -> true
          | n -> as_.(i) * as_.(n - 1) <= p
      else
        ( - ) i @@ lower_bound (-1) i @@ fun n ->
          if n = i
          then true
          else as_.(i) * as_.(n) <= p in
    somme < k

Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

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

1 \leq K \leq \frac{N(N-1)}{2}

-10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


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\n" @@ fun n k ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Array.sort compare as_;
  Printf.printf "%d\n" @@ succ @@ upper_bound (-1500000000000000000) 1500000000000000000 @@ fun p ->
    let somme = Array.fold_left ( + ) 0 @@ Array.init n @@ fun i ->
      upper_bound 0 (i + 1) @@ function
        | 0 -> true
        | n -> as_.(i) * as_.(n - 1) <= p in
    somme < k


let kuku a b =
  if a >= 10 || b >= 10 then -1 else a * b

let a, b = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> (a, b))
let () = Printf.printf "%d\n" (kuku a b)

Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


let () = Scanf.scanf "%d %d" @@ fun a b ->
  let array = Array.make 100 false in
  for i=1 to 9 do
    for j=1 to 9 do
      array.(i*j) <- true
    done
  done;
  Printf.printf "%d\n" @@
  if a <= 9 && b <= 9
  then a*b
  else -1


(* Vicfred
 * https://atcoder.jp/contests/abc144/tasks/abc144_a
 * implementation
 * *)
Scanf.scanf "%d %d" (fun a b ->
    if a <= 9 && b <= 9 then a*b
    else -1
) |> Printf.printf "%d\n"


let a, b = Scanf.scanf "%d %d\n" @@ fun a b -> a, b
let () = print_int (if a < 10 && b < 10 then a * b else -1)


let _ = Scanf.sscanf (read_line ()) "%d %d" (fun a b ->
  match a with
  | a when a > 9 -> print_endline "-1"
  | _ ->
    match b with
    | b when b > 9 -> print_endline "-1"
    | _ -> print_endline (string_of_int (a * b))
)


open Printf
open Scanf

let solve a b = if a <= 9 && b <= 9 then a * b else -1

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


open Scanf ;;
open Printf ;;

let () =
  let a, b = scanf "%d %d" (fun x y -> x, y) in
  if a <= 9 && b <= 9 then
    printf "%d\n" (a * b)
  else
    print_endline "-1"
  ;;


let () =
  let (a, b) = Scanf.scanf "%d %d" (fun x y -> (x, y)) in
  if (a > 9 || b > 9)
    then print_int(-1)
    else print_int(a * b)


let scan_int () = Scanf.scanf " %d" (fun x -> x)
let print_int n = Printf.printf "%d\n" n

let () =
  let a = scan_int() and b = scan_int() in
  if a < 10 && b < 10 then print_int (a*b)
  else print_int (-1)


let a, b = Scanf.scanf " %d %d" @@ fun a b -> a, b
let _ = Printf.printf "%d\n" @@ if a <= 9 && b <= 9 then a * b else -1


let () = Scanf.scanf "%d %d" @@ fun a b ->
  Printf.printf "%d\n" @@
  if 1 <= a && a <= 9 && 1 <= b && b <= 9 then a * b else -1


let () = Scanf.scanf "%d %d\n" @@ fun a b ->
    Printf.printf "%d" @@ if a < 10 && b < 10 then a * b else -1


Scanf.sscanf (read_line ()) "%d %d" (fun a b -> let r = if a <= 9 && b <= 9 then a * b else (-1) in Printf.printf "%d\n" r)


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string |> Array.of_list
let k = Scanf.sscanf (read_line ()) "%d" @@ fun k -> k

let s_len = Array.length s

let f () =
  let cnt = Array.of_list @@ fst @@ Array.fold_left (fun (acc, before) ch ->
    match acc with
      | [] -> ([1], ch)
      | x :: xs -> ((if before = ch then ((x + 1) :: xs) else 1 :: acc), ch)
  ) ([], "") s in
  if Array.length cnt = 1 then s_len * k / 2
  else
  (cnt |> Array.map (fun i -> i / 2) |> Array.fold_left (+) 0) * k -
    if s.(0) = s.(s_len - 1) then let a, b = cnt.(0), cnt.(Array.length cnt - 1) in (a / 2 + b / 2 - (a + b) / 2) * (k - 1)
    else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.(0) = s.(1) then k else 0
    | _ -> f ()

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


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

let s_len = String.length s

let f () =
  let change = Array.make s_len 0 in
  for i = 1 to s_len - 1 do
    if s.[i] = s.[i - 1] && change.(i - 1) = 0 then change.(i) <- 1
  done;
  Array.fold_left (+) 0 change * k + if s.[0] = s.[s_len - 1]  && change.(s_len - 1) = 0 then k - 1  else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> s_len * k / 2
    | _ -> f ()


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

let s_len = String.length s

let f () =
  let change = Array.make s_len 0 in
  for i = 1 to s_len - 1 do
    if s.[i] = s.[i - 1] && change.(i - 1) = 0 then change.(i) <- 1
  done;
  Array.fold_left (+) 0 change * k + if s.[0] = s.[s_len - 1]  && change.(s_len - 1) = 0 then k - 1  else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.[0] = s.[1] then k else 0
    | _ -> f ()


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string |> Array.of_list
let k = Scanf.sscanf (read_line ()) "%d" @@ fun k -> k

let s_len = Array.length s

let f =
  let cnt = Array.of_list @@ fst @@ Array.fold_left (fun (acc, before) ch ->
    match acc with
      | [] -> ([1], ch)
      | x :: xs -> ((if before = ch then ((x + 1) :: xs) else 1 :: acc), ch)
  ) ([], "") s in
  if Array.length cnt = 1 then s_len * k / 2
  else
  (cnt |> Array.map (fun i -> i / 2) |> Array.fold_left (+) 0) * k -
    if s.(0) = s.(s_len - 1) then let a, b = cnt.(0), cnt.(Array.length cnt - 1) in (a / 2 + b / 2 - (a + b) / 2) * (k - 1)
    else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.(0) = s.(1) then k else 0
    | _ -> f


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string |> Array.of_list
let k = Scanf.sscanf (read_line ()) "%d" @@ fun k -> k

let s_len = Array.length s

let f =
  let cnt = Array.of_list @@ fst @@ Array.fold_left (fun (acc, before) ch ->
    match acc with
      | [] -> ([1], ch)
      | x :: xs -> ((if before = ch then ((x + 1) :: xs) else 1 :: acc), ch)
  ) ([], "") s in
  if Array.length cnt = 1 then s_len * k / 2
  else
  (cnt |> Array.map (fun i -> i / 2) |> Array.fold_left (+) 0) * k -
    if s.(0) = s.(s_len - 1) then let a, b = cnt.(0), cnt.(Array.length cnt - 1) in a / 2 + b / 2 - (a + b) / 2
    else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.(0) = s.(1) then k else 0
    | _ -> f


let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string |> Array.of_list
let k = Scanf.sscanf (read_line ()) "%d" @@ fun k -> k

let s_len = Array.length s

let f =
  let cnt = Array.of_list @@ fst @@ Array.fold_left (fun (acc, before) ch ->
    match acc with
      | [] -> ([1], ch)
      | x :: xs -> ((if before = ch then ((x + 1) :: xs) else 1 :: acc), ch)
  ) ([], "") s in
  (cnt |> Array.map (fun i -> i / 2) |> Array.fold_left (+) 0) * k -
    if s.(0) = s.(s_len - 1) then let a, b = cnt.(0), cnt.(Array.length cnt - 1) in a / 2 + b / 2 - (a + b) / 2
    else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.(0) = s.(1) then k else 0
    | _ -> f


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

let s_len = String.length s

let f () =
  let change = Array.make s_len 0 in
  for i = 1 to s_len - 1 do
    if s.[i] = s.[i - 1] && change.(i - 1) = 0 then change.(i) <- 1
  done;
  Array.fold_left (+) 0 change * k + if s.[0] = s.[s_len - 1]  && change.(s_len - 1) = 0 then k / 2 else 0

let () =
  Printf.printf "%d\n" @@ match s_len with
    | 1 -> k / 2
    | 2 -> if s.[0] = s.[1] then k else 0
    | _ -> f ()


Scanf.scanf "%s %d" (fun s k ->
    let n = String.length s in
    let check p s =
        let rec loop i p acc =
            if i = n then acc, p else
                if s.[i] = p then loop (i + 1) '@' (acc + 1) else
                                  loop (i + 1) s.[i] acc
        in
        loop 0 p 0
    in
    let a1, p = check '@' s in
    let a2, p = check p   s in
    let a3, _ = check p   s in
    let k2 = (k - 1) / 2 in
    let k1 = (k - 1) - k2 in
    Printf.printf "%d\n" @@ a1 + a2 * k1 + a3 * k2
)


Scanf.scanf "%s %d" (fun s k ->
    let n = String.length s in
    let check p s =
        let rec loop i p acc =
            if i = n then acc, p else
                if s.[i] = p then loop (i + 1) '@' (acc + 1) else
                                  loop (i + 1) s.[i] acc
        in
        loop 0 p 0
    in
    let a1, p = check '@' s in
    let a2, _ = check p   s in
    Printf.printf "%d\n" @@ a1 + a2 * (k - 1)
)


let () = Scanf.scanf "%s\n%d" @@ fun s k ->
  let s = Array.init (String.length s) (String.get s) in
  let a = Array.init 2 @@ fun i ->
    snd @@
    Array.fold_left (fun (c, (n, b)) c' ->
      (c', if b = 1 || c <> c' then (n, 0) else (n + 1, 1)))
    (s.(Array.length s - 1), (0, i)) s in
  Printf.printf "%d\n" @@
  if snd a.(1) = 1
  then k * fst a.(1)
  else if snd a.(0) = 0
  then fst a.(1) + (k - 1) * fst a.(0)
  else (k / 2) * (fst a.(0) + fst a.(1)) + (k mod 2) * fst a.(1)


module M = Map.Make (struct
  type t = int
  let compare = compare
end)
let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let im = ref M.empty
let g a = im := M.add a (1 + try M.find a !im with _ -> 0) !im
let h a = im := match M.find a !im with 1 -> M.remove a !im | c -> M.add a (c - 1) !im
let rec f i = if i >= m then M.fold (fun a c s -> s + a * c) !im 0
  else let a, _ = M.max_binding !im in g @@ a / 2; h a; f @@ i + 1
let _ = Array.iter g a_s; Printf.printf "%d\n" @@ f 0

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


let n, m = Scanf.scanf " %d %d" @@ fun a b -> a, b
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let rec f a n = if n = 0 then a else f ((n + 1) / 2 :: a) (n / 2)
let ds, s = Array.(fold_left f [] a_s |> List.sort (fun x y -> y - x) |> of_list, fold_left (+) 0 a_s)
let _ = Printf.printf "%d\n" @@ s - Array.(sub ds 0 (min m @@ length ds) |> fold_left (+) 0)


module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let add a s =
  IntMap.add a (1 + try IntMap.find a s with Not_found -> 0) s

let remove a s =
  match IntMap.find a s with
  | exception Not_found -> s
  | 1 -> IntMap.remove a s
  | n -> IntMap.add a (n - 1) s

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let rec solve s = function
    | 0 -> s
    | n ->
        let (a, _) = IntMap.max_binding s in
        solve (add (a / 2) (remove a s)) (n - 1) in
  Printf.printf "%d\n" @@
  IntMap.fold (fun a n acc -> acc + a * n)
    (solve (Array.fold_right add as_ IntMap.empty) m) 0


module M = Map.Make (
    struct
       type t = int * int
       let compare (a1, a2) (b1, b2) = compare (a1 lsr a2) (b1 lsr b2)
    end)
let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let add key v map = M.add key (v + try M.find key map with _ -> 0) map in
    let main () =
        let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
        let list = split (read_line ()) in
        let map = List.fold_left (fun map a -> add (a, 0) 1 map) M.empty list in
        let rec loop rest map =
            if rest = 0 then map else
                let ((a1, a2) as mk, mv) = M.max_binding map in
                if rest < mv then (
                    let map = M.remove mk map in
                    let map = add mk (mv - rest) map in
                    let map = add (a1, a2 + 1) rest map in
                    map
                ) else (
                    let map = M.remove mk map in
                    let map = add (a1, a2 + 1) mv map in
                    loop (rest - mv) map
                )
        in
        let map = loop m map in
        let r = M.fold (fun (a1, a2) v acc -> acc + v * (a1 lsr a2)) map 0 in
        Printf.printf "%d\n" r
    in
    main ()


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

let rec upper_bound l r p =
  if r - l <= 1
  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 rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let code c = Char.code c - Char.code 'a'

exception Exists

let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  Printf.printf "%d\n" @@
  upper_bound 0 (n / 2 + 1) @@ fun l ->
    Fun.flip List.for_all [1000000007; 1000000009] @@ fun modulo ->
      let ( +^ ) x y = (x + y) mod modulo in
      let ( -^ ) x y = x +^ (modulo - y) in
      let ( *^ ) x y = (x * y) mod modulo in
      let hash = Array.fold_left (fun h c -> 26 *^ h +^ code c) 0 in
      let radix = power ( *^ ) 1 26 l in
      let h = ref @@ hash @@ Array.init l @@ String.get s in
      let h' = ref @@ hash @@ Array.init l @@ fun i -> s.[i + l] in
      let set = ref @@ IntSet.singleton !h in
      try
        if IntSet.mem !h' !set then raise Exists;
        for i = 0 to n - 2 * l - 1 do
          h := 26 *^ !h +^ code s.[i + l] -^ radix *^ code s.[i];
          h' := 26 *^ !h' +^ code s.[i + 2 * l] -^ radix *^ code s.[i + l];
          set := IntSet.add !h !set;
          if IntSet.mem !h' !set then raise Exists;
        done; false
      with Exists -> true

Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

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

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


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

let rec upper_bound l r p =
  if r - l <= 1
  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 rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let modulo = 2305843009213693951
let ( +^ ) x y = (x + y) mod modulo
let ( -^ ) x y = x +^ (modulo - y)
let ( *^ ) x y = (x * y) mod modulo
let code c = Char.code c - Char.code 'a'

let hash = Array.fold_left (fun h c -> 26 *^ h +^ code c) 0
exception Exists

let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  Printf.printf "%d\n" @@
  upper_bound 0 (n / 2 + 1) @@ fun l ->
    let h = ref @@ hash @@ Array.init l @@ String.get s in
    let h' = ref @@ hash @@ Array.init l @@ fun i -> s.[i + l] in
    let set = ref @@ IntSet.singleton !h in
    try
      if IntSet.mem !h' !set then raise Exists;
      for i = 0 to n - 2 * l - 1 do
        h := 26 *^ !h +^ code s.[i + l] -^ power (power ( +^ ) 0) (code s.[i]) 26 l;
        h' := 26 *^ !h' +^ code s.[i + 2 * l] -^ power (power ( +^ ) 0) (code s.[i + l]) 26 l;
        set := IntSet.add !h !set;
        if IntSet.mem !h' !set then raise Exists;
      done; false
    with Exists -> true


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

let rec upper_bound l r p =
  if r - l <= 1
  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 rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let modulo = 2147483647
let ( +^ ) x y = (x + y) mod modulo
let ( -^ ) x y = x +^ (modulo - y)
let ( *^ ) x y = (x * y) mod modulo
let code c = Char.code c - Char.code 'a'

let hash = Array.fold_left (fun h c -> 26 *^ h +^ code c) 0
exception Exists

let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  Printf.printf "%d\n" @@
  upper_bound 0 (n / 2 + 1) @@ fun l ->
    let radix = power ( *^ ) 1 26 l in
    let h = ref @@ hash @@ Array.init l @@ String.get s in
    let h' = ref @@ hash @@ Array.init l @@ fun i -> s.[i + l] in
    let set = ref @@ IntSet.singleton !h in
    try
      if IntSet.mem !h' !set then raise Exists;
      for i = 0 to n - 2 * l - 1 do
        h := 26 *^ !h +^ code s.[i + l] -^ radix *^ code s.[i];
        h' := 26 *^ !h' +^ code s.[i + 2 * l] -^ radix *^ code s.[i + l];
        set := IntSet.add !h !set;
        if IntSet.mem !h' !set then raise Exists;
      done; false
    with Exists -> true


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

let rec upper_bound l r p =
  if r - l <= 1
  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 rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let modulo = 1000000007
let ( +^ ) x y = (x + y) mod modulo
let ( -^ ) x y = x +^ (modulo - y)
let ( *^ ) x y = (x * y) mod modulo
let code c = Char.code c - Char.code 'a'

let hash = Array.fold_left (fun h c -> 26 *^ h +^ code c) 0

exception Exists

let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  Printf.printf "%d\n" @@
  upper_bound 0 (n / 2 + 1) @@ fun l ->
    let radix = power ( *^ ) 1 26 l in
    let h = ref @@ hash @@ Array.init l @@ String.get s in
    let h' = ref @@ hash @@ Array.init l @@ fun i -> s.[i + l] in
    let set = ref @@ IntSet.singleton !h in
    try
      if IntSet.mem !h' !set then raise Exists;
      for i = 0 to n - 2 * l - 1 do
        h := 26 *^ !h +^ code s.[i + l] -^ radix *^ code s.[i];
        h' := 26 *^ !h' +^ code s.[i + 2 * l] -^ radix *^ code s.[i + l];
        set := IntSet.add !h !set;
        if IntSet.mem !h' !set then raise Exists;
      done; false
    with Exists -> true


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

let rec upper_bound l r p =
  if r - l <= 1
  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 rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let modulo = 999999937
let ( +^ ) x y = (x + y) mod modulo
let ( -^ ) x y = x +^ (modulo - y)
let ( *^ ) x y = (x * y) mod modulo
let code c = Char.code c - Char.code 'a'

let hash = Array.fold_left (fun h c -> 26 *^ h +^ code c) 0

exception Exists

let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  Printf.printf "%d\n" @@
  upper_bound 0 (n / 2 + 1) @@ fun l ->
    let radix = power ( *^ ) 1 26 l in
    let h = ref @@ hash @@ Array.init l @@ String.get s in
    let h' = ref @@ hash @@ Array.init l @@ fun i -> s.[i + l] in
    let set = ref @@ IntSet.singleton !h in
    try
      if IntSet.mem !h' !set then raise Exists;
      for i = 0 to n - 2 * l - 1 do
        h := 26 *^ !h +^ code s.[i + l] -^ radix *^ code s.[i];
        h' := 26 *^ !h' +^ code s.[i + 2 * l] -^ radix *^ code s.[i + l];
        set := IntSet.add !h !set;
        if IntSet.mem !h' !set then raise Exists;
      done; false
    with Exists -> true


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in
        let rec loop_i i acc =
            let rec loop_j j acc t =
                if j + i = n then max acc (min t i) else
                    loop_j (j + 1) acc (if s.[j] = s.[j + i] then t + 1 else 0)
            in
            if i > n - acc then acc else loop_i (i + 1) (loop_j 0 acc 0)
        in
        Printf.printf "%d\n" (loop_i 0 0)
    in
    main ()


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in

        let rec loop_l l acc =
            if l = n then acc else (
                let nn = n - l in
                let a = Array.make nn 0 in
                let rec loop_i i j =
                    if i < nn then (
                        let rec loop_j j =
                            if i + j = nn || s.[j + l] != s.[i + j + l] then j else loop_j (j + 1)
                        in
                        let j = loop_j j in
                        a.(i) <- j;
                        if j = 0 then loop_i (i + 1) j else
                            let rec loop_k k =
                                if i + k = nn || k + a.(k) >= j then k else
                                    let () = a.(i + k) <- a.(k) in
                                    loop_k (k + 1)
                            in
                            let k = loop_k 1 in
                            loop_i (i + k) (j - k)
                    )
                in
                loop_i 1 0;
                let rec loop i acc =
                    if i = nn then acc else loop (i + 1) (max acc (min i a.(i)))
                in
                let acc = loop 1 acc in
                loop_l (l + 1) acc
            )
        in
        let r = loop_l 0 0 in
        Printf.printf "%d\n" r
    in
    main ()


let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  let dp = Array.make_matrix (n + 1) (n + 1) 0 in
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      dp.(i + 1).(j + 1) <-
        if s.[i] = s.[j]
        then 1 + dp.(i).(j)
        else 0
    done
  done;
  Printf.printf "%d\n" @@
  Array.fold_left (Array.fold_left max) min_int @@
  Array.init (n + 1) @@ fun i ->
    Array.init (n + 1) @@ fun j ->
      min dp.(i).(j) @@ abs @@ i - j


let () = Scanf.scanf "%d\n%s" @@ fun n s ->
  let dp = Array.make_matrix (n + 1) (n + 1) 0 in
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      if s.[i] = s.[j] && dp.(i).(j) < j - i then
        dp.(i + 1).(j + 1) <- 1 + dp.(i).(j)
    done
  done;
  Printf.printf "%d\n" @@
  Array.fold_left (Array.fold_left max) min_int dp


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in
        let rec loop_i i acc =
            let rec loop_j j acc =
                let rec loop_k k =
                    if j + k = n then k else
                        if i + k = j then k else
                        if s.[i + k] <> s.[j + k] then k else
                            loop_k (k + 1)
                in
                if j = n then acc else loop_j (j + 1) (max acc (loop_k 0))
            in
            if i = n - 1 then acc else loop_i (i + 1) (loop_j (i + 1) acc)
        in
        let r = loop_i 0 0 in
        Printf.printf "%d\n" r
    in
    main ()


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in
        let compare_sa rank k i j =
            let c = compare rank.(i) rank.(j) in
            if c <> 0 then c else
                let ri = if i + k <= n then rank.(i + k) else (-1) in
                let rj = if j + k <= n then rank.(j + k) else (-1) in
                compare ri rj
        in
        let construct_sa s n =
            let sa   = Array.init (n + 1) (fun i -> i) in
            let rec loop_k rank k =
                if k > n then sa else (
                    Array.sort (compare_sa rank k) sa;
                    let tmp = Array.make (n + 1) 0 in
                    tmp.(sa.(0)) <- 0;
                    for i = 1 to n do
                        tmp.(sa.(i)) <- tmp.(sa.(i - 1)) + if compare_sa rank k sa.(i - 1) sa.(i) < 0 then 1 else 0
                    done;
                    loop_k tmp (k * 2)
                )
            in
            let rank = Array.init (n + 1) (fun i -> if i < n then int_of_char s.[i] else (-1)) in
            loop_k rank 1;
        in
        let sa = construct_sa s n in

        let construct_lcp s n sa =
            let rank = Array.make (n + 1) 0 in
            for i = 0 to n do rank.(sa.(i)) <- i done;
            let lcp = Array.make (n + 1) 0 in
            let rec loop_i i h =
                if i >= n then lcp else (
                    let j = sa.(rank.(i) - 1) in
                    let h = if h > 0 then h - 1 else h in
                    let rec shorten h =
                        if h = 0 then h else
                            if (i < j && i + h > j) then shorten (h - 1) else
                                if (j < i && j + h > i) then shorten (h - 1) else h
                    in
                    let rec loop_h h =
                        if j + h >= n || i + h >= n then h else (
                            if s.[j + h] <> s.[i + h] then h else
                                if (i < j && i + h >= j) || (j < i && j + h >= i) then shorten h else
                                loop_h (h + 1)
                        )
                    in
                    let h = loop_h h in
                    lcp.(rank.(i) - 1) <- h;
                    loop_i (i + 1) h
                )
            in
            loop_i 0 0
        in
        let lcp = construct_lcp s n sa in
        let r = Array.fold_left max 0 lcp in
        Printf.printf "%d\n" r
    in
    main ()


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\n%s" @@ fun n s ->
  let dp = Array.make_matrix (n + 1) (n + 1) 0 in
  let f k =
    for i = 0 to k - 1 do
      for j = 0 to n - k - 1 do
        dp.(i + 1).(j + 1) <-
          if s.[i] = s.[k + j]
          then 1 + dp.(i).(j)
          else max dp.(i + 1).(j) dp.(i).(j + 1)
      done
    done;
    dp.(k).(n - k) in
  Printf.printf "%d\n" @@
  f @@
  upper_bound 0 (n - 1) @@
  fun i -> f i <= f (i + 1)


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in
        let compare_sa rank k i j =
            let c = compare rank.(i) rank.(j) in
            if c <> 0 then c else
                let ri = if i + k <= n then rank.(i + k) else (-1) in
                let rj = if j + k <= n then rank.(j + k) else (-1) in
                compare ri rj
        in
        let construct_sa s n =
            let sa   = Array.init (n + 1) (fun i -> i) in
            let rec loop_k rank k =
                if k > n then sa else (
                    Array.sort (compare_sa rank k) sa;
                    let tmp = Array.make (n + 1) 0 in
                    tmp.(sa.(0)) <- 0;
                    for i = 1 to n do
                        tmp.(sa.(i)) <- tmp.(sa.(i - 1)) + if compare_sa rank k sa.(i - 1) sa.(i) < 0 then 1 else 0
                    done;
                    loop_k tmp (k * 2)
                )
            in
            let rank = Array.init (n + 1) (fun i -> if i < n then int_of_char s.[i] else (-1)) in
            loop_k rank 1;
        in
        let sa = construct_sa s n in

        let construct_lcp s n sa =
            let rank = Array.make (n + 1) 0 in
            for i = 0 to n do rank.(sa.(i)) <- i done;
            let lcp = Array.make (n + 1) 0 in
            let rec loop_i i h =
                if i >= n then lcp else (
                    let j = sa.(rank.(i) - 1) in
                    let h = if h > 0 then h - 1 else h in
                    let rec loop_h h =
                        if j + h >= n || i + h >= n then h else (
                            if s.[j + h] <> s.[i + h] then h else
                                if (i < j && i + h >= j) || (j < i && j + h >= i) then h else
                                loop_h (h + 1)
                        )
                    in
                    let h = loop_h h in
                    lcp.(rank.(i) - 1) <- h;
                    loop_i (i + 1) h
                )
            in
            loop_i 1 0
        in
        let lcp = construct_lcp s n sa in
        let r = Array.fold_left max 0 lcp in
        Printf.printf "%d\n" r
    in
    main ()


let () =
    let main () =
        let n = int_of_string (read_line ()) in
        let s = read_line () in
        let compare_sa rank k i j =
            let c = compare rank.(i) rank.(j) in
            if c <> 0 then c else
                let ri = if i + k <= n then rank.(i + k) else (-1) in
                let rj = if j + k <= n then rank.(j + k) else (-1) in
                compare ri rj
        in
        let construct_sa s n =
            let sa   = Array.init (n + 1) (fun i -> i) in
            let rec loop_k rank k =
                if k > n then sa else (
                    Array.sort (compare_sa rank k) sa;
                    let tmp = Array.make (n + 1) 0 in
                    tmp.(sa.(0)) <- 0;
                    for i = 1 to n do
                        tmp.(sa.(i)) <- tmp.(sa.(i - 1)) + if compare_sa rank k sa.(i - 1) sa.(i) < 0 then 1 else 0
                    done;
                    loop_k tmp (k * 2)
                )
            in
            let rank = Array.init (n + 1) (fun i -> if i < n then int_of_char s.[i] else (-1)) in
            loop_k rank 1;
        in
        let sa = construct_sa s n in

        let construct_lcp s n sa =
            let rank = Array.make (n + 1) 0 in
            for i = 0 to n do rank.(sa.(i)) <- i done;
            let lcp = Array.make (n + 1) 0 in
            let rec loop_i i h =
                if i = n then lcp else (
                    let j = sa.(rank.(i) - 1) in
                    let h = if h > 0 then h - 1 else h in
                    let rec loop_h h =
                        if j + h >= n || i + h >= n then h else (
                            if s.[j + h] <> s.[i + h] then h else
                                if (i < j && i + h >= j) || (j < i && j + h >= i) then h else
                                loop_h (h + 1)
                        )
                    in
                    let h = loop_h h in
                    lcp.(rank.(i) - 1) <- h;
                    loop_i (i + 1) h
                )
            in
            loop_i 0 0
        in
        let lcp = construct_lcp s n sa in
        let r = Array.fold_left max 0 lcp in
        Printf.printf "%d\n" r
    in
    main ()


let () = Scanf.scanf "%d %s" @@ fun a s ->
  Printf.printf "%s\n" @@ if a >= 3200 then s else "red"

Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

s is a string of length between 1 and 10 (inclusive).

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


let red_or_not a s =
  if a < 3200 then "red" else s

let a = read_int()
let s = read_line()
let () = Printf.printf "%s\n" (red_or_not a s)


(* Vicfred
 * https://atcoder.jp/contests/abc138/tasks/abc138_a
 * implementation
 * *)
Scanf.scanf "%d\n%s\n" (fun a s ->
    if a >= 3200 then s
    else "red"
) |> Printf.printf "%s\n"


let a = read_int ()
let s = read_line ()
let () = print_endline (if a < 3200 then "red" else s)


open Printf
open Scanf

let solve a s =
  if a >= 3200 then s else "red"

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


(* abc138 a problem *)
open Scanf;;

let () = Scanf.scanf "%d %s"
           (fun a s -> print_endline
                         (if a >= 3200 then s else "red"));;

exit 0;;


open Printf
open Scanf

let () =
  let a, s = scanf "%d %s" (fun x y -> x, y) in
  (if a >= 3200 then s else "red") |> print_endline


let a = read_int ()
let s = read_line ()
let _ = print_endline @@ if a >= 3200 then s else "red"


let () = Scanf.scanf "%d\n%s" @@ fun a s ->
  print_endline @@
  if 3200 <= a then s else "red"


let () = Scanf.scanf "%d\n%s" @@ fun a s ->
  print_endline @@
  if 3200 <= a then "red" else s