let print_list l =
  List.map string_of_int l |> String.concat " " |> print_endline;;

let insertion_sort a n g count=
  let ct = (ref count) in
  for i = g to n-1 do
    let v = a.(i) in
    let rec loop j =
      if j < 0 || a.(j) <= v then (j+g)
      else
        begin
          a.(j+g) <- a.(j);incr ct;loop (j-g)
        end
    in a.(loop (i-g)) <-v;
  done;
  !ct
;;

let () =
  let n = read_int () in
  let n' = float_of_int n in
  let rec gen g b =
    let b2 = b *. 2.25 +. 1. in
    if b2 > n' then g
    else gen ((int_of_float (ceil b2))::g) b2
  in
  let a = Array.init n (fun x -> read_int ()) and
      g = gen [] 0. in
  let m = List.length g in
  let rec loop ct = function
      [] -> print_int m;print_newline ();
            print_list g;
            print_int ct;print_newline ();
            Array.iter (fun x -> print_int x;print_newline ()) a
    | h::t -> loop (insertion_sort a n h ct) t
  in loop 0 g
;;

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

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Shell Sort

Shell Sort is a generalization of Insertion Sort to arrange a list of $n$ elements $A$.

1 insertionSort(A, n, g)
2 for i = g to n-1
3 v = A[i]
4 j = i - g
5 while j >= 0 && A[j] > v
6 A[j+g] = A[j]
7 j = j - g
8 cnt++
9 A[j+g] = v
10
11 shellSort(A, n)
12 cnt = 0
13 m = ?
14 G[] = {?, ?,..., ?}
15 for i = 0 to m-1
16 insertionSort(A, n, G[i])

A function shellSort(A, n) performs a function insertionSort(A, n, g), which considers every $g$-th elements. Beginning with large values of $g$, it repeats the insertion sort with smaller $g$.

Your task is to complete the above program by filling ?. Write a program which reads an integer $n$ and a sequence $A$, and prints $m$, $G_i (i = 0, 1, ..., m − 1)$ in the pseudo code and the sequence $A$ in ascending order. The output of your program must meet the following requirements:

$1 \leq m \leq 100$

$0 \leq G_i \leq n$

cnt does not exceed $\lceil n^{1.5}\rceil$

Input

In the first line, an integer $n$ is given. In the following $n$ lines, $A_i (i=0,1,...,n-1)$ are given for each line.

Output

In the first line, print an integer $m$. In the second line, print $m$ integers $G_i (i=0,1,...,m-1)$ separated by single space character in a line.

In the third line, print cnt in a line. In the following $n$ lines, print $A_i (i=0,1,...,n-1)$ respectively.

This problem has multiple solutions and the judge will be performed by a special validator.

Constraints

$1 \leq n \leq 1,000,000$

$0 \leq A_i \leq 10^9$

Sample Input 1

5
5
1
4
3
2

Sample Output 1

2
4 1
3
1
2
3
4
5

Sample Input 2

3
3
2
1

Sample Output 2

1
1
3
1
2
3

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 cnt = ref 0
let g = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array a = Array.iter (fun n -> Printf.printf "%d\n" n) a

