It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving. Formally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i) ≠ f2(i).Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109 + 7.

The first line contains two integers n and m (1 ≤ n ≤ 105, 1 ≤ m ≤ 106) — the number of gyms and the number of Pokemon types. The next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer gi (1 ≤ gi ≤ 105) — the number of Pokemon in the i-th gym. After that gi integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m. The total number of Pokemons (the sum of all gi) does not exceed 5·105.

Output the number of valid evolution plans modulo 109 + 7.

NoteIn the first case, the only possible evolution plan is: In the second case, any permutation of (1,  2,  3) is valid.In the third case, there are two possible plans:  In the fourth case, the only possible evolution plan is:

let getnum =
  let l = 512000 in
  let buf = String.make l ' ' in
  let s = ref 0 in
  let pos = ref 0 in
  let getc () =
    if !pos >= !s then (
      let new_s = input stdin buf 0 l in
	(*if new_s = 0 then raise End_of_file;*)
	s := new_s;
	if new_s = 0 then (
	  buf.[0] <- '\000';
	  s := 1;
	);
	pos := 0;
    );
    let c = buf.[!pos] in
      incr pos;
      c
  in
  let aux () =
    let n = ref 0 in
    let k = ref 1 in
      if !s - !pos < 50 then (
	let c = ref (getc ()) in
	  while !c <= ' ' do
	    c := getc ()
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := getc ();
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := getc ()
	  done;
	  !n * !k
      ) else (
	let c = ref (buf.[!pos]) in
	  incr pos;
	  while !c <= ' ' do
	    c := buf.[!pos];
	    incr pos;
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := buf.[!pos];
	    incr pos;
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := buf.[!pos];
	    incr pos;
	  done;
	  !n * !k
      )
  in
    aux

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let mm = 1000000007 in
  let mmm = Int64.of_int mm in
  let mmod n =
    let r = Int64.rem n mmm in
      if r < 0L
      then r +| mmm
      else r
  in
  let n = getnum () in
  let m = getnum () in
  let fact = Array.make (m + 1) 1 in
  let () =
    for i = 2 to m do
      fact.(i) <-
	Int64.to_int
	(Int64.rem
	   (Int64.of_int fact.(i - 1) *| Int64.of_int i) mmm);
    done;
  in
  let s = Array.make (m + 1) [] in
  let sidx = ref 0 in
    for i = 0 to n - 1 do
      let g = getnum () in
      let a = Array.make (g + 1) 0 in
	for j = 0 to g - 1 do
	  a.(j) <- getnum ();
	done;
	a.(g) <- 10000000;
	Array.sort compare a;
	let prev = ref a.(0) in
	let cnt = ref 1 in
	let ht = Hashtbl.create 10 in
	  for j = 1 to g do
	    if a.(j) = !prev
	    then incr cnt
	    else (
	      (try
		 let xs = Hashtbl.find ht !cnt in
		   Hashtbl.replace ht !cnt (!prev :: xs)
	       with
		 | Not_found ->
		     Hashtbl.replace ht !cnt [!prev]
	      );
	      prev := a.(j);
	      cnt := 1;
	    )
	  done;
	  Hashtbl.iter
	    (fun _cnt xs ->
	       List.iter
		 (fun x ->
		    (*Printf.printf "qwe %d %d\n" _cnt x;*)
		    s.(x) <- !sidx :: s.(x)
		 ) xs;
	       incr sidx;
	    ) ht
    done;
    let ht = Hashtbl.create 10 in
      (*for i = 1 to m do
	Printf.printf "%d" i;
	List.iter (Printf.printf " %d") s.(i);
	Printf.printf "\n"
	done;*)
      for i = 1 to m do
	(try
	   let c = Hashtbl.find ht s.(i) in
	     Hashtbl.replace ht s.(i) (c + 1)
	 with
	   | Not_found ->
	       Hashtbl.replace ht s.(i) 1
	);
      done;
      let res = ref 1L in
	Hashtbl.iter
	  (fun _ c ->
	     res := mmod (!res *| Int64.of_int fact.(c))
	  ) ht;
	Printf.printf "%Ld\n" !res

open Printf
open Scanf

let p = 1_000_000_007L

let ( %% ) x n = let y = Int64.rem x n in if y<0L then Int64.add y n else y
let ( ** ) a b = (Int64.mul a b) %% p
let ( ++ ) a b = (Int64.add a b) %% p
let long x = Int64.of_int x

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

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in  (* gyms *)
  let m = read_int () in  (* pokymon types *)
  let dummy = Hashtbl.create 10 in
  let ht = Array.make n dummy in

  for i=0 to n-1 do
    let np = read_int () in
    let h = Hashtbl.create 10 in
    ht.(i) <- h;
    let increment t =
      let c = try Hashtbl.find h t with Not_found -> 0 in
      Hashtbl.replace h t (c+1)
    in
    for j=0 to np-1 do
      let pk = read_int() in
      increment (pk-1);
    done;
  done;

  let cl_array = Array.make m 0 in

  let classno = ref 1 in (* the next class number to assign *)

  for gym = 0 to n-1 do
  (* incorporate the effect of this guy on our class array *)
    let classht = Hashtbl.create 10 in
    Hashtbl.iter (fun pk count ->
      let pair = (cl_array.(pk), count) in
      if not (Hashtbl.mem classht pair) then (
	Hashtbl.add classht pair !classno;
	cl_array.(pk) <- !classno;
	classno := 1 + !classno
      ) else (
	cl_array.(pk) <- Hashtbl.find classht pair
      )
    ) ht.(gym);
  done;

  let hist = Array.make !classno 0 in

  for i=0 to m-1 do
    hist.(cl_array.(i)) <- hist.(cl_array.(i)) + 1
  done;

  let fact = Array.make (m+1) 1L in

  for i=1 to m do
    fact.(i) <- fact.(i-1) ** (long i)
  done;

  let answer = prod 0 (!classno -1) (fun cl -> fact.(hist.(cl))) in

  printf "%Ld\n" answer

