You are given two integers $$$a$$$ and $$$b$$$. You may perform any number of operations on them (possibly zero).During each operation you should choose any positive integer $$$x$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$ or $$$a := a - 2x$$$, $$$b := b - x$$$. Note that you may choose different values of $$$x$$$ in different operations.Is it possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously?Your program should answer $$$t$$$ independent test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers $$$a$$$ and $$$b$$$ for this test case ($$$0 \le a, b \le 10^9$$$).

For each test case print the answer to it — YES if it is possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

NoteIn the first test case of the example two operations can be used to make both $$$a$$$ and $$$b$$$ equal to zero:  choose $$$x = 4$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$. Then $$$a = 6 - 4 = 2$$$, $$$b = 9 - 8 = 1$$$;  choose $$$x = 1$$$ and set $$$a := a - 2x$$$, $$$b := b - x$$$. Then $$$a = 2 - 2 = 0$$$, $$$b = 1 - 1 = 0$$$.

let () =
  let n = read_int () in
  let rec iter i =
    if i < n then (
      let (a,b) = Scanf.scanf "%d %d " (fun x y -> (x,y)) in
      print_endline (if (a mod 3 + b) mod 3 = 0 && a - b <= b && b - a <= a then "YES" else "NO");
      iter (i+1)
    ) in iter 0;

let () =
  let n = read_int () in
  let rec iter i =
    if i < n then (
      let (a,b) = Scanf.scanf " %d %d " (fun x y -> (x,y)) in
      print_endline (if (a mod 3 + b mod 3) mod 3 = 0 && a <= 2*b && b <= 2*a then "YES" else "NO");
      iter (i+1)
    ) in iter 0;

let () =
  let n = read_int () in
  let rec iter i =
    if i < n then (
      let (a,b) = Scanf.scanf " %d %d " (fun x y -> (x,y)) in
      print_endline (if (a+b) mod 3 = 0 && a <= 2*b && b <= 2*a then "YES" else "NO");
      iter (i+1)
    ) in iter 0;

let () =
  let n = read_int () in
  let rec iter i =
    if i < n then (
      let (a,b) = Scanf.scanf " %d %d " (fun x y -> (x,y)) in
      print_endline (if (a mod 3 + b) mod 3 = 0 && a <= 2*b && b <= 2*a then "YES" else "NO");
      iter (i+1)
    ) in iter 0;


You are given a tree, consisting of $$$n$$$ vertices. Each edge has an integer value written on it.Let $$$f(v, u)$$$ be the number of values that appear exactly once on the edges of a simple path between vertices $$$v$$$ and $$$u$$$.Calculate the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$1 \le v < u \le n$$$.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) — the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains three integers $$$v, u$$$ and $$$x$$$ ($$$1 \le v, u, x \le n$$$) — the description of an edge: the vertices it connects and the value written on it. The given edges form a tree.

Print a single integer — the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$v < u$$$.

(* An implementation of link-cut trees in ocaml *)

(* In this version the weight of a node is one plus the total number of nodes
   in its dotted children.  The size of a node is its weight plus
   the sizes of its left and right children.  Thus, the size of the root
   of the real tree is the number of nodes in it. *)

type node = {
  mutable id:int;  (*  For this aplication we don't need ids *)
  mutable l:node;
  mutable r:node;
  mutable p:node;
  mutable size:int;
  mutable weight:int
}

let rec null = {id=0; l=null; r=null; p=null; size=0; weight=0}

let isroot n = n.p == null || (n.p.l != n && n.p.r != n)
let update p = if p == null then failwith "update called on null"
  else (p.size <- p.l.size + p.weight + p.r.size)
let discharge p = ()

let rot_right p =
  let q = p.p in
  let r = q.p in
  q.l <- p.r;
  if q.l != null then q.l.p <- q;
  p.r <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.l == q then r.l <- p
    else if r.r == q then r.r <- p
  );
  update q

let rot_left p =
  let q = p.p in
  let r = q.p in
  q.r <- p.l;
  if q.r != null then q.r.p <- q;
  p.l <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.r == q then r.r <- p
    else if r.l == q then r.l <- p
  );
  update q

let splay p =
  let rec loop p = if not (isroot p) then
      let q = p.p in
      if isroot q then (
	discharge q;
	discharge p;
	if q.l == p then rot_right p else rot_left p
      ) else (
	let r = q.p in
	discharge r;
	discharge q;
	discharge p;
	if r.l == q then (
	  if q.l == p then (rot_right q; rot_right p)
	  else (rot_left p; rot_right p)
	) else (
	  if q.r == p then (rot_left q; rot_left p)
	  else (rot_right p; rot_left p)
	)
      );
      loop p
  in
  loop p;
  discharge p;  (* useful only if p was already the root *)
  update p      (* only useful if p was not already a root *)

let expose q =
  let rec loop r p = if p != null then (
    splay p;
    p.weight <- p.weight + p.l.size - r.size;
    p.l <- r;
    update p;
    loop p p.p
  ) in

  loop null q;
  splay q

let link p q =
  expose p;
  if p.r != null then failwith "p's right child should be null";
  expose q;  (* this expose was missing, but it is needed for this application. *)
  p.p <- q;
  q.weight <- q.weight + p.size;
  update q

(* let lca a b =
  ignore(expose a);
  expose(b) *)

let cut a =
  expose a;
  if a.r != null then (
    a.r.p <- null;
    a.r <- null;
    update a
  )

let findroot p =
  expose p;
  let rec loop p = if p.r == null then p else loop p.r in
  let p = loop p in
  splay p;
  p

let getsize p = (findroot p).size

let st n =
  if n == null then "null" else Printf.sprintf "%d" n.id

let printnode n =
  Printf.printf "node %s: l=%s  r=%s  p=%s  size=%d  weight=%d\n"
    (st n) (st n.l) (st n.r) (st n.p) n.size n.weight

(*--------------------------------------------------------------------------*)

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let long x = Int64.of_int x
let short x = Int64.to_int x

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) ++ a) 0L

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int() in
  let treenode = Array.init n (fun i -> {null with id=i; size=1; weight=1}) in

  let tree = Array.make n [] in

  let edgelist = Array.make n [] in
  (* the list of edges of a given label. edgelist.(l) = [(v1,u1)...]
     where v1 is a child of u1 and the edge has label l.  *)

  let add_edge u v x =
    tree.(u) <- (v,x)::tree.(u);
    tree.(v) <- (u,x)::tree.(v)
  in

  for i=1 to n-1 do
    let u = read_int() in
    let v = read_int() in
    let x = read_int() in
    add_edge (u-1) (v-1) (x-1);
  done;

  let rec dfs u p =
    List.iter (fun (v,x) ->
      if v <> p then (
	edgelist.(x) <- (treenode.(v),treenode.(u))::edgelist.(x);
	dfs v u
      )
    ) tree.(u)
  in
  dfs 0 (-1);

  for x = 0 to n-1 do
    List.iter (fun (v,u) -> link v u) edgelist.(x)
  done;

  (* we've built the tree *)

  let answer = sum 0 (n-1) (fun x ->
    List.iter (fun (v,u) -> cut v) edgelist.(x);
    let summand = List.fold_left (fun ac (v,u) ->
      ac ++ (long (getsize v)) ** (long (getsize u))
    ) 0L edgelist.(x)
    in
    List.iter (fun (v,u) -> link v u) edgelist.(x);
    summand
  ) in

  printf "%Ld\n" answer


Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).

The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000. The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes.  It is guaranteed that the input data is correct, and the answer always exists.