let insertion_sort a n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i >= n then ()
    else (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i lst =
    if i > n then lst
    else doit (3*i + 1) (i :: lst)
  in
  doit 1 []

let shell_sort a n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i >= n then ()
    else (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let cnt = ref 0
let g = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array a = Array.iter (fun n -> Printf.printf "%d\n" n) a

let insertion_sort a n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i >= n then ()
    else (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let rec power m = function
  | 0 -> 1
  | n when n mod 2 = 0 -> power (m * m) (n / 2)
  | n -> m * power m (n - 1)

let generate_g n =
  let rec doit i lst =
    if lst <> [] && List.hd lst > n then List.tl lst
    else doit (i + 1) ((power 4 i + 3 * power 2 (i - 1) + 1) :: lst)
  in
  doit 1 [1]

let shell_sort a n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i >= n then ()
    else (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let cnt = ref 0
let g = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array = Array.iter (fun n -> Printf.printf "%d\n" n)

let insertion_sort a n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i >= n then ()
    else (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i lst =
    if i > n then lst
    else doit (3*i + 1) (i :: lst)
  in
  doit 1 []

let shell_sort a n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i >= n then ()
    else (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let cnt = ref 0
let g = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array = Array.iter (fun n -> Printf.printf "%d\n" n)

let insertion_sort a n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i >= n then ()
    else (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i g lst =
    if g > n then lst
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: lst)
  in
  doit 1 1 []

let shell_sort a n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i >= n then ()
    else (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let cnt = ref 0
let g = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array = Array.iter (fun n -> Printf.printf "%d\n" n)

let insertion_sort a n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i < n then (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i g lst =
    if g > n then lst
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: lst)
  in
  doit 1 1 []

let shell_sort a n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i < n then (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let print_list l =
  List.map string_of_int l |> String.concat " " |> print_endline;;

let () =
  let ct = ref 0 in
  let insertion_sort a n g =
    for i = g to n-1 do
      let v = a.(i) in
      let rec loop j =
        if j < 0 || a.(j) <= v then j
        else begin a.(j+g) <- a.(j);incr ct;loop (j-g) end
      in
      let j = loop (i - g) in
      a.(j+g) <- v;
    done;
  in
  let n = read_int () in
  let rec gen g b =
    let b2 = b*.2.25+.1. in
    if b2 > (float_of_int n) then g
    else gen ((int_of_float (ceil b2))::g) b2
  in
  let a = Array.init n (fun x -> read_int ()) and
      g = gen [] 0. in
  let m = List.length g in
  let rec loop = function
      [] -> print_int m;print_newline ();
            print_list g;
            print_int !ct;print_newline ();
            Array.iter (fun x -> print_int x;print_newline ()) a
    | h::t ->insertion_sort a n h;loop t
  in loop g
;;


let print_list l =
  List.map string_of_int l |> String.concat " " |> print_endline;;

let () =
  let ct = ref 0 in
  let insertion_sort a n g =
    for i = g to n-1 do
      let v = a.(i) in
      let rec loop j =
        if j < 0 || a.(j) <= v then a.(j+g) <-v
        else begin a.(j+g) <- a.(j);incr ct;loop (j-g) end
      in loop (i - g)
    done;
  in
  let n = read_int () in
  let rec gen g b =
    let b2 = ceil ((float_of_int b)*.2.25+.1.) |> int_of_float in
    if b2 > n then g
    else gen (b2::g) b2
  in
  let a = Array.init n (fun x -> read_int ()) and
      g = gen [] 0 in
  let m = List.length g in
  let rec loop = function
      [] -> print_int m;print_newline ();
            print_list g;
            print_int !ct;print_newline ();
            Array.iter (fun x -> print_int x;print_newline ()) a
    | h::t ->insertion_sort a n h;loop t
  in loop g
;;


let print_list l =
  List.map string_of_int l |> String.concat " " |> print_endline;;

let insertion_sort a n g count=
  let ct = (ref count) in
  for i = g to n-1 do
    let v = a.(i) in
    let rec loop j =
      if j < 0 || a.(j) <= v then (j+g)
      else
        begin
          a.(j+g) <- a.(j);incr ct;loop (j-g)
        end
    in a.(loop (i-g)) <-v;
  done;
  !ct
;;

let () =
  let n = read_int () in
  let n' = float_of_int n in
  let rec gen g b =
    let b2 = b *. 2.25 +. 1. in
    if b2 > n' then g
    else gen ((int_of_float (ceil b2))::g) b2
  in
  let a = Array.init n (fun x -> read_int ()) and
      g = gen [] 0. in
  let m = List.length g in
  let rec loop ct = function
      [] -> print_int m;print_newline ();
            print_list g;
            print_int ct;print_newline ();
            Array.iter (fun x -> Printf.printf "%d\n" x) a
    | h::t -> loop (insertion_sort a n h ct) t
  in loop 0 g
;;


let cnt = ref 0
let (g : int list ref) = ref []

let print_list l =
  List.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) l;
  print_newline ()

let print_array = Array.iter (fun n -> Printf.printf "%d\n" n)

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i < n then (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i g lst =
    if g > n then lst
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: lst)
  in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i < n then (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  print_list !g;
  Printf.printf "%d\n" !cnt;
  print_array a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i < n then (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i g lst =
    if g > n then lst
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: lst)
  in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i < n then (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j + g) <- v
    else (a.(j + g) <- a.(j); incr cnt; insert (j - g) v)
  in
  let rec doit i =
    if i < n then (insert (i - g) a.(i); doit (i + 1))
  in
  doit g

let generate_g n =
  let rec doit i g lst =
    if g > n then lst
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: lst)
  in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in
  doit !g

let () =
  let n = read_int () in
  let a = Array.make n 0 in
  let rec doit i =
    if i < n then (a.(i) <- read_int (); doit (i + 1))
  in
  doit 0;
  shell_sort a n;
  print_int (List.length !g);
  print_newline ();
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  print_int !cnt;
  print_newline ();
  Array.iter (fun n -> print_int n; print_newline ()) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j+g) <- v
    else (a.(j+g) <- a.(j); incr cnt; insert (j - g) v)
  and doit i =
    if i < n then (insert (i - g) a.(i); doit (i + 1))
  in doit g

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc)
  in doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl -> (insertion_sort a n hd; doit tl)
  in doit !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j+g) <- v
    else begin
      a.(j+g) <- a.(j);
      incr cnt;
      insert (j - g) v
    end in
  let rec doit i =
    if i < n then begin
      insert (i - g) a.(i);
      doit (i + 1)
    end in
  doit g

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  let rec doit = function
    | [] -> ()
    | hd :: tl ->
      insertion_sort a n hd;
      doit tl in
  doit !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort n ?(gap=1) a =
  for i=gap to n-1 do
    let v = a.(i) in
    let j = ref (i - gap) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+gap) <- a.(!j);
      j := !j - gap;
      incr cnt;
    done;
    a.(!j+gap) <- v
  done;
  a

let shell_sort array n =
  let rec gen_gaps = function
      [] -> gen_gaps [1]
    | h::t when h > n -> t
    | h::_ as l -> gen_gaps ((3*h + 1)::l) in
  let gaps = gen_gaps [] in
  let m = List.length gaps in
  let rec loop a = function
      [] -> a
    | h::t -> loop (insertion_sort n ~gap:h a) t in
  let a = loop array gaps in
  Printf.printf "%d\n" m;
  Array.iteri
    (fun i x -> Printf.printf (if i=m-1 then "%d\n" else "%d ") x)
    (Array.of_list gaps);
  Printf.printf "%d\n" !cnt;
  a

let () =
  let n = read_int () in
  let rec read a = function
      i when i = n -> a
    | i -> a.(i) <- (read_int ());
      read a (i+1) in
  let a = read (Array.make n 0) 0 in
  Array.iter (Printf.printf "%d\n") (shell_sort a n)


let cnt = ref 0

let insertion_sort n ?(gap=1) a =
  for i=gap to n-1 do
    let v = a.(i) in
    let j = ref (i - gap) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+gap) <- a.(!j);
      j := !j - gap;
      incr cnt;
    done;
    a.(!j+gap) <- v
  done;
  a

let shell_sort array n =
  let rec gen_gaps x = function
      [] -> gen_gaps x [1]
    | h::t when h > n -> t
    | _::_ as l -> let a,b = x.(0), x.(1) in
      x.(0) <- a*4; x.(1) <- b*2;
      gen_gaps x ((a+b+1)::l) in
  let gaps = gen_gaps [|4;3|] [] in
  let m = List.length gaps in
  let rec loop a = function
      [] -> a
    | h::t -> loop (insertion_sort n ~gap:h a) t in
  let a = loop array gaps in
  Printf.printf "%d\n" m;
  Array.iteri
    (fun i x -> Printf.printf (if i=m-1 then "%d\n" else "%d ") x)
    (Array.of_list gaps);
  Printf.printf "%d\n" !cnt;
  a

let () =
  let n = read_int () in
  let rec read a = function
      i when i = n -> a
    | i -> a.(i) <- (read_int ());
      read a (i+1) in
  let a = read (Array.make n 0) 0 in
  Array.iter (Printf.printf "%d\n") (shell_sort a n)


let cnt = ref 0

let insertion_sort (n:int) ?(gap=1) (a:int array) =
  for i=gap to n-1 do
    let v = a.(i) in
    let j = ref (i - gap) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+gap) <- a.(!j);
      j := !j - gap;
      incr cnt;
    done;
    a.(!j+gap) <- v
  done

let shell_sort (array:int array) (n: int) =
  let rec gen_gaps = function
      [] -> gen_gaps [1]
    | h::t when h > n -> t
    | h::_ as l -> gen_gaps ((float_of_int h |> ( *. ) 2.25 |> int_of_float) ::l) in
  let gaps = (if n > 701 then gen_gaps [701;301;132;57;23;10;4;1]
              else List.filter ((>=) n) [701;301;132;57;23;10;4;1])
             |> Array.of_list in
  let m = Array.length gaps in
  for i=0 to m-1 do
    insertion_sort n ~gap:gaps.(i) array;
  done;
  Printf.printf "%d\n" m;
  Array.iteri
    (fun i x -> Printf.printf (if i=m-1 then "%d\n" else "%d ") x)
    gaps;
  Printf.printf "%d\n" !cnt;
  array

let () =
  let n = read_int () in
  let rec read a = function
      i when i = n -> a
    | i -> a.(i) <- (read_int ());
      read a (i+1) in
  let a = read (Array.make n 0) 0 in
  Array.iter (Printf.printf "%d\n") (shell_sort a n)


let cnt = ref 0

let insertion_sort (n:int) ?(gap=1) (a:int array) =
  for i=gap to n-1 do
    let v = a.(i) in
    let j = ref (i - gap) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+gap) <- a.(!j);
      j := !j - gap;
      incr cnt;
    done;
    a.(!j+gap) <- v
  done