let getnum =
  let l = 512000 in
  let buf = String.make l ' ' in
  let s = ref 0 in
  let pos = ref 0 in
  let getc () =
    if !pos >= !s then (
      let new_s = input stdin buf 0 l in
	(*if new_s = 0 then raise End_of_file;*)
	s := new_s;
	if new_s = 0 then (
	  buf.[0] <- '\000';
	  s := 1;
	);
	pos := 0;
    );
    let c = buf.[!pos] in
      incr pos;
      c
  in
  let aux () =
    let n = ref 0 in
    let k = ref 1 in
      if !s - !pos < 50 then (
	let c = ref (getc ()) in
	  while !c <= ' ' do
	    c := getc ()
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := getc ();
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := getc ()
	  done;
	  !n * !k
      ) else (
	let c = ref (buf.[!pos]) in
	  incr pos;
	  while !c <= ' ' do
	    c := buf.[!pos];
	    incr pos;
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := buf.[!pos];
	    incr pos;
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := buf.[!pos];
	    incr pos;
	  done;
	  !n * !k
      )
  in
    aux

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let mm = 1000000007 in
  let mmm = Int64.of_int mm in
  let mmod n =
    let r = Int64.rem n mmm in
      if r < 0L
      then r +| mmm
      else r
  in
  let n = getnum () in
  let m = getnum () in
  let fact = Array.make (m + 1) 1 in
  let () =
    for i = 2 to m do
      fact.(i) <-
	Int64.to_int
	(Int64.rem
	   (Int64.of_int fact.(i - 1) *| Int64.of_int i) mmm);
    done;
  in
  let s = Array.make (m + 1) [] in
  let sidx = ref 0 in
    for i = 0 to n - 1 do
      let g = getnum () in
      let a = Array.make (g + 1) 0 in
	for j = 0 to g - 1 do
	  a.(j) <- getnum ();
	done;
	a.(g) <- 1000000;
	Array.sort compare a;
	let prev = ref a.(0) in
	let cnt = ref 1 in
	let ht = Hashtbl.create 10 in
	  for j = 1 to g do
	    if a.(j) = !prev
	    then incr cnt
	    else (
	      (try
		 let xs = Hashtbl.find ht !cnt in
		   Hashtbl.replace ht !cnt (!prev :: xs)
	       with
		 | Not_found ->
		     Hashtbl.replace ht !cnt [!prev]
	      );
	      prev := a.(j);
	      cnt := 1;
	    )
	  done;
	  Hashtbl.iter
	    (fun _cnt xs ->
	       List.iter
		 (fun x ->
		    (*Printf.printf "qwe %d %d\n" _cnt x;*)
		    s.(x) <- !sidx :: s.(x)
		 ) xs;
	       incr sidx;
	    ) ht
    done;
    let ht = Hashtbl.create 10 in
      (*for i = 1 to m do
	Printf.printf "%d" i;
	List.iter (Printf.printf " %d") s.(i);
	Printf.printf "\n"
	done;*)
      for i = 1 to m do
	(try
	   let c = Hashtbl.find ht s.(i) in
	     Hashtbl.replace ht s.(i) (c + 1)
	 with
	   | Not_found ->
	       Hashtbl.replace ht s.(i) 1
	);
      done;
      let res = ref 1L in
	Hashtbl.iter
	  (fun _ c ->
	     res := mmod (!res *| Int64.of_int fact.(c))
	  ) ht;
	Printf.printf "%Ld\n" !res