Print the smalles integer n which Vasya could pass to Kate.

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let rec non_zero i = if i=10 then -1
    else if hist.(i) > 0 then i else non_zero (i+1)
  in

  if non_zero 0 = -1 then printf "%s\n" sub else

  let make_low_string d =
    let first_digit = non_zero d in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    if first_digit < 0 then
      String.init len (fun i -> '0')
    else
      let pa = Array.make len 0 in
      pa.(0) <- first_digit;
      hist.(first_digit) <- hist.(first_digit) - 1;
      let p = ref 1 in
      for i=0 to 9 do
	for j=0 to hist.(i)-1 do
	  pa.(!p) <- i;
	  p := !p+1
	done
      done;
      hist.(first_digit) <- hist.(first_digit) + 1;
      String.init len (fun i -> n2l pa.(i))
  in

  let low_string = make_low_string 1 in

  (*  printf "low_string = %s\n" low_string; *)

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in
  let en = if low_string.[0] = '0' then 0 else lsn in

  let try_places = fold st en (
    fun i ac ->
      if i=0 || i=1 || i=lsn || (low_string.[i-1] <> low_string.[i])
      then i::ac else ac
  ) [] in

  (*
  printf "try_places = ";
  List.iter (fun i -> printf "%d " i) try_places;
  print_newline();
  *)

  let min_string = List.fold_left (fun best i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    let s = String.concat "" [front; sub; back] in
    if best = "" || s < best then s else best
  ) "" try_places in

  let min_string =
    if sub.[0] = '0' then min_string
    else
      let alternate = String.concat "" [sub; make_low_string 0] in
      min alternate min_string
  in

  printf "%s\n" min_string

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let rec non_zero i = if i=10 then -1
    else if hist.(i) > 0 then i else non_zero (i+1)
  in

  if non_zero 0 = -1 then printf "%s\n" sub else

  let low_string =
    let first_digit = non_zero 1 in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    if first_digit < 0 then
      String.init len (fun i -> '0')
    else
      let pa = Array.make len 0 in
      pa.(0) <- first_digit;
      hist.(first_digit) <- hist.(first_digit) - 1;
      let p = ref 1 in
      for i=0 to 9 do
	for j=0 to hist.(i)-1 do
	  pa.(!p) <- i;
	  p := !p+1
	done
      done;
      String.init len (fun i -> n2l pa.(i))
  in

  (* printf "low_string = %s\n" low_string; *)

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in
  let en = if low_string.[0] = '0' then 0 else lsn in

  let try_places = fold st en (
    fun i ac ->
      if i=0 || i=1 || i=lsn || (low_string.[i-1] <> low_string.[i])
      then i::ac else ac
  ) [] in

  (*
  printf "try_places = ";
  List.iter (fun i -> printf "%d " i) try_places;
  print_newline();
  *)

  let min_string = List.fold_left (fun best i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    let s = String.concat "" [front; sub; back] in
    if best = "" || s < best then s else best
  ) "" try_places in

  printf "%s\n" min_string

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let rec non_zero i = if i=10 then -1
    else if hist.(i) > 0 then i else non_zero (i+1)
  in

  if non_zero 0 = -1 then printf "%s\n" sub else

  let low_string =
    let first_digit = non_zero 1 in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    if first_digit < 0 then
      String.init len (fun i -> '0')
    else
      let pa = Array.make len 0 in
      pa.(0) <- first_digit;
      hist.(first_digit) <- hist.(first_digit) - 1;
      let p = ref 1 in
      for i=0 to 9 do
	for j=0 to hist.(i)-1 do
	  pa.(!p) <- i;
	  p := !p+1
	done
      done;
      String.init len (fun i -> n2l pa.(i))
  in

  (* printf "low_string = %s\n" low_string; *)

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in

  let try_places = fold st lsn (
    fun i ac ->
      if i=0 || i=lsn || (low_string.[i-1] <> low_string.[i])
      then i::ac else ac
  ) [] in

  (*
  printf "try_places = ";
  List.iter (fun i -> printf "%d " i) try_places;
  print_newline();
  *)

  let min_string = List.fold_left (fun best i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    let s = String.concat "" [front; sub; back] in
    if best = "" || s < best then s else best
  ) "" try_places in

  printf "%s\n" min_string

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  printf "length_of_n = %d\n" length_of_n;

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let low_string =
    let rec non_zero i = if i=10 then failwith "no-non-zero"
      else if hist.(i) > 0 then i else non_zero (i+1)
    in
    let first_digit = non_zero 1 in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    let pa = Array.make len 0 in
    pa.(0) <- first_digit;
    hist.(first_digit) <- hist.(first_digit) - 1;
    let p = ref 1 in
    for i=0 to 9 do
      for j=0 to hist.(i)-1 do
	pa.(!p) <- i;
	p := !p+1
      done
    done;
    String.init len (fun i -> n2l pa.(i))
  in

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in

  let try_places = fold st lsn (
    fun i ac ->
      if (i>0 && low_string.[i-1] = sub.[0]) || (i<lsn && low_string.[i] = sub.[0])
      then i::ac else ac
  ) [] in

  let try_strings = List.map (fun i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    String.concat "" [front; sub; back]
  ) try_places in

  let min_string = List.fold_left (
    fun best s -> if best = "" || s < best then s else best
  ) "" try_strings in

  printf "%s\n" min_string

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let rec non_zero i = if i=10 then -1
    else if hist.(i) > 0 then i else non_zero (i+1)
  in

  if non_zero 0 = -1 then printf "%s\n" sub else

  let low_string =
    let first_digit = non_zero 1 in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    if first_digit < 0 then
      String.init len (fun i -> '0')
    else
      let pa = Array.make len 0 in
      pa.(0) <- first_digit;
      hist.(first_digit) <- hist.(first_digit) - 1;
      let p = ref 1 in
      for i=0 to 9 do
	for j=0 to hist.(i)-1 do
	  pa.(!p) <- i;
	  p := !p+1
	done
      done;
      String.init len (fun i -> n2l pa.(i))
  in

  (*  printf "low_string = %s\n" low_string; *)

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in

  let try_places = fold st lsn (
    fun i ac ->
      if (i>0 && low_string.[i-1] = sub.[0]) <> (i<lsn && low_string.[i] = sub.[0])
      then i::ac else ac
  ) [] in

  let try_places = if try_places = [] then [0] else try_places in

  let min_string = List.fold_left (fun best i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    let s = String.concat "" [front; sub; back] in
    if best = "" || s < best then s else best
  ) "" try_places in

  printf "%s\n" min_string

(* my rank would have been 271 if it had been a real contest *)

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))

let rec log10 x = if x=0 then 0 else 1 + log10 (x/10)

let l2n c = (int_of_char c) - (int_of_char '0')
let n2l n = char_of_int ((int_of_char '0') + n)

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let r = read_string() in
  let sub = read_string() in

  let lr = String.length r in

  let length_of_n =
    let x1 = log10 lr in
    if x1 = log10 (lr - x1) then lr-x1 else lr-x1+1
  in

  let hist = Array.make 10 0 in

  let add_to_hist str d =
    for i=0 to (String.length str) -1 do
      let j = l2n str.[i] in
      hist.(j) <- hist.(j) + d
    done;
  in

  add_to_hist r 1;
  add_to_hist (sprintf "%d" length_of_n) (-1);
  add_to_hist sub (-1);

  let rec non_zero i = if i=10 then -1
    else if hist.(i) > 0 then i else non_zero (i+1)
  in

  if non_zero 0 = -1 then printf "%s\n" sub else

  let low_string =
    let first_digit = non_zero 1 in
    let len = sum 0 9 (fun i -> hist.(i)) 0 in
    if first_digit < 0 then
      String.init len (fun i -> '0')
    else
      let pa = Array.make len 0 in
      pa.(0) <- first_digit;
      hist.(first_digit) <- hist.(first_digit) - 1;
      let p = ref 1 in
      for i=0 to 9 do
	for j=0 to hist.(i)-1 do
	  pa.(!p) <- i;
	  p := !p+1
	done
      done;
      String.init len (fun i -> n2l pa.(i))
  in

  (* printf "low_string = %s\n" low_string; *)

  let lsn = String.length low_string in

  let st = if sub.[0] = '0' then 1 else 0 in
  let en = if low_string.[0] = '0' then 0 else lsn in

  let try_places = fold st en (
    fun i ac ->
      if i=0 || i=lsn || (low_string.[i-1] <> low_string.[i])
      then i::ac else ac
  ) [] in

  (*
  printf "try_places = ";
  List.iter (fun i -> printf "%d " i) try_places;
  print_newline();
  *)

  let min_string = List.fold_left (fun best i ->
    let front = String.sub low_string 0 i in
    let back = String.sub low_string i (lsn-i) in
    let s = String.concat "" [front; sub; back] in
    if best = "" || s < best then s else best
  ) "" try_places in

  printf "%s\n" min_string


After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type. Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.

The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000). The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.

Print a single integer — the number of Fedor's potential friends.

let read () = Scanf.scanf "%d " (fun n -> n)

let make_list n =
    let rec loop i list =
        if i < n + 1
        then
            let temp = read () in
            loop (i + 1) (temp :: list)
        else
            list
    in loop 0 []

let bin_of_int number types =
    let rec loop i number list =
        match i < types with
        | false -> List.rev list
        | true -> loop (i + 1) (number / 2) (number mod 2 :: list)
    in loop 0 number []

let compare contender target =
    let rec loop l ll count =
        match l, ll with
        | (hd :: tl), (hhd :: ttl) ->
            if hd != hhd
            then
                loop tl ttl (count + 1)
            else
                loop tl ttl count
        | [], [] -> count
        | _, [] | [], _ -> failwith "Lists are not conformant"
    in loop contender target 0

let counter max_error total count =
    if count <= max_error
    then
        total + 1
    else
        total

let input () =
    let types = read () in
    let commanders = read () in
    let max_error = read () in
    match make_list commanders with
    | (head :: tail) ->
        let army = tail in
        let target = bin_of_int head types in
    (types, max_error, army, target)

let solve (types, max_error, army, target) =
    let rec loop army total =
        match army with
        | (head :: tail) ->
            let bin_contender = bin_of_int head types in
            let count = compare bin_contender target in
            let total_upd = counter max_error total count in
            loop tail total_upd
        | [] -> total
    in loop army 0

let print result = Printf.printf "%d\n" result
let () = print (solve (input ()))


Let's consider a k × k square, divided into unit squares. Please note that k ≥ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of two cases: either we've reached the square's border or the square following after the next square is already painted. We finish painting at the moment when we cannot move in any direction and paint a square. The figure that consists of the painted squares is a spiral.   The figure shows examples of spirals for k = 3, 5, 7, 9.  You have an n × m table, each of its cells contains a number. Let's consider all possible spirals, formed by the table cells. It means that we consider all spirals of any size that don't go beyond the borders of the table. Let's find the sum of the numbers of the cells that form the spiral. You have to find the maximum of those values among all spirals.

The first line contains two integers n and m (3 ≤ n, m ≤ 500) — the sizes of the table. Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≤ aij ≤ 1000) is the number recorded in the j-th cell of the i-th row of the table.

Print a single number — the maximum sum of numbers among all spirals.