let gen_gaps n =
  let rec aux = function
      [] -> aux [1]
    | h::t when h > n -> t
    | h::_ as l -> aux ((float_of_int h |> ( *. ) 2.25 |> int_of_float) ::l) in
  (if n > 701 then aux [701;301;132;57;23;10;4;1]
   else List.filter ((>=) n) [701;301;132;57;23;10;4;1])
  |> Array.of_list

let shell_sort (array:int array) (n: int) =
  let g = gen_gaps n in
  let m = Array.length g in
  for i=0 to m-1 do
    insertion_sort n ~gap:g.(i) array;
  done;
  Printf.printf "%d\n" m;
  Array.iteri
    (fun i x -> Printf.printf (if i=m-1 then "%d\n" else "%d ") x)
    g;
  Printf.printf "%d\n" !cnt;
  array

let () =
  let n = read_int () in
  let rec read a = function
      i when i = n -> a
    | i -> a.(i) <- (read_int ());
      read a (i+1) in
  let a = read (Array.make n 0) 0 in
  Array.iter (Printf.printf "%d\n") (shell_sort a n)


let (cnt: int ref) = ref 0

let insertion_sort (n:int) (gap:int) (a:int array) =
  for i=gap to n-1 do
    let (v:int) = a.(i) in
    let (j:int ref) = ref (i - gap) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+gap) <- a.(!j);
      j := !j - gap;
      incr cnt;
    done;
    a.(!j+gap) <- v
  done

let gen_gaps (n:int) =
  let rec aux (l:int list) = match l with
      [] -> aux [1]
    | h::t when h > n -> t
    | h::_ as l -> aux ((float_of_int h |> ( *. ) 2.25 |> int_of_float) ::l) in
  (if n > 701 then aux [701;301;132;57;23;10;4;1]
   else List.filter ((>=) n) [701;301;132;57;23;10;4;1])
  |> Array.of_list

let shell_sort (array:int array) (n: int) =
  let (g:int array) = gen_gaps n in
  let (m:int) = Array.length g in
  for i=0 to m-1 do
    insertion_sort n g.(i) array;
  done;
  Printf.printf "%d\n" m;
  Array.iteri
    (fun i x -> Printf.printf (if i=m-1 then "%d\n" else "%d ") x)
    g;
  Printf.printf "%d\n" !cnt;
  array

let () =
  let (n:int) = read_int () in
  let (a:int array) = (Array.make n 0) in
  for i=0 to n-1 do
    a.(i) <- (read_int ())
  done;
  Array.iter (Printf.printf "%d\n") (shell_sort a n)


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j+g) <- v
    else begin
      a.(j+g) <- a.(j);
      incr cnt;
      insert (j - g) v
    end in
  let rec doit i =
    if i < n then begin
      insert (i - g) a.(i);
      doit (i + 1)
    end in
  doit g

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  List.iter (fun g -> insertion_sort a n g) !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  List.iter (fun g -> insertion_sort a n g) !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  print_int (List.length !g); print_newline ();
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  print_int !cnt; print_newline ();
  Array.iter (fun n -> print_int n; print_newline ()) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  let rec insert j v =
    if j < 0 || a.(j) <= v then a.(j+g) <- v
    else begin
      a.(j+g) <- a.(j);
      incr cnt;
      insert (j - g) v
    end in
  let rec doit i =
    if i < n then begin
      insert (i - g) a.(i);
      doit (i + 1)
    end in
  doit g

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  List.iter (fun g -> insertion_sort a n g) !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  print_int (List.length !g); print_newline ();
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  print_int !cnt; print_newline ();
  Array.iter (fun n -> print_int n; print_newline ()) a


let cnt = ref 0
let (g : int list ref) = ref []

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  g := generate_g n;
  List.iter (fun g -> insertion_sort a n g) !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  Printf.printf "%d\n" (List.length !g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0
let (g : int list ref) = ref []
let n_g = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let new_g = ref 1 in
  incr n_g;
  while !new_g <= n do
    g := !new_g :: !g;
    new_g := int_of_float (4.**(float_of_int !n_g) +. 3.*.2.**(float_of_int (!n_g - 1)) +. 1.);
    incr n_g;
  done;
  decr n_g

let shell_sort (a : int array) n =
  cnt := 0;
  generate_g n;
  List.iter (fun g -> insertion_sort a n g) !g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  shell_sort a n;
  Printf.printf "%d\n" !n_g;
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) !g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  List.iteri (fun i n -> (if i = 0 then Printf.printf "%d" else Printf.printf " %d") n) g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  List.iteri (fun i n -> if i <> 0 then print_string " "; print_int n) g;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  for i = 0 to n - 1 do
    Printf.printf "%d\n" a.(i)
  done


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  match g with
  | [] -> assert false;
  | hd :: tl -> print_int hd; List.iter (fun x -> Printf.printf " %d" x) tl;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  print_int (List.length g);
  print_newline ();
  match g with
  | [] -> assert false;
  | hd :: tl -> print_int hd; List.iter (fun x -> print_string " "; print_int x) tl;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  print_int (List.length g);
  print_newline ();
  match g with
  | [] -> assert false;
  | hd :: tl -> print_int hd; List.iter (fun x -> print_string " "; print_int x) tl;
  print_newline ();
  print_int !cnt;
  print_newline ();
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  print_int (List.length g);
  print_newline ();
  match g with
  | [] -> assert false;
  | hd :: tl -> print_int hd; List.iter (fun x -> print_string " "; print_int x) tl;
  print_newline ();
  print_int !cnt;
  print_newline ();
  Array.iter (fun n -> print_int n; print_newline ()) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  match g with
  | [] -> assert false;
  | hd :: tl -> print_int hd; List.iter (fun x -> print_string " "; print_int x) tl;
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


let cnt = ref 0

let insertion_sort (a : int array) n g =
  for i = g to n - 1 do
    let v = a.(i) in
    let j = ref (i - g) in
    while !j >= 0 && a.(!j) > v do
      a.(!j+g) <- a.(!j);
      j := !j - g;
      incr cnt;
    done;
    a.(!j+g) <- v;
  done

let generate_g n =
  let rec doit i g acc =
    if g > n then acc
    else doit (i + 1) (int_of_float (4.**(float_of_int i) +. 3.*.2.**(float_of_int (i - 1)) +. 1.)) (g :: acc) in
  doit 1 1 []

let shell_sort (a : int array) n =
  cnt := 0;
  let g = generate_g n in
  List.iter (fun g -> insertion_sort a n g) g;
  g

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int ()) in
  let g = shell_sort a n in
  Printf.printf "%d\n" (List.length g);
  print_int (List.hd g);
  List.iter (fun x -> print_string " "; print_int x) (List.tl g);
  print_newline ();
  Printf.printf "%d\n" !cnt;
  Array.iter (fun n -> Printf.printf "%d\n" n) a


module SS = Set.Make(String);;