let getnum =
  let l = 5120000 in
  let buf = String.make l ' ' in
  let s = ref 0 in
  let pos = ref 0 in
  let getc () =
    if !pos >= !s then (
      let new_s = input stdin buf 0 l in
	(*if new_s = 0 then raise End_of_file;*)
	s := new_s;
	if new_s = 0 then (
	  buf.[0] <- '\000';
	  s := 1;
	);
	pos := 0;
    );
    let c = buf.[!pos] in
      incr pos;
      c
  in
  let aux () =
    let n = ref 0 in
    let k = ref 1 in
      if !s - !pos < 50 then (
	let c = ref (getc ()) in
	  while !c <= ' ' do
	    c := getc ()
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := getc ();
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := getc ()
	  done;
	  !n * !k
      ) else (
	let c = ref (buf.[!pos]) in
	  incr pos;
	  while !c <= ' ' do
	    c := buf.[!pos];
	    incr pos;
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := buf.[!pos];
	    incr pos;
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := buf.[!pos];
	    incr pos;
	  done;
	  !n * !k
      )
  in
    aux

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let mm = 1000000007 in
  let mmm = Int64.of_int mm in
  let mmod n =
    let r = Int64.rem n mmm in
      if r < 0L
      then r +| mmm
      else r
  in
  let n = getnum () in
  let m = getnum () in
  let fact = Array.make (m + 1) 1 in
  let () =
    for i = 2 to m do
      fact.(i) <-
	Int64.to_int
	(Int64.rem
	   (Int64.of_int fact.(i - 1) *| Int64.of_int i) mmm);
    done;
  in
  let s = Array.make (m + 1) [] in
  let sidx = ref 0 in
    for i = 0 to n - 1 do
      let g = getnum () in
      let a = Array.make (g + 1) 0 in
	for j = 0 to g - 1 do
	  a.(j) <- getnum ();
	done;
	a.(g) <- 1000000;
	Array.sort compare a;
	let prev = ref a.(0) in
	let cnt = ref 1 in
	let ht = Hashtbl.create 10 in
	  for j = 1 to g do
	    if a.(j) = !prev
	    then incr cnt
	    else (
	      (try
		 let xs = Hashtbl.find ht !cnt in
		   Hashtbl.replace ht !cnt (!prev :: xs)
	       with
		 | Not_found ->
		     Hashtbl.replace ht !cnt [!prev]
	      );
	      prev := a.(j);
	      cnt := 1;
	    )
	  done;
	  Hashtbl.iter
	    (fun _cnt xs ->
	       List.iter
		 (fun x ->
		    (*Printf.printf "qwe %d %d\n" _cnt x;*)
		    s.(x) <- !sidx :: s.(x)
		 ) xs;
	       incr sidx;
	    ) ht
    done;
    let ht = Hashtbl.create 10 in
      (*for i = 1 to m do
	Printf.printf "%d" i;
	List.iter (Printf.printf " %d") s.(i);
	Printf.printf "\n"
	done;*)
      for i = 1 to m do
	(try
	   let c = Hashtbl.find ht s.(i) in
	     Hashtbl.replace ht s.(i) (c + 1)
	 with
	   | Not_found ->
	       Hashtbl.replace ht s.(i) 1
	);
      done;
      let res = ref 1L in
	Hashtbl.iter
	  (fun _ c ->
	     res := mmod (!res *| Int64.of_int fact.(c))
	  ) ht;
	Printf.printf "%Ld\n" !res

let getnum =
  let l = 512000 in
  let buf = String.make l ' ' in
  let s = ref 0 in
  let pos = ref 0 in
  let getc () =
    if !pos >= !s then (
      let new_s = input stdin buf 0 l in
	(*if new_s = 0 then raise End_of_file;*)
	s := new_s;
	if new_s = 0 then (
	  buf.[0] <- '\000';
	  s := 1;
	);
	pos := 0;
    );
    let c = buf.[!pos] in
      incr pos;
      c
  in
  let aux () =
    let n = ref 0 in
    let k = ref 1 in
      if !s - !pos < 50 then (
	let c = ref (getc ()) in
	  while !c <= ' ' do
	    c := getc ()
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := getc ();
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := getc ()
	  done;
	  !n * !k
      ) else (
	let c = ref (buf.[!pos]) in
	  incr pos;
	  while !c <= ' ' do
	    c := buf.[!pos];
	    incr pos;
	  done;
	  if !c = '-' then (
	    k := -1;
	    c := buf.[!pos];
	    incr pos;
	  );
	  while !c > ' ' do
	    n := !n * 10 + (Char.code !c - Char.code '0');
	    c := buf.[!pos];
	    incr pos;
	  done;
	  !n * !k
      )
  in
    aux

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let mm = 1000000007 in
  let mmm = Int64.of_int mm in
  let mmod n =
    let r = Int64.rem n mmm in
      if r < 0L
      then r +| mmm
      else r
  in
  let n = getnum () in
  let m = getnum () in
  let fact = Array.make (m + 1) 1 in
  let () =
    for i = 2 to m do
      fact.(i) <-
	Int64.to_int
	(Int64.rem
	   (Int64.of_int fact.(i - 1) *| Int64.of_int i) mmm);
    done;
  in
  let s = Array.make (m + 1) [] in
  let sidx = ref 0 in
    for i = 0 to n - 1 do
      let g = getnum () in
      let a = Array.make (g + 1) 0 in
	for j = 0 to g - 1 do
	  a.(j) <- getnum ();
	done;
	a.(g) <- 1000000;
	Array.sort compare a;
	let prev = ref a.(0) in
	let cnt = ref 1 in
	let ht = Hashtbl.create 10 in
	  for j = 1 to g do
	    if a.(j) = !prev
	    then incr cnt
	    else (
	      (try
		 let xs = Hashtbl.find ht !cnt in
		   Hashtbl.replace ht !cnt (!prev :: xs)
	       with
		 | Not_found ->
		     Hashtbl.replace ht !cnt [!prev]
	      );
	      prev := a.(j);
	      cnt := 1;
	    )
	  done;
	  Hashtbl.iter
	    (fun _cnt xs ->
	       List.iter
		 (fun x ->
		    (*Printf.printf "qwe %d %d\n" _cnt x;*)
		    s.(x) <- !sidx :: s.(x)
		 ) xs;
	       incr sidx;
	    ) ht
    done;
    let ht = Hashtbl.create 10 in
      (*for i = 1 to m do
	Printf.printf "%d" i;
	List.iter (Printf.printf " %d") s.(i);
	Printf.printf "\n"
	done;*)
      for i = 1 to m do
	(try
	   let c = Hashtbl.find ht s.(i) in
	     Hashtbl.replace ht s.(i) (c + 1)
	 with
	   | Not_found ->
	       Hashtbl.replace ht s.(i) 1
	);
      done;
      let res = ref 1L in
	Hashtbl.iter
	  (fun _ c ->
	     res := !res *| Int64.of_int fact.(c)
	  ) ht;
	Printf.printf "%Ld\n" !res


This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

NoteIn the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.

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

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