NoteIn the first sample the spiral with maximum sum will cover all 1's of the table.In the second sample the spiral may cover only six 1's.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to m - 1 do
	let k = Scanf.bscanf sb "%d " (fun s -> s) in
	  a.(i).(j) <- k
      done
    done
  in
  let max (x : int) y = if x > y then x else y in
  let res = ref (-1000000000) in
  let v = Array.make_matrix (n + 1) m 0 in
  let h = Array.make_matrix n (m + 1) 0 in
    for i = 0 to n - 1 do
      for j = 1 to m do
	h.(i).(j) <- h.(i).(j - 1) + a.(i).(j - 1)
      done
    done;
    for i = 1 to n do
      for j = 0 to m - 1 do
	v.(i).(j) <- v.(i - 1).(j) + a.(i - 1).(j)
      done
    done;
    for y = 0 to n - 3 do
      for x = 0 to m - 3 do
	let s = ref (a.(y).(x) +
		       a.(y).(x + 1) +
		       a.(y).(x + 2) +
		       a.(y + 1).(x + 2) +
		       a.(y + 2).(x) +
		       a.(y + 2).(x + 1) +
		       a.(y + 2).(x + 2))
	in
	let () = res := max !res !s in
	let d = ref 1 in
	  while x - 2 * !d >= 0 && x + 2 + 2 * !d < m &&
	    y - 2 * !d >= 0 && y + 2 + 2 * !d < n do
	      let x' = x - 2 * !d in
	      let y' = y - 2 * !d in
		s := !s + h.(y').(x' + 3 + 4 * !d) - h.(y').(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x' + 2 + 4 * !d) -
		  v.(y' + 1).(x' + 2 + 4 * !d);
		s := !s +
		  h.(y' + 2 + 4 * !d).(x' + 3 + 4 * !d) -
		  h.(y' + 2 + 4 * !d).(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x') -
		  v.(y' + 2).(x');
		s := !s + a.(y' + 2).(x' + 1);
		res := max !res !s;
		incr d;
	  done
      done
    done;
    for y = 0 to n - 5 do
      for x = 0 to m - 5 do
	let s = ref 0 in
	let () =
	  for i = 0 to 4 do
	    s := !s + a.(y).(x + i);
	  done;
	  for i = 1 to 3 do
	    s := !s + a.(y + i).(x + 4);
	  done;
	  for i = 0 to 4 do
	    s := !s + a.(y + 4).(x + i);
	  done;
	  for i = 2 to 3 do
	    s := !s + a.(y + i).(x);
	  done;
	  s := !s + a.(y + 2).(x + 1) + a.(y + 2).(x + 2);
	in
	let () = res := max !res !s in
	let d = ref 1 in
	  while x - 2 * !d >= 0 && x + 4 + 2 * !d < m &&
	    y - 2 * !d >= 0 && y + 4 + 2 * !d < n do
	      let x' = x - 2 * !d in
	      let y' = y - 2 * !d in
		s := !s + h.(y').(x' + 5 + 4 * !d) - h.(y').(x');
		s := !s +
		  v.(y' + 4 + 4 * !d).(x' + 4 + 4 * !d) -
		  v.(y' + 1).(x' + 4 + 4 * !d);
		s := !s +
		  h.(y' + 4 + 4 * !d).(x' + 5 + 4 * !d) -
		  h.(y' + 4 + 4 * !d).(x');
		s := !s +
		  v.(y' + 4 + 4 * !d).(x') -
		  v.(y' + 2).(x');
		s := !s + a.(y' + 2).(x' + 1);
		res := max !res !s;
		incr d;
	  done
      done
    done;
    Printf.printf "%d\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m 0 in
  let () =
    for i = 0 to n - 1 do
      for j = 0 to m - 1 do
	let k = Scanf.bscanf sb "%d " (fun s -> s) in
	  a.(i).(j) <- k
      done
    done
  in
  let max (x : int) y = if x > y then x else y in
  let res = ref (-1000000000) in
  let v = Array.make_matrix (n + 1) m 0 in
  let h = Array.make_matrix n (m + 1) 0 in
    for i = 0 to n - 1 do
      for j = 1 to m do
	h.(i).(j) <- h.(i).(j - 1) + a.(i).(j - 1)
      done
    done;
    for i = 1 to n do
      for j = 0 to m - 1 do
	v.(i).(j) <- v.(i - 1).(j) + a.(i - 1).(j)
      done
    done;
    for y = 0 to n - 3 do
      for x = 0 to m - 3 do
	let s = ref (a.(y).(x) +
		       a.(y).(x + 1) +
		       a.(y).(x + 2) +
		       a.(y + 1).(x + 2) +
		       a.(y + 2).(x) +
		       a.(y + 2).(x + 1) +
		       a.(y + 2).(x + 2))
	in
	let () = res := max !res !s in
	let d = ref 1 in
	  while x - 2 * !d >= 0 && x + 2 + 2 * !d < m &&
	    y - 2 * !d >= 0 && y + 2 + 2 * !d < n do
	      let x' = x - 2 * !d in
	      let y' = y - 2 * !d in
		s := !s + h.(y').(x' + 3 + 4 * !d) - h.(y').(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x' + 2 + 4 * !d) -
		  v.(y' + 1).(x' + 2 + 4 * !d);
		s := !s +
		  h.(y' + 2 + 4 * !d).(x' + 3 + 4 * !d) -
		  h.(y' + 2 + 4 * !d).(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x') -
		  v.(y' + 2).(x');
		s := !s + a.(y' + 2).(x' + 1);
		res := max !res !s;
		incr d;
	  done
      done
    done;
    for y = 0 to n - 5 do
      for x = 0 to m - 5 do
	let s = ref 0 in
	let () =
	  for i = 0 to 4 do
	    s := !s + a.(y).(x + i);
	  done;
	  for i = 1 to 3 do
	    s := !s + a.(y + i).(x + 4);
	  done;
	  for i = 0 to 4 do
	    s := !s + a.(y + 4).(x + i);
	  done;
	  for i = 2 to 3 do
	    s := !s + a.(y + i).(x);
	  done;
	  s := !s + a.(y + 2).(x + 1) + a.(y + 2).(x + 2);
	in
	let () = res := max !res !s in
	let d = ref 1 in
	  while x - 2 * !d >= 0 && x + 2 + 2 * !d < m &&
	    y - 2 * !d >= 0 && y + 2 + 2 * !d < n do
	      let x' = x - 2 * !d in
	      let y' = y - 2 * !d in
		s := !s + h.(y').(x' + 3 + 4 * !d) - h.(y').(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x' + 2 + 4 * !d) -
		  v.(y' + 1).(x' + 2 + 4 * !d);
		s := !s +
		  h.(y' + 2 + 4 * !d).(x' + 3 + 4 * !d) -
		  h.(y' + 2 + 4 * !d).(x');
		s := !s +
		  v.(y' + 2 + 4 * !d).(x') -
		  v.(y' + 2).(x');
		s := !s + a.(y' + 2).(x' + 1);
		res := max !res !s;
		incr d;
	  done
      done
    done;
    Printf.printf "%d\n" !res;


Polycarp found a rectangular table consisting of $$$n$$$ rows and $$$m$$$ columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns":   cells are numbered starting from one;  cells are numbered from left to right by columns, and inside each column from top to bottom;  number of each cell is an integer one greater than in the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, the table will be numbered as follows:$$$$$$ \begin{matrix} 1 & 4 & 7 & 10 & 13 \\ 2 & 5 & 8 & 11 & 14 \\ 3 & 6 & 9 & 12 & 15 \\ \end{matrix} $$$$$$However, Polycarp considers such numbering inconvenient. He likes the numbering "by rows":   cells are numbered starting from one;  cells are numbered from top to bottom by rows, and inside each row from left to right;  number of each cell is an integer one greater than the number of the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, then Polycarp likes the following table numbering: $$$$$$ \begin{matrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ \end{matrix} $$$$$$Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering "by rows", if in the numbering "by columns" the cell has the number $$$x$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. Each test case consists of a single line containing three integers $$$n$$$, $$$m$$$, $$$x$$$ ($$$1 \le n, m \le 10^6$$$, $$$1 \le x \le n \cdot m$$$), where $$$n$$$ and $$$m$$$ are the number of rows and columns in the table, and $$$x$$$ is the cell number. Note that the numbers in some test cases do not fit into the $$$32$$$-bit integer type, so you must use at least the $$$64$$$-bit integer type of your programming language.

For each test case, output the cell number in the numbering "by rows".