let _ =
  let n = read_int () and
      s = SS.empty
  in
  let rec read s = function
      0 -> ()
    | i ->
       let (op,key) =
         match read_line () |> Str.split (Str.regexp " ") with
           [h;t] -> (h,t)
         | _ -> ("","")
       in
       if op = "insert" then
         read (SS.add key s) (i-1)
       else if op = "find" then
         begin
           if SS.mem key s then print_endline "yes"
           else print_endline "no"
          ;read s (i-1)
         end
  in read s n;;

Search III

Your task is to write a program of a simple dictionary which implements the following instructions:

insert str: insert a string str in to the dictionary

find str: if the distionary contains str, then print 'yes', otherwise print 'no'

Input

In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format.

Output

Print yes or no for each find instruction in a line.

Constraints

A string consists of 'A', 'C', 'G', or 'T'

1 ≤ length of a string ≤ 12

n ≤ 1000000

Sample Input 1

5
insert A
insert T
insert C
find G
find A

Sample Output 1

no
yes

Sample Input 2

13
insert AAA
insert AAC
insert AGA
insert AGG
insert TTT
find AAA
find CCC
find CCC
insert CCC
find CCC
insert T
find TTT
find T

Sample Output 2

yes
no
no
yes
yes
yes

Notes

Template in C


module SS = Set.Make(String);;

let _ =
  let n = read_int () and
      s = SS.empty
  in
  let rec read s = function
      0 -> ()
    | i ->
       let line = read_line () |> Str.split (Str.regexp " ") |> Array.of_list in
       if line.(0) = "insert" then
         read (SS.add line.(1) s) (i-1)
       else if line.(0) = "find" then
         begin
           if SS.mem line.(1) s then print_endline "yes"
           else print_endline "no"
          ;read s (i-1)
         end
  in read s n;;


module SS = Set.Make(String);;

let _ =
  let n = read_int () and
      s = SS.empty
  in
  let rec read s = function
      0 -> ()
    | i ->
       let (op, key) = Scanf.scanf "%s %s\n" (fun x y -> (x,y)) in
       if op = "insert" then
         read (SS.add key s) (i-1)
       else if op = "find" then
         begin
           if SS.mem key s then print_endline "yes"
           else print_endline "no"
          ;read s (i-1)
         end
  in read s n;;


module SS = Set.Make(String)

let _ =
  let n = read_int () in
  let s = SS.empty in
  let rec read i acc =
    if i >= n then acc
    else begin
      match Str.split (Str.regexp_string " ") (read_line ()) with
      | [] | [_] -> exit 1
      | op :: x :: _ -> read (i + 1) ((op, x) :: acc)
    end
  in
  let cmds = read 0 [] in
  let doit (op, x) s =
    if op = "insert" then SS.add x s
    else if op = "find" then (print_endline (if SS.mem x s then "yes" else "no"); s)
    else exit 1
  in
  List.fold_right doit cmds s


module SS = Set.Make(String)

let () =
  let n = read_int () in
  let doit (op, x) s =
    if op = "insert" then SS.add x s
    else if op = "find" then (print_endline (if SS.mem x s then "yes" else "no"); s)
    else exit 1
  in
  let rec read i s =
    if i >= n then ()
    else begin
      match Str.split (Str.regexp_string " ") (read_line ()) with
      | [] | [_] -> exit 1
      | op :: x :: _ -> read (i + 1) (doit (op, x) s)
    end
  in
  read 0 (SS.empty)


let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true n in
  let doit (op, x) =
    if op = "insert" then Hashtbl.add tbl x true
    else if op = "find" then print_endline (if Hashtbl.mem tbl x then "yes" else "no")
    else exit 1
  in
  let rec read i =
    if i >= n then ()
    else begin
      match Str.split (Str.regexp_string " ") (read_line ()) with
      | [] | [_] -> exit 1
      | op :: x :: _ -> (doit (op, x); read (i + 1))
    end
  in
  read 0


let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true n in
  let rec read i =
    if i <= 0 then ()
    else begin
      match Scanf.scanf "%s %s\n" (fun x y -> (x, y)) with
      | ("insert", x) -> (Hashtbl.add tbl x true; read (i - 1))
      | ("find", x) -> (print_endline (if Hashtbl.mem tbl x then "yes" else "no"); read (i - 1))
      | _ -> exit 1
    end
  in
  read n


let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i >= n then ()
    else begin
      match Scanf.scanf "%s %s\n" (fun x y -> (x, y)) with
      | ("insert", x) -> (add tbl x true; doit (i + 1))
      | ("find", x) -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i < n then begin
      match Scanf.scanf "%s %s\n" (fun x y -> (x, y)) with
      | ("insert", x) -> (add tbl x true; doit (i + 1))
      | ("find", x) -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let () =
  let hash = Hashtbl.create 1000000 in
  let n = read_int ()
  in
  let rec read = function
      0 -> ()
    | i ->
       let (op, key) = Scanf.scanf "%s %s\n" (fun x y -> (x,y)) in
       if op = "insert" then
         Hashtbl.add hash key key
       else
         if Hashtbl.mem hash key then Printf.printf "%s\n" "yes"
         else Printf.printf "%s\n" "no"
       ;read (i-1)
  in read n;;


let to_num s =
  let sym_to_num = function
    | 'A' -> 0
    | 'C' -> 1
    | 'G' -> 2
    | 'T' -> 3
    | _ -> exit 1
  in
  let n = String.length s in
  let rec doit i acc =
    if i = n then acc
    else doit (i + 1) (4*acc + sym_to_num s.[i])
  in
  doit 0 0