let long x = Int64.of_int x
let short x = Int64.to_int x

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))
let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)
let () =
  let n = read_int () in
  let k = read_long () in
  let a = Array.init n (fun _ -> read_long()) in
  let b = Array.init n (fun _ -> read_long()) in

  let can_bake m =
    let mp = sum 0 (n-1) (fun i -> max 0L (m ** a.(i) -- b.(i))) 0L in
    mp <= k
  in

  let rec bsearch lo hi =
    (* lo can be made, hi cannot *)
    if lo++1L = hi then lo else
      let m = (lo ++ hi) // 2L in
      if can_bake m then bsearch m hi else bsearch lo m
  in

  let rec findhi hi = if can_bake hi then findhi (2L ** hi) else hi in

  printf "%Ld\n" (bsearch 0L (findhi 1L))


You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose:   either $$$a_i = a_{i - 1} + x$$$  or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$.

NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$.

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
   if (x!='.') then print_char (x)
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    (*let _ = String.iter print_big_float_as_int (string_of_float sum) in*)
    Printf.printf "%.0f" sum;
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let n, b, x, y = Scanf.sscanf (input_line stdin) "%d  %d  %d  %d" (fun n b x y -> (n,b,x,y)) in
  let tot= ref (Int64.zero) in
  let curr = ref (Int64.zero) in
  for j = 1 to n do
    if Int64.add (!tot) (Int64.of_int x) <= Int64.of_int b then
      (tot := Int64.add (!tot) (Int64.of_int x) ; curr := Int64.add (!curr) (!tot))
    else (tot := Int64.add (!tot) (Int64.of_int (-y)) ; curr := Int64.add (!curr) (!tot)) ;
  done;
  print_endline (Int64.to_string !curr )
done;
;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
    if (x!='.') then print_char (x)
;;

let rec print_big_float_as_int2 = function
| current_float, current_order ->
  if (current_float <= float_of_int max_int) then current_float, current_order
  else print_big_float_as_int2 ((current_float/.10.0), (current_order+1))
;;
let rec print_order = function
  | 0 -> ()
  | n ->
    print_int 0;
    print_order (n-1)
;;
let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    (* let _ = String.iter print_big_float_as_int (string_of_float sum) in *)
    let float_int, order = print_big_float_as_int2 (sum, 0) in
    print_int (int_of_float float_int);
    print_order order;
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let print_big_float_as_int = fun x ->
   if (x!='.') then print_char (x)
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    let sum = run_case () in
    let _ = String.iter print_big_float_as_int (string_of_float sum) in
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;

(*
   build sequence a0, a1, ... , an
   a0 = 0,
   1)ai = a(i-1)+x or 2)ai = a(i-1)-y
   ai <= B

   maximise sum

   want all ai to be as close to B as possible
   - do 1) up to B then (2) and (1) alternating

   a0, a1, a2, ... , ak, a(k+1), ... , an

   0, x, 2*x, ... , k*x where k*x <= B, k*x-y, ... , an

   sum = (x+2*x+...+k*x) + floor((n-k)/2)*(k*x-y+k*x) + (k*x-y if n-k is not even)

   n=5, B=100, x=1, y=30

    0, 1, 2, 3, 4, 5 = 5/2(5+1) =15

   n=7, B=1e8, x=1e8, y=1e8

   0, 1e8, 0, 1e8, 0, 1e8, 0 = 7

   k = 1e8 / 1e8 = 1 < n
   s1 = (1e8*1*2/2)=1e8
   s2 = 6*(1e8)/2 = 3*1e8
   n-k is even so 4*1e8

   n=4, B=1, x=7, y=3

   0, -3, 4 -> PROBLEM!

   k = 1 / 7 = 0
   s1 = 0
   s2 = (4)*(2*0-3)/2=-6

   add +x until no longer possible
   need to -y until we can +x
   add +x
   need to -y until we can +x
   repeat O(n) complexity

   add +x k1 times
   subtract y k2 times
   add x
   subtract y k2 times
   O(1) complexity if we calculate k1 and k2

   k1*x-k2*y + x<=B
   (k1+1)*x-B<=k2*y
   ((k1+1)*x-B)/y <= k2, since k2 isn't necessarily constant we have to do O(n)

   e.g 0, -3, -6, 1, -2, -5, -8, 0 (goes from 2 to 3)

   n=5, B=100, x=1, y=30

   0 + alternate_sum 100 1 30 1 4
   0 + 1 + alternate_sum ... 2 3
   0 + 1 + 2 + ... 3 2
   0 + 1 + 2 + 3 + ... 4 1
   0 + 1 + 2 + 3 + 4 + ... 5 0
   0 + 1 + 2 + 3 + 4 + 5

    *)

let read_int () = Scanf.scanf " %d" (fun x -> x);;
let read_float () = Scanf.scanf " %f" (fun x -> x);;

let rec alternate_sum _B x y current = function
  | 0 -> current
  | n ->
    if (current +. x<=_B) then current +. alternate_sum _B x y (current +. x) (n-1)
    else current +. alternate_sum _B x y (current -. y) (n-1)
;;

let run_case = function
  | () ->
    let n = read_int () in
    let _B = read_float () in
    let x = read_float () in
    let y = read_float () in
    alternate_sum _B x y 0.0 n
;;

let rec iter_cases = function
  | 0 -> 0
  | n ->
    print_float (run_case ());
    print_newline ();
    iter_cases (n-1)
;;

let t = read_int () in
iter_cases t ;;