(* https://codeforces.com/contest/1506/problem/0 *)

let (--) = Int64.sub and
(++) = Int64.add and
(//) = Int64.div and
(%%) = Int64.rem and
( ** ) = Int64.mul

let xy_of_columnsi i rows columns =
    ((i--1L) // rows, (i--1L) %% rows)

let i_of_rowsxy (x, y) rows columns =
    x ++ y ** columns ++ 1L

let solve_n n =
    let rec helper i =
        if i <> 0
        then (
            let [rows; columns; index] =
                read_line ()
                |> Str.split (Str.regexp " ")
                |> List.map Int64.of_string in
            let (x, y) = xy_of_columnsi index rows columns in
            let index2 = i_of_rowsxy (x, y) rows columns in
            Printf.printf "%Ld\n" index2;
            helper (i-1)
        ) in
    helper n

let () =
    let nb_cases = read_int () in
    solve_n nb_cases


Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.What is the maximum number of apples he can collect?

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land. The following n lines contains two integers each xi, ai ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

Output the maximum number of apples Amr can collect.

NoteIn the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x =  - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x =  - 2.In the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let n = read_int () in
  let d = Array.init n (fun _ -> read_pair()) in

  let rec make_lists l r i = if i=n then (l,r) else
      let (x,a) = d.(i) in
      if x < 0 then make_lists ((-x,a)::l) r (i+1)
      else make_lists l ((x,a)::r) (i+1)
  in

  let (l,r) = make_lists [] [] 0 in

  let l = List.sort compare l in
  let r = List.sort compare r in

  let (nl, nr) = (List.length l, List.length r) in

  let (l,r) = if nl < nr then (r,l) else (l,r) in
  let (nl,nr) = (max nl nr, min nl nr) in

  let sum_r = List.fold_left (fun t (_,a) -> t+a) 0 r in

  let (sum_l,_) = List.fold_left (fun (t,i) (_,a) -> if (i>=nr+1) then (t,i+1) else (t+a,i+1)) (0,0) l in

  printf "%d\n" (sum_l + sum_r)

open List
let read_int _ = Scanf.scanf " %d" (fun x -> x);;

let min x y = if x < y then x else y;;

let rec read_list l n =
    if n = 0 then l
    else let x = read_int() and y = read_int() in
    read_list (l @ [(x, y)]) (n-1);;

let rec answer l r n = match (l, r) with
      | ([], []) -> n
      | ((x, y) :: l, []) -> n + y
      | ([], (x, y)::l) -> n + y
      | ((x1, y1) :: l', (x2, y2) :: r') -> answer l' r' (n + y1 + y2);;

let mysort (x1, y1) (x2, y2) = x1 - x2;;

let () =
    let n = read_int () in
    let xa = read_list [] n in
    let left = filter (fun (x, y) -> (x < 0)) xa and right = filter (fun (x, y) -> (x > 0)) xa in
    let left = sort mysort left and right = sort mysort right in
    let ans = answer (rev left) right 0 in
    Printf.printf "%d\n" ans
;;


You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$.A median of an array of integers of length $$$m$$$ is the number standing on the $$$\lceil {\frac{m}{2}} \rceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$\lceil \frac{m}{2} \rceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with two integers $$$n$$$ and $$$s$$$ ($$$1 \le n, s \le 10^9$$$) — the length of the array and the required sum of the elements.

For each test case print a single integer — the maximum possible median.

NotePossible arrays for the first three test cases (in each array the median is underlined):  In the first test case $$$[\underline{5}]$$$  In the second test case $$$[\underline{2}, 3]$$$  In the third test case $$$[1, \underline{2}, 2]$$$

let w = read_int () in
let rec aux z = (if z > 0 then
    (
    let y = read_line () in
    let len = String.length y in
    let resolution y =
        let rec breaker = (function
            (a,b) when y.[b] = ' ' -> (int_of_string(String.sub y 0 b), int_of_string(String.sub y (b+1) (len-(b+1))))
            |(a,b) -> breaker(a,b+1)) in
        let a = breaker (y,0) in let (x,y) = a in
        print_int (match x with
            1 -> y
            |x when x mod 2 = 0 -> y/(x-((x-1)/2))
            |x -> y/(x-(x/2))); print_newline ()
        in
    (resolution y; aux(z-1) )
    )) in aux (w);;

let solve n s =
  let elts = n/2 + 1 in
  s/elts;;

let divide s =
  let l = String.length s in
  let rec aux i =
     if s.[i] = ' ' then i else aux (i+1) in
  let space = aux 0 in
  let a = String.sub s 0 space and b = String.sub s (space+1) (l-space-1) in
  (int_of_string a, int_of_string b);;

let read_test i =
  let test = read_line () in divide test;;

let t = read_int ();;

let process x =
  let (n, s) = read_test () in print_int(solve n s); print_newline ();;

let rec execute i =
  if i < t then
  begin
    process i; execute (i+1)
  end;;

let () = execute 0;;


Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.

The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.

Print the sequence of operations that will rearrange the animals by non-decreasing height. The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed. The number of operations should not exceed 20 000. If the animals are arranged correctly from the start, you are allowed to output nothing.

NoteNote that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed.

let n = read_int ()
let a = read_line () |> Str.(split (regexp " ")) |> List.map int_of_string |> Array.of_list

let () =
  for i = 0 to n - 2 do
    let min = ref i in
    for j = i + 1 to n - 1 do
      if a.(j) < a.(!min) then
        min := j
    done;

    for j = !min - 1 downto i do
      Printf.printf "%d %d\n" (j+1) (j+2);
      let tmp = a.(j) in
      a.(j) <- a.(j+1);
      a.(j+1) <- tmp
    done
  done

let _ =
   let n = Scanf.scanf "%d" (fun i -> i) in
   let tab = Array.init n (fun _ -> Scanf.scanf " %d" (fun j -> j)) in
   let perm i j =
      let z = tab.(i) in
         tab.(i) <- tab.(j);
         tab.(j) <- z
   in
   let a = ref (-1) in
      while !a < n-1 do
         if !a < 0
         then incr a
         else if tab.(!a) > tab.(1 + !a)
              then begin
                   perm !a (1 + !a);
                   Printf.printf "%d %d\n" (1 + !a) (2 + !a);
                   decr a
                   end
              else incr a;
      done

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in

  for i=1 to n-1 do
    for j=i downto 1 do
      if a.(j-1) > a.(j) then (
	printf "%d %d\n" (j) (j+1);
	let (x,y) = (a.(j-1), a.(j)) in
	a.(j-1) <- y;
	a.(j) <- x
      )
    done
  done

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in

  for i=0 to n-2 do
    for j=i to n-2 do
      if a.(j) > a.(j+1) then (
	printf "%d %d\n" (j+1) (j+2);
	let (x,y) = (a.(j), a.(j+1)) in
	a.(j) <- y;
	a.(j+1) <- x
      )
    done
  done


This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each.As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball.The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies?Polycarp can throw his fire ball into an archer if the latter is already killed.

The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has.

In the first line print t — the required minimum amount of fire balls. In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)

let () =
  let n = read_int 0 in
  let a = read_int 0 in
  let b = read_int 0 in
  let h = Array.init n read_int in
  let dp = Array.init (n-1) (fun _ -> Array.make_matrix 17 17 200) in
  let pr = Array.init (n-1) (fun _ -> Array.make_matrix 17 17 (-1)) in
  dp.(0).(0).(0) <- 0;
  for i = 1 to n-2 do
    for j = 0 to 16 do
      for k = 0 to 16 do
        let t = dp.(i-1).(j).(k) in
        if t < 200 then
          for l = 0 to 16 do
            if (j+l)*b+k*a > h.(i-1) && t+l < dp.(i).(k).(l) then (
              if i=2 && k=0 && l=2 then
                Printf.printf "+++ %d %d %d\n" i j k;
              dp.(i).(k).(l) <- t+l;
              pr.(i).(k).(l) <- j
            )
          done
      done
    done
  done;

  let ans = ref 200 and ansj = ref (-1) and ansk = ref (-1) in
  for j = 0 to 16 do
    for k = 0 to 16 do
      if j*b+k*a > h.(n-2) && k*b > h.(n-1) && dp.(n-2).(j).(k) < !ans then (
        ans := dp.(n-2).(j).(k);
        ansj := j;
        ansk := k
      )
    done
  done;

  Printf.printf "%d\n" !ans;
  let rec plan i j k =
    if i > 0 then
      plan (i-1) pr.(i).(j).(k) j;
    for g = 1 to k do
      Printf.printf "%d " (i+1)
    done
  in
  plan (n-2) !ansj !ansk


Dreamoon likes coloring cells very much.There is a row of $$$n$$$ cells. Initially, all cells are empty (don't contain any color). Cells are numbered from $$$1$$$ to $$$n$$$.You are given an integer $$$m$$$ and $$$m$$$ integers $$$l_1, l_2, \ldots, l_m$$$ ($$$1 \le l_i \le n$$$)Dreamoon will perform $$$m$$$ operations.In $$$i$$$-th operation, Dreamoon will choose a number $$$p_i$$$ from range $$$[1, n-l_i+1]$$$ (inclusive) and will paint all cells from $$$p_i$$$ to $$$p_i+l_i-1$$$ (inclusive) in $$$i$$$-th color. Note that cells may be colored more one than once, in this case, cell will have the color from the latest operation.Dreamoon hopes that after these $$$m$$$ operations, all colors will appear at least once and all cells will be colored. Please help Dreamoon to choose $$$p_i$$$ in each operation to satisfy all constraints.

The first line contains two integers $$$n,m$$$ ($$$1 \leq m \leq n \leq 100\,000$$$). The second line contains $$$m$$$ integers $$$l_1, l_2, \ldots, l_m$$$ ($$$1 \leq l_i \leq n$$$).

If it's impossible to perform $$$m$$$ operations to satisfy all constraints, print "'-1" (without quotes). Otherwise, print $$$m$$$ integers $$$p_1, p_2, \ldots, p_m$$$ ($$$1 \leq p_i \leq n - l_i + 1$$$), after these $$$m$$$ operations, all colors should appear at least once and all cells should be colored. If there are several possible solutions, you can print any.

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_pair () = let x = scan_int() in let y = scan_int() in (x,y);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int());;
let show_int n = Printf.printf "%d\n" n;;
let show_int_array t = Array.iter (fun v -> Printf.printf "%d " v) t; print_newline ();;

let valide tab n =
	let test = ref true and count = ref 0L and n = Int64.of_int n in
	for i = 0 to (Array.length tab)-1 do
		count := Int64.add !count (Int64.of_int tab.(i));
		test := !test && (Int64.sub n (Int64.of_int tab.(i)) >= (Int64.of_int i))
	done;
	!test && (!count >= n);;

let suffixe_sum tab =
	let n = Array.length tab in
	let sum = Array.make n 0L in
	let somme = ref 0L in
	for i = n-1 downto 0 do
		somme := Int64.add !somme (Int64.of_int tab.(i));
		sum.(i) <- !somme
	done;
	sum;;

let creation_sol l n m =
	let sol = Array.make m 0 in
	let n = Int64.of_int n in
	let sum = suffixe_sum l in
	for i = 0 to m-1 do sol.(i) <- Int64.to_int (Int64.add (max (Int64.of_int i) (Int64.sub n sum.(i))) 1L) done;
	sol;;

let n,m = scan_int_pair () in
let l = scan_int_array_of_size m in
if not (valide l n)
	then show_int (-1)
	else (let sol = creation_sol l n m in show_int_array sol)

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_pair () = let x = scan_int() in let y = scan_int() in (x,y);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int());;
let show_int n = Printf.printf "%d\n" n;;
let show_int_array t = Array.iter (fun v -> Printf.printf "%d " v) t; print_newline ();;

let valide tab n =
	let test = ref true and count = ref 0 in
	for i = 0 to (Array.length tab)-1 do
		count := !count + tab.(i);
		test := !test && (n - tab.(i) >= i)
	done;
	!test && (!count >= n);;

let suffixe_sum tab =
	let n = Array.length tab in
	let sum = Array.make n 0L in
	let somme = ref 0L in
	for i = n-1 downto 0 do
		somme := Int64.add !somme (Int64.of_int tab.(i));
		sum.(i) <- !somme
	done;
	sum;;

let creation_sol l n m =
	let sol = Array.make m 0 in
	let n = Int64.of_int n in
	let sum = suffixe_sum l in
	for i = 0 to m-1 do sol.(i) <- Int64.to_int (Int64.add (max (Int64.of_int i) (Int64.sub n sum.(i))) 1L) done;
	sol;;

let n,m = scan_int_pair () in
let l = scan_int_array_of_size m in
if not (valide l n)
	then show_int (-1)
	else (let sol = creation_sol l n m in show_int_array sol)

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_pair () = let x = scan_int() in let y = scan_int() in (x,y);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int());;
let show_int n = Printf.printf "%d\n" n;;
let show_int_array t = Array.iter (fun v -> Printf.printf "%d " v) t;;