let () =
  let tbl = Array.make (16777215 + 1) false in
  let n = read_int () in
  let rec doit i =
    if i < n then begin
      match Scanf.scanf "%s %s\n" (fun x y -> x, y) with
      | ("insert", x) -> (tbl.(to_num x) <- true; doit (i + 1))
      | ("find", x) -> (print_endline (if tbl.(to_num x) then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let to_num s =
  let sym_to_num = function
    | 'A' -> 1
    | 'C' -> 2
    | 'G' -> 3
    | 'T' -> 4
    | _ -> exit 1
  in
  let n = String.length s in
  let rec doit i acc =
    if i = n then acc
    else doit (i + 1) (4*acc + sym_to_num s.[i])
  in
  doit 0 0

let () =
  let tbl = Array.make (22369620 + 1) false in
  let n = read_int () in
  let rec doit i =
    if i < n then begin
      match Scanf.scanf "%s %s\n" (fun x y -> x, y) with
      | ("insert", x) -> (tbl.(to_num x) <- true; doit (i + 1))
      | ("find", x) -> (print_endline (if tbl.(to_num x) then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i < n then begin
      match Scanf.scanf "%s %s\n" (fun x y -> x, y) with
      | ("insert", x) -> (add tbl x true; doit (i + 1))
      | ("find", x) -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let split str delim =
  let rec doit s acc =
    match
      try Some (String.rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i ->
        let s1 = String.sub s 0 i in
        let s2 = String.sub s (i + 1) (String.length s - String.length s1 - 1) in
        doit s1 (s2 :: acc)
  in
  doit str []

let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i < n then begin
      match split (read_line ()) ' ' with
      | ["insert"; x] -> (add tbl x true; doit (i + 1))
      | ["find"; x] -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let split str delim =
  let rec doit s acc =
    match
      try Some (String.rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i ->
        let s1 = String.sub s 0 i in
        let s2 = String.sub s (i + 1) (String.length s - String.length s1 - 1) in
        doit s1 (s2 :: acc)
  in
  doit str []

let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create n in
  let rec doit i =
    if i < n then begin
      match split (read_line ()) ' ' with
      | ["insert"; x] -> (add tbl x true; doit (i + 1))
      | ["find"; x] -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
    end
  in
  doit 0


let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc)
  in doit str []

let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i < n then
      match split (read_line ()) ' ' with
      | ["insert"; x] -> (add tbl x true; doit (i + 1))
      | ["find"; x] -> (print_endline (if mem tbl x then "yes" else "no"); doit (i + 1))
      | _ -> exit 1
  in doit 0


let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let () =
  let open Hashtbl in
  let n = read_int () in
  let tbl = create ~random:true n in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; x] -> add tbl x true
      | ["find"; x] -> print_endline (if mem tbl x then "yes" else "no")
      | _ -> exit 1
      end;
      doit (i + 1)
    end in
  doit 0


let m = 950527

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_key s =
  let char_to_x c =
    if c = 'A' then 1
    else if c = 'C' then 2
    else if c = 'G' then 3
    else if c = 'T' then 4
    else 0 in
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(char_to_x s.[i])) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 950527

let to_key s =
  let char_to_x c =
    if c = 'A' then 1
    else if c = 'C' then 2
    else if c = 'G' then 3
    else if c = 'T' then 4
    else 0 in
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(char_to_x s.[i])) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match Str.split (Str.regexp " ") (read_line ()) with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 33554432

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i acc =
    if i = n then acc
    else doit (i + 1) (4*acc + to_x.(Char.code s.[i])) in
  doit 0 1

let () =
  to_x.(Char.code 'A') <- 0;
  to_x.(Char.code 'C') <- 1;
  to_x.(Char.code 'G') <- 2;
  to_x.(Char.code 'T') <- 3;
  let n = read_int () in
  let t = Array.make m false in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> t.(to_key s) <- true
      | ["find"; s] -> print_endline (if t.(to_key s) then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 9999973

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 1046527

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 1000003

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let m = 1020001

let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  let rec doit i =
    if i < n then begin
      begin match split (read_line ()) ' ' with
      | ["insert"; s] -> insert s t
      | ["find"; s] -> print_endline (if find s t then "yes" else "no")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0


let () =
  let n = read_int () in
  let tbl = Hashtbl.create 1000000 in
  let rec loop = function
      0 -> ()
    | i ->
      begin match Scanf.scanf "%s %s\n" (fun a b -> a,b) with
          "insert",word -> Hashtbl.add tbl word 0
        | _,word ->
          print_endline
            (if Hashtbl.mem tbl word then "yes" else "no")
      end;
      loop (i-1)
  in
  loop n


let hash s =
  let len = String.length s in
  let rec loop acc = function
      i when i = len -> acc
    | i ->
      let x = match s.[i] with 'A' -> 1 | 'C' -> 2 | 'G' -> 3 |  _  -> 4 in
      loop (acc*5+x) (i+1)
  in loop 0 0

let () =
  let n = read_int () in
  let dic = Array.make (5. ** 12. |> int_of_float) false in
  let rec loop = function
      0 -> ()
    | i ->
      begin match Scanf.scanf "%s %s\n" (fun a b -> a,b) with
          "insert",word -> dic.(hash word) <- true
        | _,word ->
          print_endline
            (if dic.(hash word) then "yes" else "no")
      end;
      loop (i-1)
  in
  loop n


module Trie = struct
  type 'a t = {mutable is_term : bool;
               next : 'a t option array}

  let to_int c = match c with 'A' -> 0 | 'C' -> 1 | 'G' -> 2 | _ -> 3

  let create () =
    {is_term = false; next = Array.make 4 None}

  let insert t str =
    let len = String.length str in
    let rec loop node = function
        i when i = len -> node.is_term <- true
      | i ->
        let ind = to_int str.[i] in
        match node.next.(ind) with
          None ->
          let new_node = create () in
          node.next.(ind) <- Some new_node;
          loop new_node (i+1)
        | Some e -> loop e (i+1)
    in loop t 0

  let find t str =
    let len = String.length str in
    let rec loop node = function
      i when i = len -> node.is_term
      | i ->
        let ind = to_int str.[i] in
        match node.next.(ind) with
          None -> false
        | Some e -> loop e (i+1)
    in loop t 0

end

let () =
  let n = read_int () in
  let t = Trie.create () in
  let rec loop = function
      0 -> ()
    | i ->
      begin match Scanf.scanf "%s %s\n" (fun a b -> a,b) with
          "insert", word -> Trie.insert t word
        | _, word ->
          print_endline (if Trie.find t word then "yes" else "no")
      end;
      loop (i-1)
  in loop n


let () =
  let n = read_int () in
  let tbl = Hashtbl.create 1000000 in
  for i=1 to n do
    begin match Scanf.scanf "%s %s\n" (fun a b -> a,b) with
        "insert",word -> Hashtbl.add tbl word 0
      | _,word ->
        Printf.printf "%s\n"
          (if Hashtbl.mem tbl word then "yes" else "no")
    end
  done


let () =
  let n = read_int () in
  let tbl = Hashtbl.create 2000000 in
  for i=1 to n do
    begin match Scanf.scanf "%s %s\n" (fun a b -> a,b) with
        "insert",word -> Hashtbl.add tbl word 0
      | _,word ->
        Printf.printf "%s\n"
          (if Hashtbl.mem tbl word then "yes" else "no")
    end
  done


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let (tbl : (string, unit) Hashtbl.t) = Hashtbl.create n in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["insert"; s] -> Hashtbl.add tbl s ()
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | _ -> ()
  done


let () =
  let n = read_int () in
  let (tbl : (string, unit) Hashtbl.t) = Hashtbl.create n in
  for _ = 0 to n - 1 do
    match Scanf.scanf "%s %s\n" (fun inst str -> (inst, str)) with
    | ("insert", s) -> Hashtbl.add tbl s ()
    | ("find", s) -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | _ -> assert false
  done


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let (tbl : (string, unit) Hashtbl.t) = Hashtbl.create ~random:true n in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["insert"; s] -> Hashtbl.add tbl s ()
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | _ -> assert false
  done


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true n in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["insert"; s] -> Hashtbl.add tbl s ()
    | ["find"; s] -> Printf.printf "%s\n" (if Hashtbl.mem tbl s then "yes" else "no")
    | _ -> assert false
  done


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true n in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | [_; s] -> Hashtbl.add tbl s ()
    | _ -> assert false
  done


let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true n in
  for _ = 0 to n - 1 do
    match Scanf.scanf "%s %s\n" (fun inst str -> (inst, str)) with
    | ("find", s) -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | (_, s) -> Hashtbl.add tbl s ()
  done


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let tbl = Hashtbl.create ~random:true 1000000 in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | [_; s] -> Hashtbl.add tbl s ()
    | _ -> assert false
  done


let (tbl : (string, unit) Hashtbl.t) = Hashtbl.create ~random:true 1000000

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | [_; s] -> Hashtbl.add tbl s ()
    | _ -> assert false
  done


let (tbl : (string, unit) Hashtbl.t) = Hashtbl.create 1000000

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if Hashtbl.mem tbl s then "yes" else "no")
    | [_; s] -> Hashtbl.add tbl s ()
    | _ -> assert false
  done


let m = 1039999

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["insert"; s] -> insert s t
    | ["find"; s] -> print_endline (if find s t then "yes" else "no")
    | _ -> ()
  done


let m = 1039999

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if find s t then "yes" else "no")
    | [_; s] -> insert s t
    | _ -> assert false
  done


let m = 1039999

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let to_x = Array.make 128 0

let to_key s =
  let n = String.length s in
  let rec doit i p acc =
    if i = n then acc
    else doit (i + 1) (5*p) (acc + p*(to_x.(Char.code s.[i]))) in
  doit 0 1 0

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> Printf.printf "%s\n" (if find s t then "yes" else "no")
    | [_; s] -> insert s t
    | _ -> assert false
  done


let m = 1039999

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let to_x = Array.make 128 0

let to_key s =
  let ret = ref 0 in
  let p = ref 1 in
  for i = 0 to String.length s - 1 do
    ret := !ret + !p * (to_x.(Char.code s.[i]));
    p := 5 * !p;
  done;
  !ret

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s t =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  let t = Array.make m "\000" in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if find s t then "yes" else "no")
    | [_; s] -> insert s t
    | _ -> assert false
  done