The BFS algorithm is defined as follows.  Consider an undirected graph with vertices numbered from $$$1$$$ to $$$n$$$. Initialize $$$q$$$ as a new queue containing only vertex $$$1$$$, mark the vertex $$$1$$$ as used.  Extract a vertex $$$v$$$ from the head of the queue $$$q$$$.  Print the index of vertex $$$v$$$.  Iterate in arbitrary order through all such vertices $$$u$$$ that $$$u$$$ is a neighbor of $$$v$$$ and is not marked yet as used. Mark the vertex $$$u$$$ as used and insert it into the tail of the queue $$$q$$$.  If the queue is not empty, continue from step 2.  Otherwise finish. Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex $$$1$$$. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) which denotes the number of nodes in the tree.  The following $$$n - 1$$$ lines describe the edges of the tree. Each of them contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le n$$$) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree. The last line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the sequence to check.

Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower).

NoteBoth sample tests have the same tree in them.In this tree, there are two valid BFS orderings:   $$$1, 2, 3, 4$$$,  $$$1, 3, 2, 4$$$. The ordering $$$1, 2, 4, 3$$$ doesn't correspond to any valid BFS order.

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	(*  let licznik = ref 0 in
	let liczba () =
		let tab = [|7;1;2;1;5;2;3;2;4;5;6;6;7;1;2;5;6;3;4;7|] in
		let wynik = tab.(!licznik) in
		incr licznik;
		wynik in  *)
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	let ojc = Array.make (n + 1) 0 in
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; ojc.(x) <- w; Queue.add x kolejka end) (wez w)
	done;
	let tbl = Array.make (n + 1) 0 in
	let num = Array.make (n + 1) 0 in
	for i = 0 to n - 1 do
		let w = liczba () in
		num.(w) <- i;
		tbl.(i) <- w
	done;
	let poprz = ref 0 in
	let poprz_num_ojc = ref 0 in
	let czygut = ref 1 in
	(* dbg kol;
	dbg ojc; *)
	for i = 0 to n - 1 do
		let u = tbl.(i) in
		(* print_int u;
		print_string " ";
		print_int ojc.(u);
		print_string " ";
		print_int num.(ojc.(u));
		print_string " ";
		print_int !poprz_num_ojc;
		print_endline ""; *)
		(* print_int u;
		print_endline ""; *)
		if kol.(u) < !poprz  || (u <> 1 && num.(ojc.(u)) < !poprz_num_ojc) then czygut := 0;
		poprz := kol.(u);
		poprz_num_ojc := num.(ojc.(u))
	done;
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	(* let licznik = ref 0 in
	let liczba () =
		let tab = [|6;1;2;1;5;2;3;2;4;5;6;1;5;2;3;4;6|] in
		let wynik = tab.(!licznik) in
		incr licznik;
		wynik in *)
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	let ojc = Array.make (n + 1) 0 in
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; ojc.(x) <- w; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let tbl = Array.make (n + 1) 0 in
	let num = Array.make (n + 1) 0 in
	for i = 0 to n - 1 do
		let w = liczba () in
		num.(w) <- i;
		tbl.(i) <- w
	done;
	let poprz = ref 0 in
	let poprz_num_ojc = ref 0 in
	let czygut = ref 1 in
	(* dbg kol;
	dbg ojc; *)
	for i = 0 to n - 1 do
		let u = tbl.(i) in
		(* print_int u;
		print_endline ""; *)
		if kol.(u) < !poprz  || (u <> 1 && num.(ojc.(u)) < !poprz_num_ojc) then czygut := 0;
		if (!poprz < kol.(u)) then begin
			poprz_num_ojc := 0;
			poprz := kol.(u)
		end else
			poprz_num_ojc := num.(ojc.(u));
	done;
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		Hashtbl.add mapa u (v :: Hashtbl.find mapa u);
		Hashtbl.add mapa v (u :: Hashtbl.find mapa v);
	done;

	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(0) <- 1;
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let poprz = ref 0 in
	let czygut = ref 1 in
	for i = 0 to n - 1 do
		let u = liczba () in
		if u > !poprz then czygut := 0;
		poprz := u
	done;
	if !czygut = 1 then print_string "Yes" else print_string "No";;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let poprz = ref 0 in
	let czygut = ref 1 in
	for i = 0 to n - 1 do
		let u = liczba () in
		if u < !poprz then czygut := 0;
		poprz := u
	done;
	(* dbg kol; *)
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	 let licznik = ref 0 in
	let liczba () =
		let tab = [|7;1;2;1;5;2;3;2;4;5;6;6;7;1;2;5;6;3;4;7|] in
		let wynik = tab.(!licznik) in
		incr licznik;
		wynik in
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	let ojc = Array.make (n + 1) 0 in
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; ojc.(x) <- w; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let tbl = Array.make (n + 1) 0 in
	let num = Array.make (n + 1) 0 in
	for i = 0 to n - 1 do
		let w = liczba () in
		num.(w) <- i;
		tbl.(i) <- w
	done;
	let poprz = ref 0 in
	let poprz_num_ojc = ref 0 in
	let czygut = ref 1 in
	(* dbg kol;
	dbg ojc; *)
	for i = 0 to n - 1 do
		let u = tbl.(i) in
		(* print_int u;
		print_string " ";
		print_int ojc.(u);
		print_string " ";
		print_int num.(ojc.(u));
		print_string " ";
		print_int !poprz_num_ojc;
		print_endline ""; *)
		(* print_int u;
		print_endline ""; *)
		if kol.(u) < !poprz  || (u <> 1 && num.(ojc.(u)) < !poprz_num_ojc) then czygut := 0;
		poprz := kol.(u);
		poprz_num_ojc := num.(ojc.(u))
	done;
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let poprz = ref 0 in
	let czygut = ref 1 in
	for i = 0 to n - 1 do
		let u = liczba () in
		if kol.(u) < !poprz then czygut := 0;
		poprz := kol.(u)
	done;
	(* dbg kol; *)
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik

let jajka n k =
	let rzm = min (k + 1) 31 in
	let a = Array.make_matrix (n + 1) rzm 0 in
	let wynik = ref 0 in
	for i = 1 to n do
		for j = 1 to rzm - 1 do
			a.(i).(j) <- a.(i - 1).(j - 1) + a.(i - 1).(j) + 1;
			print_int a.(i).(j);
			print_string " ";
			if a.(i).(j) >= n && !wynik = 0 then wynik := i
		done;
		print_endline ""
	done;
	!wynik;;

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

let plecak l p =
	let a = Array.make (p + 1) 0 in
	let dodaj (poj, k) =
		for i = p downto poj do
			a.(i) <- max a.(i) (a.(i - poj) + k)
		done in
	List.iter dodaj l;
	Array.fold_left max 0 a;;

let repair_road t x =
	let p = ref 0 in
	let dl = (Array.length t) in
	let wynik = ref 0 in
	let odp = ref 0 in
	for i = 0 to dl - 1 do
		while !wynik <= x && !p < dl do
			wynik := !wynik + 1 - t.(!p);
			incr p
		done;
		if !wynik > x then begin
			decr p;
			wynik := !wynik - 1 + t.(!p)
		end;
		odp := max (!odp) (!p - i);
		wynik := !wynik - 1 + t.(i);(*
		print_int i;
		print_string " ";
		print_int !p;
		print_endline ""; *)
	done;
	!odp

let wczytaj () =
	let n = liczba () in
	let s = liczba () in
	let t = Array.make n 0 in
	for i = 0 to n - 1 do
		let pole = liczba () in(*
		print_int pole;
		print_endline ""; *)
		t.(i) <- pole
	done;
	print_int (repair_road t s);
	print_endline "";;

let main () =
	let n = liczba () in
	for i = 1 to n do
		wczytaj ()
	done;;

let examin x =
	let sito = Array.make (x + 1) 0 in
	let kolejka = Queue.create () in
	let mapa = Hashtbl.create 1000000 in
	let skad = Hashtbl.create 1000000 in
	let prims = ref [] in
	for i = 2 to x do
		prims := i :: !prims
	done;
	let primes = !prims in
	let dodaj a =
		if sito.(a) = 0 then
		let it = ref (a * a) in
		while !it <= x do
			sito.(!it) <- a;
			it := !it + a
		done in
	let rec dzielniki a =
		let akt_dziel = sito.(a) in
		if akt_dziel = 0 then [a] else
		let aktliczba = ref a in
		while !aktliczba mod akt_dziel = 0 do
			aktliczba := !aktliczba / akt_dziel
		done;
		(akt_dziel :: dzielniki !aktliczba) in
	List.iter dodaj primes;
	Hashtbl.add mapa x 0;
	Queue.add x kolejka;
	while not (Hashtbl.mem mapa 1) do
		let gora = Queue.take kolejka in
		let przejscia = dzielniki gora in
		let dodaj liczba wynik =
			Hashtbl.add mapa liczba wynik;
			Hashtbl.add skad liczba gora;
			Queue.add liczba kolejka in
		let przejdz dzielnik =
			if not (Hashtbl.mem mapa (gora / dzielnik)) then
				dodaj (gora / dzielnik) (Hashtbl.find mapa gora + 1) in
		List.iter przejdz przejscia;
		if not (Hashtbl.mem mapa (gora - 1)) then
			dodaj (gora - 1) (Hashtbl.find mapa gora + 1)
	done;
	let wynik = ref [] in
	let akt = ref 1 in
	while !akt <> x do
		wynik := (!akt :: !wynik);
		akt := Hashtbl.find skad !akt
	done;
	wynik := (x :: !wynik);
	List.rev !wynik

let to_array l =
	let n = List.length l in
	let tbl = Array.make n (List.hd l) in
	let lista = ref l in
	for i = 0 to n - 1 do
		tbl.(i) <- List.hd !lista;
		lista := List.tl !lista
	done;
	tbl

let dbg t =
	Array.iter (fun x -> print_int x) t;
	print_endline ""

let palindromy l =
	let n = List.length l in
	let l2 = l @ (List.rev l) in
	let tbl = to_array l2 in
	let knp = Array.make (List.length l2) 0 in
	for j = 0 to 1 do
		for i = 1 + n * j to n * (j + 1) - 1 do
			let odp = ref knp.(i - 1) in
			while tbl.(!odp) <> tbl.(i) && !odp <> 0 do
				odp := knp.(!odp - 1)
			done;
			if tbl.(!odp) = tbl.(i) then
				incr odp;
			knp.(i) <- !odp
		done;
		if tbl.(n) = tbl.(0) then
			knp.(n) <- 1
	done;
	knp.(2 * n - 1);;