let valide tab n =
	let test = ref true and count = ref 0 in
	for i = 0 to (Array.length tab)-1 do
		count := !count + tab.(i);
		test := !test && (n - tab.(i) > i)
	done;
	!test && (!count >= n);;

let suffixe_sum tab =
	let n = Array.length tab in
	let sum = Array.make n 0 in
	let somme = ref 0 in
	for i = n-1 downto 0 do
		somme := !somme + tab.(i);
		sum.(i) <- !somme
	done;
	sum;;

let creation_sol l n m =
	let sol = Array.make m 0 in
	let sum = suffixe_sum l in
	for i = 0 to m-1 do sol.(i) <- (max i (n-sum.(i)))+1 done;
	sol;;

let n,m = scan_int_pair () in
let l = scan_int_array_of_size m in
if not (valide l n)
	then show_int (-1)
	else (let sol = creation_sol l n m in show_int_array sol)

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_pair () = let x = scan_int() in let y = scan_int() in (x,y);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int());;
let show_int n = Printf.printf "%d\n" n;;
let show_int_array t = Array.iter (fun v -> Printf.printf "%d " v) t;;

let valide tab n =
	let test = ref true and count = ref 0 in
	for i = 0 to (Array.length tab)-1 do
		count := !count + tab.(i);
		test := !test && (n - tab.(i) >= i)
	done;
	!test && (!count >= n);;

let suffixe_sum tab =
	let n = Array.length tab in
	let sum = Array.make n 0 in
	let somme = ref 0 in
	for i = n-1 downto 0 do
		somme := !somme + tab.(i);
		sum.(i) <- !somme
	done;
	sum;;

let creation_sol l n m =
	let sol = Array.make m 0 in
	let sum = suffixe_sum l in
	for i = 0 to m-1 do sol.(i) <- (max i (n-sum.(i)))+1 done;
	sol;;

let n,m = scan_int_pair () in
let l = scan_int_array_of_size m in
if not (valide l n)
	then show_int (-1)
	else (let sol = creation_sol l n m in show_int_array sol)


As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (x, y, z). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formula: . Mushroom scientists that work for the Great Mushroom King think that the Universe isn't exactly right and the distance from the center of the Universe to a point equals xa·yb·zc.To test the metric of mushroom scientists, the usual scientists offered them a task: find such x, y, z (0 ≤ x, y, z; x + y + z ≤ S), that the distance between the center of the Universe and the point (x, y, z) is maximum possible in the metric of mushroom scientists. The mushroom scientists aren't good at maths, so they commissioned you to do the task.Note that in this problem, it is considered that 00 = 1.

The first line contains a single integer S (1 ≤ S ≤ 103) — the maximum sum of coordinates of the sought point. The second line contains three space-separated integers a, b, c (0 ≤ a, b, c ≤ 103) — the numbers that describe the metric of mushroom scientists.

Print three real numbers — the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations. A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists shouldn't differ from the natural logarithm of the maximum distance by more than 10 - 6. We think that ln(0) =  - ∞.

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = float_of_int s in
  let a = float_of_int a in
  let b = float_of_int b in
  let c = float_of_int c in
    if false then (
  let l = ref 0.0 in
  let r = ref s in
  let calc x y z =
    let xa = if a = 0.0 then 0.0 else a *. log x in
    let yb = if b = 0.0 then 0.0 else b *. log y in
    let zc = if c = 0.0 then 0.0 else c *. log z in
      xa +. yb +. zc
  in
    for i = 1 to 400 do
      let x1 = (2.0 *. !l +. !r) /. 3.0 in
      let y1 = b *. (s -. x1) /. (b +. c) in
      let y1 = if y1 >= 0.0 && y1 <= s then y1 else if y1 > s then s else 0.0 in
      let z1 = s -. x1 -. y1 in
      let x2 = (!l +. 2.0 *. !r) /. 3.0 in
      let y2 = b *. (s -. x2) /. (b +. c) in
      let y2 = if y2 >= 0.0 && y2 <= s then y2 else if y2 > s then s else 0.0 in
      let z2 = s -. x2 -. y2 in
	if calc x1 y1 z1 > calc x2 y2 z2
	then r := x2
	else l := x1
    done;
    let x = (!l +. !r) /. 2.0 in
    let y = b *. (s -. x) /. (b +. c) in
    let y = if y >= 0.0 && y <= s then y else if y > s then s else 0.0 in
    let z = s -. x -. y in
      Printf.printf "asd %f\n" (x ** a *. y ** b *. z ** c);
      Printf.printf "%.8f %.8f %.8f\n" x y z;
    );
    if a +. b +. c > 0.0 then (
      let x = s *. a /. (a +. b +. c) in
      let y = s *. b /. (a +. b +. c) in
      let z = s *. c /. (a +. b +. c) in
      let z = s -. x -. y in
      let z = max z 0.0 in
      let d = 1e10 in
      let x = floor (x *. d) /. d in
      let y = floor (y *. d) /. d in
      let z = floor (z *. d) /. d in
	Printf.printf "%.10f %.10f %.10f\n" x y z
    ) else
      Printf.printf "%.8f %.8f %.8f\n" s 0.0 0.0

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = float_of_int s in
  let a = float_of_int a in
  let b = float_of_int b in
  let c = float_of_int c in
  let l = ref 0.0 in
  let r = ref s in
  let calc x y z =
    let xa = if a = 0.0 then 0.0 else a *. log x in
    let yb = if b = 0.0 then 0.0 else b *. log y in
    let zc = if c = 0.0 then 0.0 else c *. log z in
      xa +. yb +. zc
  in
    for i = 1 to 400 do
      let x1 = (2.0 *. !l +. !r) /. 3.0 in
      let y1 = b *. (s -. x1) /. (b +. c) in
      let y1 = if y1 >= 0.0 && y1 <= s then y1 else if y1 > s then s else 0.0 in
      let z1 = s -. x1 -. y1 in
      let x2 = (!l +. 2.0 *. !r) /. 3.0 in
      let y2 = b *. (s -. x2) /. (b +. c) in
      let y2 = if y2 >= 0.0 && y2 <= s then y2 else if y2 > s then s else 0.0 in
      let z2 = s -. x2 -. y2 in
	if calc x1 y1 z1 > calc x2 y2 z2
	then r := x2
	else l := x1
    done;
    let x = (!l +. !r) /. 2.0 in
    let y = b *. (s -. x) /. (b +. c) in
    let y = if y >= 0.0 && y <= s then y else if y > s then s else 0.0 in
    let z = s -. x -. y in
      Printf.printf "%.8f %.8f %.8f\n" x y z

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = float_of_int s in
  let a = float_of_int a in
  let b = float_of_int b in
  let c = float_of_int c in
  (*let l = ref 0.0 in
  let r = ref s in
  let calc x y z =
    let xa = if a = 0.0 then 0.0 else a *. log x in
    let yb = if b = 0.0 then 0.0 else b *. log y in
    let zc = if c = 0.0 then 0.0 else c *. log z in
      xa +. yb +. zc
  in
    for i = 1 to 400 do
      let x1 = (2.0 *. !l +. !r) /. 3.0 in
      let y1 = b *. (s -. x1) /. (b +. c) in
      let y1 = if y1 >= 0.0 && y1 <= s then y1 else if y1 > s then s else 0.0 in
      let z1 = s -. x1 -. y1 in
      let x2 = (!l +. 2.0 *. !r) /. 3.0 in
      let y2 = b *. (s -. x2) /. (b +. c) in
      let y2 = if y2 >= 0.0 && y2 <= s then y2 else if y2 > s then s else 0.0 in
      let z2 = s -. x2 -. y2 in
	if calc x1 y1 z1 > calc x2 y2 z2
	then r := x2
	else l := x1
    done;
    let x = (!l +. !r) /. 2.0 in
    let y = b *. (s -. x) /. (b +. c) in
    let y = if y >= 0.0 && y <= s then y else if y > s then s else 0.0 in
    let z = s -. x -. y in
      Printf.printf "asd %f\n" (x ** a *. y ** b *. z ** c);
      Printf.printf "%.8f %.8f %.8f\n" x y z;*)
      let x = s *. a /. (a +. b +. c) in
      let y = s *. b /. (a +. b +. c) in
      let z = s *. c /. (a +. b +. c) in
	Printf.printf "%.8f %.8f %.8f\n" x y z;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = float_of_int s in
  let a = float_of_int a in
  let b = float_of_int b in
  let c = float_of_int c in
  (*let l = ref 0.0 in
  let r = ref s in
  let calc x y z =
    let xa = if a = 0.0 then 0.0 else a *. log x in
    let yb = if b = 0.0 then 0.0 else b *. log y in
    let zc = if c = 0.0 then 0.0 else c *. log z in
      xa +. yb +. zc
  in
    for i = 1 to 400 do
      let x1 = (2.0 *. !l +. !r) /. 3.0 in
      let y1 = b *. (s -. x1) /. (b +. c) in
      let y1 = if y1 >= 0.0 && y1 <= s then y1 else if y1 > s then s else 0.0 in
      let z1 = s -. x1 -. y1 in
      let x2 = (!l +. 2.0 *. !r) /. 3.0 in
      let y2 = b *. (s -. x2) /. (b +. c) in
      let y2 = if y2 >= 0.0 && y2 <= s then y2 else if y2 > s then s else 0.0 in
      let z2 = s -. x2 -. y2 in
	if calc x1 y1 z1 > calc x2 y2 z2
	then r := x2
	else l := x1
    done;
    let x = (!l +. !r) /. 2.0 in
    let y = b *. (s -. x) /. (b +. c) in
    let y = if y >= 0.0 && y <= s then y else if y > s then s else 0.0 in
    let z = s -. x -. y in
      Printf.printf "asd %f\n" (x ** a *. y ** b *. z ** c);
      Printf.printf "%.8f %.8f %.8f\n" x y z;*)
    if a +. b +. c > 0.0 then (
      let x = s *. a /. (a +. b +. c) in
      let y = s *. b /. (a +. b +. c) in
      let z = s *. c /. (a +. b +. c) in
	Printf.printf "%.8f %.8f %.8f\n" x y z
    ) else
      Printf.printf "%.8f %.8f %.8f\n" s 0.0 0.0

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Scanf.bscanf sb "%d " (fun s -> s) in
  let b = Scanf.bscanf sb "%d " (fun s -> s) in
  let c = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = float_of_int s in
  let a = float_of_int a in
  let b = float_of_int b in
  let c = float_of_int c in
  let l = ref 0.0 in
  let r = ref s in
  let calc x y z =
    a *. log x +. b *. log y +. c *. log z
  in
    for i = 1 to 100 do
      let x1 = (2.0 *. !l +. !r) /. 3.0 in
      let y1 = b *. (s -. x1) /. (b +. c) in
      let y1 = if y1 >= 0.0 && y1 <= s then y1 else if y1 > s then s else 0.0 in
      let z1 = s -. x1 -. y1 in
      let x2 = (!l +. 2.0 *. !r) /. 3.0 in
      let y2 = b *. (s -. x2) /. (b +. c) in
      let y2 = if y2 >= 0.0 && y2 <= s then y2 else if y2 > s then s else 0.0 in
      let z2 = s -. x2 -. y2 in
	if calc x1 y1 z1 > calc x2 y2 z2
	then r := x2
	else l := x1
    done;
    let x = (!l +. !r) /. 2.0 in
    let y = b *. (s -. x) /. (b +. c) in
    let y = if y >= 0.0 && y <= s then y else if y > s then s else 0.0 in
    let z = s -. x -. y in
      Printf.printf "%.8f %.8f %.8f\n" x y z