let m = 1039999
let t = Array.make m "\000"

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let to_x = Array.make 128 0

let to_key s =
  let ret = ref 0 in
  let p = ref 1 in
  for i = 0 to String.length s - 1 do
    ret := !ret + !p * (to_x.(Char.code s.[i]));
    p := 5 * !p;
  done;
  !ret

let to_h1 k = k mod m
let to_h2 k = 1 + (k mod (m - 1))

let insert s =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then ()
    else if t.(h) = "\000" then t.(h) <- s
    else doit (i + 1) in
  doit 0

let find s =
  let k = to_key s in
  let h1 = to_h1 k in
  let h2 = to_h2 k in
  let rec doit i =
    let h = (h1 + h2*i) mod m in
    if t.(h) = s then true
    else if t.(h) = "\000" then false
    else doit (i + 1) in
  doit 0

let () =
  to_x.(Char.code 'A') <- 1;
  to_x.(Char.code 'C') <- 2;
  to_x.(Char.code 'G') <- 3;
  to_x.(Char.code 'T') <- 4;
  let n = read_int () in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if find s then "yes" else "no")
    | [_; s] -> insert s
    | _ -> assert false
  done


module SS = Set.Make(String)

let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let () =
  let n = read_int () in
  let rec doit set i =
    if i = n then ()
    else
      match split_on_char ' ' (read_line ()) with
      | ["find"; s] ->
        begin
          print_endline (if SS.mem s set then "yes" else "no");
          doit set (i + 1)
        end
      | [_; s] -> doit (SS.add s set) (i + 1)
      | _ -> assert false in
  doit SS.empty 0


let split_on_char sep s =
  let open String in
  let r = ref [] in
  let j = ref (length s) in
  for i = length s - 1 downto 0 do
    if get s i = sep then begin
      r := sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  sub s 0 !j :: !r

let fold_left f init str =
  let acc = ref init in
  for i = 0 to String.length str - 1 do
    acc := f !acc str.[i];
  done;
  !acc

let () =
  let to_x = Array.make 128 (-1) in
  to_x.(Char.code 'A') <- 0;
  to_x.(Char.code 'C') <- 1;
  to_x.(Char.code 'G') <- 2;
  to_x.(Char.code 'T') <- 3;
  let set = Array.make 9999973 false in
  let hash s = fold_left (fun acc c -> 4*acc + to_x.(Char.code c)) 1 s in
  let n = read_int () in
  for _ = 0 to n - 1 do
    match split_on_char ' ' (read_line ()) with
    | ["find"; s] -> print_endline (if set.(hash s) then "yes" else "no")
    | [_; s] -> set.(hash s) <- true
    | _ -> assert false
  done


open Scanf

let () =
  let n = read_int () in
  let dic = Hashtbl.create n in
  let f c a =
    if c = "insert" then Hashtbl.add dic a true
    else if
      try Hashtbl.find dic a with _ -> false then print_endline "yes"
    else print_endline "no" in
  let rec loop x =
    if x = 0 then ()
    else let (c, a) = sscanf (read_line ()) "%s %s" (fun x y -> (x, y)) in
         f c a ; loop (x-1)
  in loop n


open Scanf
open Printf

let n = scanf "%d\n" (fun x -> x)
let dict = Array.make n "NIL"

let to_num = function
 'A' -> 1
 |'C' -> 2
 |'G' -> 3
 |_ -> 4

let to_key s =
 let len_s = String.length s in
 let rec getKey i p sum =
  if i = len_s then sum
  else
   let sum2 = sum + p * to_num s.[i] in
   getKey (i+1) (p*5) sum2
 in
 getKey 0 1 0

let h1 key = (to_key key) mod n
let h2 key = (1 + (to_key key)) mod (n-1)
let h key i = (h1 key + i * h2 key) mod n

let insert key =
 let rec insert_key i =
 let j = h key i in
 if dict.(j) = "NIL" then (dict.(j) <- key)
 else insert_key (i+1)
in insert_key 1

let find key =
 let rec find_key i =
  let j = h key i in
  if dict.(j) = key then true
  else if dict.(j) = "NIL" || i >= n then false
  else find_key (i+1)
 in find_key 1

let () =
 let rec input_s i =
  if i = n then ()
  else
   let o, s = scanf "%s %s\n" (fun x y -> (x, y)) in
   if o.[0] = 'i' then (insert s; input_s (i+1))
   else
    if find s then (printf "yes\n"; input_s (i+1))
    else (printf "no\n"; input_s (i+1))
 in input_s 0


open Scanf
open Printf

let n = scanf "%d\n" (fun x -> x)
let dict = Array.make (n+1) "NIL"

let to_num = function
 'A' -> 1
 |'C' -> 2
 |'G' -> 3
 |_ -> 4

let to_key s =
 let len_s = String.length s in
 let rec getKey i p sum =
  if i = len_s then sum
  else
   let sum2 = sum + p * to_num s.[i] in
   getKey (i+1) (p*5) sum2
 in
 getKey 0 1 0

let h1 key = (to_key key) mod n
let h2 key = (1 + (to_key key)) mod (n-1)
let h key i = (h1 key + i * h2 key) mod n

let insert key =
 let rec insert_key i =
 let j = h key i in
 if dict.(j) = "NIL" then (dict.(j) <- key)
 else insert_key (i+1)
in insert_key 1