let main () =
	let n = liczba () in
	let m = liczba () in
	let kraw = Array.make (m + 1) (0, 0, 0) in
	for i = 1 to m do
		let u = liczba () in
		let v = liczba () in
		let w = liczba () in
		kraw.(i) <- (u, v, w)
	done;
	let fu = Array.make (n + 1) 0 in
	let rozm = Array.make (n + 1) 0 in
	for i = 1 to n do
		fu.(i) <- i;
		rozm.(i) <- 1
	done;
	let rec znajdz x =
		if fu.(x) = x then x else
			let wynik = znajdz (fu.(x)) in
			fu.(x) <- wynik;
			wynik in
	let rec znajdz_rozm x =
		if fu.(x) = x then rozm.(x) else znajdz_rozm (fu.(x)) in
	let merge (x, y, _) =
		let v1 = znajdz x in
		let v2 = znajdz y in
		if znajdz_rozm v1 < znajdz_rozm v2 then begin
			fu.(v1) <- v2;
			rozm.(v2) <- rozm.(v2) + rozm.(v1)
		end else begin
			fu.(v2) <- v1;
			rozm.(v1) <- rozm.(v1) + rozm.(v2);
		end in
	let waga_kraw (_, _, w) = w in
	let pierw_kraw (w, _, _) = w in
	let drug_kraw (_, w, _) = w in
	Array.sort (fun (u1, v2, w1) (u2, v2, w2) -> w1 - w2) kraw;
	let poprz_waga = ref 0 in
	let lista = ref [] in
	let wynik = ref (m - n + 1) in
	for i = 1 to m do
		if waga_kraw kraw.(i) > !poprz_waga then begin
			List.iter (fun x -> merge kraw.(x)) !lista;
			lista := [];
			poprz_waga := waga_kraw kraw.(i)
		end;
		if znajdz (pierw_kraw kraw.(i)) = znajdz (drug_kraw kraw.(i)) then
			decr wynik
		else
			lista := (i :: !lista)
	done;
	print_int !wynik;;

let pierw (a, b) = a
let drug (a, b) = b

let daleko tbl =
	let n = Array.length tbl in
	if n = 0 then (0, 0) else
	let tbl_pom = Array.make n (0, 0) in
	for i = 0 to n - 1 do
		tbl_pom.(i) <- (tbl.(i), i)
	done;
	Array.sort (fun x y -> (-1) * (compare x y)) tbl_pom;
	let wal = Array.make n 0 in
	let ind = Array.make n 0 in
	for i = 0 to n - 1 do
		wal.(i) <- (pierw (tbl_pom.(i)));
		ind.(i) <- (drug (tbl_pom.(i)))
	done;
	let wynik = ref 0 in
	let lewy = ref 0 in
	let prawy = ref 0 in
	let maks_ind = ref 0 in
	let poprz = ref wal.(0) in
	let akt_maks = ref 0 in
	for i = 0 to n - 1 do
		if wal.(i) <> !poprz then begin
			maks_ind := !akt_maks;
			poprz := wal.(i)
		end;
		if !wynik < !maks_ind - ind.(i) then begin
			lewy := ind.(i);
			prawy := !maks_ind;
			wynik := !maks_ind - ind.(i)
		end;
		akt_maks := max !akt_maks ind.(i)
	done;
	(!lewy, !prawy)

let kominek k m tbl =
	let plecak = Array.make (k + 1) (-1) in
	Array.sort compare tbl;
	let wynik = ref 0 in
	plecak.(0) <- 0;
	let d = Array.length tbl in
	for i = 0 to d - 1 do
		for j = k downto tbl.(i) do
			if plecak.(j - tbl.(i)) = 0 then begin
				plecak.(j) <- 0;
				wynik := max !wynik (tbl.(i) + min m j)
			end;
		done
	done;
	!wynik

let bfs () =
	let licznik = ref 0 in
	let liczba () =
		let tab = [|6;1;2;1;5;2;3;2;4;5;6;1;5;2;3;4;6|] in
		let wynik = tab.(!licznik) in
		incr licznik;
		wynik in
	let n = liczba () in
	let mapa = Hashtbl.create 1000000 in

	let wez x =
		if Hashtbl.mem mapa x then
			Hashtbl.find mapa x
		else
			[] in

	for i = 0 to n - 2 do
		let u = liczba () in
		let v = liczba () in
		(*print_int u;
		print_string " ";
		print_int v;
		print_endline "";*)
		Hashtbl.add mapa u (v :: wez u);
		Hashtbl.add mapa v (u :: wez v)
	done;
	(*for i = 0 to n do
	 print_int i;
	 print_string ": ";
	 List.iter (fun x -> print_int x; print_string " ") (wez i);
	 print_endline ""
	done;*)
	let kolejka = Queue.create () in
	Queue.push 1 kolejka;
	let kol = Array.make (n + 1) 0 in
	kol.(1) <- 1;
	let ojc = Array.make (n + 1) 0 in
	while not (Queue.is_empty kolejka) do
		let w = Queue.take kolejka in
		List.iter (fun x -> (* print_int x; print_string " ";  *)if kol.(x) = 0 then begin kol.(x) <- kol.(w) + 1; ojc.(x) <- w; Queue.add x kolejka end) (Hashtbl.find mapa w)
	done;
	let tbl = Array.make (n + 1) 0 in
	let num = Array.make (n + 1) 0 in
	for i = 0 to n - 1 do
		let w = liczba () in
		num.(w) <- i;
		tbl.(i) <- w
	done;
	let poprz = ref 0 in
	let poprz_num_ojc = ref 0 in
	let czygut = ref 1 in
	(* dbg kol;
	dbg ojc; *)
	for i = 0 to n - 1 do
		let u = tbl.(i) in
		(* print_int u;
		print_endline ""; *)
		if kol.(u) < !poprz  || (u <> 1 && num.(ojc.(u)) < !poprz_num_ojc) then czygut := 0;
		if (!poprz < kol.(u)) then begin
			poprz_num_ojc := 0;
			poprz := kol.(u)
		end else
			poprz_num_ojc := num.(ojc.(u));
	done;
	if !czygut = 1 then print_string "Yes" else print_string "No";;