Gerald has a friend, Pollard. Pollard is interested in lucky tickets (ticket is a sequence of digits). At first he thought that a ticket is lucky if between some its digits we can add arithmetic signs and brackets so that the result obtained by the arithmetic expression was number 100. But he quickly analyzed all such tickets and moved on to a more general question. Now he explores k-lucky tickets.Pollard sais that a ticket is k-lucky if we can add arithmetic operation signs between its digits to the left or right of them (i.e., "+", "-", " × ") and brackets so as to obtain the correct arithmetic expression whose value would equal k. For example, ticket "224201016" is 1000-lucky as ( - 2 - (2 + 4)) × (2 + 0) + 1016 = 1000.Pollard was so carried away by the lucky tickets that he signed up for a seminar on lucky tickets and, as far as Gerald knows, Pollard will attend it daily at 7 pm in some famous institute and will commute to it in the same tram for m days. In this tram tickets have eight digits. And Gerald wants to make a surprise for Pollard: each day Pollard will receive a tram k-lucky ticket. The conductor has already agreed to give Pollard certain tickets during all these m days and he only wants Gerald to tell him what kind of tickets to give out. In this regard, help Gerald pick exactly m distinct k-lucky tickets.

The single line contains two integers k and m (0 ≤ k ≤ 104, 1 ≤ m ≤ 3·105).

Print m lines. Each line must contain exactly 8 digits — the k-winning ticket. The tickets may begin with 0, all tickets must be distinct. If there are more than m distinct k-lucky tickets, print any m of them. It is guaranteed that at least m distinct k-lucky tickets exist. The tickets can be printed in any order.

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

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;

let (k, m) = (read_int(), read_int());;

let ans = ref IntSet.empty;;

let counter = ref 0;;

let rec build cval rest pos orig =
    let append v = (
        if (IntSet.mem v !ans) or (!counter == k) then ()
        else (counter := !counter + 1; ans := IntSet.add v !ans)
    )
    in
        if pos = 4 then
            let delta = abs ((abs cval) - m) in
                if (delta < 10000) then (
                    append (delta * 10000 + orig);
                    append (orig * 10000 + delta);
                )
                else ()
        else
            let (dig, rest') = (rest mod 10, rest / 10) in (
                build (cval + dig) rest' (pos + 1) orig;
                build (cval - dig) rest' (pos + 1) orig;
                build (cval * dig) rest' (pos + 1) orig
            )
    ;;

for i = 0 to 9999 do
    build (i mod 10) (i / 10) 1 i
done;;

let print a t = Printf.printf "%08d\n" a in IntSet.fold print !ans ();;

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

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x);;

let (k, m) = (read_int(), read_int());;

Printf.printf "%d %d\n" m k;;

let ans = ref IntSet.empty;;

let rec build set cval rest pos orig =
    let append s v =
        if (IntSet.cardinal s < k) then (IntSet.add v s) else s
    in
        if pos = 4 then
            let delta = abs ((abs cval) - m) in
                if (delta < 10000) then
                    append (append set (delta * 10000 + orig)) (orig * 10000 + delta)
                else set
        else
            let (dig, rest') = (rest mod 10, rest / 10) in
               let s0 = build set (cval + dig) rest' (pos + 1) orig in
               let s1 = build s0 (cval - dig) rest' (pos + 1) orig in
               let s2 = build s1 (cval * dig) rest' (pos + 1) orig
               in s2
    ;;

for i = 0 to 9999 do
    ans := build !ans (i mod 10) (i / 10) 1 i
done;;

let print a t = Printf.printf "%08d\n" a in IntSet.fold print !ans ();;


We are sum for we are manySome NumberThis version of the problem differs from the previous one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved.You are given two positive integers $$$l$$$ and $$$r$$$.Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l \le i < j < k \le r$$$ and $$$\operatorname{lcm}(i,j,k) \ge i + j + k$$$.Here $$$\operatorname{lcm}(i, j, k)$$$ denotes the least common multiple (LCM) of integers $$$i$$$, $$$j$$$, and $$$k$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$\bf{1 \le t \le 10^5}$$$). Description of the test cases follows. The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^5$$$, $$$l + 2 \le r$$$).

For each test case print one integer — the number of suitable triplets.

NoteIn the first test case, there are $$$3$$$ suitable triplets:   $$$(1,2,3)$$$,  $$$(1,3,4)$$$,  $$$(2,3,4)$$$. In the second test case, there is $$$1$$$ suitable triplet:   $$$(3,4,5)$$$.

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let max_n = 200000

module SegTree = struct
  type t = int64 array

  let init n = A.make n I.zero

  let add t i x =
    let v = ref i in
    let n = A.length t in
    while !v < n do
      t.(!v) <- I.add t.(!v) x;
      v := !v lor (!v + 1);
    done

  let pref_sum t i =
    let r = ref i in
    let res = ref I.zero in
    while !r >= 0 do
      res := I.add !res t.(!r);
      r := (!r land (!r + 1)) - 1;
    done;
    !res

  let sum t l r =  I.sub (pref_sum t r) (pref_sum t (l - 1))
end

module L_ = struct
  let filter_map f =
    let rec aux accu = function
      | [] -> L.rev accu
      | x :: l ->
          match f x with
          | None -> aux accu l
          | Some v -> aux (v :: accu) l
    in
    aux []
end

type query = {
  l: int;
  q_index: int;
}

let find_first_div n =
  let i = ref 2 in
  let found = ref (-1) in
  while !i * !i <= n && !found = -1 do
    if (n mod !i = 0) then found := !i;
    i := !i + 1;
  done;
  if (!found = -1) then n
  else !found