let find key =
 let rec find_key i =
  let j = h key i in
  if dict.(j) = key then true
  else if dict.(j) = "NIL" || i >= n then false
  else find_key (i+1)
 in find_key 1

let () =
 let rec input_s i =
  if i = n then ()
  else
   let o, s = scanf "%s %s\n" (fun x y -> (x, y)) in
   if o.[0] = 'i' then (insert s; input_s (i+1))
   else
    if find s then (printf "yes\n"; input_s (i+1))
    else (printf "no\n"; input_s (i+1))
 in input_s 0


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

(* 累積和を作る *)
let rec make_cumulative_sum sum_a a_list = match a_list with
    [] -> []
  | first :: rest ->
     let sumMinusFirst = sum_a - first in
     let nextSum = if sumMinusFirst < 0
                   then sumMinusFirst + 1000000007
                   else sumMinusFirst in
     nextSum :: make_cumulative_sum nextSum rest

let find_sum_of_product_of_pairs a_list cumulative_sum =
  List.fold_left2
    (fun acc x y -> (acc + x * y) mod 1000000007)
    0
    a_list
    cumulative_sum

let main () =
  let _ = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in (* nは飛ばす *)
  let as_str = read_line () in
  let a_list = as_num_list as_str in
  let sum_a = List.fold_left (fun acc x -> (x + acc) mod 1000000007) 0 a_list in
  let cumulative_sum = make_cumulative_sum sum_a a_list in
  let ans = find_sum_of_product_of_pairs a_list cumulative_sum in
  (
    Printf.printf "%d" ans;
    print_newline ()
  )

let _ = main ()

Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

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

Constraints

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

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

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

Sample Input 1

3
1 2 3

Sample Output 1

11

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

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829


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

(* 累積和を作る *)
let rec make_cumulative_sum sum_a a_list = match a_list with
    [] -> []
  | first :: rest -> (sum_a - first) :: make_cumulative_sum (sum_a - first) rest

let find_sum_of_product_of_pairs a_list cumulative_sum =
  List.fold_left2
    (fun acc x y-> (acc + x * y) mod 1000000007)
    0
    a_list
    cumulative_sum

let main () =
  let _ = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in (* nは飛ばす *)
  let as_str = read_line () in
  let a_list = as_num_list as_str in
  let sum_a = List.fold_left (fun acc x -> x + acc) 0 a_list in
  let cumulative_sum = make_cumulative_sum sum_a a_list in
  let ans = find_sum_of_product_of_pairs a_list cumulative_sum in
  (
    Printf.printf "%d" ans;
    print_newline ()
  )

let _ = main ()


let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let acc = Array.make (n + 1) 0 in
  for i = n - 1 downto 0 do
    acc.(i) <- as_.(i) +^ acc.(i + 1)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( +^ ) 0 @@
  Array.init (n - 1) @@ fun i -> as_.(i) *^ acc.(i + 1)


let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let acc = Array.make (n + 1) 0 in
  for i = n - 1 downto 0 do
    acc.(i) <- as_.(i) + acc.(i + 1)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( +^ ) 0 @@
  Array.init n @@ fun i -> as_.(i) *^ acc.(i + 1)


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left
            (fun s x -> (s + x) mod m) 0 xs in
  let s2 = (s * s) mod m in
  let d =
    List.fold_left
      (fun s x -> (s + (x * x)) mod m)
      0 xs
  in
  let rec loop x =
    if x >= d && (x - d) mod 2 = 0 then x else loop (x+m)
  in
  let s2 = loop s2 in
  let r = ((s2 - d) / 2) mod m in
  printf "%d\n" r


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left
            (fun s x -> (s + x) mod m) 0 xs in
  let s2 = (s * s) mod m in
  let d =
    List.fold_left
      (fun s x -> (s + (x * x)) mod m)
      0 xs
  in
  let rec loop x =
    if x < d then loop (x+m) else x
  in
  let s2 = loop s2 in
  let r = ((s2 - d) / 2) mod m in
  printf "%d\n" r


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left
            (fun s x -> (s + x) mod m) 0 xs in
  let s2 = (s * s) mod m in
  let d =
    List.fold_left
      (fun s x -> (s + (x * x) mod m) mod m)
      0 xs
  in
  let rec loop x =
    if x < d then loop (x+m) else x
  in
  let s2 = loop s2 in
  let r = (s2 - d) mod (2 * m) in
  let r = r / 2 in
  printf "%d\n" r


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

let main () =
  let n = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in
  let as_str = read_line () in
  let a_list = as_num_list as_str in
  let sum = ref 0 in
  (
    for i = 0 to n - 2 do
      for j = i + 1 to n - 1 do
        sum := (!sum +
                  (List.nth a_list i) * (List.nth a_list j)) mod 1000000007
      done
    done;
    Printf.printf "%d" !sum;
    print_newline ()
  )

let _ = main ()


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left
            (fun s x -> (s + x) mod m) 0 xs in
  let s2 = (s * s) mod m in
  let d =
    List.fold_left
      (fun s x -> (s + (x * x)) mod m)
      0 xs
  in
  let rec loop x =
    if x < d then loop (x+m) else x
  in
  let s2 = loop s2 in
  let r = (s2 - d) mod (2 * m) in
  let r = r / 2 in
  printf "%d\n" r


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left
            (fun s x -> (s + x)) 0 xs in
  let s2 = (s * s) mod m in
  let d =
    List.fold_left
      (fun s x -> (s + (x * x)) mod m)
      0 xs
  in
  let rec loop x =
    if x < d then loop (x+m) else x
  in
  let s2 = loop s2 in
  let r = (s2 - d) mod (2 * m) in
  let r = r / 2 in
  printf "%d\n" r


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

let main () =
  let n = Scanf.sscanf (read_line ()) "%d" (fun x -> x) in
  let as_str = read_line () in
  let a_list = as_num_list as_str in
  let sum = ref 0 in
  (
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
        if i < j
        then sum := !sum + (List.nth a_list i) * (List.nth a_list j)
      done
    done;
    Printf.printf "%d" (!sum mod 1000000007);
    print_newline ()
  )

let _ = main ()


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = Int64.of_int 1000000007 in
  let n = num1 () in
  let xs = List.map Int64.of_int (numN n) in
  let s = List.fold_left
            (fun s x -> (Int64.add s x)) Int64.zero xs in
  let s2 = Int64.mul s s in
  let d =
    List.fold_left
      (fun s x -> (Int64.add s (Int64.mul x x)))
      Int64.zero xs
  in
  let r = Int64.div (Int64.sub s2 d) (Int64.of_int 2) in
  let r = Int64.rem r m in
  printf "%d\n" (Int64.to_int r)


open Printf
open Scanf

let prn s x = printf "%s %d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left (fun s x -> (s + x)) 0 xs in
  let s = s mod m in
  let s2 = s * s in
  let d =
    List.fold_left
      (fun s x -> (s + x * x))
      0 xs
  in
  let rec loop x =
    if x < d then
      loop (x + m)
    else
      x
  in
  let s2 = loop s2 in
  let r = (s2 - d) / 2 in
  let r = r mod m in
  printf "%d\n" r