bfs ();;

let segment t =
	let n = Array.length t in
	let p = ref 0 in
	let lista = Array.make n 0 in
	let pocz = ref 0 in
	let kon = ref (-1) in
	let wynik = ref 0 in
	for i = 0 to n - 1 do
		print_int i;
		print_string " ";
		let mini () =
			if !pocz > !kon then t.(!p) else
			min t.(lista.(!pocz)) t.(!p) in
		while !p < n && mini () >= !p - i + 1 do
			while pocz <= kon && t.(lista.(!kon)) > t.(i) do
				decr kon
			done;
			incr kon;
			lista.(!kon) <- !p;
			incr p
		done;
		wynik := max !wynik (!p - i);
		if lista.(!pocz) = i && !pocz <= !kon then
			incr pocz;
		print_int !p;
		if !p = i then
			incr p;
		print_int !p;
		print_endline ""
	done;
	!wynik


Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.

The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively. The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks. Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.

Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.

NoteIn the first sample, Arseniy can repaint the first and the third socks to the second color.In the second sample, there is no need to change any colors.

let read_int() = Scanf.scanf " %d" (fun x -> x);;
let n = read_int()
and m = read_int()
and k = read_int();;
let r = Array.make (n + 1) 0
and c = Array.make (n + 1) 0
and g = Array.make (n + 1) [];;

let rec repr u =
  if r.(u) = u then
    u
  else
    begin
      r.(u) <- repr r.(u);
      r.(u)
    end
;;

for u = 1 to n do
  c.(u) <- read_int();
  r.(u) <- u
done;

for i = 1 to m do
  let u = read_int()
  and v = read_int() in
  if repr u <> repr v then
    r.(repr u) <- repr v;
done;;

for u = 1 to n do
  g.(repr u) <- u :: g.(repr u)
done;;

let rec trier =
  let rec split = function
    | [] -> [], []
    | x :: xs -> let ys, zs = split xs in x :: zs, ys
  in
  let rec merge xs ys = match xs, ys with
    | [], xs -> xs
    | xs, [] -> xs
    | x :: xs, y :: ys when c.(x) < c.(y) -> x :: (merge xs (y :: ys))
    | x :: xs, y :: ys -> y :: (merge (x :: xs) ys)
  in
  function
  | [] -> []
  | [x] -> [x]
  | xs -> let ys, zs = split xs in merge (trier ys) (trier zs)
;;

let rec majoritaire = function
  | [] -> 0, 0
  | x :: [] -> 1, 1
  | x :: y :: ys when c.(x) = c.(y) -> let i, j = majoritaire (y :: ys) in i + 1, max (i + 1) j
  | x :: y :: ys -> let _, j = majoritaire (y :: ys) in 1, max 1 j
;;

let x = ref 0 in
for u = 1 to n do
  let _, l = majoritaire (trier g.(u)) in
    x := !x + List.length g.(u) - l
done;
print_int !x;;

let read_int() = Scanf.scanf " %d" (fun x -> x);;
let n = read_int()
and m = read_int()
and k = read_int();;
let r = Array.make (n + 1) 0
and c = Array.make (n + 1) 0
and g = Array.make (n + 1) [];;
for u = 1 to n do
  c.(u) <- read_int();
  r.(u) <- u
done;;

let rec repr u =
  if r.(u) = u then
    u
  else
    begin
      r.(u) <- repr r.(u);
      r.(u)
    end
;;

for i = 1 to m do
  let u = read_int()
  and v = read_int() in
  if repr u <> repr v then
    r.(repr u) <- repr v;
done;;

for u = 1 to n do
  g.(repr u) <- u :: g.(repr u)
done;;

let rec trier =
  let rec split = function
    | [] -> [], []
    | x :: xs -> let ys, zs = split xs in x :: zs, ys
  in
  let rec merge xs ys = match xs, ys with
    | [], xs -> xs
    | xs, [] -> xs
    | x :: xs, y :: ys when c.(x) < c.(y) -> x :: (merge xs (y :: ys))
    | x :: xs, y :: ys -> y :: (merge (x :: xs) ys)
  in
  function
  | [] -> []
  | [x] -> [x]
  | xs -> let ys, zs = split xs in merge (trier ys) (trier zs)
in
let rec majoritaire = function
  | [] -> 0, 0
  | x :: [] -> 1, 1
  | x :: y :: ys when x = y -> let i, j = majoritaire (y :: ys) in i + 1, max (i + 1) j
  | x :: y :: ys -> let _, j = majoritaire (y :: ys) in 1, max 1 j
in

let x = ref 0 in
for u = 1 to n do
  let _, l = majoritaire (trier g.(u)) in
  x := !x + List.length g.(u) - l
done;
print_int !x;;


Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.Consider that m (m ≤ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat.After occupying all the window seats (for m > 2n) the non-window seats are occupied:1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat.All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat.  The seating for n = 9 and m = 36. You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.

The only line contains two integers, n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 4n) — the number of pairs of rows and the number of passengers.

Print m distinct integers from 1 to m — the order in which the passengers will get off the bus.

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 r = ref [] in
    for i = 0 to n - 1 do
      let row = i in
      let ord =
	[ 2 * n + 1 + 2 * row;
	  2 * row + 1;
	  2 * n + 2 + 2 * row;
	  2 * row + 2]
      in
	List.iter
	  (fun x ->
	     if x <= m
	     then r := x :: !r) ord
    done;
    List.iter (Printf.printf "%d ") (List.rev !r);
    Printf.printf "\n"