let () =
  let q = read_int () in
  let original_queries: (int * int) array = Array.make q (0, 0) in
  let ans = Array.make q I.zero in
  let (events: query list array) = Array.init (max_n + 1) (fun _ -> []) in
  for i = 0 to q - 1 do
    let l = read_int () in
    let r = read_int () in
    original_queries.(i) <- (l, r);
    events.(r) <- {l; q_index = i;} :: events.(r);
  done;

  let (divisors: int list array) = Array.make (max_n + 1) [] in
  let pw2 = Array.make (max_n + 1) 0 in
  divisors.(1) <- [1];

  let inc_tree = SegTree.init (max_n + 1) in
  for k = 2 to max_n do
    let fd = find_first_div k in
    let divs_prev = divisors.(k / fd) in
    divisors.(k) <- L.sort_uniq compare (divs_prev @ List.map (fun x -> x * fd) divs_prev);
    if (k mod 2 = 0) then pw2.(k) <- pw2.(k / 2) + 1;

    let divs = divisors.(k) in
    let divs_num = L.length divs in
    L.iteri (fun idx i ->
      if (i <> k) then
        SegTree.add inc_tree i @@ I.of_int @@ divs_num - idx - 2;
    ) divs;
    let p = pw2.(k) in

    let new_divs =
        A.of_list @@
        L.tl @@
        L.fast_sort (fun x y -> -compare x y) @@
        divs @ L_.filter_map (fun d ->
          if (d * 2 < k && pw2.(d) == p) then Some (d * 2)
          else None
        ) divs in
    let sj_p = ref 0 and ptr_j = ref (-1) in
    A.iteri (fun idx i ->
      while (!ptr_j <> -1 && new_divs.(!ptr_j) + i <= k) do
        let cd = new_divs.(!ptr_j) in
        if (pw2.(cd) = p + 1) then sj_p := !sj_p - 1;
        ptr_j := !ptr_j - 1
      done;

      if (pw2.(i) = p + 1) then
        SegTree.add inc_tree i @@ I.of_int @@ !ptr_j + 1
      else
        SegTree.add inc_tree i @@ I.of_int !sj_p;

      while (!ptr_j + 1 <= idx && new_divs.(!ptr_j + 1) + i > k) do
        let cd = new_divs.(!ptr_j + 1) in
        if (pw2.(cd) = p + 1) then sj_p := !sj_p + 1;
        ptr_j := !ptr_j + 1
      done;
    ) new_divs;

    L.iter (fun q ->
      ans.(q.q_index) <- SegTree.sum inc_tree q.l k;
    ) events.(k)
  done;

  for i = 0 to q - 1 do
    let n = snd original_queries.(i) - fst original_queries.(i) + 1 in
    let real_ans =
      I.(sub
        (div
          (mul (mul (of_int n) (of_int @@ n - 1)) (of_int @@ n - 2))
          (of_int 6))
        ans.(i)) in
    pf "%Ld\n" real_ans;
  done;

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let max_n = 200000

module SegTree = struct
  type t = int64 array

  let init n = A.make (2 * n) I.zero

  let add t i x =
    let v = ref i in
    let n = A.length t in
    while !v < n do
      t.(!v) <- I.add t.(!v) x;
      v := !v lor (!v + 1);
    done

  let pref_sum t i =
    let r = ref i in
    let res = ref I.zero in
    while !r >= 0 do
      res := I.add !res t.(!r);
      r := (!r land (!r + 1)) - 1;
    done;
    !res

  let sum t l r =  I.sub (pref_sum t r) (pref_sum t (l - 1))
end

module L_ = struct
  let filter_map f =
    let rec aux accu = function
      | [] -> L.rev accu
      | x :: l ->
          match f x with
          | None -> aux accu l
          | Some v -> aux (v :: accu) l
    in
    aux []
end

type query = {
  l: int;
  q_index: int;
}

let find_first_div n =
  let i = ref 2 in
  let found = ref (-1) in
  while !i * !i <= n && !found = -1 do
    if (n mod !i = 0) then found := !i;
    i := !i + 1;
  done;
  if (!found = -1) then n
  else !found

let () =
  let q = read_int () in
  let original_queries: (int * int) array = Array.make q (0, 0) in
  let ans = Array.make q I.zero in
  let (events: query list array) = Array.init (max_n + 1) (fun _ -> []) in
  for i = 0 to q - 1 do
    let l = read_int () in
    let r = read_int () in
    original_queries.(i) <- (l, r);
    events.(r) <- {l; q_index = i;} :: events.(r);
  done;

  let (divisors: int list array) = Array.make (max_n + 1) [] in
  let pw2 = Array.make (max_n + 1) 0 in
  divisors.(1) <- [1];

  let inc_tree = SegTree.init max_n in
  for k = 2 to max_n do
    let fd = find_first_div k in
    let divs_prev = divisors.(k / fd) in
    divisors.(k) <- L.sort_uniq compare (divs_prev @ List.map (fun x -> x * fd) divs_prev);
    if (k mod 2 = 0) then pw2.(k) <- pw2.(k / 2) + 1;

    let divs = divisors.(k) in
    let divs_num = L.length divs in
    L.iteri (fun idx i ->
      if (i <> k) then
        SegTree.add inc_tree i @@ I.of_int @@ divs_num - idx - 2;
    ) divs;
    let p = pw2.(k) in

    let new_divs =
        A.of_list @@
        L.tl @@
        L.fast_sort (fun x y -> -compare x y) @@
        divs @ L_.filter_map (fun d ->
          if (d * 2 < k && pw2.(d) == p) then Some (d * 2)
          else None
        ) divs in
    let sj_p = ref 0 and ptr_j = ref (-1) in
    A.iteri (fun idx i ->
      while (!ptr_j <> -1 && new_divs.(!ptr_j) + i <= k) do
        let cd = new_divs.(!ptr_j) in
        if (pw2.(cd) = p + 1) then sj_p := !sj_p - 1;
        ptr_j := !ptr_j - 1
      done;

      if (pw2.(i) = p + 1) then
        SegTree.add inc_tree i @@ I.of_int @@ !ptr_j + 1
      else
        SegTree.add inc_tree i @@ I.of_int !sj_p;

      while (!ptr_j + 1 <= idx && new_divs.(!ptr_j + 1) + i > k) do
        let cd = new_divs.(!ptr_j + 1) in
        if (pw2.(cd) = p + 1) then sj_p := !sj_p + 1;
        ptr_j := !ptr_j + 1
      done;
    ) new_divs;

    L.iter (fun q ->
      ans.(q.q_index) <- SegTree.sum inc_tree q.l k;
    ) events.(k)
  done;

  for i = 0 to q - 1 do
    let n = snd original_queries.(i) - fst original_queries.(i) + 1 in
    let real_ans =
      I.(sub
        (div
          (mul (mul (of_int n) (of_int @@ n - 1)) (of_int @@ n - 2))
          (of_int 6))
        ans.(i)) in
    pf "%Ld\n" real_ans;
  done;

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let max_n = 200000

module SegTree = struct
  let init = ()
  let add _i (_x: int64) = ()

  let sum _l _r: int64 = I.zero

end

type query = {
  l: int;
  q_index: int;
}

let find_first_div n =
  let i = ref 2 in
  let found = ref (-1) in
  while !i * !i <= n && !found = -1 do
    if (n mod !i = 0) then found := !i;
    i := !i + 1;
  done;
  if (!found = -1) then n
  else !found

let () =
  (* let q = read_int () in
  let original_queries: (int * int) array = Array.make q (0, 0) in
  let ans = Array.make q I.zero in
  let (events: query list array) = Array.init (max_n + 1) (fun _ -> []) in
  for i = 0 to q - 1 do
    let l = read_int () in
    let r = read_int () in
    original_queries.(i) <- (l, r);
    events.(r) <- {l; q_index = i;} :: events.(r);
  done; *)

  let (divisors: int list array) = Array.make (max_n + 1) [] in
  let pw2 = Array.make (max_n + 1) 0 in
  divisors.(1) <- [1];

  for i = 2 to max_n do
    let fd = find_first_div i in
    let divs_prev = divisors.(i / fd) in
    divisors.(i) <- L.sort_uniq compare (divs_prev @ List.map (fun x -> x * fd) divs_prev);
    if (i mod 2 = 0) then pw2.(i) <- pw2.(i / 2) + 1;
  done;

module A = Array
module L = List
module I = Int64

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let max_n = 200000

module SegTree = struct
  let init = ()
  let add _i (_x: int64) = ()

  let sum _l _r: int64 = I.zero

end

type query = {
  l: int;
  q_index: int;
}

let find_first_div n =
  let i = ref 2 in
  let found = ref (-1) in
  while !i * !i <= n && !found = -1 do
    if (n mod !i = 0) then found := !i;
    i := !i + 1;
  done;
  if (!found = -1) then n
  else !found

let () =
  (* let q = read_int () in
  let original_queries: (int * int) array = Array.make q (0, 0) in
  let (events: query list array) = Array.init max_n (fun _ -> []) in
  for i = 0 to q - 1 do
    let l = read_int () in
    let r = read_int () in
    original_queries.(i) <- (l, r);
    events.(r) <- {l; q_index = i;} :: events.(r);
  done; *)

  let (divisors: int list array) = Array.make (max_n + 1) [] in
  let pw2 = Array.make (max_n + 1) 0 in
  divisors.(0) <- [1];

  for i = 2 to max_n do
    let fd = find_first_div i in
    let divs_prev = divisors.(i / fd) in
    divisors.(i) <- List.sort_uniq compare (divs_prev @ List.map (fun x -> x * fd) divs_prev);
    if (i mod 2 = 0) then pw2.(i) <- pw2.(i / 2) + 1;
  done;


Peter likes to travel by train. He likes it so much that on the train he falls asleep. Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.

The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order.  The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order.

Output one of the four words without inverted commas:    «forward» — if Peter could see such sequences only on the way from A to B;  «backward» — if Peter could see such sequences on the way from B to A;  «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A;  «fantasy» — if Peter could not see such sequences.

NoteIt is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.

let (|>) x f = f x

module String = struct
  include String

  exception Exit

  let find_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = pos to n-m do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let find str sub = find_from str 0 sub

  let rfind_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = min (n-m) pos downto 0 do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let rfind str sub = rfind_from str (String.length str) sub

  let rev str =
    let r = String.copy str in
    let rec go i j =
      if i < j then (
        let t = r.[i] in
        r.[i] <- r.[j];
        r.[j] <- t;
        go (i+1) (j-1)
      )
    in
    go 0 (String.length str-1);
    r
end