open Batteries
open Printf

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

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let f2i = int_of_float
let i2f = float_of_int

let c2a = int_of_char
let a2c = char_of_int

let c2i c = c2a c - 48
let i2c i = a2c (i + 48)

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

let puts s = print_endline s
let putw ?yn:(yn=("Yes", "No")) b = puts (if b then fst yn else snd yn)
let puti i = puts @@ i2s i

module StaleList = struct
  include List

  let nmem x l = not @@ mem x l

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

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

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

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

  let split_by = span

  let chunk = ntake

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

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

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

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

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

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = StaleList

module StaleArray = struct
  include Array

  let nmem x l = not @@ mem x l

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

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

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

  (* let takewh = take_while *)

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

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

  (* let split_by = span *)

  (* let chunk = ntake *)

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

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

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

  let concat2 = append

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

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

end

module A = StaleArray

module StaleString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

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

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

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

end

module S = StaleString

let spsp s = S.split_on_char ' ' s

module StaleSortedSet = struct
  include Set

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

    let nmem x st = not @@ mem x st

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

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

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

  let size = cardinal
end

module Ss = StaleSortedSet

module StaleSortedMap  = struct
  include Map

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

    let nmem x st = not @@ mem x st

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

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

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

  let size = cardinal
end

module Ms = StaleSortedMap

module StaleRange = struct

  type t = Range of int * int

  let make lb ub = Range (lb, ub)

  let to_list r =
    let Range (lb, ub) = r in
    L.make (ub - lb) ((+) lb)

  let to_array r =
    let Range (lb, ub) = r in
      A.make (ub - lb) ((+) lb)

  let iter r f =
    let Range (lb, ub) = r in
    for i = lb to ub do f i done

  let iter2 r1 r2 f =
    let Range (lb1, ub1) = r1 and Range (lb2, ub2) = r2 in
      for i = lb1 to ub2 do
        for j = lb2 to ub2 do
          f i j
        done
      done
end

module R = StaleRange

let (---) lb ub = R.make lb ub
let (--^) lb ub = R.make lb (ub - 1)

module StaleGenericAlgorithms = struct

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

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

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

end

module Gal = StaleGenericAlgorithms

let rds () = read_line ()

let rdi () = s2i @@ rds ()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

let print_matrix mat =
  puts "----";
  mat |> A.iter (fun r -> print_carray r);
  puts "----"

let _ =
  let n = rdi () in
  let al = rdhi () in
  let m = 1_000_000_007 in

  let rec summo s = function
    | [] -> s
    | h::t -> summo ((s + h) mod m) t
  in

  let sumpo x l = summo 0 @@ L.map (fun y -> x * y) l in

  let rec sumpa s = function
    | [] -> s
    | h::t -> sumpa ((s + h * summo 0 t) mod m) t
  in

  puti @@ sumpa 0 al


open Batteries
open Printf

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

let swap = Tuple2.swap

let s2i = int_of_string
let i2s = string_of_int
let s2f = float_of_string
let f2s = string_of_float
let f2i = int_of_float
let i2f = float_of_int

let c2a = int_of_char
let a2c = char_of_int

let c2i c = c2a c - 48
let i2c i = a2c (i + 48)

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

let puts s = print_endline s
let putw ?yn:(yn=("Yes", "No")) b = puts (if b then fst yn else snd yn)
let puti i = puts @@ i2s i

module StaleList = struct
  include List

  let nmem x l = not @@ mem x l

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

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

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

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

  let split_by = span

  let chunk = ntake

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

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

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

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

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

  let flatmap = concat_map

  let zipf = map2
  let zipfi = map2i

end

module L = StaleList

module StaleArray = struct
  include Array

  let nmem x l = not @@ mem x l

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

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

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

  (* let takewh = take_while *)

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

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

  (* let split_by = span *)

  (* let chunk = ntake *)

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

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

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

  let concat2 = append

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

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

end

module A = StaleArray

module StaleString = struct
  include String

  let len = length

  let replace_chars = map
  let map = replace

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

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

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

end

module S = StaleString

let spsp s = S.split_on_char ' ' s

module StaleSortedSet = struct
  include Set

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

    let nmem x st = not @@ mem x st

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

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

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

  let size = cardinal
end

module Ss = StaleSortedSet

module StaleSortedMap  = struct
  include Map

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

    let nmem x st = not @@ mem x st

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

    let size = cardinal
  end

  let nmem x st = not @@ mem x st

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

  let size = cardinal
end

module Ms = StaleSortedMap

module StaleRange = struct

  type t = Range of int * int

  let make lb ub = Range (lb, ub)

  let to_list r =
    let Range (lb, ub) = r in
    L.make (ub - lb) ((+) lb)

  let to_array r =
    let Range (lb, ub) = r in
      A.make (ub - lb) ((+) lb)

  let iter r f =
    let Range (lb, ub) = r in
    for i = lb to ub do f i done

  let iter2 r1 r2 f =
    let Range (lb1, ub1) = r1 and Range (lb2, ub2) = r2 in
      for i = lb1 to ub2 do
        for j = lb2 to ub2 do
          f i j
        done
      done
end

module R = StaleRange

let (---) lb ub = R.make lb ub
let (--^) lb ub = R.make lb (ub - 1)

module StaleGenericAlgorithms = struct

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

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

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

end

module Gal = StaleGenericAlgorithms

let rds () = read_line ()

let rdi () = s2i @@ rds ()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

let print_matrix mat =
  puts "----";
  mat |> A.iter (fun r -> print_carray r);
  puts "----"

let _ =
  let n = rdi () in
  let al = rdhi () in
  let m = 1_000_000_007 in

  let rec sum_mod s = function
    | [] -> s
    | h::t -> sum_mod ((s + h) mod m) t
  in

  let sum_prod x l = sum_mod 0 @@ L.map (fun y -> x * y) l in

  let rec sumpa s = function
    | [] -> s
    | h::t -> sumpa ((s + sum_prod h t) mod m) t
  in

  puti @@ sumpa 0 al


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


open Printf
open Scanf

let prn s x = printf "%s%d\n" s x

let num () = scanf "%d" (fun x -> x)
let num1 () = scanf "%d\n" (fun x -> x)
let num2 () = scanf "%d %d\n" (fun x y -> (x, y))
let num3 () = scanf "%d %d %d\n" (fun x y z -> (x, y, z))

let numN n =
  let rec loop rs k =
    if k=n then
      List.rev rs
    else
      let x = scanf " %d" (fun x -> x) in
      loop (x::rs) (k+1)
  in
  if n=0 then
    []
  else if n=1 then
    [num ()]
  else
    loop [num ()] 1

let () =
  let m = 1000000007 in
  let n = num1 () in
  let xs = numN n in
  let s = List.fold_left (fun s x -> s + x) 0 xs in
  let s = s mod m in
  let s2 = s * s in
  let d = List.fold_left (fun s x -> s + x * x) 0 xs in
  let r = (s2 - d) / 2 in
  let r = r mod m in
  printf "%d\n" r


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