let () =
  let a = read_line () in
  let aa = String.rev a in
  let b = read_line () in
  let c = read_line () in
  let b1 = String.find a b
  and b2 = String.find aa b
  and c1 = String.rfind a c
  and c2 = String.rfind aa c in
  print_endline [|"fantasy"; "forward"; "backward"; "both"|].(
    (if 0 <= b1 && b1+String.length b <= c1 then 1 else 0) + (if 0 <= b2 && b2+String.length b <= c2 then 2 else 0))

let (|>) x f = f x

module String = struct
  include String

  exception Exit

  let find_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = pos to n-m do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let find str sub = find_from str 0 sub

  let rfind_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = min (n-m) pos downto 0 do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let rfind str sub = rfind_from str (String.length str) sub
end

let () =
  let a = read_line () in
  let b = read_line () in
  let c = read_line () in
  let b1 = String.find a b
  and b2 = String.rfind a b
  and c1 = String.find a c
  and c2 = String.rfind a c in
  print_endline [|"fantasy"; "forward"; "backward"; "both"|].(
    (if b1+String.length b <= c2 then 1 else 0) + (if c1+String.length c <= b2 then 2 else 0))

let (|>) x f = f x

module String = struct
  include String

  exception Exit

  let find_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = pos to n-m do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let find str sub = find_from str 0 sub

  let rfind_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = min (n-m) pos downto 0 do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let rfind str sub = rfind_from str (String.length str) sub

  let rev str =
    let r = String.copy str in
    let rec go i j =
      if i < j then (
        let t = r.[i] in
        r.[i] <- r.[j];
        r.[j] <- t
      )
    in
    go 0 (String.length str-1);
    r
end

let () =
  let a = read_line () in
  let aa = String.rev a in
  let b = read_line () in
  let c = read_line () in
  let b1 = String.find a b
  and b2 = String.find aa b
  and c1 = String.find a c
  and c2 = String.find aa c in
  print_endline [|"fantasy"; "forward"; "backward"; "both"|].(
    (if 0 <= b1 && b1+String.length b <= c2 then 1 else 0) + (if 0 <= c1 && c1+String.length c <= b2 then 2 else 0))

let (|>) x f = f x

module String = struct
  include String

  exception Exit

  let find_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = pos to n-m do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let find str sub = find_from str 0 sub

  let rfind_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = min (n-m) pos downto 0 do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let rfind str sub = rfind_from str (String.length str) sub
end

let () =
  let a = read_line () in
  let b = read_line () in
  let c = read_line () in
  let b1 = String.find a b
  and b2 = String.rfind a b
  and c1 = String.find a c
  and c2 = String.rfind a c in
  print_endline [|"fantasy"; "forward"; "backward"; "both"|].((if b1 < c2 then 1 else 0) + (if c1 < b2 then 2 else 0))

let (|>) x f = f x

module String = struct
  include String

  exception Exit

  let find_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = pos to n-m do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let find str sub = find_from str 0 sub

  let rfind_from str pos sub =
    let n = length str
    and m = length sub in
    if m = 0 then
      pos
    else
      let found = ref (-1) in
      try
        for i = min (n-m) pos downto 0 do
          let j = ref 0 in
          while unsafe_get str (i + !j) = unsafe_get sub (!j) do
            incr j;
            if !j = m then (found := i; raise Exit)
          done
        done;
        -1
      with Exit ->
        !found

  let rfind str sub = rfind_from str (String.length str) sub
end

let () =
  let a = read_line () in
  let b = read_line () in
  let c = read_line () in
  let b1 = String.find a b
  and b2 = String.rfind a b
  and c1 = String.find a c
  and c2 = String.rfind a c in
  print_endline [|"fantasy"; "forward"; "backward"; "both"|].(
    (if 0 <= b1 && b1+String.length b <= c2 then 1 else 0) + (if 0 <= c1 && c1+String.length c <= b2 then 2 else 0))


Polycarp found under the Christmas tree an array $$$a$$$ of $$$n$$$ elements and instructions for playing with it:   At first, choose index $$$i$$$ ($$$1 \leq i \leq n$$$) — starting position in the array. Put the chip at the index $$$i$$$ (on the value $$$a_i$$$).  While $$$i \leq n$$$, add $$$a_i$$$ to your score and move the chip $$$a_i$$$ positions to the right (i.e. replace $$$i$$$ with $$$i + a_i$$$).  If $$$i > n$$$, then Polycarp ends the game. For example, if $$$n = 5$$$ and $$$a = [7, 3, 1, 2, 3]$$$, then the following game options are possible:   Polycarp chooses $$$i = 1$$$. Game process: $$$i = 1 \overset{+7}{\longrightarrow} 8$$$. The score of the game is: $$$a_1 = 7$$$.  Polycarp chooses $$$i = 2$$$. Game process: $$$i = 2 \overset{+3}{\longrightarrow} 5 \overset{+3}{\longrightarrow} 8$$$. The score of the game is: $$$a_2 + a_5 = 6$$$.  Polycarp chooses $$$i = 3$$$. Game process: $$$i = 3 \overset{+1}{\longrightarrow} 4 \overset{+2}{\longrightarrow} 6$$$. The score of the game is: $$$a_3 + a_4 = 3$$$.  Polycarp chooses $$$i = 4$$$. Game process: $$$i = 4 \overset{+2}{\longrightarrow} 6$$$. The score of the game is: $$$a_4 = 2$$$.  Polycarp chooses $$$i = 5$$$. Game process: $$$i = 5 \overset{+3}{\longrightarrow} 8$$$. The score of the game is: $$$a_5 = 3$$$. Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from $$$1$$$ to $$$n$$$ in such a way as to maximize his result.

NoteThe first test case is explained in the statement.In the second test case, the maximum score can be achieved by choosing $$$i = 1$$$.In the third test case, the maximum score can be achieved by choosing $$$i = 2$$$.In the fourth test case, the maximum score can be achieved by choosing $$$i = 1$$$.

(** https://codeforces.com/contest/1472/problem/C *)

let read_int () = Scanf.scanf " %d " (fun a -> a)

let get_array size_array =
    let arr = Array.make size_array 0L in
    let rec helper i =
        if i < size_array
        then (
            let new_val =
                if i = size_array-1
                then Scanf.scanf "%Ld\n" (fun a -> a)
                else Scanf.scanf " %Ld " (fun a -> a) in
            arr.(i) <- new_val;
            helper (i+1)
        ) in
    helper 0;
    arr

let max_score arr =
    let (++) = Int64.add and
    (~++) = Int64.of_int and
    (~-+) = Int64.to_int and
    size = Array.length arr in
    let size64 = ~++size in
    let score_arr = Array.make size 0L in
    let rec helper i =
        if i >= 0
        then (
            let tile_delta = arr.(i) in
            if ~++i ++ tile_delta >= size64
            then score_arr.(i) <- tile_delta
            else score_arr.(i) <- score_arr.(i + (~-+tile_delta)) ++ tile_delta;
            helper (i-1)
        ) in
    helper (size-1);
    Array.fold_left
        (fun acc el -> if el > acc then el else acc)
        0L
        score_arr

let solve_n nb_cases =
    let rec helper i =
        if i > 0
        then (
            let size_array = read_int () in
            let arr = get_array size_array in
            max_score arr
            |> Printf.printf "%Ld\n";
            helper (i-1)
        ) in
    helper nb_cases

let () =
    let nb_cases = read_int () in
    solve_n nb_cases


It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows:   Choose to stop or to continue to collect bottles.  If the choice was to continue then choose some bottle and walk towards it.  Pick this bottle and walk to the recycling bin.  Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.

First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively. The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground. Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle. It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.

Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

NoteConsider the first sample.Adil will use the following path: .Bera will use the following path: .Adil's path will be  units long, while Bera's path will be  units long.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) +. a) 0.0

let inf = 1e20
let argmin i j f =
  fold i j (
    fun k (index,v) -> let m = (f k) in if m<v then (k,m) else (index,v)
  ) (i-1, inf)

let ( -- ) (x1,y1) (x2,y2) = (x1-.x2, y1-.y2)
let len (x,y) = sqrt(x*.x +. y*.y)
let dist a b = len (a -- b)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_float () = float (read_int())
let read2 () = let x = read_float() in (x, read_float())

let () =
  let a = read2() in
  let b = read2() in
  let t = read2() in
  let n = read_int () in

  let bot = Array.init n (fun i -> read2()) in

  let ca = Array.init n (fun i -> (dist bot.(i) a) -. (dist bot.(i) t)) in
  let cb = Array.init n (fun i -> (dist bot.(i) b) -. (dist bot.(i) t)) in

  let (besta, _) = argmin 0 (n-1) (fun i -> ca.(i)) in
  let (bestb, _) = argmin 0 (n-1) (fun i -> cb.(i)) in

  if n = 1 then
    let answer = (min (dist bot.(0) a) (dist bot.(0) b)) +. (dist bot.(0) t)  in
    printf "%.8f\n" answer
  else

  let adjust =
    let trypair besta bestb =
      match (ca.(besta) < 0.0, cb.(bestb) < 0.0) with
	| (true,true) -> ca.(besta) +. cb.(bestb)
	| (true,false) -> ca.(besta)
	| (false,true) -> cb.(bestb)
	| (false,false) -> min ca.(besta) cb.(bestb)
    in

    if besta = bestb then (
      let rec loop mincost i = if i=n then mincost else
	  if i = besta then loop mincost (i+1)
	  else (
	    let opt1 = trypair besta i in
	    let opt2 = trypair i besta in
	    let bopt = min opt1 opt2 in
	    loop (min bopt mincost) (i+1)
	  )
      in
      loop inf 0
    ) else trypair besta bestb
  in

  let total = sum 0 (n-1) (fun i -> 2.0 *. (dist bot.(i) t)) in
  let answer = total +. adjust in

  printf "%.8f\n" answer